Email alert on high cpu usage

My server is running oracle on fedora core 8.
I want to have an alert to my email address if cpu consumption reaches 90%. i have an smtp server running on windows in the same network.
Please guide me how to do this?
systemsautomationAsked:
Who is Participating?
 
arnoldCommented:
The IDLE will be adjusted accourding to the system.
It either would round below .5 down above .5 up. or it could do an even round below and equal .5 down for even numbers as the result or up for odd.  2.5 will be rounded down while 3.5 will be rounded up.
#!/bin/bash
#snmpwalk -v 2c -c 'RO community' hostname HOST-RESOURCES-MIB::hrProcessorLoad
#will give you a list of the loads on individual porcessors. (multi CPU/Multi-core)
#that will need to be parsed A simpler method below

IDLE=$(top -n 1 | grep Cpu | awk ' (length($1)>0) { printf "%.0f",$5 } ' | sed -e 's/%//' -e 's/id\,//')
CPU=$(echo 100 - $IDLE | bc)
if [ $CPU -gt 90 ] ; then
echo CPU load is $CPU.
#do email message here
echo "To: <recipient>
From: <Sender>
Subject: CPU Load is $CPU

some message
" |/usr/bin/sendmail -oi -t 
#if you have an SNMP monitoring utility that has SNMPtrapd running
# you can use snmptrapto generate alerts HOST-RESOURCES-MIB::hrProcessorLoad.1 $CPU
#but you have to convert the HOST-RESOURCES-MIB::hrProcessorLoad.1  into an OID
fi

Open in new window

0
 
arnoldCommented:
Run a cron job that checks the CPU usage.
Are you familiar with a bash,perl script?
Do you have a local SMTP service setup as well?


echo "To: <emailaddress>
From: <senderemailaddress>
Subject: CPU level

CPU load hi
" | /usr/bin/sendmail -oi -t

If you have SNMP polling setup, you could use SNMP to collect data and generate alerts.
snmptrap.
0
 
systemsautomationAuthor Commented:
Hello

1. How to check if the CPU is running with heavy load dynamically (within the script)?
2. At what interval cronjob should run? Running every 5 min may put extra load on machine.
3. The mail should be sent ONLY when the load is high say over 90%
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
arnoldCommented:
You can use uptime, top,snmpget
every five minutes seems reasonable. If you run it less frequently you may not detect the CPU spikes.
 
A less load intensive way of monitoring a system is using SNMP.
note if you have multiple CPUs or multi-core CPU, you may have a list of Cpu entries (individual cpu/core information.
#!/bin/bash
#snmpwalk -v 2c -c 'RO community' hostname HOST-RESOURCES-MIB::hrProcessorLoad
#will give you a list of the loads on individual porcessors. (multi CPU/Multi-core)
#that will need to be parsed A simpler method below

IDLE=$(top -n 1 | grep Cpu | awk ' (length($1)>0) { print $5 } ' | sed -e 's/%//' -e 's/id\,//')
CPU=$(echo 100 - $IDLE | bc)
if [ $CPU -gt 90 ) ; then
echo CPU load is $CPU.
#do email message here
echo "To: <recipient>
From: <Sender>
Subject: CPU Load is $CPU

some message
" |/usr/bin/sendmail -oi -t 
#if you have an SNMP monitoring utility that has SNMPtrapd running
# you can use snmptrapto generate alerts HOST-RESOURCES-MIB::hrProcessorLoad.1 $CPU
#but you have to convert the HOST-RESOURCES-MIB::hrProcessorLoad.1  into an OID
fi

Open in new window

0
 
systemsautomationAuthor Commented:
Hello.

When I runt the script following errors came:

(standard_in) 1: illegal character: ^[
(standard_in) 1: syntax error
(standard_in) 1: illegal character: ^[
(standard_in) 1: illegal character: ^[
(standard_in) 1: syntax error
(standard_in) 1: syntax error
(standard_in) 1: illegal character: ^[
(standard_in) 1: illegal character: ^[
(standard_in) 1: illegal character: ^[
(standard_in) 1: syntax error
(standard_in) 1: syntax error
(standard_in) 1: illegal character: ^[
(standard_in) 1: illegal character: ^[
(standard_in) 2: syntax error
./highload.sh: line 8: syntax error near unexpected token `)'
./highload.sh: line 8: `if [ $CPU -gt 90 ) ; then'
0
 
arnoldCommented:
The issue seems to be with the CPU= line.
Just use the IDLE information instead.
#!/bin/bash
#snmpwalk -v 2c -c 'RO community' hostname HOST-RESOURCES-MIB::hrProcessorLoad
#will give you a list of the loads on individual porcessors. (multi CPU/Multi-core)
#that will need to be parsed A simpler method below

IDLE=$(top -n 1 | grep Cpu | awk ' (length($1)>0) { print $5 } ' | sed -e 's/%//' -e 's/id\,//')
if [ $IDLE -le 10  ; then
echo CPU load is above 90%.
#do email message here
echo "To: <recipient>
From: <Sender>
Subject: CPU Load is above 90%

idle use is $IDLE

some message
" |/usr/bin/sendmail -oi -t 
#if you have an SNMP monitoring utility that has SNMPtrapd running
# you can use snmptrapto generate alerts HOST-RESOURCES-MIB::hrProcessorLoad.1 $CPU
#but you have to convert the HOST-RESOURCES-MIB::hrProcessorLoad.1  into an OID
fi

Open in new window

0
 
systemsautomationAuthor Commented:
Now this is the error

./highload.sh: line 7: [: missing `]'
0
 
arnoldCommented:
replace the line:
if [ $IDLE -le 10  ; then
with
if [ $IDLE -le 10 ] ; then
0
 
systemsautomationAuthor Commented:
Now this is the error:

./highload.sh: line 7: [: 67.0: integer expression expected


Can you please try to run the whole script at your end first?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.