Remote IT Support!! Please contact us

For Any Remote Support,Implementation/Upgrade Projects,Queries,Collaborations please mail us at support@funoracleapps.com

Script to Monitor CPU usage on Linux Server

No comments

Script to Monitor CPU usage on Linux Server

CPU monitoring is important when you are working on the server to know the load and the culprits. It also helps to find tune the databases, applications, servers


. There are many Web-based tools that is available to monitor the CPU. But in this post, I am sharing the shell script to monitor the CPU utilization and it will send a mail-in case the CPU crosses the defined threshold.



Shell script:

The script to alert when the CPU threshold crosses 90%.  It will also send information about top consumers.

#!/bin/bash
cpuuse=$(cat /proc/loadavg | awk '{print $3}'|cut -f 1 -d ".")
if [ "$cpuuse" -ge 90 ]; then
SUBJECT="ATTENTION: CPU load is high on $(hostname) at $(date)"
MESSAGE="/tmp/CPU_Mail.out"
TO="himanshu@funoracleapps.com"
  echo "CPU current usage is: $cpuuse%" >> $MESSAGE
  echo "" >> $MESSAGE
  echo "+------------------------------------------------------------------+" >> $MESSAGE
  echo "Top 20 processes which consuming high CPU" >> $MESSAGE
  echo "+------------------------------------------------------------------+" >> $MESSAGE
  echo "$(top -bn1 | head -20)" >> $MESSAGE
  echo "" >> $MESSAGE
  echo "+------------------------------------------------------------------+" >> $MESSAGE
  echo "Top 10 Processes which consuming high CPU using the ps command" >> $MESSAGE
  echo "+------------------------------------------------------------------+" >> $MESSAGE
  echo "$(ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10)" >> $MESSAGE
  mail -s "$SUBJECT" "$TO" < $MESSAGE
  rm /tmp/Mail.out
else
echo "Server CPU usage is in under threshold.CPU current usage is: $cpuuse%"
  fi

Schedule it in crontab as per need


 In the below example, I have scheduled it to run every 5 mins.

*/5 * * * * /bin/bash /root/scripts/cpu_load_checking.sh > /tmp/cpu_check_cron.log 2>&1
                                                    

If you like please follow and comment

No comments :

Post a Comment