Network Bandwidth Script (SNMP/Grafana/Influxsb)

(Arethusa) #1


I’m looking for a good script on centos7 to have the network bandwidth.

I already use iftop like that:

iftop -t -s 1 -n -N 2>/dev/null | awk '/send and receive/ {print $8}'

It’s good, but I have a problem with it, I can’t stare the units.

I use influxdb/Grafana, with SNMP custom OID.

If the speed is : 550 Kb/s I get “550” with snmp, so, on grafana I apply the units kb/s and it’s ok.

BUT ! If the speed is 1050 Kb/s, Iftop say “1.05 Mb/s” so I get “1.05” with snmp, and 1.05Kb on grafana…

Or, if the speed is 0.950 Kb/s, iftop say “950 b/s” so I get “950” with snmpd, and 950Kb/s on grafana.

The best way is to congeal the units on iftop (550 kb/s 1050kb/s, 0.9 kb/s) but that looks not possible.

I try regex like if [^Mb] then * 1024 , if [^n] then /1024, if [^Gb] then * 1024 * 1024….

But I am sure that is a good idea…

If you have an idea or another software or script…

Thank you.

(Arethusa) #2

Hello, this one works ! (Create by me! )


in0=$(snmpwalk -On -v3 -l authPriv -u ******** -a SHA -A '********' -x AES -X '********'
out0=$(snmpwalk -On -v3 -l authPriv -u ******** -a SHA -A '********' -x AES -X '*******'

sleep $sec

in2=$(snmpwalk -On -v3 -l authPriv -u ******** -a SHA -A '********' -x AES -X '********'
out2=$(snmpwalk -On -v3 -l authPriv -u ******** -a SHA -A '********' -x AES -X '********'

in00=$(echo $in0 | awk '{print $4}')
out00=$(echo $out0 | awk '{print $4}')

in22=$(echo $in2  | awk '{print $4}')
out22=$(echo $out2  | awk '{print $4}')

#echo débit Entrant : "$(((($in22 - $in00) * 8) / 1024 /3))"
#echo débit Sortant : "$(((($out22 - $out00) * 8) / 1024 /3))"

#echo débit Total : "$((((($in22 - $in00) * 8) / 1024 /3) + ((($out22 - $out00) * 8) / 1024 /3)))"
#total=$"$((((($in22 - $in00) * 8) / 1024 /3) + ((($out22 - $out00) * 8) / 1024 /3)))"

entrant=$"$(((($in22 - $in00) * 8) / 1024 / $sec))"
sortant=$"$(((($out22 - $out00) * 8) / 1024 / $sec))"

total=$"$(($entrant + $sortant))"

echo input $entrant
echo output $sortant
echo total $total

But when I try with snmpwalk, I have time out, of course, I need some second to have comparisons.
But on bash that works good…

That was difficult to create it, but I have to create a better solution.

I still have my problem ^^


Looking at the developer guidelines you’ll find the unit “c” identifying the value as a counter. This way you don’t need any comparison values. The graphing addon itself will take care.

(Arethusa) #4


I understand what you mean, but I don’t know how to apply on icinga2, I will try.

Thank you.


You already wrote a script modifying the output. Having an output like
Network traffic: OK | in=123456789c;222222222;33333333 out=121212121c;222222222;333333333
should work

(Arethusa) #6

The problem is when the script is too complexes, the “snmpwalk -On -v3 -l authPriv -u laurent -a SHA -A "laurentSHA" -x AES -X "laurentAES" .......” doesn’t works.

My script works on bash but not with snmpwalk, in this case, the “sleep” is not the only problem.

But, I try a new thing:


deb=$(iftop -t -s 1 -n -N 2>/dev/null | awk '/send and receive/ {print $8}')
deb0=$(iftop -t -s 1 -n -N 2>/dev/null | awk '/send and receive/ {print $8}' | sed 's/[a-zA-Z]//g')

deb2=$(echo $deb | sed -e 's/[^Kb$]//g')
deb3=$(echo $deb | sed -e 's/[^Mb$]//g')
deb4=$(echo $deb | sed -e 's/[^Gb$]//g')

if [ "$deb2" = Kb ]; then echo $"$(($deb0 * 1))" ;
if [ "$deb3" = Mb ]; then echo $"$(($deb0 * 1024))" ;
if [ "$deb4" = Gb ]; then echo $"$(($deb0 * 1024 * 1024))" ;

if [ "$deb2" || "$deb3" || "$deb4" ] = b; then echo 0


That looks good, we will see after some time ^^

Thank you


One plugin to use might be check_nwc_health. I’m not sure where you’ll find an english description apart from check_nwc_health -h but you should at least give it a try.
Using snmpget instead of snmpwalk will dramatically reduce the time to retrieve the relevant values.

(Arethusa) #8


Thank you for your answer.

On my test I user snmpwalk, but the result is the same with “check_snmp” the syntax is little difference.
And Icingaweb2 do a check_snmp because I user : check_command =”snmpv3” on icinga2.

I think I can’t use check_nwc_health instead of check_snmp because I use OID.

But usually what do you use to check the network usage on a server?


Try check_snmp_int instead.