1

I Have a request to alert usage of disk every 30 minutes, The thing is recent output should check old alert to avoid to send same alert again and again.

#!/bin/bash

#export [email protected]
export [email protected];
#df -PH | grep -vE '^Filesystem|none|cdrom'|awk '{ print $5 " " $6 }' | while read output;
df -PH | grep -vE '^Filesystem|none|cdrom|swdepot'|awk '{ print $5 " " $6 }' > diskcheck.log;

#diskcheck is current output whereas disk_alert is previous runned output

if [ -s "$HOME/DBA/monitor/log/disk_alert.log" ]; then
#Getting variables and compare with old
  usep=$(awk '{ if($1 > 60) print $0 }' $HOME/DBA/monitor/diskcheck.log | cut -d'%' -f1)
  usep1=$(awk '{ if($1 > 60) print $0 }' $HOME/DBA/monitor/log/disk_alert.log | cut -d'%' -f1)
  partition=$(cat $HOME/DBA/monitor/diskcheck.log | awk '{ print $2 }' )
else
   cat $HOME/DBA/monitor/diskcheck.log > $HOME/DBA/monitor/log/disk_alert.log
fi
**echo $usep;
echo $usep1;**
if [ "$usep" -ge 60 ]; then
        if [ "$usep" -eq "$usep1" ]; then
                mail=$(awk '{ if("$usep" == "$usep1") print $0 }' $HOME/DBA/monitor/diskcheck.log)
                echo "Running out of space \"$mail ($usep%)\" on $(hostname) as on $(date)" | mail -s "Disk Space Alert: Mount $mail is $usep% Used" $maillist;
        fi
fi

Output (ERROR):

66 65 85 66
66 65 85 66
disk_alert.sh: line 19: [: 66
65
85
66: integer expression expected

I think the problem is in variables($usep and $usep1) it stores the values in single line which means (66 65 85 66), But it should be

66
65
85
66

Then only:

if [ "$usep" -ge 60 ]; then 
       this condition will pass.
3
  • First replace first df -PH | grep .... by df -h --output=pcent,target | awk 'NR>1 {print $1+0,$2}' See what it outputs, show the output.. and I might help further.. May 5, 2017 at 18:40
  • You could also try while read -r used_sp dev; do if ((used_sp > 10)); then echo $used_sp $dev; fi ; done < <(df -h --output=pcent,target | awk 'NR>1 {print $1+0,$2}') which will do what you want. May 5, 2017 at 18:49
  • df -h --output=pcent,target | awk 'NR>1 {print $1+0,$2}' it shows (df: unrecognized option '--output=pcent,target'). am a new bee to shell but fortunately I got this kinda request, I want to make alert every 30 minutes unless current usage of space is increased than previous alert.
    – Tom
    May 5, 2017 at 19:02

1 Answer 1

1

Let's investigate this line:

usep=$(awk '{ if($1 > 60) print $0 }' $HOME/DBA/monitor/diskcheck.log | cut -d'%' -f1)

In this case $0 has value 66 65 85 66. So cut -d'%' command can't find % separator in such value and returns it as is.

It should be:

usep=$(awk '{ if($1 > 60) print $1 }' $HOME/DBA/monitor/diskcheck.log

where $1 points to the first field


The same holds to the line:

usep1=$(awk '{ if($1 > 60) print $0 }' $HOME/DBA/monitor/log/disk_alert.log | cut -d'%' -f1)
2
  • usep=$(awk '{ if($1 > 60) print $1 }' $HOME/DBA/monitor/diskcheck.log 66% 65% 85% 66% It remains the same with this syntax also :(
    – Tom
    May 5, 2017 at 18:09
  • @Tom, if that could be true, consider the output of this command: echo "66% 65% 85% 66%" | cut -d'%' -f1 May 6, 2017 at 7:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .