python -----一个简单的小程序(监控电脑内存,cpu,硬盘)

一个简单的小程序

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

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

废话不多说 

源代码如下:

import yagmail
def sendmail(subject,contents):
yag = yagmail.SMTP(user='[email protected]',password='xxxxxxxxxx',host='xxxx.xxx.com')
yag.send(to='[email protected]',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()

猜你喜欢

转载自www.cnblogs.com/myxxjie/p/10764818.html