SHELL SCRIPT TO MONITOR LOAD AVERAGE IN LINUX

Its too critical to monitor the load in linux system where multiple process spawn to bring down the performance and in the worst case, collapse the server. Even we can use top,vmstat or sar command to monitor load average but still in some of the releases, we dont get sar output stored in sa* files.

A custom shell script to monitor the load for every 30 seconds

Script:

One liner for load average monitor per 30 seconds

We get two output per every minute.So in a day there are 24 hours (1440 min) which gives us 1440 * 2 = 2880 outputs

So for 7 days we get 10080 min * 2 = 20160 outputs

[oracle@exdbadm01 ~]$ cat ldavg.sh
while true;do date;top -b -n 1 |awk '/load average/ {printf "5m -> %s 10m -> %s 15m -> %s\n",$10,$11,$12}';sleep 30;done

If you want to delete the old lines on the report,then you can use this script

#!/bin/bash


#load average per 30 seconds 

while true
  do
   date
   top -b -n 1 |awk '/load average/ {printf "5m - %s 10m - %s 15m - %s\n",$10,$11,$12}'
  sleep 30
done > ldavg


#delete old lines greater than 7 days

if [ `grep 'IST' ldavg |wc -l` -gt 20160 ]
  then
    sed -i '1,100d' ldavg
  else
    break
fi

Output:

[oracle@exdbadm01 ~]$ while true;do date;top -b -n 1 |awk '/load average/ {printf "5m -> %s 10m -> %s 15m -> %s\n",$10,$11,$12}';sleep 30;done



Sun May  9 13:55:25 IST 2021
5m -> 0.03, 10m -> 0.04, 15m -> 0.16
Sun May  9 13:55:56 IST 2021
5m -> 0.02, 10m -> 0.04, 15m -> 0.15
Sun May  9 13:56:26 IST 2021
5m -> 0.01, 10m -> 0.03, 15m -> 0.14
Sun May  9 13:56:57 IST 2021

NOTE: This script is an alternative for crontab schedule which runs in a loop. Run the script using nohup mode in background to initiate the script

[oracle@exdbadm01 ~]$ nohup sh ldavg.sh &
[1] 11723
[oracle@exdbadm01 ~]$ nohup: appending output to `nohup.out'

Monitor the output using tail

[oracle@exdbadm01 ~]$ tail -f ldavg
Tue May 11 13:44:59 IST 2021
5m - 0.05, 10m - 0.07, 15m - 0.04
Tue May 11 13:45:30 IST 2021
5m - 0.03, 10m - 0.06, 15m - 0.04
Tue May 11 13:46:01 IST 2021
5m - 0.02, 10m - 0.06, 15m - 0.04
Tue May 11 13:46:31 IST 2021
5m - 0.01, 10m - 0.05, 15m - 0.03
Tue May 11 13:47:02 IST 2021
5m - 0.00, 10m - 0.04, 15m - 0.03

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s