summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAshymad <czilukim@o2.pl>2017-09-24 12:09:11 +0200
committerAshymad <czilukim@o2.pl>2017-09-24 12:09:11 +0200
commit09f757fe9e8bd1103a860653a2bfea437c53ad80 (patch)
treed85d09f9051bd00c4969aebf69d9e016b85c3cf9
parent479cf161316f4f89f039f66a9bc9fe719afde2b4 (diff)
Added data usage counter
-rwxr-xr-xrrd_count_data_usage/cgi-bin/rrd-graph59
-rw-r--r--rrd_count_data_usage/db/weigths.conf4
-rwxr-xr-xrrd_count_data_usage/update-usage.sh60
3 files changed, 123 insertions, 0 deletions
diff --git a/rrd_count_data_usage/cgi-bin/rrd-graph b/rrd_count_data_usage/cgi-bin/rrd-graph
new file mode 100755
index 0000000..b6f4636
--- /dev/null
+++ b/rrd_count_data_usage/cgi-bin/rrd-graph
@@ -0,0 +1,59 @@
+#!/bin/sh
+rrdtool graph /usr/local/rrd/bandwidth_4h.png --end now --start end-14400 -S 600 -a PNG -h 125 -v "Data Throughput" \
+ 'DEF:in=/usr/local/rrd/bandwidth.rrd:in:AVERAGE' \
+ 'DEF:out=/usr/local/rrd/bandwidth.rrd:out:AVERAGE' \
+ 'CDEF:bin=in,600,*' \
+ 'CDEF:bout=out,600,*' \
+ 'CDEF:mbin=bin,1048576,/' \
+ 'CDEF:mbout=bout,1048576,/' \
+ 'AREA:bin#00FF00:Data In' 'LINE1:bout#0000FF:Data Out\j' \
+ 'GPRINT:mbin:LAST:Last Data In\: %3.2lf MB' 'GPRINT:mbout:LAST:Last Data Out\: %3.2lf MB\j' \
+ 'GPRINT:mbin:AVERAGE:Average Data In\: %3.2lf MB' 'GPRINT:mbout:AVERAGE:Average Data Out\:%3.2lf MB\j' > /dev/null
+rrdtool graph /usr/local/rrd/bandwidth_2d.png --end now --start end-172800 -S 3600 -a PNG -h 125 -v "Data Througput" \
+'DEF:in=/usr/local/rrd/bandwidth.rrd:in:AVERAGE' \
+ 'DEF:out=/usr/local/rrd/bandwidth.rrd:out:AVERAGE' \
+ 'CDEF:bin=in,3600,*' \
+ 'CDEF:bout=out,3600,*' \
+ 'CDEF:mbin=bin,1048576,/' \
+ 'CDEF:mbout=bout,1048576,/' \
+ 'AREA:bin#00FF00:Data In' 'LINE1:bout#0000FF:Data Out\j' \
+ 'GPRINT:mbin:LAST:Last Data In\: %3.2lf MB' 'GPRINT:mbout:LAST:Last Data Out\: %3.2lf MB\j' \
+ 'GPRINT:mbin:AVERAGE:Average Data In\: %3.2lf MB' 'GPRINT:mbout:AVERAGE:Average Data Out\:%3.2lf MB\j' > /dev/null
+rrdtool graph /usr/local/rrd/bandwidth_m.png --end now --start end-2678400 -S 86400 --x-grid DAY:1:WEEK:1:WEEK:1:0:"%d.%m" -a PNG -h 125 -v "Data Througput" \
+ 'DEF:in=/usr/local/rrd/bandwidth.rrd:in:AVERAGE' \
+ 'DEF:out=/usr/local/rrd/bandwidth.rrd:out:AVERAGE' \
+ 'CDEF:bin=in,86400,*' \
+ 'CDEF:bout=out,86400,*' \
+ 'CDEF:mbin=bin,1073741824,/' \
+ 'CDEF:mbout=bout,1073741824,/' \
+ 'AREA:bin#00FF00:Data In' 'LINE1:bout#0000FF:Data Out\j' \
+ 'GPRINT:mbin:LAST:Last Data In\: %3.2lf GB' 'GPRINT:mbout:LAST:Last Data Out\: %3.2lf GB\j' \
+ 'GPRINT:mbin:AVERAGE:Average Data In\: %3.2lf GB' 'GPRINT:mbout:AVERAGE:Average Data Out\:%3.2lf GB\j' > /dev/null
+cat << EOF
+Content-type: text/html
+
+<!DOCTYPE html>
+<html>
+<meta charset="UTF-8">
+<body>
+<center>
+EOF
+awk 'END { printf "<progress value=\"%.0f\" max=\"107374182400\"></progress></br>\n", $1 } \
+ { printf "<h1>Dane zużyte w tym miesiącu: <ins>%.2f GB</ins></br>\n", $1 / 1073741824 }' /usr/local/rrd/db/datausage
+cat << EOF
+Ostatnie 4 godziny (co 10 min)</br>
+<img src="/bandwidth_4h.png" width="75%">
+</br>Ostatnie 2 dni (co godzinę)</br>
+<img src="/bandwidth_2d.png" width="75%">
+</br>Ostatni miesiąc (co dzień)</br>
+<img src="/bandwidth_m.png" width="75%">
+<style>
+progress[value] {
+ width: 530px;
+ height: 50px;
+}
+</style>
+</center>
+</body>
+</html>
+EOF
diff --git a/rrd_count_data_usage/db/weigths.conf b/rrd_count_data_usage/db/weigths.conf
new file mode 100644
index 0000000..023f967
--- /dev/null
+++ b/rrd_count_data_usage/db/weigths.conf
@@ -0,0 +1,4 @@
+# Weights of hours in the day (from 0 to 1)
+# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
+INWEIGTHS=( 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 )
+OUTWEIGTHS=( 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 )
diff --git a/rrd_count_data_usage/update-usage.sh b/rrd_count_data_usage/update-usage.sh
new file mode 100755
index 0000000..52e0ea0
--- /dev/null
+++ b/rrd_count_data_usage/update-usage.sh
@@ -0,0 +1,60 @@
+#!/usr/bin/env bash
+PUBLIC="public"
+ROUTERIP="192.168.2.1"
+SNMPVER="2c" # SNMP version
+MAXVAL="4294967296" # 2^32 for int32
+IFACE="14" # Number of the interface to track. Discover with snmpwalk
+DIR="/usr/local/rrd"
+LOG="$DIR/db/log"
+
+echo "$(tail -1000 $LOG)" > $LOG
+
+source $DIR/db/weigths.conf
+
+UPTIME="$(snmpget -v $SNMPVER -c $PUBLIC -O0qtv $ROUTERIP DISMAN-EVENT-MIB::sysUpTimeInstance)"
+DATAOUT="$(snmpget -v $SNMPVER -c $PUBLIC -Oqv $ROUTERIP IF-MIB::ifOutOctets.$IFACE)"
+DATAIN="$(snmpget -v $SNMPVER -c $PUBLIC -Oqv $ROUTERIP IF-MIB::ifInOctets.$IFACE)"
+
+if [ $UPTIME ] && [ $DATAOUT ] && [ $DATAIN ]; then
+ LASTUPTIME=$(cat $DIR/db/uptime)
+ LASTDATAOUT=$(cat $DIR/db/dataout)
+ LASTDATAIN=$(cat $DIR/db/datain)
+ LASTHOUR=$(cat $DIR/db/hour)
+ DATAUSAGE=$(cat $DIR/db/datausage)
+ INUSAGE=$DATAIN
+ OUTUSAGE=$DATAOUT
+
+ if [ $LASTUPTIME -lt $UPTIME ]; then # Router not restarted
+ INUSAGE=$(echo "$INUSAGE - $LASTDATAIN" | bc)
+ OUTUSAGE=$(echo "$OUTUSAGE - $LASTDATAOUT" | bc)
+
+ if [ $DATAIN -lt $LASTDATAIN ]; then # Counter overflowed
+ INUSAGE=$(echo "$INUSAGE + $MAXVAL" | bc)
+ echo "[$(date)] INFO: Counter overflow. Reason: $LASTDATAIN > $DATAIN" >> $LOG
+ fi
+
+ if [ $DATAOUT -lt $LASTDATAOUT ]; then # Counter overflowed
+ OUTUSAGE=$(echo "$OUTUSAGE + $MAXVAL" | bc)
+ echo "[$(date)] INFO: Counter overflow. Reason: $LASTDATAOUT > $DATAOUT" >> $LOG
+ fi
+ else
+ echo "[$(date)] WARN: Router has problably restarted. Reason: $LASTUPTIME > $UPTIME. Some data lost." >> $LOG
+ fi
+
+
+
+ INUSAGE=$(echo "$INUSAGE * ${INWEIGTHS[$LASTHOUR]}" | bc)
+ OUTUSAGE=$(echo "$OUTUSAGE * ${OUTWEIGTHS[$LASTHOUR]}" | bc)
+ DATAUSAGE=$(echo "$DATAUSAGE + $OUTUSAGE + $INUSAGE" | bc)
+
+ echo $UPTIME > $DIR/db/uptime
+ echo $DATAOUT > $DIR/db/dataout
+ echo $DATAIN > $DIR/db/datain
+ echo $DATAUSAGE > $DIR/db/datausage
+
+ rrdupdate $DIR/bandwidth.rrd N:$DATAIN:$DATAOUT
+else
+ echo "[$(date)] ERROR: no response from router" >> $LOG
+fi
+
+echo $(date +%-H) > $DIR/db/hour