Share Pastebin
Guest
Private paste!

rdnm

By: a guest | Jul 26th, 2010 | Syntax: Bash | Size: 3.12 KB | Hits: 50 | Expires: Never
Copy text to clipboard
  1. #!/bin/bash
  2. #
  3. # RDNM by Subban
  4. #
  5. # Rolling Duration Net Meter
  6. #  
  7. # Simple script to output the total download
  8. # so you can track your quota more easily.
  9. # Only useful in conjuntion with RDNMcron which
  10. # collects the days total and outputs a log file.
  11. #
  12. # This script depends on vnstat being installed and working
  13. # The following 2 lines should get it setup, correct the eth0
  14. # depending on your active network card.
  15. #
  16. # sudo aptitude install vnstat
  17. # sudo vnstat -u -i eth0
  18. #
  19. # For the love of god check your wire is plugged in eth0
  20. # or adjust the location in the second command accordingly.
  21. # I wasted a day and half trying to "fix" vnstat to find
  22. # the cable was in the wrong farking hole.
  23. #
  24. #
  25.  
  26. basename=~/.rdnm
  27. total=0
  28. count=0
  29. combined=0
  30. rolling=0
  31. part=0
  32.  
  33. #
  34. # check if vnstat is installed, spank user if not.
  35. #
  36. if [ -z "$(which vnstat)" ]; then
  37.         echo "$0: error: vnstat is not installed"
  38.         exit 1
  39. fi
  40.  
  41. if [ "$1" = "-l" ]; then
  42.         echo -e "\nDays 1-10 \t      Days 11-20 \t      Days 21-30"
  43.          tail -30 $basename/bwdata > $basename/last30.temp
  44.          tac $basename/last30.temp | pr -tl 12 --columns=3
  45.  
  46.          echo -e ""
  47. elif [ "$1" = "-h" ]; then
  48.         echo -e "\n  rdnm is a script to track the Demon internet 30 day download quota."
  49.         echo -e "  The data file lives as a txt file in ~/.rdnm/ called bwdata"
  50.         echo -e "\n\tUseage:\n\t-------"
  51.         echo -e "\trdnm\t= Shows the current totals"
  52.         echo -e "\trdnm -l\t= List the last 30 days figures"
  53.         echo -e "\trdnm -h\t= Displays this help"
  54.         echo -e "\n"
  55. exit 0
  56. fi
  57.  
  58. if [ -d $basename ]; then
  59.   touch $basename/bwdata
  60.   touch $basename/temp
  61. else
  62.   mkdir $basename
  63.   touch $basename/bwdata
  64.   touch $basename/temp
  65. fi
  66. #
  67. # calculate total from the logfile (upto 30days for me)
  68. #
  69. rolling=`tail -30 $basename/bwdata`
  70. echo $rolling > $basename/temp
  71. for part in `cat $basename/temp`
  72.    do
  73.     total=$(echo "scale=2; ${total}+${part}" | bc)
  74.     count=$(($count+1))
  75.    done
  76.  
  77. #
  78. # get todays current total
  79. #
  80. vndata=`vnstat -s`
  81. leftcrop=${vndata#*today}
  82. #
  83. # Try and snip off the MiB
  84. # If data is in GiB, this will fail, but we don't mind because
  85. # Andabata helped me fix it in the next code chunk
  86. #
  87. todaytotal=${leftcrop%% MiB*}
  88.  
  89. #
  90. # Now check if the $todaytotal from above has GiB in, if it does
  91. # we amputate it, and then multiply the value by 1024, to make it
  92. # into megabytes, which we prefer.
  93. #
  94. echo $todaytotal|grep -q GiB &&
  95. todaytotal=${todaytotal%% GiB*} &&
  96. todaytotal=$(echo "scale=2; ${todaytotal}*1024" | bc)
  97.  
  98. #
  99. # Tally up the current values for todays leeching and the previous
  100. # 30 days ISP abuse for a combined total.
  101. #
  102. combined=$(echo "scale=2; ${todaytotal}+${total}" | bc)
  103.  
  104. printf "\n"
  105. printf "\t  Bandwidth usage breakdown\n"
  106. printf "\t%-10s ..... %10s MiB\n" "$count day" $total
  107. printf "\t%-10s ..... %10s MiB\n" "Todays" $todaytotal
  108. printf "\t%-10s ..... %10s MiB\n\n" "combined" $combined
  109.  
  110. #echo -e "Logged ($count day) rolling download total is \t$total MiB"
  111. #echo -e "Current download today only \t\t$todaytotal MiB"
  112. #echo -e "combined total for log and today \t\t$combined MiB"
  113.  
  114. # tell the system we finished ok
  115. exit 0