zabbix transmitted image with micro-channel alarm

with micro-channel transmission zabbix alarm picture

2.1 realization of ideas

zabbix transmitted image with micro-channel alarm

2.2 Preparing the environment

  • Script using python script, operating environment for python 2.7.5
  • Dependent libraries pre-installed: requests

2.3 ×××tid,secret

This section can be viewed without a front view of the article described in detail

2.4 script to achieve

#!/usr/bin/python
#coding=utf-8
_author__ = 'zhangdongdong'
import requests, json
import urllib3
import smtplib,sys,os,time,re,requests
from email.mime.image import MIMEImage
if sys.getdefaultencoding() != 'utf-8':
    reload(sys)
    sys.setdefaultencoding('utf-8')
urllib3.disable_warnings()
class WechatImage(object): # 根据企业微信api接口文档,定义一个类,使用mpnews类型,https://qydev.weixin.qq.com/wiki/index.php?title=%E6%B6%88%E6%81%AF%E7%B1%BB%E5%9E%8B%E5%8F%8A%E6%95%B0%E6%8D%AE%E6%A0%BC%E5%BC%8F

    def get_token(self, corpid, secret): # 获取token
        url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
        data = {"corpid": corpid,
                "corpsecret": secret}
        r = requests.get(url=url, params=data, verify=False)
        token = r.json()['access_token']
        return token

    def get_image_url(self, token, path): # 上传临时素材图片,然后返回media_id
        url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=image" % token
        data = {"media": open(path, 'rb')}
        r = requests.post(url=url, files=data)
        dict_data = r.json()
        return dict_data['media_id']
    def get_messages( self,subject,content,path): #定义mpnews类型中的参数字典
        data = ''
        messages = {}
        body = {}
        content_html=text_to_html(content)
        token = self.get_token(corpid, secret)
        image = self.get_image_url(token, path)
        content_html += "<br/> <img src='https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s'>" % (token, image)
        body["title"] = subject
        body['digest'] = content
        body['content'] = content_html
        body['thumb_media_id'] = image
        data = []
        data.append(body)
        messages['articles'] = data
        return messages
    def send_news_message(self, corpid, secret,to_user, agentid,path): #定义发送mpnews类型的数据
        token = self.get_token(corpid, secret)
        messages = self.get_messages( subject, content,path)
        url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % token
        data = {"toparty": to_user,                                 # 企业号中的用户帐号
                "agentid": agentid,                             # 企业号中的应用id
                "msgtype": "mpnews",
                "mpnews": messages,
                "safe": "0"}
        headers = {'content-type': 'application/json'}
        data_dict = json.dumps(data, ensure_ascii=False).encode('utf-8')
        r = requests.post(url=url, headers=headers, data=data_dict)
        return r.text
def text_to_html(text): #将邮件内容text字段转换成HTML格式
    d=text.splitlines()
    #将邮件内容以每行作为一个列表元素存储在列表中
    html_text=''
    for i in d:
        i='' + i + '<br>'
        html_text+=i + '\n'
    #为列表的每个元素后加上html的换行标签
    return html_text
def get_itemid():
    #获取报警的itemid
    itemid=re.search(r'监控ID:(\d+)',sys.argv[3]).group(1)
    return itemid
def get_graph(itemid):
    #获取报警的图表并保存
    session=requests.Session()   #创建一个session会话
    try:
        loginheaders={
        "Host":host,
        "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
        }
        #定义请求消息头

        payload = {
        "name":user,
        "password":password,
        "autologin":"1",
        "enter":"Sign in",
        }
        #定义传入的data
        login=session.post(url=loginurl,headers=loginheaders,data=payload)
        #进行登录
        graph_params={
            "from" :"now-10m",
            "to" : "now",
            "itemids" : itemid,
            "width" : "290", #图片的高宽参数
            "height" : "40",
        }
        #定义获取图片的参数
        graph_req=session.get(url=graph_url,params=graph_params)
        #发送get请求获取图片数据
        time_tag=time.strftime("%Y%m%d%H%M%S", time.localtime())
        graph_name='baojing_'+time_tag+'.png'
        #用报警时间来作为图片名进行保存
        graph_name = os.path.join(graph_path, graph_name)
        #使用绝对路径保存图片
        with open(graph_name,'wb') as f:
            f.write(graph_req.content)
            #将获取到的图片数据写入到文件中去
        return graph_name
    except Exception as e:
        print(e)
        return False
if __name__ == '__main__':
    user='Admin'    #定义zabbix用户名
    password='zabbix'    #定义zabbix用户i密
    graph_path='/usr/lib/zabbix/alertscripts/graph/'   #定义图片存储路径,图片需要定时清理
    graph_url='http://192.168.73.133/chart.php'     #定义图表的url
    loginurl="http://192.168.73.133/index.php"          #定义登录的url
    host='192.168.73.133'
    itemid=get_itemid()
    path =get_graph(itemid)
    to_user = str(sys.argv[1]) 
    subject = str(sys.argv[2]) 
    content = str(sys.argv[3])
    corpid= "xxxxx"
    secret = "xxxxxxx"
    agentid = "1000002"
    wechat_img = WechatImage()
    wechat_img.send_news_message(corpid, secret,to_user, agentid, path)

2.5 defines the media type alarm

Open zabbix monitoring web, select the menu in the management of the police media types, create media type, select the script, fill in just wrote a letter with a view of the micro-script name zabbix_weixin_pic.py, script parameters, and finally add

zabbix transmitted image with micro-channel alarm
Open user management, click the need to set up e-mail alerts the user, and then add the alarm in the alarm media media, select the type just defined in the bomb box, and then fill in the micro-enterprise sector id to create the letter, and finally add
zabbix transmitted image with micro-channel alarm

2.6 alarm actions defined

  • Click Configure on the Actions menu, create an action, and then fill in under the picture
操作
默认标题 
Zabbix告警:
副务器:{HOSTNAME}发生: {TRIGGER.NAME}故障!
监控ID:{ITEM.ID}
告警主机:{HOST.NAME}
告警主机:{HOST.IP}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目:{TRIGGER.KEY}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE}
事件ID:{EVENT.ID}
恢复操作
Zabbix告警:
副务器:{HOST.NAME}发生: {TRIGGER.NAME}已恢复!
监控ID:{ITEM.ID}
告警主机:{HOST.NAME}
告警主机:{HOST.IP}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目:{TRIGGER.KEY}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE}
事件ID:{EVENT.ID}

zabbix transmitted image with micro-channel alarm
zabbix transmitted image with micro-channel alarm
zabbix transmitted image with micro-channel alarm

2.7 Test results

An alarm can be triggered manually test results on the phone you can receive alarm band diagram of the page after clicking the message can also see the history of the picture
zabbix transmitted image with micro-channel alarm
zabbix transmitted image with micro-channel alarm


Master Chen Huan ××× "
zabbix transmitted image with micro-channel alarm

Guess you like

Origin blog.51cto.com/12970189/2448442