script to email cpu usage from top command every 30min

Please assist in scripting cpu monitoring alert.
The server is red hat linux

1) How can i script the cpu utlization output from 'top' command and than have that output be sent to my email every 30minutes.

2) Similar to the first question. How can i write a script, so that when cpu utlization has been above 90% for more than 60minutes, i would get an email alert.
assistunixAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
Shell script

#!/bin/bash

sender='senderemailaddress'
recipient='recipientemailaddress'

echo "To: $recipientemailaddrees
From: $senderemailaddress
Subject: CPU usage

$(/bin/top -1)

" |/usr/sbin/sendmail -oi -f$senderemailaddress -t

Open in new window


You need to have a simple shell script that loads the prior level and looks at the current load uptime or using snmp polling. It would need to store the data it gets.

The notification depends on how frequently you poll.  Have you considered using a monitoring tool such as nagios, etc.  you can configure the rules and get notification accordingly?

There are other alternatives.
0
Gerwin Jansen, EE MVETopic Advisor Commented:
You could get the output from top using batch mode like this:

top -b -n1 | grep -i ^cpu

This would get you this:

Cpu(s):  0.1%us,  0.2%sy,  0.0%ni, 98.0%id,  1.5%wa,  0.1%hi,  0.1%si,  0.0%st

You're interested in the current usage you could filter it like this:
top -b -n1 | grep -i ^cpu | awk '{print $2}' | cut -d"%" -f1

Open in new window

This would get you 0.1 from the above sample - you can use that number in your script for checking, mailing etc.

Depending on your top output, you may have to change the grep command and the filter accordingly.
0
assistunixAuthor Commented:
gerwinjansen, thank you for that.

how can i setup a script to send me an email alert for when the cpu remains above 90% for 60minutes.
the below command is able to give me current cpu usage.
top -b -n1 | grep -i ^cpu | awk '{print $2}' | cut -d"%" -f1

I want to receive email alert whenever the cpu remains above 90% for 60minutes.
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

arnoldCommented:
You can assign the above line to a variable
Cpuusage=$(top -b -n1.... )

If test "$Cpuusage" -ge "90"; then
echo .. Example before.


The above will email when during the check the threshold is met, if you need to maintain data, that requires additional logic and might be best served in use of other scripting languages to preserve prior state.
0
gheistCommented:
1) nagios cacti zabbix zenoss
2) nagios cacti zabbix zenoss
0
gheistCommented:
cacti is 100% pure PHP script, I suspect most of others too...
0
Gerwin Jansen, EE MVETopic Advisor Commented:
@gheist - In that case, I suggest you pick one of the 4 and post a working suggestion ;)
0
gheistCommented:
Roll the dice. All are working solutions with websites full of documentation icnluding beginners guide.

Think about returning from christmas vacation and mail every 30 minutes...

CPU usage tells very little
60% usage can be at highest or lowest power state of CPU (say 100Mhz and 3GHz) and does not indicate anything.
0
skullnobrainsCommented:
something like this will do

counter=0
top -d 1 | grep --line-buffered %Cpu | while read line
do
  if echo $line | grep --line-buffered^C[[:space:]][0-9],[0-9] id' \
  then
    counter=`expr $counter + 1`
    if test $counter -gt 1800
    then SEND YOUR ALERT MAIL HERE
      counter=0
    fi
  else
    counter=0
done

Open in new window


this will check cpu usage every second and send a mail if the idle cpu usage is a single digit 1800 times in a row

---

then i must say i totally agree this is a very inefficient way to measure sever load

you can either turn to accounting

or easier and reasonably efficient :

use the load average in a similar way as in the above script
reasonable thresholds for cpu usage could be
1* number of cpus : throttle
2.5*ncpu : normal load
5*ncpu : heavy load
10*ncpu : you're probably trashing already. response times are poor

additionnally, load averages are already given as averages so you can have a better idea of what is actually going on while polling every few seconds or minutes

load averages can be extracted using system-specific commands such as sysctl or uptime or from /proc or snmp depending on your system and they will also raise if you are short on I/O

of course thresholds would depend on the server usage
0
gheistCommented:
Formulas seem reasonable. Actually there are ways to stabilise system even with load average 100... (like cgroups for Linux)
Load averages also are shown in "uptime" and /proc/loadavg. It is less resources to read them there.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.