linux检测磁盘 报警,linux服务器磁盘监控脚本分享(含报警邮件)

本文介绍了一个用于监控服务器磁盘使用情况的脚本,当根磁盘和home磁盘达到预设的使用率阈值时,会自动发送警告邮件。脚本通过`df`命令获取磁盘使用率,结合`sendEmail`工具发送报警邮件,并通过`cron`定时任务进行定期检查。此外,还提供了`sendEmail`的安装和使用说明。
摘要由CSDN通过智能技术生成

在日常的运维工作中,我们经常会对服务器的磁盘使用情况进行巡检,以防止磁盘爆满导致的业务故障.

如果能编写一个合理完善的监控脚本,当磁盘使用率达到我们设置的阀值时,就自动发送报警邮件,以便我们及时获悉到快爆满的磁盘情况!

下面分享一个脚本:

监控本机的根磁盘和home盘,当根磁盘使用率达到90%和home磁盘使用率达到95%的时候,发报警邮件至wangshibo@huanqiu.cn和liugang@huanqiu.cn

[root@haunqiu-beta ~]# df -h

Filesystem Size Used Avail Use%Mounted on/dev/mapper/VolGroup-lv_root 50G 46G 12G 90% /tmpfs 32G 68K 32G1% /dev/shm/dev/sda1 485M 40M 421M 9% /boot/dev/mapper/VolGroup-lv_home 836G 795G 673G 95% /home

取根磁盘当前利用率的百分值

[root@haunqiu-beta ~]# /bin/df -h|grep /dev/mapper/VolGroup-lv_root|awk -F" " '{print $5}'|cut -d"%" -f1

90

取home盘当前利用率的百分值

[root@haunqiu-beta ~]# /bin/df -h|grep /dev/mapper/VolGroup-lv_home|awk -F" " '{print $5}'|cut -d"%" -f1

95

编写邮件报警脚本

[root@haunqiu-beta ~]# 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 ops@huanqiu.cn -t wangshibo@huanqiu.cn -s smtp.huanqiu.cn -u" The ROOT_DISK of $SERVER_IP-$HOSTNAME is warning!" -o message-content-type=html -o message-charset=utf8 -xu ops@huanqiu.cn -xp zh@123bj -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 ops@huanqiu.cn -t liugang@huanqiu.cn -s smtp.huanqiu.cn -u" The ROOT_DISK of $SERVER_IP-$HOSTNAME is warning!" -o message-content-type=html -o message-charset=utf8 -xu ops@huanqiu.cn -xp zh@123bj -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 ops@huanqiu.cn -t wangshibo@huanqiu.cn -s smtp.huanqiu.cn -u" The HOME_DISK of $SERVER_IP-$HOSTNAME is warning!" -o message-content-type=html -o message-charset=utf8 -xu ops@huanqiu.cn -xp zh@123bj -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 ops@huanqiu.cn -t liugang@huanqiu.cn -s smtp.huanqiu.cn -u" The HOME_DISK of $SERVER_IP-$HOSTNAME is warning!" -o message-content-type=html -o message-charset=utf8 -xu ops@huanqiu.cn -xp zh@123bj -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

设置计划任务

[root@haunqiu-beta ~]# crontab -e

*/30 * * * * /bin/bash -x /root/root_disk.sh > /dev/null 2>&1

1312763775e3e1a52636d2c92679739b.png

f5b93bdd6f03d6c577daa6464f9992fb.png

-----------------------------------------------------------------------------------------------------------------

上面脚本中的邮件报警用的是sendemail,需要提前安装sendemail环境,安装操作如下:

1)先下载安装包到本地,解压。

[root@haunqiu-beta ~]# cd /usr/local/src/

[root@haunqiu-beta src]# wget -c http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz

[root@haunqiu-beta src]# tar -zvxf sendEmail-v1.56.tar.gz

[root@haunqiu-beta src]# cd sendEmail-v1.56

[root@haunqiu-beta sendEmail-v1.56]# cp -a sendEmail /usr/local/bin/

[root@haunqiu-beta sendEmail-v1.56]# chmod +x /usr/local/bin/sendEmail

[root@haunqiu-beta sendEmail-v1.56]# file /usr/local/bin/sendEmail

/usr/local/bin/sendEmail: a /usr/bin/perl -w script text executable

2)安装下依赖

[root@haunqiu-beta sendEmail-v1.56]# yum install perl-Net-SSLeay perl-IO-Socket-SSL -y

[root@haunqiu-beta sendEmail-v1.56]# /usr/local/bin/sendEmail -f from@huanqiu.cn -t to@huanqiu.cn -s smtp.huanqiu.cn -u "我是邮件主题" -o message-content-type=html -o message-charset=utf8 -xu from@huanqiu.cn -xp zh@123bj -m "我是邮件内容"

命令说明:

/usr/local/bin/sendEmail                            #命令主程序

-f from@uhanqiu.cn                                  #发件人邮箱

-t to@huanqiu.cn                                      #收件人邮箱

-s smtp.huanqi.cn                                     #发件人邮箱的smtp服务器

-u "我是邮件主题"                                       #邮件的标题

-o message-content-type=html               #邮件内容的格式,html表示它是html格式

-o message-charset=utf8                        #邮件内容编码

-xu from@huanqiu.cn                              #发件人邮箱的用户名

-xp zh@123bj                                    #发件人邮箱密码

-m "我是邮件内容"                                    #邮件的具体内容

例如:

[root@haunqiu-beta alertscripts]# /usr/local/bin/sendEmail -f ops@huanqiu.cn -t wangshibo@huanqiu.cn -s smtp.huanqiu.cn -u "我是邮件主题" -o message-content-type=html -o message-charset=utf8 -xu ops@huanqiu.cn -xp zh@123bj -m "我是邮件内容"

Oct 14 19:38:29 haunqiu-beta sendEmail[65454]: Email was sent successfully!

[root@haunqiu-beta alertscripts]#

登陆wangshibo@huanqiu.cn邮箱,发现已经收到了上面发送的邮件:

84c743ff7594e966831fa217302f70b1.png

【版权声明】本文内容来源于互联网,本站不拥有所有权,不承担相关法律责任。如果发现本站有侵权的内容,欢迎发送邮件至 benumon@163.com 举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值