?
Solved

script to get cpu utilization and compare it against a value

Posted on 2009-05-05
11
Medium Priority
?
582 Views
Last Modified: 2013-12-26
Hi I need assistance in a script which main purpose would be to display the cpu utilization and if exceeds threshold it will generate a file and it will send out and email.
#!/bin/bash
 
#LOGFILE=/var/log/monitoring/cpu/util
 
#mkdir -p $LOGFILE
 
#mpstat -u > $LOGFILE/util
UTIL="mpstat -u"
 
#cat $LOGFILE/util | grep -Ev "Linux|%" > $LOGFILE/util1
UTIL1="mpstat -u|grep -Ev "Linux|%""
 
#Deleting empty lines
#sed '/^$/d' $LOGFILE/util1 >> $LOGFILE/util2
UTIL2="sed '/^$/d' $UTIL1"
 
UTIL3="cut -c 21-24 $UTIL2"
echo  $UTIL3

Open in new window

0
Comment
Question by:dpoper1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 24306554
what did you get in  $UTIL3 ?
0
 

Author Comment

by:dpoper1
ID: 24306580
cut -c 21-24 $UTIL2
./util.sh: line 22: [: too many arguments
0
 

Author Comment

by:dpoper1
ID: 24306623
Here is my code, but I still get the error provided above.

#!/bin/bash
 
LIMIT="50"
ADMINS=myemail@gmail.com
LOGFILE=/var/log/monitoring/cpu/cpuutil.log
 
#mkdir -p $LOGFILE
 
UTIL='mpstat -u'
 
UTIL1='$UTIL|grep -Ev "Linux|%"'
 
#Deleting empty lines
UTIL2='sed '/^$/d' $UTIL1'
 
UTIL3='cut -c 21-24 $UTIL2'
echo  $UTIL3
 
if [ $UTIL3 -gt $LIMIT ]
then
    echo "Current CPU Utilization: $UTIL3" >> $LOGFILE
    echo "CPU Threshold: $LIMIT" >> $LOGFILE
    mail -s "CPU Utilization on $hostname is over CPU Threshold" $ADMINS < $LOGFILE                      
#    scp $LOGFILE root@headnode:/mnt/disk01/alerts/cpu/
fi

Open in new window

0
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
LVL 84

Expert Comment

by:ozo
ID: 24306805
did you mean to use ` instead of ' ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 24306839
and you may have wanted
UTIL3=`mpstat -u | grep -Ev "Linux|%" | sed '/^$/d' | cut -c 21-24`
note `
 not  '

0
 

Author Comment

by:dpoper1
ID: 24306843
I have narrowed down the code, please take a look below, but I still get the following error:


./util.sh: line 13: [: 0.16: integer expression expected




#!/bin/bash
 
LIMIT="50"
ADMINS=myemail@gmail.com
LOGFILE=/var/log/monitoring/cpu            
 
mkdir -p $LOGFILE
 
UTIL1=$(mpstat -u|grep -Ev "Linux|%" | sed '/^$/d' | cut -c 21-24)
 
#echo $UTIL1
 
if [ $UTIL1 -gt $LIMIT ]
then
    echo "Current CPU Utilization: $UTIL3" >> $LOGFILE/cpuutil.log
    echo "CPU Threshold: $LIMIT" >> $LOGFILE/cpuutil.log
#    mail -s "CPU Utilization on $hostname is over CPU Threshold" $ADMINS < $LOGFILE/cpuutil.log
#    scp $LOGFILE/cpuutil.log root@headnode:/mnt/disk01/alerts/cpu/
    rm -f $LOGFILE/cpuutil.log
fi

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 24306905
can we see what
echo $UTIL1
and
echo $LIMIT
show?
you may need
if [ "$UTIL1" -gt "$LIMIT" ]
0
 
LVL 84

Expert Comment

by:ozo
ID: 24306966
sorry, I see that the error now is different
you might try
if awk "BEGIN{exit $UTIL1 < $LIMIT}"
then
0
 

Author Comment

by:dpoper1
ID: 24306985
take a look at the code, I have modified the if statement to reflect integers as well as the LIMIT VALUE with 2 decimals and I still get an error

0.16
50.01
./util.sh: line 14: [: 0.16: integer expression expected



#!/bin/bash
 
LIMIT="50.01"
ADMINS=myemail@gmail.com
LOGFILE=/var/log/monitoring/cpu
 
mkdir -p $LOGFILE
 
UTIL1=$(mpstat -u|grep -Ev "Linux|%" | sed '/^$/d' | cut -c 21-24)
 
echo $UTIL1
echo $LIMIT
 
if [ "$UTIL1" -gt "$LIMIT" ]
then 
    echo "Current CPU Utilization = $UTIL3" >> $LOGFILE/cpuutil.log
    echo "CPU Threshold = $LIMIT" >> $LOGFILE/cpuutil.log
#    mail -s "CPU Utilization on $hostname is over CPU Threshold" $ADMINS < $LOGFILE/cp
uutil.log
#    scp $LOGFILE/cpuutil.log root@headnode:/mnt/disk01/alerts/cpu/
    rm -f $LOGFILE/cpuutil.log
fi

Open in new window

0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 24307195
-gt only compares integers, not values with 2 decimal places
you can use a different function for comparing, such as awk "BEGIN{exit $UTIL1 < $LIMIT}"
or, if it is always exactly 2 decimal places, you can convert it to an integer by removing the  .
0
 

Author Comment

by:dpoper1
ID: 24307226
It worked thanks a million, I have changed the LIMIT value to test out the script.

Thanks once again.

Regards,

Michael

#!/bin/bash
 
#LIMIT="50.01"
LIMIT="0.3"
ADMINS=myemail@gmail.com
LOGFILE=/var/log/monitoring/cpu
 
mkdir -p $LOGFILE
 
UTIL1=$(mpstat -u|grep -Ev "Linux|%" | sed '/^$/d' | cut -c 21-24)
 
#echo $UTIL1
#echo $LIMIT
 
#if [ "$UTIL1" -gt "$LIMIT" ]
if awk "BEGIN{exit $UTIL1 < $LIMIT}"
then 
    echo "Current CPU Utilization = $UTIL1" >> $LOGFILE/cpuutil.log
    echo "CPU Threshold = $LIMIT" >> $LOGFILE/cpuutil.log
    mail -s "CPU Utilization on $hostname is over CPU Threshold" $ADMINS < $LOGFILE/cpu
util.log
#    scp $LOGFILE/cpuutil.log root@headnode:/mnt/disk01/alerts/cpu/
    rm -f $LOGFILE/cpuutil.log
fi

Open in new window

0

Featured Post

Stack Overflow Podcast - Frustrating Miracles

In this podcast, Stack Overflow interviewed Linux Academy CEO/Founder, Anthony James, and got his developer story!

"Follow your passion, be prepared to work hard and sacrifice, and, above all, don't let anyone limit your dreams."  - Donovan Bailey

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

743 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