#!/usr/local/bin/bash # --------------------------------- # The Hovgaard Klan, DH, 2006-05-07 # --------------------------------- # This script fetches the WAN incoming bytes per second from my internet # router (a Soekris/m0n0wall box) through SNMP. # The counter is created as "middle mode" which means it will # summarize all data and divide by the number of datapoints. # # Start command: # /usr/bin/nohup /root/scripts/wan_out.sh > /var/log/wan_out.log 2>&1 & # # This script starts up when my FreeBSD box does. It runs in a never # ending loop. I prefer the other way of doing this (see WAN_IN.SH) # but this is just to show how it is possible to get data every one # second. # # Enable debug information #set -x # My router IP address and my counter ID (CID) - change this to your own router_ip=192.168.1.1 counter_id=84df75c1123f7248f5105a40759f4715bee38270 first_run=1 while [ 1 -eq 1 ]; do snmp_get=`/usr/local/bin/snmpget -v2c -Oqvt -c public $router_ip system.sysUpTime.0 interfaces.ifTable.ifEntry.ifOutOctets.2` uptime_secs=`echo $snmp_get | /usr/bin/awk '{print $1}'` bytes_total=`echo $snmp_get | /usr/bin/awk '{print $2}'` if [ $first_run -eq 0 ]; then uptime_diff=`echo "scale=2; ($uptime_secs - $uptime_last) / 100" | /usr/bin/bc` bytes_diff=`echo "scale=0; $bytes_total - $bytes_last" | /usr/bin/bc` bytes_sec=`echo "scale=0; $bytes_diff / $uptime_diff" | /usr/bin/bc` if [ $uptime_secs -ge 0 ] && [ $bytes_total -ge 0 ]; then echo `/bin/date | /usr/bin/awk '{print $4}'`, "Uptime: $uptime_secs, Total bytes: $bytes_total, Diff secs: $uptime_diff, Bytes diff: $bytes_diff, Bytes/sec: $bytes_sec (wan_out.sh)" /usr/local/bin/curl --silent http://data.hovmon.com/?cid=$counter_id\&val=$bytes_sec > /dev/null fi fi uptime_last=$uptime_secs bytes_last=$bytes_total first_run=0 sleep 1 done