Operating System - HP-UX
1834742 Members
2936 Online
110070 Solutions
New Discussion

Re: Need scripts to generate the alterts if CPU utilization goes above 90%

 
SOLVED
Go to solution
praveen..
Super Advisor

Need scripts to generate the alterts if CPU utilization goes above 90%

Hi,
does anybody can help me regarding this:

I need to generate the alterts if CPU utilization goes above 90%, does any body has any script?

please suggest
15 REPLIES 15
Arunvijai_4
Honored Contributor

Re: Need scripts to generate the alterts if CPU utilization goes above 90%

Hi Praveen,

Try this.

perl -e 'alarm 360;while (1) {}'

while :
do
:
done

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
inventsekar_1
Respected Contributor

Re: Need scripts to generate the alterts if CPU utilization goes above 90%

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

Note: You want to read %idle less then 10%.
Be Tomorrow, Today.
praveen..
Super Advisor

Re: Need scripts to generate the alterts if CPU utilization goes above 90%

#vi cpu_alert.sh

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

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

will it mail to me if cpu utilization goes over 90%?

what will be crontab entry to run this script after 15 mins regularly?

please suggest
thankx in advance
Raj D.
Honored Contributor

Re: Need scripts to generate the alterts if CPU utilization goes above 90%

Praveen,

Check out this script to maintain a log for cpu and memory usage:

Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
praveen..
Super Advisor

Re: Need scripts to generate the alterts if CPU utilization goes above 90%

Hi,
I have created two files in /home/cpumem directory.

#cd /home/cpumem
#ll
-rw-r--r-- 1 root sys 1462 Jun 22 03:59 cpumem.log
-rwxrwxrwx 1 root sys 1282 Jun 22 03:52 cpumem.sh
-rwxrwxrwx 1 root sys 114 Jun 22 03:53 cpumem.syn

# more cpumem.sh
#File name : cpumem.sh
###########################################################################
# script for capturing CPU and Memory usuage over a period. 24 hours. #
# File Name: cpumem.sh By: Raj.D. #
# can be run throguh cron or script with nohup . #
# Files required: 1. cpumem.sh 2. cpumem.syn #
###########################################################################
cd /home/cpumem
touch cpumem.log

echo "----------------------------------------------------------------------------------" >> cpumem.log
echo " Date: Time: (%)CPU Used: (%)Memory Used: " >> cpumem.log
echo "----------------------------------------------------------------------------------" >> cpumem.log

cd /home/cpumem
/opt/perf/bin/glance -adviser_only -j 15 -iterations 5760 -syntax /home/cpumem/cpumem.syn >> cpumem.log

echo " " >> cpumem.log
echo "----------------------------------------------------------------------------------" >> cpumem.log
echo "Monitoring of CPU and Memory done at `date` . " >> cpumem.log
echo "UPTIME= `uptime ` " >> cpumem.log
echo "----------------------------------------------------------------------------------" >> cpumem.log


# more cpumem.syn
print gbl_statdate, " ", gbl_stattime, " ", gbl_cpu_total_util, " ", gbl_mem_util


#./cpumem.sh
Welcome to GlancePlus


::::
:::
it is a continous process, i need to kill it using .

can i kill it in scripts and how can i email logs to my email id?
Raj D.
Honored Contributor

Re: Need scripts to generate the alterts if CPU utilization goes above 90%

Hi Praveen ,

The script made to run for 24 hours once it has been executed , also you can interrupt by [ ctrl+c ].

check:
" -j 15 -iterations 5760 ", ie (15*5760 sec /3600 = 24 hr. ). You can customize as per need.


You can add a cron entry to run the script every day at 2am by:
###################################
00 2 * * * /home/cpumem/cpumem.sh
###################################


------------------------------------------

For emailing you can add this line to the end of the script of cpumem.sh .

########################################
# moving log file to todays log file name.
mv cpumem.log cpumem.`date +%m%d%y`.log
#--------------------------------------------

