服务器磁盘监控脚本分享(含报警邮件)
在日常的运维工作中,我们经常会对服务器的磁盘使用情况进行巡检,以防止磁盘爆满导致的业务故障.
如果能编写一个合理完善的监控脚本,当磁盘使用率达到我们设置的阀值时,就自动发送报警邮件,以便我们及时获悉到快爆满的磁盘情况!
下面分享一个脚本:
监控本机的根磁盘和home盘,当根磁盘使用率达到90%和home磁盘使用率达到95%的时候,发报警邮件至[email protected]和[email protected]
[[email protected] ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 50G 46G 12G 90% / tmpfs 32G 68K 32G 1% /dev/shm /dev/sda1 485M 40M 421M 9% /boot /dev/mapper/VolGroup-lv_home 836G 795G 673G 95% /home
取根磁盘当前利用率的百分值
[[email protected] ~]# /bin/df -h|grep /dev/mapper/VolGroup-lv_root|awk -F" " '{print $5}'|cut -d"%" -f1
90
取home盘当前利用率的百分值
[[email protected] ~]# /bin/df -h|grep /dev/mapper/VolGroup-lv_home|awk -F" " '{print $5}'|cut -d"%" -f1
95
编写邮件报警脚本
[[email protected] ~]# vim /root/root_disk.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#!/bin/bash SERVER_IP=` ifconfig | grep 192.168.1| awk -F ":" '{print $2}' | cut -d " " -f1`
ROOT_DISK=` /bin/df -h| grep /dev/mapper/VolGroup-lv_root | awk -F " " '{print $5}' | cut -d "%" -f1`
HOME_DISK=` /bin/df -h| grep /dev/mapper/VolGroup-lv_home | awk -F " " '{print $5}' | cut -d "%" -f1`
if [ $ROOT_DISK - ge 90 ]; then
/usr/local/bin/sendEmail -f [email protected] -t [email protected] -s smtp.huanqiu.cn -u " The ROOT_DISK of $SERVER_IP-$HOSTNAME is warning!" -o message-content- type =html -o message-charset=utf8 -xu [email protected] -xp [email protected] -m "The ROOT_DISK of $SERVER_IP-$HOSTNAME,now use% is 90%,please deal with it as soon as possible"
/usr/local/bin/sendEmail -f [email protected] -t [email protected] -s smtp.huanqiu.cn -u " The ROOT_DISK of $SERVER_IP-$HOSTNAME is warning!" -o message-content- type =html -o message-charset=utf8 -xu [email protected] -xp [email protected] -m "The ROOT_DISK of $SERVER_IP-$HOSTNAME,now use% is 90%,please deal with it as soon as possible"
else echo "The ROOT_DISK of $SERVER_IP-$HOSTNAME is Enough to use"
fi sleep 5
if [ $HOME_DISK - ge 95 ]; then
/usr/local/bin/sendEmail -f [email protected] -t wang[email protected] -s smtp.huanqiu.cn -u " The HOME_DISK of $SERVER_IP-$HOSTNAME is warning!" -o message-content- type =html -o message-charset=utf8 -xu [email protected] -xp [email protected] -m "The HOME_DISK of $SERVER_IP-$HOSTNAME,now use% is 95%,please deal with it as soon as possible"
/usr/local/bin/sendEmail -f [email protected] -t [email protected] -s smtp.huanqiu.cn -u " The HOME_DISK of $SERVER_IP-$HOSTNAME is warning!" -o message-content- type =html -o message-charset=utf8 -xu [email protected] -xp [email protected] -m "The HOME_DISK of $SERVER_IP-$HOSTNAME,now use% is 95%,please deal with it as soon as possible"
else echo "The ROOT_DISK of $SERVER_IP-$HOSTNAME is Enough to use"
fi |
设置计划任务
[[email protected] ~]# crontab -e
*/30 * * * * /bin/bash -x /root/root_disk.sh > /dev/null 2>&1
-----------------------------------------------------------------------------------------------------------------
上面脚本中的邮件报警用的是sendemail,需要提前安装sendemail环境,安装操作如下:
1)先下载安装包到本地,解压。
[[email protected] ~]# cd /usr/local/src/
[[email protected] src]# wget -c http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
[[email protected] src]# tar -zvxf sendEmail-v1.56.tar.gz
[[email protected] src]# cd sendEmail-v1.56
[[email protected] sendEmail-v1.56]# cp -a sendEmail /usr/local/bin/
[[email protected] sendEmail-v1.56]# chmod +x /usr/local/bin/sendEmail
[[email protected] sendEmail-v1.56]# file /usr/local/bin/sendEmail
/usr/local/bin/sendEmail: a /usr/bin/perl -w script text executable
2)安装下依赖
[[email protected] sendEmail-v1.56]# yum install perl-Net-SSLeay perl-IO-Socket-SSL -y
[[email protected] sendEmail-v1.56]# /usr/local/bin/sendEmail -f [email protected] -t [email protected] -s smtp.huanqiu.cn -u "我是邮件主题" -o message-content-type=html -o message-charset=utf8 -xu [email protected] -xp [email protected] -m "我是邮件内容"
命令说明:
/usr/local/bin/sendEmail #命令主程序
-f [email protected] #发件人邮箱
-t [email protected] #收件人邮箱
-s smtp.huanqi.cn #发件人邮箱的smtp服务器
-u "我是邮件主题" #邮件的标题
-o message-content-type=html #邮件内容的格式,html表示它是html格式
-o message-charset=utf8 #邮件内容编码
-xu [email protected] #发件人邮箱的用户名
-xp [email protected] #发件人邮箱密码
-m "我是邮件内容" #邮件的具体内容
例如:
[[email protected] alertscripts]# /usr/local/bin/sendEmail -f [email protected] -t [email protected] -s smtp.huanqiu.cn -u "我是邮件主题" -o message-content-type=html -o message-charset=utf8 -xu [email protected] -xp [email protected] -m "我是邮件内容"
Oct 14 19:38:29 haunqiu-beta sendEmail[65454]: Email was sent successfully!
[[email protected] alertscripts]#
登陆[email protected]邮箱,发现已经收到了上面发送的邮件: