SoFunction
Updated on 2025-03-10

CPU load monitoring script code


#!/bin/bash 
#Extract the IP address information of this server
IP=`ifconfig eth0 | grep "inet addr" | cut -f 2 -d ":" | cut -f 1 -d " "` 
#Extract the number of CPUs on this server
cpu_num=`grep -c 'model name' /proc/cpuinfo` 
count_uptime=`uptime |wc -w` 
#The current system's average load value of 15 minutes
load_15=`uptime | awk '{print $'$count_uptime'}'` 
#Get the average load value of a single core of the current system for 15 minutes, and if the result is less than 1.0, the previous single digits are supplemented with 0.
average_load=`echo "scale=2;a=$load_15/$cpu_num;if(length(a)==scale(a)) print 0;print a" | bc` 
#Take single-digit integers of the average load value above
average_int=`echo $average_load | cut -f 1 -d "."` 
#Get the warning value of the shell input (within 0-100)
warn_input=$1 
if [[ ! $warn_input =~ ^[0-100]+$ ]] 
then 
    exit 0 
else 
    if [[ "$warn_input" -lt 0 || "$warn_input" -gt 100 ]] 
    then 
        exit 0 
    else 
        load_warn=$(($warn_input/100)) 
    fi 
fi 
if [ $average_int -gt 0  ] 
then 
echo "The average load of a single core of $IP server in 15 minutes is $average_load, exceeding the alert value of 1.0, please deal with it immediately!!! $(date +'%Y-%m-%d %H:%M:%S')"
else 
    load_now=`expr $average_load \> $load_warn` 
    if [ $load_now -eq 1 ] 
    then 
echo "The average load of a single core of $IP server in 15 minutes is $average_load, exceeding the alert value of 0.7, please deal with it immediately!!! $(date +'%Y-%m-%d %H:%M:%S')"
    else 
echo "The average load value of a single core of the $IP server in 15 minutes is $average_load, the number of CPU cores is $cpu_num, and the average load of the system in 15 minutes is $load_15. The load is normal. $(date +'%Y-%m-%d %H:%M:%S')"
    fi 
fi