linux服务(web/mysql/disk/memory)监控和邮件报警脚本



前面写过一篇在linux上通过postfix和dovecot进行邮件的收发,主要是为了达到监控服务并发送邮件报警。

例如:监控apache状态监控mysql的状态监控内存使用以及硬盘的使用

一、监控apache

为了更好地实现该功能,使用的命令是 nc -w

主要命令如下:

nc – w 3 localhost 80 &>/dev/null

-w 3 表示与localhost 80 端口通信3秒,如果成功通信证明apache当前状态正常。

通过if条件判断,并发送邮件给 admin@ptbird.cn(之前添加的用户admin在windows下的客户端中接收邮件)

 

nc -w 3 localhost 80 &>/dev/null

if [[ $? -eq 0 ]]; then
	str="apache server status OK!"
else
	str="apache server status ERROR!"
fi

echo $str | mail -s 'apache web server' admin@ptbird.com.cn
#向admin@ptbird.com.cn发送邮件,主题为 apache web server

 

二、监控mysql

mysql不允许外IP连接,因此需要监控localhost 3306

其他的和apache一样

主要命令如下:

nc – w 3 localhost 3306&>/dev/null

-w 3 表示与localhost 3306 端口通信3秒,如果成功通信证明mysql当前状态正常。

通过if条件判断,并发送邮件给 admin@ptbird.cn(之前添加的用户admin在windows下的客户端中接收邮件)

 

nc -w 3 localhost 3306 &>/dev/null

if [[ $? -eq 0 ]]; then
	str="mysql server status OK!"
else
	str="mysql server status ERROR!"
fi

echo $str | mail -s 'mysql server' admin@ptbird.com.cn

 

三、监控disk用量

disk的用量我用了 df -Th

主要命令如下:

df -Th | sed -n ‘3p’ |awk ‘{print int($5)}’

sed -n ‘3p’ 取第三行

awk ‘{print int($5)}’   输出第五列 因为第五列是百分比,去掉百分号用来比较。

(我不太习惯用awk获取行)

通过if条件判断,并发送邮件给 admin@ptbird.cn(之前添加的用户admin在windows下的客户端中接收邮件)

ds=`df -Th | sed -n '3p' |awk '{print int($5)}'`

if [[ ds -lt 45 ]]; then
	str="disk space is less than 45%!"
else
	str="disk space is greater than 45%!"
fi

echo $str | mail -s 'linux server disk space' admin@ptbird.com.cn

 

三、监控memory用量

mem 用的是 free -m

主要命令如下:

free -m | sed -n ‘2p’|awk ‘{print int($3*100/$2)}’

取第二行并且第三列/第二列的结果为百分比,但是因为结果其实是0.23… 所以*100再取整得到的是百分比的整数

通过if条件判断,并发送邮件给 admin@ptbird.cn(之前添加的用户admin在windows下的客户端中接收邮件)

per=`free -m | sed -n '2p'|awk '{print int($3*100/$2)}'`

if [[ per -lt 45 ]]; then
	str="mem space is less than 45%!Now mem space is used ${per}%"
else
	str="mem space is greater than 45%!Now mem space is used ${per}%"
fi

echo $str | mail -s 'linux server mem space' admin@ptbird.com.cn

四、crontab计划

将上面的脚本写在一个脚本中可以发送4封邮件,也可以发送一封,把字符串拼接就行了。

为了避免编码问题,全用的英文发送。

编辑crontab:

crontab -e 

10 14 * * * bash /mnt/monitor.sh

五、效果:

计划任务:

linux3

邮件发送:

linux4

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值