“Python监控今测状态并实现告警”的版本间的差异
(→Python监控进程状态并实现告警) |
(→Python监控进程状态并实现告警) |
||
(未显示同一用户的5个中间版本) | |||
第36行: | 第36行: | ||
'''(通过多次实验发现,需要先获取对于的SMTP邮箱授权码,我这里使用的163邮箱)''' | '''(通过多次实验发现,需要先获取对于的SMTP邮箱授权码,我这里使用的163邮箱)''' | ||
[[文件:2020-08-09 094100.png]] | [[文件:2020-08-09 094100.png]] | ||
+ | |||
+ | 代码内容中的密码填写邮箱服务器上获取的授权码,不是邮箱密码,注意区分 | ||
Python代码: | Python代码: | ||
第63行: | 第65行: | ||
sm = SendEMail() | sm = SendEMail() | ||
sm.sendmail(['15610347662@163.com'], 'Python测试邮箱', '人生苦短') | sm.sendmail(['15610347662@163.com'], 'Python测试邮箱', '人生苦短') | ||
+ | |||
+ | 运行截图:<br> | ||
+ | [[文件:2020-08-09 101601.png]]<br> | ||
+ | 接收到的邮件截图:<br> | ||
+ | [[文件:2020-08-09_101134.png]] | ||
+ | 发现给qq邮箱发送会被服务器拦截可能存在检测机制: | ||
+ | [[文件:2020-08-09 101210.png]] | ||
+ | |||
+ | '''我们整合前两个代码,实现可以检测进程又可以发送邮箱代码如下:''' | ||
+ | |||
+ | #coding:utf-8 | ||
+ | import smtplib | ||
+ | from email.mime.text import MIMEText | ||
+ | class SendEMail(object): | ||
+ | # 定义第三方 SMTP 服务 | ||
+ | def __init__(self): | ||
+ | self.mail_host = "smtp.exmail.qq.com" # SMTP服务器 | ||
+ | self.mail_user = "tech.sys@aa.cn" # 用户名 | ||
+ | self.mail_pass = "aapwd" # 密码 | ||
+ | self.sender = 'tech.sys@aa.cn' # 发件人邮箱 | ||
+ | self.smtpObj = smtplib.SMTP_SSL(self.mail_host, 465) | ||
+ | self.smtpObj.login(self.mail_user, self.mail_pass) # 登录验证 | ||
+ | def sendmail(self, receivers, title, content): | ||
+ | message = MIMEText(content, 'plain', 'utf-8') # 内容, 格式, 编码 | ||
+ | message['From'] = "{}".format(self.sender) | ||
+ | message['To'] = ",".join(receivers) | ||
+ | message['Subject'] = title | ||
+ | try: | ||
+ | self.smtpObj.sendmail(self.sender, message['To'].split(','), message.as_string()) # 发送 | ||
+ | print("mail has been send successfully.") | ||
+ | except smtplib.SMTPException as e: | ||
+ | print(e) | ||
+ | if __name__ == '__main__': | ||
+ | sm = SendEMail() | ||
+ | sm.sendmail(['1093381395@qq.com'], '主题', '正文') | ||
+ | |||
+ | |||
+ | 至此我们基本上实现了可以通过检测进程然后实现告警了。但是在我们编程当中,我们需要有模块化的编程思想,也就是有-些组件如果能模块化就进行模块化,那样子如果 | ||
+ | 你有其他需求的话,想复用原来脚本的函数的话就不需要再去写重复的函数了。所以我们可以通过类的方式进行导入。然后实现模块化的编程。 | ||
+ | |||
+ | |||
+ | 模块化编程:我们可以把邮件发送封装成一个类,用的时候直接导入就行了,将代码放在同一个目录下: | ||
+ | |||
+ | [[文件:2020-08-09 110258.png]] | ||
+ | |||
+ | |||
+ | S_mail.py代码: | ||
+ | |||
+ | #coding:utf-8 | ||
+ | import smtplib | ||
+ | from email.mime.text import MIMEText | ||
+ | class SendEMail(object): | ||
+ | # 定义第三方 SMTP 服务 | ||
+ | def __init__(self): | ||
+ | self.mail_host = "smtp.163.com" # SMTP服务器 | ||
+ | self.mail_user = "15610347662@163.com" # 用户名 | ||
+ | self.mail_pass = "APL" # 密码 | ||
+ | self.sender = '15610347662@163.com' # 发件人邮箱 | ||
+ | self.smtpObj = smtplib.SMTP_SSL(self.mail_host, 465) | ||
+ | self.smtpObj.login(self.mail_user,self.mail_pass) # 登录验证 | ||
+ | def sendmail(self,receivers,title,content): | ||
+ | message = MIMEText(content,'plain','utf-8') # 内容, 格式, 编码 | ||
+ | message['From'] = "{}".format(self.sender) | ||
+ | message['To'] = ",".join(receivers) | ||
+ | message['Subject'] = title | ||
+ | try: | ||
+ | self.smtpObj.sendmail(self.sender, message['To'].split(','), message.as_string()) # 发送 | ||
+ | print("mail has been send successfully.") | ||
+ | except smtplib.SMTPException as e: | ||
+ | print(e) | ||
+ | if __name__ == '__main__': | ||
+ | sm = SendEMail() | ||
+ | sm.sendmail(['15610347662@163.com'], '主题', '正文') | ||
+ | |||
+ | |||
+ | 解析:构造函数中初始化邮件发送人,smtp服务器等 | ||
+ | |||
+ | |||
+ | check.py代码: | ||
+ | |||
+ | # coding:utf-8 | ||
+ | from S_mail import SendEMail #导入邮件类 | ||
+ | import psutil | ||
+ | # 实例化邮件类 | ||
+ | sm = SendEMail() | ||
+ | ## 定义收件人 | ||
+ | receivers = ['15610347662@163.com','2977023867@qq.com'] # 接收人邮箱 | ||
+ | # 定义进程名 | ||
+ | P_name="node_exporter" | ||
+ | #定义检测进程函数 | ||
+ | def checkprocess(processname): | ||
+ | pl = psutil.pids() | ||
+ | for pid in pl: | ||
+ | if psutil.Process(pid).name() == processname: | ||
+ | return pid | ||
+ | if isinstance(checkprocess(P_name),int): | ||
+ | pass # 进程存在 | ||
+ | else: | ||
+ | print("{0}进程不存在,发送邮件".format(P_name)) | ||
+ | sm.sendmail(receivers,"{0}进程down掉了".format(P_name),"{0}进程down掉了,请检测原因....".format(P_name)) | ||
+ | |||
+ | 执行脚本: python3 check.py | ||
+ | |||
+ | 当进程不存在时:<br> | ||
+ | [[文件:2020-08-09 111319.png]] | ||
+ | |||
+ | 进程不存在发送邮箱警告: | ||
+ | |||
+ | [[文件:2020-08-09 111641.png]] | ||
+ | |||
+ | 当进程存在时: | ||
+ | |||
+ | [[文件:2020-08-09 113137.png]] |
2020年8月9日 (日) 03:33的最新版本
Python监控进程状态并实现告警
公司的Java应用程序会莫名其妙的挂掉,然后我们再去登录服务器看看程序是不是挂掉了,这样是非常耗时间麻烦的事,这种情况和什么行业没有直接联系但却普遍存在。后来我们可以使用supervisor进行守护进程。
Supervisor用Python开发的一个client/server服务,是Linux/Unix系统 下的一个进程管理工具,不支持Windows系统。它可以很方便地监听、启动、停止、重劃-个或多个进程。用Supervisor管理的进程, 当-个进程意外被杀死,或者是意外被停止(系统负载过高,cpu 占用率很等),supervisor 监听到进程死后,自动将它重新拉起来,很方便地做到进程自动恢复的功能,不再需要自己写shel脚本来控制。
通过Python代码实现
第一步:我们首先判断进程是否存在的逻辑,采用psutil来实现,先安装库
pip3 install psutil
进程检测实现代码如下:
#coding:utf-8 import psutil def proc_exist(process_name): pl = psutil.pids() for pid in pl: if psutil.Process(pid).name() == process_name: return pid if isinstance(proc_exist('sshdd'),int): print('进程存在') else: print('进程不存在')
运行代码我们这里检测ssh服务是否开启:
代码解析: 解析: . 首先我们先定义一个proc_exist函数,函数的第一个参数传入进程名, 中pl =sutil.pids()示把所有的进程列出来。接着我们for循环一下pid的列表,当找到psutil. Process(pid) .name()的名词为传入的参数的名字的时候就返回pid 值,没有就不做任何操作(可以认为返回内容为空)接着isinstance用于检测返回内容。
上面可以进行进程检测了,我们实现发送邮箱代码实现:
(通过多次实验发现,需要先获取对于的SMTP邮箱授权码,我这里使用的163邮箱)
代码内容中的密码填写邮箱服务器上获取的授权码,不是邮箱密码,注意区分
Python代码:
#coding:utf-8 import smtplib from email.mime.text import MIMEText class SendEMail(object): # 定义第三方 SMTP 服务 def __init__(self): self.mail_host = "smtp.163.com" # SMTP服务器 self.mail_user = "15610347662@163.com" # 用户名 self.mail_pass = "APLL" # 密码 self.sender = '15610347662@163.com' # 发件人邮箱 self.smtpObj = smtplib.SMTP_SSL(self.mail_host, 465) self.smtpObj.login(self.mail_user, self.mail_pass) # 登录验证 def sendmail(self, receivers, title, content): message = MIMEText(content, 'plain', 'utf-8') # 内容, 格式, 编码 message['From'] = "{}".format(self.sender) message['To'] = ",".join(receivers) message['Subject'] = title try: self.smtpObj.sendmail(self.sender, message['To'].split(','), message.as_string()) # 发送 print("mail has been send successfully.") except smtplib.SMTPException as e: print(e) if __name__ == '__main__': sm = SendEMail() sm.sendmail(['15610347662@163.com'], 'Python测试邮箱', '人生苦短')
运行截图:
接收到的邮件截图:
发现给qq邮箱发送会被服务器拦截可能存在检测机制:
我们整合前两个代码,实现可以检测进程又可以发送邮箱代码如下:
#coding:utf-8 import smtplib from email.mime.text import MIMEText class SendEMail(object): # 定义第三方 SMTP 服务 def __init__(self): self.mail_host = "smtp.exmail.qq.com" # SMTP服务器 self.mail_user = "tech.sys@aa.cn" # 用户名 self.mail_pass = "aapwd" # 密码 self.sender = 'tech.sys@aa.cn' # 发件人邮箱 self.smtpObj = smtplib.SMTP_SSL(self.mail_host, 465) self.smtpObj.login(self.mail_user, self.mail_pass) # 登录验证 def sendmail(self, receivers, title, content): message = MIMEText(content, 'plain', 'utf-8') # 内容, 格式, 编码 message['From'] = "{}".format(self.sender) message['To'] = ",".join(receivers) message['Subject'] = title try: self.smtpObj.sendmail(self.sender, message['To'].split(','), message.as_string()) # 发送 print("mail has been send successfully.") except smtplib.SMTPException as e: print(e) if __name__ == '__main__': sm = SendEMail() sm.sendmail(['1093381395@qq.com'], '主题', '正文')
至此我们基本上实现了可以通过检测进程然后实现告警了。但是在我们编程当中,我们需要有模块化的编程思想,也就是有-些组件如果能模块化就进行模块化,那样子如果
你有其他需求的话,想复用原来脚本的函数的话就不需要再去写重复的函数了。所以我们可以通过类的方式进行导入。然后实现模块化的编程。
模块化编程:我们可以把邮件发送封装成一个类,用的时候直接导入就行了,将代码放在同一个目录下:
S_mail.py代码:
#coding:utf-8 import smtplib from email.mime.text import MIMEText class SendEMail(object): # 定义第三方 SMTP 服务 def __init__(self): self.mail_host = "smtp.163.com" # SMTP服务器 self.mail_user = "15610347662@163.com" # 用户名 self.mail_pass = "APL" # 密码 self.sender = '15610347662@163.com' # 发件人邮箱 self.smtpObj = smtplib.SMTP_SSL(self.mail_host, 465) self.smtpObj.login(self.mail_user,self.mail_pass) # 登录验证 def sendmail(self,receivers,title,content): message = MIMEText(content,'plain','utf-8') # 内容, 格式, 编码 message['From'] = "{}".format(self.sender) message['To'] = ",".join(receivers) message['Subject'] = title try: self.smtpObj.sendmail(self.sender, message['To'].split(','), message.as_string()) # 发送 print("mail has been send successfully.") except smtplib.SMTPException as e: print(e) if __name__ == '__main__': sm = SendEMail() sm.sendmail(['15610347662@163.com'], '主题', '正文')
解析:构造函数中初始化邮件发送人,smtp服务器等
check.py代码:
# coding:utf-8 from S_mail import SendEMail #导入邮件类 import psutil # 实例化邮件类 sm = SendEMail() ## 定义收件人 receivers = ['15610347662@163.com','2977023867@qq.com'] # 接收人邮箱 # 定义进程名 P_name="node_exporter" #定义检测进程函数 def checkprocess(processname): pl = psutil.pids() for pid in pl: if psutil.Process(pid).name() == processname: return pid if isinstance(checkprocess(P_name),int): pass # 进程存在 else: print("{0}进程不存在,发送邮件".format(P_name)) sm.sendmail(receivers,"{0}进程down掉了".format(P_name),"{0}进程down掉了,请检测原因....".format(P_name))
执行脚本: python3 check.py
进程不存在发送邮箱警告:
当进程存在时: