Link to home
Start Free TrialLog in
Avatar of systemsautomation
systemsautomationFlag for Pakistan

asked on

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?
Avatar of arnold
arnold
Flag of United States of America image

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.
Avatar of systemsautomation

ASKER

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%
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

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'
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

Now this is the error

./highload.sh: line 7: [: missing `]'
replace the line:
if [ $IDLE -le 10  ; then
with
if [ $IDLE -le 10 ] ; then
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?
ASKER CERTIFIED SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial