3

微信报警

#!/usr/bin/python
# -- coding:utf-8 --
"""
参考文档:
        1、https://work.weixin.qq.com/api/doc#10013/%E7%AC%AC%E4%B8%89%E6%AD%A5%EF%BC%9A%E8%8E%B7%E5%8F%96access_token
        2、https://work.weixin.qq.com/api/doc#10167/%E6%96%87%E6%9C%AC%E6%B6%88%E6%81%AF
"""
import requests
import json
import sys

class Wechat():
    def __init__(self,corpid,corpsecret):
        self.url = "https://qyapi.weixin.qq.com/cgi-bin/"
        self.corpid = corpid
        self.corpsecret = corpsecret
    #获取access_token
    def get_token(self):
        token = "{url}gettoken?corpid={corpid}&corpsecret={corpsecret}".format(url=self.url,corpid=self.corpid,corpsecret=self.corpsecret)
        json_data =  json.loads(requests.get(token).content.decode())
        access_token = json_data["access_token"]
        return access_token
    #获取发送消息
    def send_message(self,user,agentid,subject,content):
        send_url = "{url}message/send?access_token={access_token}".format(url=self.url,access_token=self.get_token())
        data = {
            "touser": user,
            "toparty": "2",    #获取用户失败会将消息发送给部门的人,可以查看部门id修改,多个部门用|分割
            "msgtype": "text",
            "agentid":agentid ,
            "text": {
                "content": subject + '\n' + content
            },
             "safe":0

        }
        #发送报警消息
        requests.post(send_url,json.dumps(data))

if __name__ == '__main__':
    #abbix传过来的第一个参数
    user = sys.argv[1]
    #zabbix传过来的第二个参数
    subject = str(sys.argv[2])
    #zabbix传过来的第三个参数
    content = str(sys.argv[3])
    #调用Wechat类,绑定企业微信的id和应用的Secret
    wechat = Wechat("ww3f7e13339beb9a1d","fWN9iyF9X8vEETLi7xgjRZ40g3vOT-NA18lvCe93EdI")
    #调用实例化的类的发送信息功能,其中agentid等于自建应用的AgentId
    wechat.send_message(user,1000002,subject,content)

邮件报警

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import smtplib
from email.mime.text import MIMEText
class Memcache_Monitor():
    def __init__(self):
        pass
    def send_mail(self,to_list, subject, content):
        # 邮件地址的smtp地址
        mail_host = 'smtp.exmail.qq.com'
        # 用来发邮件的邮箱
        mail_user = 'xxxxxx'
        # 邮箱的密码
        mail_pass = 'xxxxx'
        # smtp地址的主网站地址
        mail_postfix = 'exmail.qq.com'
        sender = "{name}<{name}@{postfix}".format(name=mail_user, postfix=mail_postfix)
        msg = MIMEText(content, 'plain', 'utf-8')
        # 必须使用'utf-8'参数,解决在部分邮件客户端中文会显示为乱码
        msg['Subject'] = subject
        msg['From'] = sender
        msg['to'] = to_list
        try:
            smtpobj = smtplib.SMTP()
            smtpobj.connect(mail_host)
            smtpobj.login(mail_user, mail_pass)
            smtpobj.sendmail(sender, to_list, msg.as_string())
            smtpobj.close()
            print("发送成功")
            return True
        except Exception as e:
            return False

禁止进入i
75 声望15 粉丝

不想做开发的运维不是一名好的架构师,学习无止境。