Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement CPU load Monitoring script

Shulou Source: shulou.com Published: 2022-06-03 07:41:34 09月26日 Update

This article will explain in detail how to implement the CPU load monitoring script. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The code is as follows:

#! / bin/bash

Load= `uptime`

Shijian= `date +% YMUE% mMUE% dMui% T`

File=cpustatus.txt

Touch $file

All= `cat $file | wc-l`

Printf "$load $shijian\ n" > > $file

If [$all > 60]; then

Starttime= `cat $file | awk'{print $13}'| sort-r | tail-1`

Stoptime= `cat $file | awk'{print $13}'| sort-n | tail-1`

Loadavg= `cat $file | awk'{avg1+=$10;avg2+=$11;avg3+=$12} END {print avg1/NR,avg2/NR,avg3/NR}'`

High= `cat $file | awk'{print $10}'| sort-n | tail-1`

Low= `cat $file | awk'{print $10}'| sort-r | tail-1`

Printf "$starttime\ n load average $loadavg\ nhigh $high\ nlow $low\ n $stoptime\ n\ n" > j

# printf "load average $startus\ n" > > j

# mv $f $f.roomb

Fi

It is better to use it with Crontab

The following is written by another person, and share it.

The code is as follows:

#! / 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 CPU on this server

Cpu_num= `grep-c 'model name' / proc/ cpuinfo`

Count_uptime= `uptime | wc-w`

# APCge load of the current system for 15 minutes

Load_15= `uptime | awk'{print $'$count_uptime'}'`

# obtain the average load value of a single core of the current system for 15 minutes. If the result is less than 1.0, the first digit is 0.

Average_load= `echo "scale=2;a=$load_15/$cpu_num;if (length (a) = = scale (a)) print 0X print a" | bc`

# take the integer of the average load value above

Average_int= `echo $average_load | cut-f 1-d "." `

# get the warning value for executing 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 "$IP server average load of a single core for 15 minutes is $average_load, which exceeds the warning value of 1.0.Please deal with it immediately! $(date +'% Y-%m-%d% HRV% MRV% S')"

Else

Load_now= `expr $average_load\ > $load_ Warn`

If [$load_now-eq 1]

Then

Echo "$IP server average load of a single core for 15 minutes is $average_load, which exceeds the warning value of 0.7.Please deal with it immediately! $(date +'% Y-%m-%d% HRV% MRV% S')"

Else

Echo "$IP server average load value of a single core for 15 minutes is $average_load,cpu core is $cpu_num, and the average load of the system for 15 minutes is $load_15 load normal $(date +'% Y-%m-%d% Haverage_load,cpu% MRV% S')"

Fi

Fi

This is the end of the article on "how to implement the CPU load monitoring script". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Server core service single article system script monitoring bit code more processing alert good practical personal single digit information content address Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Huawei Shulou Tech Info MySQL vpn