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


  1. 可以使用psutil包获取cpu、内存等信息
  2. 使用qq邮箱发送,需要使用ssl协议,465端口,即smtplib.SMTP_SSL(host, 465)
import smtplib,time,wmi,psutil
import logging;logging.basicConfig(level=logging.INFO)
from email.mime.text import MIMEText

def send(content, detials):
    msg = MIMEText(content+'\nmem total:'+(str)(detials[0])+'\ncpu total:'+(str)(detials[1]))
    logging.debug(msg.as_string())
    msg['Subject'] = subject
    msg['From'] = e_from
    msg['To'] = e_to
    #qq
    # s = smtplib.SMTP_SSL(host, port)
    #163
    s = 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 = '*****@qq.com'
# password = '******'
# e_to = '******@163.com'
# host = 'smtp.qq.com'
# port = 465
#163 to qq
e_from = '*****@163.com'
password = '*****'
e_to = '****@qq.com'
host = 'smtp.163.com'
port = 25
subject = '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=2
while i>0:
    time.sleep(100)
    t = time.localtime(time.time())
    hour_and_min = time.strftime('%d-%H:%M:%S')
    #28-11:08:43
    cp = 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




  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值