python 检测nginx服务邮件报警的脚本
作者:lixinliang 时间:2023-08-04 17:34:10
$ cat checkserver.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import socket
import smtplib
from email.mime.text import MIMEText
from email.header import Header
mail_host = "smtp.exmail.qq.com"
mail_user = "yunwei-monitor@111.com"
mail_pass = "yNE8dcsx"
sender = 'yunwei-monitor@111.com'
receivers = ['lixinliang@111.com']
def Checkserverdown():
#三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
message = MIMEText('192.168.71.200 nginx is down','plain','utf-8')
message['From'] = Header("Nginx is down ", 'utf-8') # 发送者
message['To'] = Header("李鑫亮", 'utf-8') # 接收者
subject = '192.168.71.200 nginx is down'
message['Subject'] = Header(subject,'utf-8')
try:
smtpobj = smtplib.SMTP()
smtpobj.connect(mail_host,25)
smtpobj.login(mail_user,mail_pass)
smtpobj.sendmail(sender,receivers,message.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("Error: 无法发送邮件")
def Checkserverstilldown():
#三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
message = MIMEText('192.168.71.200 nginx is still down','plain','utf-8')
message['From'] = Header("Nginx is still down ", 'utf-8') # 发送者
message['To'] = Header("李鑫亮", 'utf-8') # 接收者
subject = '192.168.71.200 nginx is still down'
message['Subject'] = Header(subject,'utf-8')
try:
smtpobj = smtplib.SMTP()
smtpobj.connect(mail_host,25)
smtpobj.login(mail_user,mail_pass)
smtpobj.sendmail(sender,receivers,message.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("Error: 无法发送邮件")
def Checkserverup():
#三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
message = MIMEText('192.168.71.200 nginx is up','plain','utf-8')
message['From'] = Header("Nginx is up ", 'utf-8') # 发送者
message['To'] = Header("李鑫亮", 'utf-8') # 接收者
subject = '192.168.71.200 nginx is up'
message['Subject'] = Header(subject,'utf-8')
try:
smtpobj = smtplib.SMTP()
smtpobj.connect(mail_host,25)
smtpobj.login(mail_user,mail_pass)
smtpobj.sendmail(sender,receivers,message.as_string())
print("邮件发送成功")
except smtplib.SMTPException:
print("Error: 无法发送邮件")
# 判断 nginx 进程输出内容来确定是否要进行进程启动
file = "/tmp/nginx.txt"
os.system("""ps -ef |grep nginx |grep -Ev "grep|vim" > %s""" % file)
print (os.path.getsize(file))
if os.path.getsize(file) == 0:
Checkserverdown()
os.system("/usr/sbin/nginx")
print (os.path.getsize(file))
os.system("""ps -ef |grep nginx |grep -Ev "grep|vim" > %s""" % file)
if os.path.getsize(file) == 0:
Checkserverstilldown()
os.system("/usr/sbin/nginx")
else:
Checkserverup()
来源:https://www.cnblogs.com/lixinliang/p/13825539.html
标签:python,检测,nginx,邮件,报警
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python2爬取百度贴吧指定关键字和图片代码实例
2022-10-12 09:03:44
Django Form 实时从数据库中获取数据的操作方法
2024-01-23 14:35:54
![](https://img.aspxhome.com/file/2023/2/115182_0s.png)
详解Python执行py文件是否需要可执行权限
2021-01-19 10:52:53
怎样在JavaScript里写一个swing把数据插入数据库
2024-01-20 09:26:42
golang简单位运算示例
2024-02-08 18:53:48
Pyhhon之Pygame的Font文本和字体
2021-08-14 13:00:33
pandas数据筛选和csv操作的实现方法
2022-04-23 15:36:21
vue中项目页面空白但不报错产生的原因及分析
2024-05-03 15:12:17
![](https://img.aspxhome.com/file/2023/8/130188_0s.png)
基于MySQL架构图解
2024-01-15 16:25:43
![](https://img.aspxhome.com/file/2023/2/84552_0s.png)
《用户体验的要素》摘记
2008-08-04 17:59:00
js动态生成form 并用ajax方式提交的实现方法
2024-04-17 09:59:54
一篇文章带你入门SQL编程
2024-01-12 13:05:22
![](https://img.aspxhome.com/file/2023/0/109830_0s.png)
ThinkPHP学习笔记(一)ThinkPHP部署
2023-09-09 12:42:16
MySql中特殊运算符的使用方法总结
2024-01-19 23:27:09
![](https://img.aspxhome.com/file/2023/0/114980_0s.jpg)
Python @property原理解析和用法实例
2023-05-19 00:59:41
php浅析反序列化结构
2023-11-17 17:34:37
python3.4中清屏的处理方法
2023-11-14 04:09:21
![](https://img.aspxhome.com/file/2023/3/64603_0s.jpg)
对python中数组的del,remove,pop区别详解
2021-01-23 09:22:31
python根据list重命名文件夹里的所有文件实例
2022-02-12 15:54:30
matplotlib quiver箭图绘制案例
2023-07-13 10:04:11
![](https://img.aspxhome.com/file/2023/3/76893_0s.jpg)