shell编程之磁盘监控报警脚本

磁盘报警思路:

  1. 磁盘达到90%发送报警邮件
  2. 发送邮件命令格式
  3. 多个报警设置
  4. 把分区的信息写入文件
[root@localhost scripts]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root   17G  1.9G   16G   11% /
devtmpfs                 395M     0  395M    0% /dev
tmpfs                    407M     0  407M    0% /dev/shm
tmpfs                    407M  6.7M  400M    2% /run
tmpfs                    407M     0  407M    0% /sys/fs/cgroup
/dev/sda1               1014M  146M  869M   15% /boot
tmpfs                     82M     0   82M    0% /run/user/0
/dev/sr0                 4.3G  4.3G     0  100% /mnt
[root@localhost scripts]# vim test1.sh
#!/bin/bash

echo -e "\033[31m \033[1m"
rm -rf list.txt
LIST= df -h |grep "^/dev/" >>list.txt

# 欢迎使用自动监控系统
cat << EOF
---------------------------------------------------------------
--------------welcome to use auto monitor system---------------
---------------------------------------------------------------
EOF
echo -e "\033[32m-----------------------------------------\033[0m"
echo
# 等2秒后执行下一条
sleep 2

while read line 
do
        DATE=`date '+%Y-%m-%d %H:%M:%S'`
        IP_ADDR=`ifconfig ens33|grep "broadcast"|awk '{print $2}'|cut -d: -f 2`  # 打印IP地址  
        D_Name=`echo $line|awk '{print $1,$NF"分区"}'`  # 打印以/dev/开头的文件系统和挂载点
        D_Total=`echo $line|awk '{print $2}'`   # 打印以/dev/开头的磁盘容量
        D_Avail=`echo $line|awk '{print $4}'`   # 打印以/dev/开头的磁盘可用内存
        D_Percent=`echo $line|awk '{print $5}'|sed 's/%//g'`  # 打印以/dev/开头的已用情况(%)
        if [ "$D_Percent" -ge 10 ];then
cat >email.txt <<EOF
*********** Email ***********

通知类型:故障

服务:Disk Monitor
主机:$IP_ADDR
状态:警告

日期/时间:$DATE

额外信息:
CRITICAT - DISK Monitor:$D_Name Used more than ${D_Percent}%  # 磁盘监控器的使用率
EOF
        # 超过磁盘的使用率,提示用户检查
        echo -e "\033[32mThe $D_Name has been used for more than ${D_Percent}%,Please check.\033[0m"
        # 发送邮件报警
        mail -s "$D_Name warning" jiang3756@qq.com < email.txt
        #echo "The $D_Name has been used for more than ${D_Percent}%,Please check."|mail -s "$D_Name warning" 3028243756@qq.com
    fi

done <list.txt
echo -e "\n\033[32m-----------------------------------------\033[1m"
echo "Done."
[root@localhost scripts]# bash test1.sh 
 
---------------------------------------------------------------
--------------welcome to use auto monitor system---------------
---------------------------------------------------------------
-----------------------------------------

The /dev/mapper/centos-root /分区 has been used for more than 11%,Please check.
The /dev/sda1 /boot分区 has been used for more than 15%,Please check.
The /dev/sr0 /mnt分区 has been used for more than 100%,Please check.

-----------------------------------------
Done.
[root@localhost scripts]# cat list.txt 
/dev/mapper/centos-root   17G  1.9G   16G   11% /
/dev/sda1               1014M  146M  869M   15% /boot
/dev/sr0                 4.3G  4.3G     0  100% /mnt
[root@localhost scripts]# cat email.txt 
*********** Email ***********

通知类型:故障

服务:Disk Monitor
主机:192.168.93.110
状态:警告

日期/时间:2020-02-28 18:04:26

额外信息:
CRITICAT - DISK Monitor:/dev/sr0 /mnt分区 Used more than 100%  # 磁盘监控器的使用率
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值