用于监视磁盘空间使用情况的Shell脚本

时间:2020-03-05 15:31:42  来源:igfitidea点击:

监视任何系统上的磁盘空间使用是一项非常关键的任务。
如果监控不当,可能会导致性能下降或者应用程序崩溃。

在本教程中,将介绍一个简单的shell脚本,该脚本可检查所有已安装设备上的已用空间,并在已用空间超过阈值时发出警告。

Shell脚本监视器磁盘空间

在shell脚本下面,输出已用磁盘空间的百分比。

#!/bin/bash
threshold="20"
i=2
result=`df -kh |grep -v "Filesystem" | awk '{ print  }' | sed 's/%//g'`
for percent in $result; do
if ((percent > threshold))
then
partition=`df -kh | head -$i | tail -1| awk '{print }'`
echo "$partition at $(hostname -f) is ${percent}% full"
fi
let i=$i+1
done

脚本结果

bobbin@theitroad:/$df -kh
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 52G 4.7G 45G 10% /
tmpfs 1.9G 0 1.9G 0% /lib/init/rw
udev 1.9G 192K 1.9G 1% /dev
tmpfs 1.9G 2.6M 1.9G 1% /dev/shm
/dev/sda6 92G 22G 66G 25% /home
bobbin@theitroad:/$./df_script.sh
/dev/sda6 at theitroad.lviv.example.com is 25% full