磁盘报警思路:
- 磁盘达到90%发送报警邮件
- 发送邮件命令格式
- 多个报警设置
- 把分区的信息写入文件
[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% # 磁盘监控器的使用率