1
0

vpnstats 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/bin/bash
  2. #
  3. # vpnstats Get commaseparated file and mail it to the admin
  4. # Version 1.0 beta by Richard de Vroede - Linvision BV <richard@linvision.com>
  5. #
  6. # check for conf file, else create with defaults
  7. CONF=/etc/vpnstats.conf
  8. if [ ! -f $CONF ]; then
  9. echo "LOGFILE=/var/log/messages.1" > $CONF
  10. echo "STATFILE=/var/log/vpnstats" >> $CONF
  11. echo "TEMPFILE=/tmp/tmpvpnstats" >> $CONF
  12. echo "SMTP=localhost" >> $CONF
  13. echo "FROM=" >> $CONF
  14. echo "TO=" >> $CONF
  15. fi
  16. . $CONF
  17. # check for weekly crontab, else create it
  18. CRON=/etc/cron.weekly/vpnstats.cron
  19. if [ ! -f $CRON ]; then
  20. echo "#!/bin/bash" > $CRON
  21. echo "$0" >> $CRON
  22. chmod 755 $CRON
  23. fi
  24. echo "Name,Login,Logout,Connected Minutes,Recieved Bytes,Transmitted Bytes" > $STATFILE
  25. PPPDPID=$(grep "peer authentication succeeded" $LOGFILE | cut -d [ -f2 | cut -d ] -f 1)
  26. while [ "$PPPDPID" != "" ]; do
  27. PID=$(echo $PPPDPID | awk '{print $1}')
  28. grep $PID $LOGFILE | grep pppd | grep -v pptpd > $TEMPFILE
  29. NAME=$(grep "succeeded for" $TEMPFILE | awk -F "succeeded for" '{print $2}' | cut -d " " -f 2)
  30. LOGIN=$(grep "succeeded for" $TEMPFILE | cut -d " " -f 1-3)
  31. LOGOUT=$(grep "Connect time" $TEMPFILE | cut -d " " -f 1-3)
  32. CONNECT=$(grep "Connect time" $TEMPFILE | cut -d ] -f 2 | cut -d " " -f 4)
  33. RXB=$(grep "Sent" $TEMPFILE | cut -d ] -f 2 | cut -d " " -f 3)
  34. TXB=$(grep "Sent" $TEMPFILE | cut -d ] -f 2 | cut -d " " -f 6)
  35. echo "$NAME,$LOGIN,$LOGOUT,$CONNECT,$RXB,$TXB" >> $STATFILE
  36. PPPDPID=$(echo $PPPDPID | sed "s%$PID%%")
  37. rm -f $TEMPFILE
  38. done
  39. MAILER=$(echo $(locate sendEmail) | awk '{print$1}')
  40. if [ "$MAILER" != "" ]; then
  41. if [ "$FROM" = "" ]; then FROM=root@`uname -n`; fi
  42. if [ "$TO" = "" ]; then TO=root@localhost; fi
  43. $MAILER -s $SMTP -u "VPNstats" -f $FROM -t $TO -m " " -a $STATFILE
  44. else
  45. if [ -f "/bin/mail" ]; then
  46. cat $STATFILE | /bin/mail -s "VPNstats" $TO
  47. else
  48. echo "You need /bin/mail OR"
  49. echo "the Perl program sendEmail located at http://freshmeat.net/projects/sendemail/"
  50. fi
  51. fi