1821187 Members
3200 Online
109631 Solutions
New Discussion юеВ

CPU Usage Script

 
SOLVED
Go to solution
Sial_1
Frequent Advisor

CPU Usage Script

Hi,

can someone help me to find the script which will help me to monitor cpu usage for HP-UX 11.31 & 11.23, and if the usages go above 80% I will get a email atomically.

Thanks in advance.
11 REPLIES 11
Dennis Handly
Acclaimed Contributor

Re: CPU Usage Script

What tool is going to measure the CPU usage, top? And you only want the average of all N CPUs?
Sial_1
Frequent Advisor

Re: CPU Usage Script

Yes we can use top, and average of all CPUs.
Dennis Handly
Acclaimed Contributor

Re: CPU Usage Script

You can start with:
$ top -f /var/tmp/top.out -n 100 -d 1
$ awk '/^avg/ { print 100 - $6; exit}' /var/tmp/top.out

This prints the average % CPU utilization.
Sial_1
Frequent Advisor

Re: CPU Usage Script

Dennis

Need script that sends me an email if CPUs average usage goes above 80%.
Raj D.
Honored Contributor

Re: CPU Usage Script

Sial,
Check this out: cpumem.sh

http://forums13.itrc.hp.com/service/forums/questionanswer.do?&threadId=1037077

I havce a latest version of it also , for v2 and v3, just try that out. It works with glance ,and keeps a log of cpu/mem/diskio ..,

How ever if you have implemented OVO , that will send cpu threshold alert.

Hth,
Raj.

" If u think u can , If u think u cannot , - You are always Right . "
Sial_1
Frequent Advisor

Re: CPU Usage Script

Thanks Raj, cpumem.sh only creates log file that has cpu and mem average%. But I need script that sends me an email if my system├в s CPUs usage average touches 70%.

Rgd,
Elmar P. Kolkman
Honored Contributor

Re: CPU Usage Script

Why not use sar for information collection and running a script from cron that checks if the CPU usage gets too high ?
Every problem has at least one solution. Only some solutions are harder to find.
Raj D.
Honored Contributor

Re: CPU Usage Script

Sial,

from sar output you can get email alert if cpu usage is more than 80% , you need have a script with loop, or may be using cron ,

# sar -u 5 20
- grep the cpu usage field
- using awk '{ if ( $NF > 80 ) print $0 }' >> logfile, analyse the log file again with a condition and if it is greater than 80 for sometime then send email, else OK.

That logic could be use ,

Hth,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Sial_1
Frequent Advisor

Re: CPU Usage Script

Ok, can you help me to write a script using sar?
Raj D.
Honored Contributor
Solution

Re: CPU Usage Script

Sial,

check this out:
You can put the script in cron in 5 min interval , so that it will check every 5 min.



#!/usr/bin/sh
## Script to check cpu usage if reached 80% . cpu_alert_80_email.sc [v1] 08-18-2010
## Checks every 30 second per script execution.
######################################################################################################
HOST=`hostname`
sar -u 3 10 | grep -v idle | grep -v `hostname` |awk '{if ($NF<20) print $0 }'| awk /./ > result


if [ -s result ]
then
#result nonZero: Alert!! and Email:
echo "\n\n------------------------------------------------------------------------"
echo "ALERT!!!! CPU threshold reached and more than 80%"
echo "------------------------------------------------------------------------\n"
echo "Current cpu usage : "
echo "$HOST %usr %sys %wio %idle" >> result
echo "------------------------------------------------------------------------\n" >> result ; cat result
echo "Sending Email Alert........"
#cat result | mailx -s "CPU usage reached threshold: 80% on $HOST " youremail@domain.com

else
echo "Threshold not reached. [OK] "
ls -l result
fi
########################################################################################################










$ ./cpu_alert_80_email.sc


------------------------------------------------------------------------
ALERT!!!! CPU threshold reached and more than 80%
------------------------------------------------------------------------

Current cpu usage :
22:03:23 92 2 4 2
22:03:25 90 2 4 4
Average 91 2 4 3
hpux1120 %usr %sys %wio %idle
------------------------------------------------------------------------
Sending Email Alert........

$




Enjoy,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Sial_1
Frequent Advisor

Re: CPU Usage Script

Thanks Raj & Dennis for your assistance.