Return to Snippet

Revision: 15280
at June 30, 2009 01:35 by cshaiku


Initial Code
#!/bin/sh

# Start clean!
  if [ -f /tmp/emailTMP ]; then
    rm -rf /tmp/emailTMP
  fi

  if [ -f /tmp/procTMP ]; then
    rm -rf /tmp/procTMP
  fi

  if [ -f /tmp/sendmailTMP ]; then
    rm -rf /tmp/sendmailTMP
  fi

# Init variables
  eMail="[email protected]"
  serverLoadLog=/var/log/custom/serverLoad.log
  procTMP="/tmp/procTMP"
  memLimit=50
  memWarning=25
  memTripped=FALSE
  cpuLimit=99
  cpuWarning=50
  cpuTripped=FALSE

emailReport() {
  if [ -f /tmp/emailTMP ]; then
    echo "Subject: cshaiku.com Server Load Status"  > /tmp/sendmailTMP
    cat /tmp/emailTMP      >> /tmp/sendmailTMP
    printf "\n$emailDate"  >> /tmp/sendmailTMP
    echo "--[`date`]--"    >> $serverLoadLog
    cat /tmp/sendmailTMP   >> $serverLoadLog
    /usr/sbin/sendmail -v "$to" < /tmp/sendmailTMP > /dev/null
  fi
}

checkMemoryUsage() {
  printf "\n+----------+--------------+------+======+----------------------------------+" >> $serverLoadLog
  printf "\n|      PID |         User |  CPU |  MEM |                          Command |" >> $serverLoadLog
  printf "\n+----------+--------------+------+======+----------------------------------|" >> $serverLoadLog
  ps -eo pmem,pcpu,user,pid,comm | sort -rn | head -10 > $procTMP
  while read pmem pcpu user pid command
  do
    declare -i pmemINT=`echo $pmem | awk '{printf "%.0f",$1}'`
    if [ $pmemINT -gt $memLimit ]; then
      set memTripped=TRUE
      kill $pid
      printf "\n\nKilled pid $pid\t\tuser: $user\tcpu: $pcpu\tmem: $pmem\tCommand: $command\n" >> $serverLoadLog
      printf "\n\nKilled pid $pid\t\tuser: $user\tcpu: $pcpu\tmem: $pmem\tCommand: $command\n" >> /tmp/emailTMP
    elif [ $pmemINT -gt $memWarning ]; then
        set memTripped=TRUE
        printf "\n\npid $pid\t\tuser: $user\tcpu: $pcpu\tmem: $pmem\tommand: $command\n" >> $serverLoadLog
        printf "\n\npid $pid\t\tuser: $user\tcpu: $pcpu\tmem: $pmem\tommand: $command\n" >> /tmp/emailTMP
    else
      if [ $pid != "PID" ]; then
        printf "
|%9s | %12s | %4s | %4s | %32s |" $pid $user $pcpu $pmem $command >> $serverLoadLog
      fi
    fi
  
  done < $procTMP

  if [ $memTripped = TRUE ]; then
    printf "\nThe following commands have surpassed the Memory Usage threshold (%s)." "$memWarning%" >> /tmp/emailTMP
  fi
}

checkCPUUsage() {
  printf "\n+----------+--------------+======+------+----------------------------------+" >> $serverLoadLog
  printf "\n|      PID |         User |  CPU |  MEM |                          Command |" >> $serverLoadLog
  printf "\n+----------+--------------+======+------+----------------------------------|" >> $serverLoadLog
  ps -eo pcpu,pmem,user,pid,comm | sort -rn | head -10 > $procTMP
  while read pcpu pmem user pid command
  do
    declare -i pcpuINT=`echo $pcpu | awk '{printf "%.0f",$1}'`
    if [ "$pcpuINT" -gt "$cpuLimit" ]; then
      set cpuTripped=TRUE
      kill $pid
      printf "\n\nKilled pid $pid !\t\tuser: $user\tcpu: $pcpu\tmem: $pmem\tCommand: $command\n" >> $serverLoadLog
      printf "\n\nKilled pid $pid !\t\tuser: $user\tcpu: $pcpu\tmem: $pmem\tCommand: $command\n" >> /tmp/emailTMP
    elif [ "$pcpuINT" -gt "$cpuWarning" ]; then
        set cpuTripped=TRUE
        printf "\n\npid: $pid\tuser: $user\tcpu: $pcpu\tmem: $pmem\tCommand: $command\n" >> $serverLoadLog
        printf "\n\npid: $pid\tuser: $user\tcpu: $pcpu\tmem: $pmem\tCommand: $command\n" >> /tmp/emailTMP
    else
      if [ $pid != "PID" ]; then
        printf "
|%9s | %12s | %4s | %4s | %32s |" $pid $user $pcpu $pmem $command >> $serverLoadLog
      fi
    fi

  done < $procTMP

  if [ $cpuTripped = TRUE ]; then
    printf "\nThe following commands have surpassed the CPU Usage threshold (%s)." "$cpuWarning%" >> /tmp/emailTMP
  fi
}

# Main Loop
  checkCPUUsage
  checkMemoryUsage
  emailReport

# End Clean!
  rm -rf /tmp/emailTMP
  rm -rf /tmp/sendmailTMP
  rm -rf /tmp/procTMP

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

Initial Description


Initial Title
Server Load Emailer

Initial Tags
email, server, load

Initial Language
Bash