If you'd like something a bit simplier:

#!/bin/sh
ADMINA="[email protected]"
dt=$(date '+%a %b %e %I:%M %p')
svr=$(hostname)
ALERT=75
mpstat | awk ' /all/i {print $4;}'|
while read output;
 do
  cpuutil=$(echo $output | perl -n -a -e '$F[0]=~s/\..*$//;print "$F[0]\n";')
  if [ "$cpuutil" -ge $ALERT ]; then
  echo "CPU utilization has risen beyond the alert threshold on $svr at $dt"|\
   mail -vs "Alert:- CPU utilization is high on $svr at $dt" $ADMINA
 fi
done

I replaced the "grep -i all|awk {print $4}| cut -d'%' -f 1" with the perl code. 
The perl script looks at the first blank delimited "token" in $output and 
removes everything after the first period, assigning that to $cpuutil. Saves 
some steps. If you don't mind using perl instead of awk and cut. It should also 
work with a non-BASH shell as well. I also have problems telling a ` from a ', 
so I replaced it with $(...) . That's just me.

Another possibility would be:

cpuutil=$(echo $output|awk '/all/i {print $4}'|cut -d'%' -f 1);

which eliminated the "grep -i all" stage with awk's pattern matching instead, 
but leaves the $cputuil being "n.nn" and so still needing the ${cpuutil/%.*/} 
in the "if".


--
John McKown 
Systems Engineer IV
IT

Administrative Services Group

HealthMarkets(r)

9151 Boulevard 26 * N. Richland Hills * TX 76010
(817) 255-3225 phone * 
[email protected] * www.HealthMarkets.com

Confidentiality Notice: This e-mail message may contain confidential or 
proprietary information. If you are not the intended recipient, please contact 
the sender by reply e-mail and destroy all copies of the original message. 
HealthMarkets(r) is the brand name for products underwritten and issued by the 
insurance subsidiaries of HealthMarkets, Inc. -The Chesapeake Life Insurance 
Company(r), Mid-West National Life Insurance Company of TennesseeSM and The 
MEGA Life and Health Insurance Company.SM

 

> -----Original Message-----
> From: Linux on 390 Port [mailto:[email protected]] On 
> Behalf Of Bauer, Bobby (NIH/CIT) [E]
> Sent: Wednesday, December 08, 2010 1:41 PM
> To: [email protected]
> Subject: Need a little help with a Linux script
> 
> I'm trying to write a little script and having trouble 
> comparing integers and decimal numbers. The shell doesn't 
> like it and I haven't been able to figure out how to get 
> around it. Script is named zyx.
> 
>       1 #!/bin/sh
>       2 ADMINA="[email protected]"
>       3 dt=`date '+%a %b %e %I:%M %p' `
>       4 svr=`hostname`
>       5 ALERT=75
>       6 mpstat | grep -i all |awk '{print $4}'|
>       7 while read output;
>       8 do
>       9  cpuutil=`(echo $output | awk '{ print $1}' | cut -d'%' -f1 )`
>      10     set -x
>      11  if [ "$cpuutil" -ge $ALERT ]; then
>      12    echo "CPU utilization has risen beyond the alert 
> threshold on $svr at $dt"| mail -vs "Alert:- CPU utilization 
> is hi        gh on $svr at $dt" $ADMINA
>      13  fi
>      14 done
> 
> xyz
> + '[' 3.19 -ge 75 ']'
> /usr/sbin/xyz: line 11: [: 3.19: integer expression expected
> + read output
> 
> mpstat is returns 3.19 and I'm trying, on line 11, to compare 
> that to ALERT which is set to 75. I think I need to strip off 
> the decimal part of 3.19 but haven't found any way to do it. 
>  
> 
> Bobby Bauer
> Center for Information Technology
> National Institutes of Health
> Bethesda, MD 20892-5628
> 301-594-7474
> 
>  
> 
> ----------------------------------------------------------------------
> For LINUX-390 subscribe / signoff / archive access instructions,
> send email to [email protected] with the message: INFO 
> LINUX-390 or visit
> http://www.marist.edu/htbin/wlvindex?LINUX-390
> ----------------------------------------------------------------------
> For more information on Linux on System z, visit
> http://wiki.linuxvm.org/
> 
> 
----------------------------------------------------------------------
For LINUX-390 subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO LINUX-390 or visit
http://www.marist.edu/htbin/wlvindex?LINUX-390
----------------------------------------------------------------------
For more information on Linux on System z, visit
http://wiki.linuxvm.org/

Reply via email to