我有一个请求,每 30 分钟提醒一次磁盘使用情况,问题是最近的输出应该检查旧的警报,以避免一次又一次发送相同的警报。
#!/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
输出(错误):
66 65 85 66
66 65 85 66
disk_alert.sh: line 19: [: 66
65
85
66: integer expression expected
我认为问题出在变量($usep 和 $usep1)中,它将值存储在单行中,这意味着(66 65 85 66),但它应该是
66
65
85
66
那么只有:
if [ "$usep" -ge 60 ]; then
this condition will pass.
答案1
让我们研究一下这一行:
usep=$(awk '{ if($1 > 60) print $0 }' $HOME/DBA/monitor/diskcheck.log | cut -d'%' -f1)
在这种情况下$0
是有价值的66 65 85 66
。因此cut -d'%'
命令无法%
在该值中找到分隔符并按原样返回。
它应该是:
usep=$(awk '{ if($1 > 60) print $1 }' $HOME/DBA/monitor/diskcheck.log
其中$1
指向第一个字段
这同样适用于这一行:
usep1=$(awk '{ if($1 > 60) print $0 }' $HOME/DBA/monitor/log/disk_alert.log | cut -d'%' -f1)