
复制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}%,
云服务器提供商超过了{}%,现邮信报请关注.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 = 监控程序 # 发送者的
b2b供应网信息 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.
亿华云计算