zoukankan      html  css  js  c++  java
  • 【python】Psutil指定Pid监控机器cpu和内存

    借鉴地址:https://www.liaoxuefeng.com/wiki/1016959663602400/1183565811281984

    import smtplib
    from email import encoders
    from email.mime.base import MIMEBase
    from email.mime.multipart import MIMEMultipart
    from email.header import Header
    
    import psutil
    from datetime import datetime
    import time
    from matplotlib import pyplot as plt
    import csv
    
    from util.http_rtc_until import get_time, send
    
    pid = 9214
    proc = psutil.Process(pid)
    
    def timer():
        out = open("timer.csv", 'w')
        start_time = int(time.time())
        while True:
            x = datetime.now().strftime("%M:%S")
            time.sleep(1)
            y = proc.memory_info().rss / 1024 / 1024
            print(x,'Used Memory:', y, 'MB')
    
            list = [x, y]
            csv_write = csv.writer(out)
    
            csv_write.writerow(list)
            end_time = int(time.time())
            if end_time - start_time > 5:
                break
    
    def pic():
        with open("timer.csv") as Filename:
            reader = csv.reader(Filename)
            x = []
            y = []
            for row in reader:
                x.append(row[0])
                y.append(row[1])
    
        plt.plot(x, y)
        plt.xticks(x, x, rotation=60)
        plt.rcParams["figure.figsize"] = (8.0,8.0)
        plt.savefig('test.png')
        image = 'test.png'
        plt.show()
    
        # send("meizhuo@conew.com","来自251的图片",image)
        sender = "meizhuo@conew.com"
        receiver = "meizhuo@conew.com"
        password = "cbidcdmrsnupphtf"   #密钥
        smtp = "smtp.gmail.com"
        port = "465"    #gmai固定端口号
    
        message = MIMEMultipart()
        message['From'] = sender
        message['To'] = receiver
        message['Subject'] = Header("251的图片", 'UTF-8')  # 邮件主题
    
        #附件带图片
        with open(image, 'rb') as f:
            mime = MIMEBase('image', 'image', filename=image)
            mime.add_header('Content-Disposition', 'attachment', filename=image)
            mime.set_payload(f.read())
            encoders.encode_base64(mime)
            message.attach(mime)
    
        smtp_obj = smtplib.SMTP_SSL(smtp, port)
        smtp_obj.login(sender, password)
        receiver and smtp_obj.sendmail(sender, receiver, message.as_string())
        print("success")
    
    timer()
    pic()
  • 相关阅读:
    linux下安装elasticsearch5.6.3
    linux下安装git
    环境安装备忘录 Zookeeper
    环境安装备忘录 JDK
    环境安装备忘录 Tomcat
    MySql 通过show status 优化数据库性能
    MySQL执行计划解读 转他人文章
    2015年12月21日 my.cnf 配置
    mysql 如何查看my.cnf的 位置
    mysql状态查看 QPS/TPS/缓存命中率查看
  • 原文地址:https://www.cnblogs.com/darlingmz/p/12838669.html
Copyright © 2011-2022 走看看