Operating System - HP-UX
1834247 Members
2503 Online
110066 Solutions
New Discussion

problem with "sar" command

 
praveen..
Super Advisor

problem with "sar" command

hi,
I got a script to generate the alerts if CPU utilization goes over 80%:

#vi cpu_alert.sh
sar -u 1 10 |awk '{print $5}' | if [ "$5" -le 20 ]; then
echo "High CPU Utilization" | mailx -s 'subject' praveen@domain.com
fi

:wq!
#chmod 777 cpu_alert.sh
#./cpu_alert.sh
#

then everytime i am getting a email alert while the cpu utilization is only 2% means idle value is 99%.

this is the output of :

#sar 1 5
HP-UX blkbrd B.11.11 U 9000/800 06/22/06

07:05:43 %usr %sys %wio %idle
07:05:44 0 0 1 99
07:05:45 0 0 0 100
07:05:46 0 0 1 99
07:05:47 0 1 0 99
07:05:48 0 0 2 98

Average 0 0 1 99

why i am getting alert?



7 REPLIES 7
Peter Nikitka
Honored Contributor

Re: problem with "sar" command

Hi,

your pipe to the 'if construct' needs repair:

sar -u 1 10 |awk 'NF==5 && $0 !~
"%" {if ($5+0 <= 20) print $1,"High CPU Utilization"}' | mailx -s 'subject' praveen@domain.com

BTW: I assume this mail gets filled up with more information in your real application...

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Hein van den Heuvel
Honored Contributor

Re: problem with "sar" command


Praveen,

your simple awk command will send the word '%idle' to the shell for comparison against 20. You don't want that.

Try to skip the sar header line with something like:

awk "/idle/{next};{print $5}"

Hein.
Ninad_1
Honored Contributor

Re: problem with "sar" command

Praveen,

I think it should be like this

sar -u 10 1 | egrep -v "^$|HP|Average|idle" | awk '{print $5}'| while read idleper
do
if [[ "$idleper" -le "20" ]]
then
echo "High CPU Utilization" | mailx -s 'subject' praveen@domain.com
fi
done


Regards,
Ninad
Ninad_1
Honored Contributor

Re: problem with "sar" command

Peter,

With your command, I feel that mail will be sent to user whatever the case may be (even if cpu is not highly used), only thing is that the message High CPU utilisation will be sent if condition of high cpu util is satisfied.

Regards,
Ninad
James R. Ferguson
Acclaimed Contributor

Re: problem with "sar" command

Hi Praveen:

Well, several things are amiss. You can't reference $5 outside 'awk'. You are testing for a value that is less than 20 but you want to test for a value that is greater than 80, and, you are passing "down the pipe" every sample rather than looking at the average!

One possible variation is:

sar -u 1 10 | \
if [ `awk '/^Average/ {print $5}'` -gt 80 ]; then
echo "High CPU Utilization" | mailx -s 'subject' praveen@domain.com
fi

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: problem with "sar" command

Hi (again) Praveen:

Ooops! [ENOCOFFEE]! Testing less than or equal 20 is correct since it is *idle*, not a total utilization:

sar -u 1 10 | \
if [ `awk '/^Average/ {print $5}'` -le 20 ]; then
echo "High CPU Utilization" | mailx -s 'subject' praveen@domain.com
fi

Regards!

...JRF...

Peter Nikitka
Honored Contributor

Re: problem with "sar" command

Hi Ninad,

you are partially correct; I will send a mail with no data when the idle value is not above the threshold. So we should change

sar -u 1 10 |awk 'NF==5 && $0 !~ "%" {if ($5+0 <= 20) print $1,"High CPU Utilization"}' | mailx ...

to

message=$(sar -u 1 10 |awk 'NF==5 && $0 !~ "%" {if ($5+0 <= 20) print $1,"High CPU Utilization"}')
[ -n "$message" ] && print "$message" | mailx ...

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"