Strange error for disk space alert email script
Hello, This script gives me a strange error after the mount was done on this server. (pasting the error at the bottom)
DATE=`date '+%Y%m%d'` df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $6 }' | while read output; do echo $output usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 ) partition=$(echo $output | awk '{ print $2 }' ) if [ $usep -ge 70 ] then echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" >/tmp/dfh.txt df -H >> /tmp/dfh.txt mail -s "$(hostname) -:- Alert: Level 1. Almost out of disk space $usep%" aabb@ld.com < /tmp/dfh.txt elif [ $usep -ge 80 ] then echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" >/tmp/dfh.txt df -H >> /tmp/dfh.txt mail -s "$(hostname) -:- Alert: Level 2. Almost out of disk space $usep%" abc@se.com < /tmp/dfh.txt elif [ $usep -ge 90 ] then echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" >/tmp/dfh.txt df -H >> /tmp/dfh.txt mail -s "$(hostname) -:- Alert: Level 3. Disk Space critical - Almost out of disk space $usep%" abc@kk.com < /tmp/dfh.txt fi done ERROR - /memory_status_alert.sh: line 10: [: -ge: unary operator expected ./memory_status_alert.sh: line 15: [: -ge: unary operator expected ./memory_status_alert.sh: line 20: [: -ge: unary operator expected /dataman/proddb_bkp ./memory_status_alert.sh: line 10: [: /ataman/db_bkp: integer expression expected ./memory_status_alert.sh: line 15: [: /ataman/db_bkp: integer expression expected ./memory_status_alert.sh: line 20: [: /ataman/db_bkp: integer expression expected ./memory_status_alert.sh: line 10: [: -ge: unary operator expected ./memory_status_alert.sh: line 15: [: -ge: unary operator expected ./memory_status_alert.sh: line 20: [: -ge: unary operator expected /dataman/prodapp_bkp ./memory_status_alert.sh: line 10: [: /ataman/app_bkp: integer expression expected ./memory_status_alert.sh: line 15: [: /ataman/app_bkp: integer expression expected ./memory_status_alert.sh: line 20: [: /ataman/app_bkp: integer expression expected IF df -h is run independantly this is what i get - Filesystem Size Used Avail Use% Mounted on /dev/sda3 29G 11G 17G 38% / /dev/sda6 84G 70G 11G 88% /opt /dev/sda2 29G 8.7G 19G 33% /home /dev/sda1 190M 19M 162M 11% /boot tmpfs 1.7G 0 1.7G 0% /dev/shm 192.168.2.7:/DataVolume/proddb_bkp 915G 433G 482G 48% /atamandb_bkp 192.168.2.7:/DataVolume/prodapp_bkp 915G 433G 482G 48% /ataman/app_bkp Cant get the problem!! please help! |
It bugs when $usep is not a number, maybe from
192.168.2.7:/DataVolume/proddb_bkp 915G 433G 482G 48% /atamandb_bkp 192.168.2.7:/DataVolume/prodapp_bkp 915G 433G 482G 48% /ataman/app_bkp df lines output |
I am unable to get "output" - where are we getting it from
Code:
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $6 }' | while read output; |
Basically those errors are saying that $usep is not being correctly extracted for use in the 'if' statements.
Try these 2 suggestions 1. add Code:
set -xv 2. http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS ie [[ ]] is more robust than [ ] |
Quote:
Try : Code:
LC_ALL=C df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $6 }' |
Quote:
I changed [] to [[ ]], and did set -xv, somehow it does send me a mail when the script is run and alert matches but still gives me the same error. This is the output i get when set -xv is done - + df -H + grep -vE '^Filesystem|tmpfs|cdrom' + awk '{ print $5 " " $6 }' + read output + echo 39% / 39% / echo $output | awk '{ print $1}' | cut -d'%' -f1 ++ echo 39% / ++ cut -d% -f1 ++ awk '{ print $1}' + usep=39 echo $output | awk '{ print $2 }' ++ echo 39% / ++ awk '{ print $2 }' + partition=/ + [[ 39 -ge 70 ]] + [[ 39 -ge 80 ]] + [[ 39 -ge 90 ]] + read output + echo 88% /opt 88% /opt echo $output | awk '{ print $1}' | cut -d'%' -f1 ++ echo 88% /opt ++ awk '{ print $1}' ++ cut -d% -f1 + usep=88 echo $output | awk '{ print $2 }' ++ echo 88% /opt ++ awk '{ print $2 }' + partition=/opt + [[ 88 -ge 70 ]] hostname ++ hostname date ++ date + echo 'Running out of space "/opt (88%)" on HOST as on Fri Dec 16 11:02:24 IST 2011' + df -H hostname ++ hostname + mail -s 'HOST -:- Alert: Level 1 reached. Almost out of disk space 88%' ooee@ei.com + read output + echo 33% /home 33% /home echo $output | awk '{ print $1}' | cut -d'%' -f1 ++ echo 33% /home ++ awk '{ print $1}' ++ cut -d% -f1 + usep=33 echo $output | awk '{ print $2 }' ++ echo 33% /home ++ awk '{ print $2 }' + partition=/home + [[ 33 -ge 70 ]] + [[ 33 -ge 80 ]] + [[ 33 -ge 90 ]] + read output + echo 11% /boot 11% /boot echo $output | awk '{ print $1}' | cut -d'%' -f1 ++ echo 11% /boot ++ awk '{ print $1}' ++ cut -d% -f1 + usep=11 echo $output | awk '{ print $2 }' ++ echo 11% /boot ++ awk '{ print $2 }' + partition=/boot + [[ 11 -ge 70 ]] + [[ 11 -ge 80 ]] + [[ 11 -ge 90 ]] + read output + echo echo $output | awk '{ print $1}' | cut -d'%' -f1 ++ echo ++ awk '{ print $1}' ++ cut -d% -f1 + usep= echo $output | awk '{ print $2 }' ++ echo ++ awk '{ print $2 }' + partition= + [[ '' -ge 70 ]] + [[ '' -ge 80 ]] + [[ '' -ge 90 ]] + read output + echo /ataman/db_bk /ataman/db_bk echo $output | awk '{ print $1}' | cut -d'%' -f1 ++ echo /ataman/db_bk ++ awk '{ print $1}' ++ cut -d% -f1 + usep=/ataman/db_bk echo $output | awk '{ print $2 }' ++ echo /ataman/db_bk ++ awk '{ print $2 }' + partition= + [[ /ataman/db_bk -ge 70 ]] ./dd.sh: line 12: [[: /ataman/db_bk: syntax error: operand expected (error token is "/ataman/db_bk") + [[ /ataman/db_bk -ge 80 ]] ./dd.sh: line 17: [[: /ataman/db_bk: syntax error: operand expected (error token is "/ataman/db_bk") + [[ /ataman/db_bk -ge 90 ]] ./dd.sh: line 22: [[: /ataman/db_bk: syntax error: operand expected (error token is "/ataman/db_bk") + read output + echo |
See ? The bug comes from the long lines :
192.168.2.7:/DataVolume/proddb_bkp 915G 433G 482G 48% /atamandb_bkp 192.168.2.7:/DataVolume/prodapp_bkp 915G 433G 482G 48% /ataman/app_bkp df seems to break long lines with new lines try add the -P df option (posix portability) which should avoid that So the second line of script should be: Code:
df -H -P | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $6 }' | while read output; |
:) thanx!!
|
All times are GMT -5. The time now is 06:28 PM. |