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