python监控cpu 硬盘 内存

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)

 

posted on 2019-11-11 20:08  负重前行岁月静好  阅读(629)  评论(0编辑  收藏  举报