How can I check the last 5 minutes of total CPU usage using SAR

I know this example sar sar -u 1 3 , which gives statistics over the next 3 seconds at intervals of 1 second.

However, sar also continues to collect information in the background (My cron is set to collect statistics for every minute). Is there a way that I can simply query with the sar command to report the last 5 minutes of statistics and its average value.

Now I use the following command

 interval=5; sar -f /var/log/sysstat/sa22 | tail -n $interval | head -n -1 | awk '{print $4+$6}'| awk '{s+=$1} END {print s/$interval}' 

to check the total CPU usage in the last 5 minutes.

Is there a better way?

+5
source share
1 answer

Unfortunately, when you use the -f option in sar along with the interval and count, it does not return the average value for this interval (as you would expect). Instead, it always returns the first recorded value in the sar file

The only way around this is to use the -s option, which allows you to specify the start time of the sampling period. I have provided a perl script below that ends with a sar call, which is constructed in such a way as to return what you are looking for.

Hope this helps.

Peter Rhodes.

 #!/usr/bin/perl $interval = 300; # seconds. $epoch = `date +%s`; $epoch -= $interval; $time = `date -d \@$epoch +%H:%M:00`; $dom = `date +%d`; chomp($time,$dom); system("sar -f /var/log/sysstat/sa$dom -B -s $time 300 1"); 
0
source

Source: https://habr.com/ru/post/1242430/


All Articles