?
Solved

Email alert on high cpu usage

Posted on 2009-12-23
9
Medium Priority
?
1,291 Views
Last Modified: 2012-08-13
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?
0
Comment
Question by:systemsautomation
  • 5
  • 4
9 Comments
 
LVL 81

Expert Comment

by:arnold
ID: 26116280
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
 

Author Comment

by:systemsautomation
ID: 26117840
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
 
LVL 81

Expert Comment

by:arnold
ID: 26119542
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:systemsautomation
ID: 26135830
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
 
LVL 81

Expert Comment

by:arnold
ID: 26137935
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
 

Author Comment

by:systemsautomation
ID: 26138094
Now this is the error

./highload.sh: line 7: [: missing `]'
0
 
LVL 81

Expert Comment

by:arnold
ID: 26138122
replace the line:
if [ $IDLE -le 10  ; then
with
if [ $IDLE -le 10 ] ; then
0
 

Author Comment

by:systemsautomation
ID: 26160951
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
 
LVL 81

Accepted Solution

by:
arnold earned 2000 total points
ID: 26162014
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month17 days, 4 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question