Advertisement
big_bum

System monitor using Twitter

May 6th, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. # Cristian Stefanescu version
  3. # Using the GPL Licence, I must provide the original source of the script:
  4. # http://pastebin.com/WZjaupQi
  5. # You should have two Twitter accounts, be logged on with the second one, accept the API access,
  6. # and follow your second account from the first one,
  7. # enabling SMS sending for every statusupdate
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12.  
  13. # Copyright (C) 2012 by +Mitesh Shah
  14. # Mr.Miteshah@gmail.com
  15. # People who works on this script to make it easier and good
  16. # +Chris Raess +Josh Sabboth and +Christopher Timm
  17. # People who want to use this script in their Mac OS X
  18. # Read the +Israel Torres comments on this post
  19.  
  20. # <beginning of Mitesh Shah original work>
  21. # Checking every hour for any Authentication Failure entry founded on
  22. # /var/log/auth.log file and update twitter status
  23.  
  24. # You should need to create a crontab entry for this script
  25. # So this script runs every hours automatically
  26.  
  27. # If using the original part of the script plus added functionalities
  28. # you should add a cronjob for every 5 minutes
  29.  
  30. # Checking twidge (command-line twitter client) is installed or not
  31. dpkg --list | grep twidge &> /dev/null
  32.  
  33. if [ $? -ne 0 ]
  34. then
  35. echo "Twidge (command-line twitter client) is not installed :("
  36. echo "For how to install and configure twidge check the following links:"
  37. echo "http://www.howtogeek.com/62018/how-to-use-the-linux-terminal-to-update-twitter/"
  38. else
  39. # Calculate 1 hour ago time in unix time format
  40. HOURAGO=$(date +%s --date "1 hour ago")
  41.  
  42. # Take the last authentication failure entry from auth.log
  43. SMS=$(cat /var/log/auth.log | grep 'authentication failure' | tail -n1 )
  44.  
  45. # Extract the time from SMS variable and convert it to unix time format
  46. SMSTIME=$(date -d "$(echo $SMS | awk '{print $1,$2,$3}')" +%s)
  47.  
  48. if [ $SMSTIME -gt $HOURAGO ]; then
  49. echo "Updating Twitter Status..."
  50. echo $SMS | awk '{print $3,$5,$9,$10,$12,$14,$15,$16}' | twidge update
  51. fi
  52.  
  53. # <end of Mitesh Shah original work>
  54.  
  55. # The following part of the script is created by Cristian Stefanescu.
  56. # It's based and inspired from the work of Mitesh Shah (above)
  57. # and added some new functionalities
  58.  
  59. # If you have ufw firewall, you can view the last log from a specific port.
  60. # In my example it's port 2150.
  61. # You should edit the grep command and use the port number of your needs
  62. # I think it also works with kernel.log or syslog, just edit the file from cat command
  63.  
  64. # Take the last port entry from ufw.log
  65. FW=$(cat /var/log/ufw.log | grep 'DPT=2150' | tail -n1 )
  66.  
  67. # Extract the time from FW variable and convert it to unix time format
  68. FWTIME=$(date -d "$(echo $FW | awk '{print $1,$2,$3}')" +%s)
  69.  
  70. if [ $FWTIME -gt $HOURAGO ]; then
  71. echo "Updating Twitter Status..."
  72. echo $FW | sed 's/\[ */\[/g' | awk '{print $3,$8,$9,$13,$14,$21,$23}' | twidge update
  73. fi
  74. #sed is used for deleting the space between the kernel log [ no.no] when overpopulating the log.
  75.  
  76. # Now let's send a SMS if load average from the last 15 minutes is higher or equal to 1.00
  77. # Store load average value from the last 15 minutes in a variable
  78. load=$(uptime|awk '{print ( $(NF) ) }')
  79. # Store the value for comparation in variable
  80. nr=1.00
  81. # You can edit the nr variable according to your needs.
  82. # For example if you want a SMS if your load is above 0.50,
  83. # edit the nr to look like nr=0.50
  84. # Now compare the load and the nr given value.
  85. if [ $(echo "$load >= $nr"| bc) -eq 1 ]; then
  86. # If load variable is greater or equal to nr,
  87. #send a SMS with the time for which the value is achieved
  88. #and the load average of the past 1,10 and 15 minutes.
  89. echo $(uptime | awk '{print "Time: " $1,"-",$(NF-4),$(NF-3),$(NF-2),$(NF-1),$(NF)}') | twidge update
  90. fi
  91.  
  92. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement