Python实时监控计算机运行信息(电子邮件警告),python,电脑,预警

发表时间:2020-10-22

直接上代码

应用场景
实时监控电脑本地系统运行情况,或某个进程
内存异常,进行邮件警示

流程 程序运行 --> 数据上传mysql数据库 --> 报表实时查看

效果图
在这里插入图片描述


import time
import psutil
import pymysql
import re
import threading
import os
import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.utils import parseaddr, formataddr



def mysql_info():
    # 配置数据库连接
    conn = pymysql.connect(
        host='localhost',
        user='root',
        password='sumei15b',
        db='cpu',
        charset='utf8'
    )
    return conn



# 数据库连接配置
def mysql_insert(sql, data, table_name):
    # 配置数据库连接

    conn= mysql_info()

    # 创建游标对象
    cur = conn.cursor()

    # 判断数据表是否存在
    if table_exists(conn, cur, table_name) == 1:
        # 插入数据
        cur.execute(sql % data)
        conn.commit()
        print('成功插入', cur.rowcount, '条数据')
    else:
        print('数据表不存在,请重新确定')






# 判断 数据库表是否存在
def table_exists(conn, cur, table_name):
    sql = "show tables;"
    cur.execute(sql)
    tables = cur.fetchall()

    table_list = re.findall('(\'.*?\')', str(tables))
    table_list = [re.sub("'", '', each) for each in table_list]
    if table_name in table_list:
        # 存在返回1
        return 1
    else:
        # (cpu_percent,memory_total, memory_available, memory_used,memory_percent,strftime)
        if table_name == 'cpu_info':
            sql = 'create table {}(' \
                  'id int(255) primary key not null auto_increment,' \
                  'cpu_percent float (6),' \
                  'memory_total float (6),' \
                  'memory_available float (6),' \
                  'memory_used float (6),' \
                  'memory_percent float (6),' \
                  'strftime datetime' \
                  ')'.format(table_name)

            cur.execute(sql)
            conn.commit()
            return 1
        # (proc_id, status, memory_percent, stime, strftime)
        elif table_name == 'finebi_memory':
            sql = 'create table {}(' \
                  'id int(255) primary key not null auto_increment,' \
                  'proc_id int (10),' \
                  'status nvarchar (10),' \
                  'memory_percent float (6),' \
                  'stime datetime,' \
                  'strftime datetime' \
                  ')'.format(table_name)

            cur.execute(sql)
            conn.commit()
            return 1
        # (device, total, used, free, strftime)
        elif table_name == 'io_info':
            sql = 'create table {}(' \
                  'id int(255) primary key not null auto_increment,' \
                  'device nvarchar (10),' \
                  'total int (10),' \
                  'used int (10),' \
                  'free int(10),' \
                  'strftime datetime' \
                  ')'.format(table_name)

            cur.execute(sql)
            conn.commit()
            return 1
        #data = (percent, etime, stime,  strftime)
        elif table_name == 'email_info':
            sql = 'create table {}(' \
                  'id int(255) primary key not null auto_increment,' \
                  'percent float ,' \
                  'etime datetime,' \
                  'stime datetime,' \
                  'strftime datetime' \
                  ')'.format(table_name)
            cur.execute(sql)
            conn.commit()
            return 1
        else:
            # 不存在返回0
            return 0




# 系统总内存 CPU占用情况
def cpu_info():
    while True:
        cpu_percent = psutil.cpu_percent()

        print('----------------------------------- CPU占用情况-------------------------------')

        #print('CPU完整信息', psutil.cpu_times());
        #print('CPU占用率', cpu_percent);

        nc = psutil.virtual_memory()

        memory_total = round(nc.total / 1024 / 1024 / 1024, 2)
        memory_available = round(nc.available / 1024 / 1024 / 1024, 2)
        memory_used = round(nc.used / 1024 / 1024 / 1024, 2)
        memory_percent = nc.percent
        strftime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

        print('内存占用信息', nc)
        print('内存总值(G)', memory_total)
        print('内存未用值(G)', memory_available)
        print('内存使用值(G)', memory_used)
        print('内存占比', memory_percent)
        print('当前系统时间', strftime)

        print('  ')
        print('  ')

        table_name = 'cpu_info'
        sql = "INSERT INTO cpu_info (cpu_percent,memory_total, memory_available, memory_used,memory_percent,strftime) VALUES ( '%s', '%s', '%s', '%s','%s','%s' )"
        data = (cpu_percent, memory_total, memory_available, memory_used, memory_percent, strftime)

        # 调用mysql,执行逻辑
        mysql_insert(sql, data, table_name)
        # 执行 完成睡眠 30秒
        time.sleep(30)





# 进程 finebi.exe 内存占比
def finebi_memory():
    while True:
        for proc in psutil.process_iter():
            # 显示进程信息
            # print(proc)
            try:
                if proc.name() == 'finebi.exe':
                    p = psutil.Process(proc.pid)
                    # print(p)
                    print(p.memory_info())

                    strftime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
                    proc_id = proc.pid
                    status = p.status()
                    memory_percent = round(p.memory_percent(), 2)
                    stime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(p.create_time()))

                    print('-----------------------------------finebi.exe 内存占比-------------------------------')

                    print('当前系统时间', strftime)

                    print('进程ID:', proc_id)
                    print('进程状态', status)
                    print('进程内存利用率', memory_percent)
                    print('进程创建时间', stime)

                    print('  ')
                    print('  ')

                    table_name = 'finebi_memory'
                    sql = "INSERT INTO finebi_memory (proc_id,status, memory_percent, stime,strftime) VALUES ( '%s', '%s', '%s', '%s','%s' )"

                    data = (proc_id, status, memory_percent, stime, strftime)

                    # 调用mysql,执行逻辑
                    mysql_insert(sql, data, table_name)


            except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
                #进程未执行,不获取数据
                # table_name = 'finebi_memory'
                # sql = "INSERT INTO finebi_memory (proc_id,status, memory_percent, stime,strftime) VALUES ( '%s', '%s', '%s', '%s','%s' )"
                #
                # strftime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
                # data = (0, 'kill', 0, strftime, strftime)
                # # 调用mysql,执行逻辑
                # mysql_insert(sql, data, table_name)

                pass
        # 执行 完成睡眠 30秒
        time.sleep(30)




# 系统磁盘占比
def io_info():
    while True:
        io = psutil.disk_partitions()

        print('------------------------------------系统磁盘----------------------------------------------------')
        print('')
        print('')
        print("系统磁盘信息:" + str(io))

        try:
            for i in io:
                o = psutil.disk_usage(i.device)
                device = i.device[0:1]
                total = int(o.total / (1024.0 * 1024.0 * 1024.0))
                used = int(o.used / (1024.0 * 1024.0 * 1024.0))
                free = int(o.free / (1024.0 * 1024.0 * 1024.0))
                strftime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
                print('磁盘:', device)
                print("总容量:", total)
                print("已用容量:", used)
                print("可用容量:", free)
                print('当前系统时间', strftime)

                table_name = 'io_info'
                sql = "INSERT INTO io_info (device,total, used, free,strftime) VALUES ( '%s', '%s', '%s', '%s','%s' )"
                data = (device, total, used, free, strftime)

                # 调用mysql,执行逻辑
                mysql_insert(sql, data, table_name)

            
        except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess,PermissionError):
            pass
       
        # 执行 完成睡眠 半小时
        time.sleep(60 * 30)





