python实现监控某个服务 服务崩溃即发送邮件报告

  • Post category:Python

实现监控某个服务并在服务崩溃时发送邮件报告可以分为以下几个步骤:

  1. 安装需要的Python库
    需要安装的库有Python自带的smtplib、time和socket库,以及第三方的psutil、schedule、email库。
pip install psutil
pip install schedule
pip install email
  1. 编写Python脚本
    首先需要导入需要的库。其中,psutil库用于获取进程信息,schedule库用于定时执行脚本任务,email库用于发送邮件。
import time
import socket
import smtplib
import psutil
import schedule
from email.mime.text import MIMEText
from email.header import Header

接着,需要编写一个函数check_service(),用于检测服务是否正常。具体实现方法为获取进程信息,判断进程名称是否在进程列表中,若不存在则说明服务崩溃了,需要发送报告邮件。

def check_service():
    # 获取所有运行中的进程
    pids = psutil.pids()
    # 遍历所有进程,查找process_name进程
    for pid in pids:
        p = psutil.Process(pid)
        if p.name() == process_name:
            return True
    return False

接下来,需要编写一个发送邮件的函数send_email(),并使用smtplib库连接发件箱服务器,将邮件发送给指定收件人。

def send_email(subject, content):
    # 发件人邮箱
    from_addr = "XXX@XXX.com"
    # 邮箱密码
    password = "*********"
    # 收件人邮箱
    to_addr = "XXX@XXX.com"
    # 邮件服务器地址
    smtp_server = "smtp.163.com"

    # 构造邮件内容
    msg = MIMEText(content, 'plain', 'utf-8')
    msg['From'] = Header(from_addr)
    msg['To'] = Header(to_addr)
    msg['Subject'] = Header(subject)

    # 连接邮件服务器,登录发件人邮箱,发送邮件
    server = smtplib.SMTP(smtp_server, 25)
    server.login(from_addr, password)
    server.sendmail(from_addr, [to_addr], msg.as_string())
    server.quit()

最后,使用schedule库定时执行监控任务,每隔一分钟检测一次服务是否正常,如果服务崩溃了则发送邮件报告。

if __name__ == '__main__':
    # 监控服务名称
    process_name = "service.exe"
    # 邮件主题
    subject = "服务崩溃报告"
    while True:
        # 检测服务是否正常
        if not check_service():
            content = "服务崩溃了,请及时处理!"
            send_email(subject, content)
        # 每隔一分钟检测一次服务是否正常
        time.sleep(60)
  1. 示例

例如,我们要监控的服务名称是notepad.exe,邮箱地址为123@qq.com,邮箱密码为abc123,邮件内容为“notepad.exe崩溃了!”,则相应的Python脚本如下:

import time
import socket
import smtplib
import psutil
import schedule
from email.mime.text import MIMEText
from email.header import Header

def check_service():
    # 获取所有运行中的进程
    pids = psutil.pids()
    # 遍历所有进程,查找process_name进程
    for pid in pids:
        p = psutil.Process(pid)
        if p.name() == process_name:
            return True
    return False

def send_email(subject, content):
    # 发件人邮箱
    from_addr = "123@qq.com"
    # 邮箱密码
    password = "abc123"
    # 收件人邮箱
    to_addr = "123@qq.com"
    # 邮件服务器地址
    smtp_server = "smtp.qq.com"

    # 构造邮件内容
    msg = MIMEText(content, 'plain', 'utf-8')
    msg['From'] = Header(from_addr)
    msg['To'] = Header(to_addr)
    msg['Subject'] = Header(subject)

    # 连接邮件服务器,登录发件人邮箱,发送邮件
    server = smtplib.SMTP(smtp_server, 25)
    server.login(from_addr, password)
    server.sendmail(from_addr, [to_addr], msg.as_string())
    server.quit()

if __name__ == '__main__':
    # 监控服务名称
    process_name = "notepad.exe"
    # 邮件主题
    subject = "notepad.exe崩溃报告"
    while True:
        # 检测服务是否正常
        if not check_service():
            content = "notepad.exe崩溃了!"
            send_email(subject, content)
        # 每隔一分钟检测一次服务是否正常
        time.sleep(60)

如果我们要监控的服务名称改为chrome.exe,邮箱地址为456@163.com,邮箱密码为def456,邮件内容为“chrome.exe崩溃了!”,则相应的Python脚本如下:

import time
import socket
import smtplib
import psutil
import schedule
from email.mime.text import MIMEText
from email.header import Header

def check_service():
    # 获取所有运行中的进程
    pids = psutil.pids()
    # 遍历所有进程,查找process_name进程
    for pid in pids:
        p = psutil.Process(pid)
        if p.name() == process_name:
            return True
    return False

def send_email(subject, content):
    # 发件人邮箱
    from_addr = "456@163.com"
    # 邮箱密码
    password = "def456"
    # 收件人邮箱
    to_addr = "456@163.com"
    # 邮件服务器地址
    smtp_server = "smtp.163.com"

    # 构造邮件内容
    msg = MIMEText(content, 'plain', 'utf-8')
    msg['From'] = Header(from_addr)
    msg['To'] = Header(to_addr)
    msg['Subject'] = Header(subject)

    # 连接邮件服务器,登录发件人邮箱,发送邮件
    server = smtplib.SMTP(smtp_server, 25)
    server.login(from_addr, password)
    server.sendmail(from_addr, [to_addr], msg.as_string())
    server.quit()

if __name__ == '__main__':
    # 监控服务名称
    process_name = "chrome.exe"
    # 邮件主题
    subject = "chrome.exe崩溃报告"
    while True:
        # 检测服务是否正常
        if not check_service():
            content = "chrome.exe崩溃了!"
            send_email(subject, content)
        # 每隔一分钟检测一次服务是否正常
        time.sleep(60)