LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Strange error for disk space alert email script (https://www.linuxquestions.org/questions/linux-newbie-8/strange-error-for-disk-space-alert-email-script-918905/)

Vi_rod 12-15-2011 10:35 AM

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!

Cedrik 12-15-2011 10:49 AM

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

asimba 12-15-2011 12:59 PM

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;

chrism01 12-15-2011 07:08 PM

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
to the top of the script; it'll show you exactly what the parser is doing/seeing

2. http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS ie [[ ]] is more robust than [ ]

Cedrik 12-15-2011 07:23 PM

Quote:

Originally Posted by asimba (Post 4550843)
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;

Maybe your $LANG is not set to english or us language ?
Try :
Code:

LC_ALL=C df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $6 }'

Vi_rod 12-15-2011 11:41 PM

Quote:

Originally Posted by chrism01 (Post 4551123)
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
to the top of the script; it'll show you exactly what the parser is doing/seeing

2. http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS ie [[ ]] is more robust than [ ]



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

Cedrik 12-16-2011 04:04 AM

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;

Vi_rod 12-16-2011 06:09 AM

:) thanx!!


All times are GMT -5. The time now is 06:28 PM.