shell脚本监控磁盘使用率

@lianst  September 2, 2017

简单的一个监控磁盘分区使用率的脚本,欢迎批评指正.

#!/bin/sh
# df -h:查看硬盘信息
# sed '1d':删除第一行
# awk '{print $5}':打印第5列
# sed 's/%//g' :将%替换成空,g是全局的意思。
# sed -n 1p:显示修改(-n选项)的第一行

# a1-4是检查磁盘分区信息
a1=$(df -h|sed '1d'|awk '{print $5}'|sed 's/%//g'|sed -n 1p)
a2=$(df -h|sed '1d'|awk '{print $5}'|sed 's/%//g'|sed -n 2p)
a3=$(df -h|sed '1d'|awk '{print $5}'|sed 's/%//g'|sed -n 3p)
a4=$(df -h|sed '1d'|awk '{print $5}'|sed 's/%//g'|sed -n 4p)

# b1-4是把分区名过滤出来
b1=$(df -h|sed 1d|awk '{print $1}'|sed -n 1p)
b2=$(df -h|sed 1d|awk '{print $1}'|sed -n 2p)
b3=$(df -h|sed 1d|awk '{print $1}'|sed -n 3p)
b4=$(df -h|sed 1d|awk '{print $1}'|sed -n 4p)

# 当前日期(年-月-日 时:分:秒)
DAY=$(date +%F\ %T)


# 判断使用率,大于(-gt)90就记录日志.
if [ $a1 -gt 89 ];then
echo "$DAY $b1 would be near 90% !" >> /var/log/check_HD.log
if [ $a2 -gt 89 ];then
echo "$DAY $b21 would be near 90% !" >> /var/log/check_HD.log
if [ $a3 -gt 89 ];then
echo "$DAY $b3 would be near 90% !" >> /var/log/check_HD.log
if [ $a4 -gt 89 ];then
echo "$DAY $b4 would be near 90% !" >> /var/log/check_HD.log
fi fi fi fi

添加新评论