# Emailing the log file :
uuencode cpumem.`date +%m%d%y`.log cpumem.`date +%m%d%y`.log | mailx -s "CPU/MEM LOG for last 24 hours , Dated: `date` " your_email@domain.com
echo "Email sent ....with the attachment with cpumem log file "
########################################



Hope this will help ,

Cheers,
Raj.

" If u think u can , If u think u cannot , - You are always Right . "
Raj D.
Honored Contributor

Re: Need scripts to generate the alterts if CPU utilization goes above 90%

Praveen,

The log file will looks like as below:


#cat cpumem.log
----------------------------------------------------------------------------------
Date: Time: (%)CPU Used: (%)Memory Used:
----------------------------------------------------------------------------------
06/23/06 01:00:10 11.0 26.9
06/23/06 01:00:40 3.0 26.9
06/23/06 01:01:10 3.7 26.9
06/23/06 01:01:40 2.6 26.9
06/23/06 01:02:10 2.9 26.9
06/23/06 01:02:40 3.0 26.9
06/23/06 01:03:10 3.2 26.9
06/23/06 01:03:40 5.4 26.9
06/23/06 01:04:10 2.6 26.9
06/23/06 01:04:40 3.1 26.9
06/23/06 01:05:10 7.6 27.1
06/23/06 01:05:40 7.2 27.0
-----------------------------------------


Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Raj D.
Honored Contributor

Re: Need scripts to generate the alterts if CPU utilization goes above 90%

Output was not displaing properly in last post ,attached here with , how the log file will look like;. This data is very perfect as taken from glance and also matches with sar.

hth,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
praveen..
Super Advisor

Re: Need scripts to generate the alterts if CPU utilization goes above 90%

Hi Raj,
thanks for you help,

please let me know as this script is generating the logs of after every 15 secs.

i need to generate the logs after 10 mins for 24 hours.

what will be entry in place of 15 & 5760?

thanks in advance
Raj D.
Honored Contributor
Solution

Re: Need scripts to generate the alterts if CPU utilization goes above 90%


Praveen ,

Good question,
what will be entry in place of 15 & 5760?

( 15, 5760 this will give the cpu/mem usuage after every 15 seconds)


You can just put 600 & 144 to get usuage after every 10 min interval, for total 24 hours.
And can be run through cron , once in everyday , as this covers 24 hours.


Cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
praveen..
Super Advisor

Re: Need scripts to generate the alterts if CPU utilization goes above 90%

hi raj,
thanks a lot for my help.

please let me know
when i use this command, i am not getting any mail. (I have created this file cpumem.`date +%m%d%y`.log)

#uuencode cpumem.`date +%m%d%y`.log cpumem.`date +%m%d%y`.log | mailx -s "CPU/MEM LOG for last 24 hours , Dated: `date` " your_email@domain.com

but when i use this command, i am getting the desired output:

#cat cpumem.`date +%m%d%y`.log | mailx -s "CPU/MEM LOG for last 24 hours , Dated: `date` " your_email@domain.com

Raj D.
Honored Contributor

Re: Need scripts to generate the alterts if CPU utilization goes above 90%

Praveen ,

uuencode gives problem with some email clients, if with cat command it is working fine, you can use the cat command .
Basically uuencode is used with mailx to attach any file with the email. As it is a text file , cat command will do the same tricks ,



cheers,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Steven E. Protter
Exalted Contributor

Re: Need scripts to generate the alterts if CPU utilization goes above 90%

Shalom,

If you persist in this as I've advised against, email your results as followes:

Route your report to a text file.

Use:
http://www.hpux.ws/mailfile2

To send the report as an attachment.

Its production code all over the world now, based on code I found with an itrc search.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
praveen..
Super Advisor

Re: Need scripts to generate the alterts if CPU utilization goes above 90%

hi raj & everybody,
is that the instantaneous cpu usage at that time or the average cpu utilization over the 10 minute period?
praveen..
Super Advisor

Re: Need scripts to generate the alterts if CPU utilization goes above 90%

hi raj & everybody,
is that the instantaneous cpu usage at that time or the average cpu utilization over the 10 minute period?