700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Python实现监控电脑 CPU或者内存占用过高自动发提醒邮件

Python实现监控电脑 CPU或者内存占用过高自动发提醒邮件

时间:2023-05-03 23:09:49

相关推荐

Python实现监控电脑 CPU或者内存占用过高自动发提醒邮件

可以使用psutil包获取cpu、内存等信息使用qq邮箱发送,需要使用ssl协议,465端口,即smtplib.SMTP_SSL(host, 465)

import smtplib,time,wmi,psutilimport logging;logging.basicConfig(level=logging.INFO)from email.mime.text import MIMETextdef send(content, detials):msg = MIMEText(content+'\nmem total:'+(str)(detials[0])+'\ncpu total:'+(str)(detials[1]))logging.debug(msg.as_string())msg['Subject'] = subjectmsg['From'] = e_frommsg['To'] = e_to#qq# s = smtplib.SMTP_SSL(host, port)#163s = smtplib.SMTP(host, port)try:s.login(e_from, password)s.sendmail(e_from, e_to, msg.as_string())logging.error('send success')except Exception as e:logging.error('send failure', e)finally:s.quit()#qq to 163# e_from = '*****@'# password = '******'# e_to = '******@'# host = ''# port = 465#163 to qqe_from = '*****@'password = '*****'e_to = '****@'host = ''port = 25subject = 'cpu使用率通知'# c = psutil.cpu_percent(1, True)# print(psutil.virtual_memory())# print('pc cpu:',psutil.cpu_percent(0))# print('py process cpu:', psutil.cpu_percent(None))# send(time_to_send)i=2while i>0:time.sleep(100)t = time.localtime(time.time())hour_and_min = time.strftime('%d-%H:%M:%S')#28-11:08:43cp = psutil.cpu_percent(0)detials = (psutil.virtual_memory(), cp)logging.info(hour_and_min)logging.info(detials[0])logging.info('cpu:'+(str)(detials[1])+'\n')if cp >3:send(hour_and_min, detials)i -= 1

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。