Real time CPU Monitoring shell script Get alert when CPU utilization is more

I have published an article previously to monitor CPU utilization See Here Now i am going to publish Real time CPU Monitoring shell script Get alert when CPU Utilization is more. If CPU utilization is high get alert immediately.

What are the distributions you can use these script to monitor real time CPU Utilization.

  1. RHEL
  2. Centos
  3. Fedora
  4. Ubuntu
  5. All Unix and Linux Environments

Real time CPU Monitoring shell script Get alert when CPU utilization is more

#!/bin/bash
## CPU Utilization Monitoring Script
## Author: Ankam Ravi Kumar
## Date: 31st August 2016
## Published Website: https://arkit.co.in

Z=`awk '/\\#define Z/ {print $3}' /usr/include/asm-generic/param.h`
CPUS=`grep ^proc /proc/cpuinfo | wc -l`
NUM1=`cat /proc/stat | awk '/^cpu / {print $5}'`
sleep 1
NUM2=`cat /proc/stat | awk '/^cpu / {print $5}'`
USED=`echo 4 k 100 $NUM2 $NUM1 - $CPUS $Z \\* / 100 \\* - p | dc`

if [ $USED -ge 95 ]; then
echo "CPU Utilization is ${USED}" | mail -s "CPU Utilization is High" admin@arkit.co.in
else
echo "CPU Utilization is Normal"
fi

How to Use CPU Utilization Monitoring Script

Create a file using below command and copy above script paste in below file

# vi cpustatus.sh
PASTE Script Here###
:wq

Then Execute using below methods

First method

# sh cpustatus.sh

Second method

# ./cpustatus.sh

Related Articles

Gather Contact information Nagios XI

Creating Multiple Users with single command

File System Usage Monitoring Script

Thanks for the read please comment your feedback.

Real time CPU Monitoring shell script Get alert when CPU utilization is more Real time CPU Monitoring shell script Get alert when CPU utilization is more Real time CPU Monitoring shell script Get alert when CPU utilization is more

Thanks for your wonderful Support and Encouragement

Ravi Kumar Ankam

My Name is ARK. Expert in grasping any new technology, Interested in Sharing the knowledge. Learn more & Earn More

6 Responses

  1. Naresh says:

    Thanks for the script, but i am getting below errors while running it.

    awk: cmd. line:1: fatal: cannot open file `/usr/include/asm-generic/param.h’ for reading (No such file or directory)
    dc: stack empty
    cpustatus.sh: line 11: [: 25.9000: integer expression expected
    CPU Utilization is Normal

    awk: cmd. line:1: fatal: cannot open file `/usr/include/asm-generic/param.h’ for reading (No such file or directory)
    dc: stack empty
    cpustatus.sh: line 11: [: 6.3400: integer expression expected
    CPU Utilization is Normal

    I am running as root and os version is RHEL 5.x

  2. blank Shaffie says:

    Am also getting the same error
    dc: stack empty
    ./cpu_usage.sh: line 14: [: 6.3400: integer expression expected
    CPU Utilization is Normal

  3. blank ankit says:

    Yes Same here getting the same error message
    dc: stack empty
    cpustatus.sh: line 8: [: 100.0000: integer expression expected
    CPU Utilization is Normal

Leave a Reply

Your email address will not be published. Required fields are marked *