# -*- coding: utf-8 -*-
import subprocess
import smtplib
import email.utils
#邮件正文ZZ
from email.mime.text import MIMEText
def send_email():
#登录邮件服务器
smtp_obj = smtplib.SMTP_SSL("smtp.qq.com", 465) # 发件⼈邮箱中的SMTP服务器,端⼝是25
smtp_obj.login("@qq.com", "xxx") # 括号中对应的是发件⼈邮箱账号、邮箱密码
smtp_obj.set_debuglevel(True)
#smtp_obj.set_debuglevel(1) # 显示调试信息
# 设置邮件头信息
message = MIMEText("dadax-web进程消失,请手动拉起!!")
message["From"] = email.utils.formataddr(('datax-web', 'xxx@qq.com')) # 发送者
message["To"] = email.utils.formataddr(('恢复进程', 'xxx@163.com')) # 接收者
message["Subject"] = 'datax-web进程消失,请手动拉起!!' # 主题
# 发送
try:
smtp_obj.sendmail("xx@qq.com", ["xxx@163.com","xx@qq.com"], msg=message.as_string())
finally:
smtp_obj.quit()
def run_jps():
try:
# 执行jps命令
result = subprocess.run(['jps'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
admin_flag = 0
executor_flag = 0
# 打印命令的输出
for line in result.stdout.splitlines():
# print(line.split()[1])
# print('========')
if(line.split()[1] == 'DataXAdminApplication'):
admin_flag = 1
if(line.split()[1] == 'DataXExecutorApplication'):
executor_flag = 1
print(admin_flag,executor_flag)
if(admin_flag == 1 & executor_flag == 1):
print('进程正常')
else:
send_email()
# 如果有错误,打印错误信息
if result.stderr:
print(result.stderr)
except Exception as e:
print(f"Error running jps: {e}")
# 调用函数
run_jps()
python监控Linux服务器进程并邮件预警
于 2024-05-09 15:19:43 首次发布