zoukankan      html  css  js  c++  java
  • python -----一个简单的小程序(监控电脑内存,cpu,硬盘)

    一个简单的小程序

    用函数实现!~~

    实现: cpu 使用率大于百分之50 时  ,  C 盘容量不足5 G 时, 内存 低于2G 时。 出现以上其中一种情况,发送自动报警邮件!

    主要运用 到了两个 模块        yagmail   与   psutil      (没有的需要下载  pip 或者导入环境变量)

    废话不多说 

    源代码如下:

    import yagmail
    def sendmail(subject,contents):
    yag = yagmail.SMTP(user='xxxxxxx@qq.com',password='xxxxxxxxxx',host='xxxx.xxx.com')
    yag.send(to='xxxxxxxx@qq.com',subject=subject, contents=contents)
    yag.close()
    import psutil
    def mem () :
    mem = psutil.virtual_memory()
    free_mem = int(mem[4]/1024/1024/1024)
    return (free_mem)
    def cpu () :
    cpu1 = psutil.cpu_percent(1)
    return (cpu1)
    def disk () :
    disk = psutil.disk_usage(r'c:')
    free_disk = int(disk[2]/1024/1024/1024)
    return (free_disk)
    def main ():
    mem1 = mem()
    cpu1 = cpu()
    disk1 = disk()
    msg1 = '''
    cpu使用率 : %s%%
    内存剩余量 : %s G
    C盘容量剩余量 : %s G
    '''%(cpu1,mem1,disk1)
                              
    if cpu1 > 50:
    print('cpu过高')
    sendmail('cpu报警',msg1)
    elif mem1 < 2 :
    print('内存剩余量不足!')
    sendmail('内存报警,可用量不足',msg1)
    elif disk1 < 5 :
    print('c盘容量过少')
    sendmail('C盘可用容量不足',msg1)
    else:
    print('您的电脑一切正常')


    if __name__ == '__main__' :
    main()
  • 相关阅读:
    如何知道电脑是几核?
    宝塔服务器管理助手Linux面版使用教程
    阿里云服务器怎么更换系统盘

    MoonScript
    webmin
    requests 0.7.6
    盘古搜索
    c练习总结
    Download a webpage using CURL in C
  • 原文地址:https://www.cnblogs.com/myxxjie/p/10764818.html
Copyright © 2011-2022 走看看