SHARE
TWEET

Bandwidth monitor bash script

a guest Oct 4th, 2012 95 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2.  
  3. ##
  4. # Bandwidth me!
  5. # Bandwidth monitor script written by Manfred Dellkrantz in 2012.
  6. # I run it every five minutes using crontab.
  7. #   */5 * * * * /home/whatever/bwme.sh >> /home/whatever/bwme.log
  8. # Outputs totals by the hour, day, week, month and year.
  9. # Also outputs some preliminary numbers
  10. ##
  11.  
  12. function byte {
  13.         echo $1|awk '
  14.                BEGIN {
  15.                        split("B,KiB,MiB,GiB,TiB,PiB,EiB,ZiB,YiB", Units, ",");
  16.                }
  17.                {
  18.                        u = 1;
  19.                        while ($1 >= 1024) {
  20.                                $1 = $1 / 1024;
  21.                                u += 1
  22.                        }
  23.                        $1 = sprintf("%.2f %s", $1, Units[u]);
  24.                        print $0;
  25.                }
  26.        '
  27. }
  28.  
  29. mkdir $0.lock || exit 1
  30.  
  31. #echo "## Running" `date`
  32.  
  33. net=(`grep eth0 /proc/net/dev`)
  34.  
  35. in=${net[1]}
  36. out=${net[9]}
  37. let tot=$in+$out
  38.  
  39. year=`date +%Y`
  40. month=`date +%B`
  41. week=`date +%W`
  42. day=`date +%d`
  43. hour=`date +%H`
  44. min=`date +%M`
  45.  
  46. if [ -f $0.db ]; then
  47.         data=(`cat $0.db`);
  48. else
  49.         data=($in $out $year $month $week $day $hour $min 0 0 0 0 0 0)
  50. fi
  51.  
  52. let oTot=${data[0]}+${data[1]}
  53. if (( $oTot > $tot )); then
  54.         echo "## Has there been a reboot?"
  55.         dIn=$in
  56.         dOut=$out
  57. else
  58.         let dIn=$in-${data[0]}
  59.         let dOut=$out-${data[1]}
  60. fi
  61.  
  62. data[0]=$in
  63. data[1]=$out
  64. for i in `seq 8 13`; do
  65.         let data[$i]=data[$i]+$dIn+$dOut
  66. done
  67.  
  68. if [ "${data[2]}" != "$year" ]; then
  69.         echo "End of year ${data[2]}:" `byte ${data[8]}`
  70.         data[2]=$year
  71.         data[8]=0
  72. fi
  73. if [ "${data[3]}" != "$month" ]; then
  74.         echo "End of month ${data[3]}: `byte ${data[9]}`, so far for year ${data[2]}: `byte ${data[8]}`"
  75.         data[3]=$month
  76.         data[9]=0
  77. fi
  78. if [ "${data[4]}" != "$week" ]; then
  79.         echo "End of week ${data[4]}:" `byte ${data[10]}`
  80.         data[4]=$week
  81.         data[10]=0
  82. fi
  83. if [ "${data[5]}" != "$day" ]; then
  84.         echo "End of day ${data[5]}: `byte ${data[11]}`, so far for week ${data[4]}: `byte ${data[10]}`, ${data[3]}: `byte ${data[9]}`"
  85.         data[5]=$day
  86.         data[11]=0
  87. fi
  88. if [ "${data[6]}" != "$hour" ]; then
  89.         echo "End of hour ${data[6]}: `byte ${data[12]}`, so far for day ${data[5]}: `byte ${data[11]}`"
  90.         data[6]=$hour
  91.         data[12]=0
  92. fi
  93. if [ "${data[7]}" != "$min" ]; then
  94. #        echo "End of minute ${data[7]}: `byte ${data[13]}`, so far for hour ${data[6]}: `byte ${data[12]}`"
  95.         data[7]=$min
  96.         data[13]=0
  97. fi
  98.  
  99. echo ${data[@]}>$0.db
  100.  
  101. rmdir $0.lock
RAW Paste Data
Top