python监控cpu 硬盘 内存

隐身守侯 提交于 2019-12-04 06:34:17
import psutil
import time
import yagmail

def sendmail(subject,contents):
yag = yagmail.SMTP(user='155xxx8589@163.com',password='邮箱授权码',host='smtp.163.com')
yag.send(to='15534828589@163.com',subject=subject,contents=contents)
yag.close()

def cpu():
cpu = psutil.cpu_percent(1)
return {'cpu_percent':cpu}
def mem():
mem_total = psutil.virtual_memory()[0]
mem_percent = psutil.virtual_memory()[2]
return {'mem_total':int(mem_total/1024/1024),'mem_percent':mem_percent}
def disk():
disk_total = psutil.disk_usage('c:')[0]
disk_percent = psutil.disk_usage('c:')[3]
return {'disk_total':int(disk_total/1024/1024/1024),'disk_percent':disk_percent}
def main():
info = {}
info.update(cpu())
info.update(mem())
info.update(disk())

msg = '''
cpu使用率:%s%%
内存使用率:%s%%
硬盘使用率:%s%%

内存总大小:%sM
硬盘总大小:%sG
''' % (info['cpu_percent'],info['mem_percent'],info['disk_percent'],info['mem_total'],info['disk_total'])
if info['cpu_percent'] > 5:
sendmail('cpu报警',msg)
if info['mem_percent'] > 50:
sendmail('内存报警',msg)
if info['disk_percent'] > 50:
sendmail('硬盘报警',msg)
while True :
if __name__ == '__main__':
main()
time.sleep(60)

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!