Share Pastebin
Guest
Public paste!

rdnm

By: a guest | Jul 26th, 2010 | Syntax: Bash | Size: 1.69 KB | Hits: 122 | Expires: Never
Copy text to clipboard
  1. #!/bin/bash
  2. #
  3. # RDNMcron by Subban
  4. #
  5. # Execute from a cron with :
  6. # 59 23 * * * /home/subbass/bin/RDNMcrom
  7. #
  8. # This equals 11:59pm. When it runs it will take the days total
  9. # from vnstat and log it, you can later parse the data in the log
  10. # with `rdnm` in a console to check your current quota used.
  11. #
  12. # This script depends on vnstat being installed and working
  13. # The following line should get it setup, correct the eth0
  14. # depending on your active network card.
  15. #
  16. # vnstat -u -i eth0
  17. #
  18.  
  19. basename=~/.rdnm
  20. todaytotal=0
  21. leftcrop=0
  22. sanitised=0
  23.  
  24. #
  25. # check if vnstat is installed, spank user if not.
  26. #
  27. if [ -z "$(which vnstat)" ]; then
  28.         echo "$0: error: vnstat is not installed"
  29.         exit 1
  30. fi
  31.  
  32. if [ -d $basename ]; then
  33.   touch $basename/bwdata
  34.   touch $basename/temp
  35. else
  36.   mkdir $basename
  37.   touch $basename/bwdata
  38.   touch $basename/temp
  39. fi
  40.  
  41. vndata=`vnstat -s`
  42.  
  43. # remove all text from the left upto and including 'today'
  44. leftcrop=${vndata#*today}
  45.  
  46. #remove text from the right upto the last 'MiB'
  47. #
  48. # GiB or MiB depending on output from VNSTAT
  49. #
  50. #  total=$(echo "scale=2; ${total}+${part}" | bc)
  51. #
  52.  
  53. todaytotal=${leftcrop%% MiB*}
  54. echo $todaytotal|grep -q GiB &&
  55. todaytotal=${todaytotal%% GiB*} &&
  56. todaytotal=$(echo "scale=2; ${todaytotal}*1024" | bc)
  57.  
  58. #echo $todaytotal|grep -q GiB && todaytotal=$(( ${leftcrop%% GiB*} * 1024 ))
  59.  
  60.  
  61. ## this next section is obsolete but left intact, now I use bc in rdnm to
  62. ## calculate with the decimal places intact.
  63. #
  64. #this bit removes anything to the right of the decimal point
  65. #sanitised=${todaytotal%%.*}
  66. #echo $sanitised >> $basename/bwdata
  67. echo $todaytotal >> $basename/bwdata
  68.  
  69. # tell the system we finished ok
  70. exit 0