首页 > 编程语言 > 详细

python监控CPU/内存/磁盘,超过指定百分比,发送邮件

时间:2019-10-14 15:46:56      阅读:101      评论:0      收藏:0      [点我收藏+]
#!/usr/bin/python
#coding:utf-8

#导入psutil模块
import psutil
import yagmail

def mail(subject,contents):      #形参为邮件的标题和内容
    yag = yagmail.SMTP(user=zhangweihappy8@163.com, password=xxx, host=smtp.163.com)  #passwd为授权的密码不是登陆密码
    #发送邮件
    yag.send(to=zui_city_zhang@163.com,subject=subject, contents=contents)
    #断开连接
    yag.close()
  


def cpu_info():
    cpu = psutil.cpu_percent(1)
    return cpu
res=cpu_info()

def mem_info():
    mem = psutil.virtual_memory()
    info1={mem_total:mem[0],mem_free:mem[1],mem_percent:mem[2],mem_used:mem[3]}
    return info1
res2=mem_info()

def disk_info():
    disk = psutil.disk_usage(/)
    info2 = {total: disk[0], used: disk[1], free: disk[2], percent: disk[3]}   #同样写入一个字典
    return info2
res3=disk_info()

def main():
    m_cpu = res
    m_mem = res2
    m_disk = res3    #将各个分函数的调用结果当作函数体输入
    msg=‘‘‘          
    cpu使用率%s
    内存总量%sM      
    内存剩余%sM
    内存使用率%s
    内存使用量%sM
    磁盘总量%sGB
    磁盘使用量%sGB
    磁盘剩余量%sGB
    磁盘使用率%s%%
    ‘‘‘%(m_cpu,int(m_mem.get(mem_total)/1024/1024),int(m_mem[mem_free]/1024/1024),m_mem[mem_percent],int(m_mem[mem_used]/1024/1024),int(m_disk[total]/1024/1024/1024),int(m_disk[used]/1024/1024/1024),int(m_disk[free]/1024/1024/1024),m_disk[percent]) 
    print(msg)
    if m_cpu >50:
        mail(cpu报警,msg)
    else:
        print(cpu正常)
    if m_mem[mem_percent] > 50:
        mail(内存占用过半,msg)
    else:
        print(relax)
    if m_disk[percent] > 50:
        mail(磁盘快炸了,msg)
    else:
        print(磁盘正常)

if __name__==__main__:
    main()

 

python监控CPU/内存/磁盘,超过指定百分比,发送邮件

原文:https://www.cnblogs.com/hello-wei/p/11671506.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!