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

服务器
本文中笔者暂时实现的只有cpu和内存的监控,python可以监控许多的主机信息,网络,硬盘,机器状态等,以下是代码的实现,代码可以实现windows和linux的监控。

 本文中笔者暂时实现的只有cpu和内存的监控,python可以监控许多的主机信息,网络,硬盘,机器状态等,以下是代码的实现,代码可以实现windows和linux的监控。

实验环境:Ubuntu16.04和windos10,python3.6.6

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(avgmax)) 
 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 = '监控程序' 
 # 发送者的信息 
 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.

下面是qq邮箱和微信实现报警的图片:

qq邮箱:

 

微信报警: 

以上就是所有的代码了。

责任编辑:武晓燕 来源: 黑白之道
相关推荐

2011-03-22 09:07:13

Nagios监控Linux

2012-02-23 23:46:26

2011-03-25 15:24:40

Nagios监控

2014-01-02 15:16:42

PythonLinux服务器服务器监控

2009-09-02 14:23:19

邮件服务器性

2009-09-01 11:18:26

邮件服务器

2012-06-26 12:14:05

容错服务器stratus

2016-09-30 13:48:25

UbuntuPython报警系统

2009-09-02 14:43:26

邮件服务器

2009-09-02 14:01:50

邮件服务器

2014-09-29 16:52:00

2009-09-02 14:27:29

邮件服务器

2009-09-02 14:34:42

邮件服务器

2009-08-20 10:19:27

故障服务器自动报警

2015-08-25 15:34:51

2009-09-02 17:25:02

邮件服务器

2010-06-02 16:48:49

postfix邮件服务

2011-02-21 14:12:15

Postfix服务器安装

2011-07-08 14:24:49

邮件服务器U-Mail

2011-01-20 10:08:25

postfix安装
点赞
收藏

51CTO技术栈公众号