Server Load Emailer


/ Published in: Bash
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #!/bin/sh
  2.  
  3. # Start clean!
  4. if [ -f /tmp/emailTMP ]; then
  5. rm -rf /tmp/emailTMP
  6. fi
  7.  
  8. if [ -f /tmp/procTMP ]; then
  9. rm -rf /tmp/procTMP
  10. fi
  11.  
  12. if [ -f /tmp/sendmailTMP ]; then
  13. rm -rf /tmp/sendmailTMP
  14. fi
  15.  
  16. # Init variables
  17. eMail="email@somewhere.com"
  18. serverLoadLog=/var/log/custom/serverLoad.log
  19. procTMP="/tmp/procTMP"
  20. memLimit=50
  21. memWarning=25
  22. memTripped=FALSE
  23. cpuLimit=99
  24. cpuWarning=50
  25. cpuTripped=FALSE
  26.  
  27. emailReport() {
  28. if [ -f /tmp/emailTMP ]; then
  29. echo "Subject: cshaiku.com Server Load Status" > /tmp/sendmailTMP
  30. cat /tmp/emailTMP >> /tmp/sendmailTMP
  31. printf "\n$emailDate" >> /tmp/sendmailTMP
  32. echo "--[`date`]--" >> $serverLoadLog
  33. cat /tmp/sendmailTMP >> $serverLoadLog
  34. /usr/sbin/sendmail -v "$to" < /tmp/sendmailTMP > /dev/null
  35. fi
  36. }
  37.  
  38. checkMemoryUsage() {
  39. printf "\n+----------+--------------+------+======+----------------------------------+" >> $serverLoadLog
  40. printf "\n| PID | User | CPU | MEM | Command |" >> $serverLoadLog
  41. printf "\n+----------+--------------+------+======+----------------------------------|" >> $serverLoadLog
  42. ps -eo pmem,pcpu,user,pid,comm | sort -rn | head -10 > $procTMP
  43. while read pmem pcpu user pid command
  44. do
  45. declare -i pmemINT=`echo $pmem | awk '{printf "%.0f",$1}'`
  46. if [ $pmemINT -gt $memLimit ]; then
  47. set memTripped=TRUE
  48. kill $pid
  49. printf "\n\nKilled pid $pid\t\tuser: $user\tcpu: $pcpu\tmem: $pmem\tCommand: $command\n" >> $serverLoadLog
  50. printf "\n\nKilled pid $pid\t\tuser: $user\tcpu: $pcpu\tmem: $pmem\tCommand: $command\n" >> /tmp/emailTMP
  51. elif [ $pmemINT -gt $memWarning ]; then
  52. set memTripped=TRUE
  53. printf "\n\npid $pid\t\tuser: $user\tcpu: $pcpu\tmem: $pmem\tommand: $command\n" >> $serverLoadLog
  54. printf "\n\npid $pid\t\tuser: $user\tcpu: $pcpu\tmem: $pmem\tommand: $command\n" >> /tmp/emailTMP
  55. else
  56. if [ $pid != "PID" ]; then
  57. printf "
  58. |%9s | %12s | %4s | %4s | %32s |" $pid $user $pcpu $pmem $command >> $serverLoadLog
  59. fi
  60. fi
  61.  
  62. done < $procTMP
  63.  
  64. if [ $memTripped = TRUE ]; then
  65. printf "\nThe following commands have surpassed the Memory Usage threshold (%s)." "$memWarning%" >> /tmp/emailTMP
  66. fi
  67. }
  68.  
  69. checkCPUUsage() {
  70. printf "\n+----------+--------------+======+------+----------------------------------+" >> $serverLoadLog
  71. printf "\n| PID | User | CPU | MEM | Command |" >> $serverLoadLog
  72. printf "\n+----------+--------------+======+------+----------------------------------|" >> $serverLoadLog
  73. ps -eo pcpu,pmem,user,pid,comm | sort -rn | head -10 > $procTMP
  74. while read pcpu pmem user pid command
  75. do
  76. declare -i pcpuINT=`echo $pcpu | awk '{printf "%.0f",$1}'`
  77. if [ "$pcpuINT" -gt "$cpuLimit" ]; then
  78. set cpuTripped=TRUE
  79. kill $pid
  80. printf "\n\nKilled pid $pid !\t\tuser: $user\tcpu: $pcpu\tmem: $pmem\tCommand: $command\n" >> $serverLoadLog
  81. printf "\n\nKilled pid $pid !\t\tuser: $user\tcpu: $pcpu\tmem: $pmem\tCommand: $command\n" >> /tmp/emailTMP
  82. elif [ "$pcpuINT" -gt "$cpuWarning" ]; then
  83. set cpuTripped=TRUE
  84. printf "\n\npid: $pid\tuser: $user\tcpu: $pcpu\tmem: $pmem\tCommand: $command\n" >> $serverLoadLog
  85. printf "\n\npid: $pid\tuser: $user\tcpu: $pcpu\tmem: $pmem\tCommand: $command\n" >> /tmp/emailTMP
  86. else
  87. if [ $pid != "PID" ]; then
  88. printf "
  89. |%9s | %12s | %4s | %4s | %32s |" $pid $user $pcpu $pmem $command >> $serverLoadLog
  90. fi
  91. fi
  92.  
  93. done < $procTMP
  94.  
  95. if [ $cpuTripped = TRUE ]; then
  96. printf "\nThe following commands have surpassed the CPU Usage threshold (%s)." "$cpuWarning%" >> /tmp/emailTMP
  97. fi
  98. }
  99.  
  100. # Main Loop
  101. checkCPUUsage
  102. checkMemoryUsage
  103. emailReport
  104.  
  105. # End Clean!
  106. rm -rf /tmp/emailTMP
  107. rm -rf /tmp/sendmailTMP
  108. rm -rf /tmp/procTMP

URL: http://code.cshaiku.com/code_server_load_emailer.php

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.