M DXYZ
asked on
script to get cpu utilization and compare it against a value
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
what did you get in $UTIL3 ?
ASKER
cut -c 21-24 $UTIL2
./util.sh: line 22: [: too many arguments
./util.sh: line 22: [: too many arguments
ASKER
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
did you mean to use ` instead of ' ?
and you may have wanted
UTIL3=`mpstat -u | grep -Ev "Linux|%" | sed '/^$/d' | cut -c 21-24`
note `
not '
UTIL3=`mpstat -u | grep -Ev "Linux|%" | sed '/^$/d' | cut -c 21-24`
note `
not '
ASKER
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
./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
can we see what
echo $UTIL1
and
echo $LIMIT
show?
you may need
if [ "$UTIL1" -gt "$LIMIT" ]
echo $UTIL1
and
echo $LIMIT
show?
you may need
if [ "$UTIL1" -gt "$LIMIT" ]
sorry, I see that the error now is different
you might try
if awk "BEGIN{exit $UTIL1 < $LIMIT}"
then
you might try
if awk "BEGIN{exit $UTIL1 < $LIMIT}"
then
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It worked thanks a million, I have changed the LIMIT value to test out the script.
Thanks once again.
Regards,
Michael
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