hooligan

Untitled

Feb 18th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Script to notify admin user if Linux,FreeBSD load crossed certain limit
  4. # It will send an email notification to admin.
  5. # Set up limit below
  6. NOTIFY="10.0"
  7. # admin user email id
  8. # Subject for email
  9. SUBJECT="Alert $(hostname) load average"
  10. # -----------------------------------------------------------------
  11. # Os Specifc tweaks do not change anything below ;)
  12. OS="$(uname)"
  13. TRUE="1"
  14. if [ "$OS" == "FreeBSD" ]; then
  15. TEMPFILE="$(mktemp /tmp/$(basename $0).tmp.XXX)"
  16. FTEXT='load averages:'
  17. elif [ "$OS" == "Linux" ]; then
  18. TEMPFILE="$(mktemp)"
  19. FTEXT='load average:'
  20. fi
  21. # get first 5 min load
  22. F5M="$(uptime | awk -F "$FTEXT" '{ print $2 }' | cut -d, -f1)"
  23. # 10 min
  24. F10M="$(uptime | awk -F "$FTEXT" '{ print $2 }' | cut -d, -f2)"
  25. # 15 min
  26. F15M="$(uptime | awk -F "$FTEXT" '{ print $2 }' | cut -d, -f3)"
  27. # mail message
  28. # keep it short coz we may send it to page or as an short message (SMS)
  29. echo "Load average Crossed allowed limit $NOTIFY." >> $TEMPFILE
  30. echo "Hostname: $(hostname)" >> $TEMPFILE
  31. echo "Local Date & Time : $(date)" >> $TEMPFILE
  32. # Look if it crossed limit
  33. # compare it with last 15 min load average
  34. RESULT=$(echo "$F15M > $NOTIFY" | bc)
  35. # if so send an email
  36. if [ "$RESULT" == "$TRUE" ]; then
  37. echo "-------------------------------------------" >> $TEMPFILE
  38. echo "System snapshot from top:" >> $TEMPFILE
  39. echo "-------------------------------------------" >> $TEMPFILE
  40. top -b -n1 >> $TEMPFILE
  41. mail -s "$SUBJECT" "$EMAIL" < $TEMPFILE
  42. fi
  43. # remove file
  44. rm -f $TEMPFILE
Advertisement
Add Comment
Please, Sign In to add comment