我在网上找到了下面提到的脚本,当服务器存储空间不足或按照定义时,它会通过电子邮件通知。我每 5 分钟运行一次,如果存储空间不足,它会在 5 分钟后通过电子邮件不断通知我。我需要的是当设置的存储限制超出时只收到 1 封电子邮件通知,因为 1 封通知就足够了。我将清理存储空间,如果再次超出,我会再次收到一次通知,依此类推。我使用 crontab 运行脚本。
#!/bin/sh
# Purpose: Monitor Linux disk space and send an email alert to $ADMIN
ALERT=90 # alert level
ADMIN="[email protected]" # dev/sysadmin email ID
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read -r 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%)\" on $(hostname) as on $(date)" |
mail -s "Alert: Almost out of disk space $usep%" "$ADMIN"
fi
done
答案1
我修改了您的脚本 - 它现在将为您收到通知的每个分区创建一个锁定文件,并停止针对该分区发送邮件。如果分区处于干净状态,锁定文件将被删除,如果分区再次处于不良状态,您可以再次收到通知:
#!/bin/bash
# Purpose: Monitor Linux disk space and send an email alert to $ADMIN
ALERT=90 # alert level
ADMIN="[email protected]" # dev/sysadmin email ID
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read -r output;
do
echo "$output"
usep=$(echo "$output" | awk '{ print $1}' | cut -d'%' -f1 )
partition=$(echo "$output" | awk '{ print $2 }' )
lockname=$(echo $partition | tr / _);
if [ $usep -ge $ALERT ]; then
if test -f "./$lockname.lock";
then echo "Found lockfile - Cancel mail";
else
touch "./$lockname.lock"
echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" |
mail -s "Alert: Almost out of disk space $usep%" "$ADMIN"
fi
else
if test -f "./$lockname.lock"; then rm "./$lockname.lock"
fi
fi
done