"integer expression expected" error with drive space monitoring script

Hi guys,

I am still kinda new to Linux.

Script template I found on the net and adapted for our environment:

#!/bin/sh
#set -x
ADMIN="admin@mydomain.com"
ALERT=10
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
  #echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $usep -ge $ALERT ]; then
    echo "Running out of space \"$partition $usep% used\" on $(hostname) as on $(date)" |
     mail -s "Alert:$(hostname) almost out of disk space $usep% used" $ADMIN
  fi
done

I get the following errors:

./monitor_EMAIL.sh: line 12: [: /dev/mapper/VolGroup00-LogVol00: integer expression expected
./monitor_EMAIL.sh: line 12: [: /: integer expression expected

This the output I get with the "set -x" enabled:

[root@s-webservice scripts]# ./monitor_EMAIL.sh
./monitor_EMAIL.sh: line 12: [: /dev/mapper/VolGroup00-LogVol00: integer expression expected
./monitor_EMAIL.sh: line 12: [: /: integer expression expected
[root@host scripts]# vim monitor_EMAIL.sh
[root@host scripts]# ./monitor_EMAIL.sh
+ ADMIN=admin@mydomain.com
+ ALERT=10
+ df -l
+ grep -vE '^Filesystem|tmpfs|cdrom'
+ awk '{ print $5 " " $1 }'
+ read output
++ echo /dev/mapper/VolGroup00-LogVol00
++ awk '{ print $1}'
++ cut -d% -f1
+ usep=/dev/mapper/VolGroup00-LogVol00
++ echo /dev/mapper/VolGroup00-LogVol00
++ awk '{ print $2 }'
+ partition=
+ '[' /dev/mapper/VolGroup00-LogVol00 -ge 10 ']'
./monitor_EMAIL.sh: line 12: [: /dev/mapper/VolGroup00-LogVol00: integer expression expected
+ read output
++ echo / 149186740
++ awk '{ print $1}'
++ cut -d% -f1
+ usep=/
++ echo / 149186740
++ awk '{ print $2 }'
+ partition=149186740
+ '[' / -ge 10 ']'
./monitor_EMAIL.sh: line 12: [: /: integer expression expected
+ read output
++ echo 16% /dev/sda1
++ awk '{ print $1}'
++ cut -d% -f1
+ usep=16
++ echo 16% /dev/sda1
++ awk '{ print $2 }'
+ partition=/dev/sda1
+ '[' 16 -ge 10 ']'
++ hostname
++ hostname
++ date
+ echo 'Running out of space "/dev/sda1 16% used" on host as on Tue Jan  5 12:34:16 SAST 2010'
+ mail -s 'Alert:host almost out of disk space 16% used'
admin@mydomain.com
+ read output

I set the ALERT level low just to test the script.
This is on Fedora Core 5.
The same script works fine on all other versions like CentOS5, Redhat etc...

It's probably that the path to your logical volumes is too long and df decides to move the information you're looking for to the next line. Of course that screws with the column numbering. Add the switch '-P' to df, and it should work fine.

Explanation: -P forces POSIX mode, which basically means that it should behave compatible to the other versions of df out there and put the information for each device on the same line. Not as readable, but better scriptable.

Thanks a mil!!

It works.

Working script:

#!/bin/sh
#set -x
# set admin email so that you can get email
ADMIN="admin@mydomain.com"
# set alert level
ALERT=85
df -HP | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
  #echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $usep -ge $ALERT ]; then
    echo "Running out of space \"$partition $usep% used\" on $(hostname) as on $(date)" |
     mail -s "Alert:$(hostname) almost out of disk space $usep% used" $ADMIN
  fi
done