内容简介:Linux基础之-利用shell脚本实现自动监控系统服务
目的:监控集群内nginx及nfs服务运行是否正常,如任一服务异常,则发送邮件通知用户
条件: 1. 主机及子机IP地址,hostname已确定;
2. 主机与子机能够免密通讯,即基于密匙通讯(相关命令:ssh-keygen;ssh-copy-id -i web1);
需要的文件:
1. python邮件发送工具;
2. nfc.sh监控脚本,监控nginx及nfs服务状态,并调用mail发送 工具 通知用户;
3. nfc-install.sh监控部署脚本,运行在主机,为子机配置文件,执行命令;
详细代码:
1. 邮件发送工具
将以下代码创建到“/usr/bin/mail”文件内,并赋予执行权限(chmod +x /usr/bin/mail)
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
import smtplib
import email.mime.multipart
import email.mime.text
server = 'smtp.163.com'
port = '25'
def sendmail(server,port,user,pwd,msg):
smtp = smtplib.SMTP()
smtp.connect(server,port)
smtp.login(user, pwd)
smtp.sendmail(msg['from'], msg['to'], msg.as_string())
smtp.quit()
print('邮件发送成功email has send out !')
if __name__ == '__main__':
msg = email.mime.multipart.MIMEMultipart()
msg['Subject'] = 'check your service of nginx and nfs'
msg['From'] = 'python4_mail@163.com'
msg['To'] = 'python4_recvmail@163.com'
user = 'python4_mail'
pwd = '123456789'
content='%s\n%s' %('\n'.join(sys.argv[1:4]),' '.join(sys.argv[4:])) #格式处理,专门针对我们的邮件格式
txt = email.mime.text.MIMEText(content, _charset='utf-8')
msg.attach(txt)
sendmail(server,port,user,pwd,msg)
python通过SMTP发送邮件失败:
错误1:smtplib.SMTPAuthenticationError: (550, b‘User has no permission‘)
我们使用 python 发送邮件时相当于自定义客户端根据用户名和密码登录,然后使用SMTP服务发送邮件,新注册的163邮箱是默认不开启客户端授权的(对指定的邮箱大师客户端默认开启),因此登录总是被拒绝,解决办法(以163邮箱为例):进入163邮箱-设置-客户端授权密码-开启(授权码是用于登录第三方邮件客户端的专用密码)
错误2:smtplib.SMTPAuthenticationError: (535, b‘Error: authentication failed‘)
以163邮箱为例,在开启POP3/SMTP服务,并开启客户端授权密码时会设置授权码,将这个授权码代替smtplib.SMTP().login(user,password)方法中的password即可。
2. nfc.sh监控脚本
#! /bin/bash
#nginx及nfs服务监控脚本,如果异常,将发送邮件通知
function monitor_nfc() {
systemctl status nginx
nginx=$?
systemctl status nfs
nfs=$?
clear
if [ $nginx -eq 0 ] && [ $nfs -eq 0 ]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig |awk 'NR==2{print $2}')
MSG:nginx.service and nfs.service is both running"
echo msg
# /usr/bin/mail $msg #服务运行正常,不发送邮件通知
elif [ $nginx -ne 0 ] && [ $nfs -eq 0 ]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig |awk 'NR==2{print $2}')
MSG:nginx.service is dead,nfs.service is running"
echo $msg
/usr/bin/mail $msg
elif [ $nginx -ne 0 ] && [ $nfs -ne 0 ]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig |awk 'NR==2{print $2}')
MSG:nginx.service and nfs.service is both dead"
echo $msg
/usr/bin/mail $msg
elif [ $nginx -eq 0 ] && [ $nfs -ne 0 ]
then
msg="TIME:$(date +%F_%T)
HOSTNAME:$(hostname)
IPADDR:$(ifconfig |awk 'NR==2{print $2}')
MSG:nginx.service is running,nfs.service is dead"
echo $msg
/usr/bin/mail $msg
fi
}
monitor_nfc &>> /tmp/monitor.log
3. nfc-install监控部署脚本
#! /bin/bash
#首先执行主机的nfc.sh服务监控脚本
/root/nfc.sh
#然后将主机的服务监控脚本nfc.sh和发送邮件文件上传至web机器
for i in {134,135,136}
do
scp /root/nfc.sh 192.168.47.$i:/share/ #将主机的服务监控脚本nfc.sh上传至web机器
scp /usr/bin/mail 192.168.47.$i:/usr/bin/ #将发送邮件文件上传至web机器
ssh root@192.168.47.$i chmod +x /share/nfc.sh #增加nfc脚本文件的执行权限
ssh root@192.168.47.$i chmod +x /usr/bin/mail #增加发送邮件文件的执行权限
ssh root@192.168.47.$i /share/nfc.sh #执行nfc脚本监控功能
done
ssh 192.168.47.133 #最终回到主机终端
详见图片
结果:
主机
子机1
本文永久更新链接地址 : http://www.linuxidc.com/Linux/2017-05/144222.htm
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 告警系统主脚本,告警系统配置文件,告警系统监控项目
- 分布式监控系统 WGCLOUD 更新,支持自定义告警脚本
- shell脚本监控系统中占用CPU,内存最大的进程
- 【监控系统】配合Graphite使用的报警系统
- WGCLOUD 监控系统更新,集成 ES 在线监控工具
- WGCLOUD 监控系统更新,进程监控模块 bug 修复
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Data Structures and Algorithms
Alfred V. Aho、Jeffrey D. Ullman、John E. Hopcroft / Addison Wesley / 1983-1-11 / USD 74.20
The authors' treatment of data structures in Data Structures and Algorithms is unified by an informal notion of "abstract data types," allowing readers to compare different implementations of the same......一起来看看 《Data Structures and Algorithms》 这本书的介绍吧!