#系统监控,进行邮件预警
def email_info():
    while True:
         conn =mysql_info()
         cur = conn.cursor()

         # 查询10分钟内内存使用占比,十分钟内如果系统内存使用量超过80 并且占比大于80% ,进行数据报警
         sql ='SELECT ROUND(SUM(百分比)/COUNT(DISTINCT id),2)  占比,MAX(strftime) etime ,MIN(strftime) stime FROM ( SELECT id,strftime,IF(memory_percent > 80,1,0) 百分比 FROM cpu_info WHERE strftime >= DATE_ADD(NOW(), INTERVAL -10  MINUTE) ) a'
         # sql = 'SELECT ROUND(SUM(百分比)/COUNT(DISTINCT id),2)  占比,MAX(strftime) etime ,MIN(strftime) stime  FROM ( SELECT id,strftime,IF(memory_percent > 30,1,0) 百分比 FROM cpu_info  ORDER BY id  DESC  LIMIT 0,20 ) a'
         cur.execute(sql)
         result = cur.fetchall()

         try:
             for it in result:
                 # 数据占比超过 80%
                 if float(it[0]) >= 0.8:
                     percent = float(it[0])
                     etime = it[1]
                     stime = it[2]
                     print('-------------------------------内存预警-----------------------------------')
                     print('占比', percent)
                     print('结束时间', etime)
                     print('开始时间', stime)
                     strftime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
                     print('当前系统时间 ',strftime)

                     print('  ')
                     print('  ')
                     data = (percent, stime, etime)

                     # 调用邮件发送
                     if int(send_report_mail(data) )== 1:
                         table_name = 'email_info'
                         sql = "INSERT INTO email_info (percent, etime, stime,  strftime) VALUES ( '%s', '%s', '%s', '%s' )"
                         datas = (percent, etime, stime,  strftime)
                         # 调用mysql,执行逻辑
                         mysql_insert(sql, datas, table_name)

         except  TypeError :
             pass
         
		 # 执行 完成睡眠 10分钟
         time.sleep(60 * 10)


def _format_addr(s):
    """格式化邮件地址"""
    name, addr = parseaddr(s)
    return formataddr((Header(name, 'utf-8').encode(), addr))


def send_report_mail(data):
    """发送最新的测试报告"""
    # 账号密码
    username = '*****@qq.com'
    #编译密码
    password = 'wnotylenvmjogifd'
    # SMTP服务器地址
    smtp_server = 'smtp.qq.com'
    smtp_port = '465'
    # 收件人地址
    addressee = '***@163.com'  # type is list

    try:
        # 初始化邮件对象
        msg = MIMEMultipart()
        msg['From'] = _format_addr("selenium爱好者 报表服务器246<%s>" % username)  # 邮件发件人
        msg['To'] = _format_addr('管理员 <%s>' % ','.join(addressee))  # 收件人地址
        msg['Subject'] = Header("报表服务器内存异常", 'utf-8').encode()  # 邮件主题



        text ='内存空间占用过大,请注意维护!!!'\
              +'  内存占比 '+str(data[0]) +'           '\
              +'开始时间 '+str(data[1]) +'        '\
              +'结束时间 '+str(data[2]) +'        '

        # 发送HTML文件
        msg.attach(MIMEText(text, 'html', 'utf-8'))

        # 括号中对应的是(SMTP服务器,端口)
        with smtplib.SMTP_SSL(smtp_server, smtp_port) as server:
            # 括号中对应的是(发件人邮箱账号、邮箱密码)
            server.login(username, password)
            # 括号中对应的是(发件人邮箱账号、收件人邮箱账号、发送邮件信息)
            server.sendmail(username, addressee, msg.as_string())
        print("测试结果邮件发送成功!")

        return 1
    except smtplib.SMTPException as e:
        print("Error: 无法发送邮件 {}".format(e))
        return 0



if __name__ == '__main__':
    #多线程执行多个方法
    threads = [threading.Thread(target=finebi_memory),
               threading.Thread(target=cpu_info),
               threading.Thread(target=io_info),
               threading.Thread(target=email_info)
               ]
    try :
        for t in threads:
            # 启动线程
            t.start()
        #打印异常,便于排查
        os.system("pause")
    except Exception as ex:
        print(ex)
        os.system("pause")


打包运行

https://www.cnblogs.com/mini-monkey/p/11195309.html

文章来源互联网,如有侵权,请联系管理员删除。邮箱:417803890@qq.com / QQ:417803890

微配音

Python Free

邮箱:417803890@qq.com
QQ:417803890

皖ICP备19001818号
© 2019 copyright www.pythonf.cn - All rights reserved

微信扫一扫关注公众号:

联系方式

Python Free