欢迎来到思维库

思维库

Python监控服务器实现邮件微信报警

时间:2025-11-05 11:21:18 出处:数据库阅读(143)

Python监控服务器实现邮件微信报警
复制import psutil,监控件微 time import datetime  from wechatpy import WeChatClient  class Monitor():   cpu_data = []   @classmethod   def mem(cls, max=90):   val = psutil.virtual_memory().percent   if val > max:   cls.send_msg(内存使用率为{:1.f}%,超过了{}%,服务请关注.format(val,器实 max))   @classmethod   def cpu(cls, max=90):   val = psutil.cpu_percent(1)   cls.cpu_data.append(val)   if len(cls.cpu_data) >= 3:   avg = sum(cls.cpu_data) / len(cls.cpu_data)   if avg > max:   cls.send_msg(CPU使用率为{:1f}%,WordPress模板超过了{}%,现邮信报请关注.format(avg,监控件微 max))   cls.cpu_data.pop(0)   @classmethod   def send_msg(cls, content):   cls.mail(content)   cls.wechat(content)   @classmethod   def mail(cls, content):   import smtplib   from email.mime.text import MIMEText   from email.utils import formataddr   nickname = 监控程序  # 发送者的IT技术网信息   sender = xxx@qq.com  password = *****  # 接收方的邮箱   receiver = aa@bb.cc  msg = MIMEText(content, html, utf-8)   msg[From] = formataddr([nickname, sender])   msg[Subject] = 自动报警  server = smtplib.SMTP_SSL(smtp.qq.com, 465)   try:   server.login(sender, password)   server.sendmail(sender, [receiver], msg.as_string())   except Exception as ex:   print(ex)   finally:   server.quit()   @classmethod   def wechat(cls, content):   client = WeChatClient(xxxx, xxxx)   template_id = xxxxx  openid = xxxx  data = {   msg: {"value": content, "color": "#173177"},   time: {"value": datetime.datetime.now().strftime(%Y-%m-%d %H:%M:%S), "color": "#173177"},   }   try:   client.message.send_template(openid, template_id, data)   except Exception as ex:   print(ex)  while True:   Monitor.mem(90)   Monitor.cpu(90)   time.sleep(5)  1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.源码下载

分享到:

温馨提示:以上内容和图片整理于网络,仅供参考,希望对您有帮助!如有侵权行为请联系删除!

猜你喜欢

友情链接: