(八)Zabbix_創建企業微信

(八)Zabbix創建企業微信

背景:

1、zabbix-3.4.4服務器搭建完成

2、主機監控已經部署(能觸發警告報警即可)

思路:

1、創建免費的企業微信

2、根據自己報警內容可建多個企業應用

3、創建報警腳本、配置、測試

4、本內容僅供參考,以便以後學習使用。

一、創建企業應用

1、企業微信註冊

註冊地址:https://qy.weixin.qq.com/截止目前20171130日此網站一直能用,不能保證以後能否使用,見諒,以前叫企業號,現在叫企業微信,哈哈。註冊步驟就不在這重複了,和註冊郵箱一個道理。

2、創建企業應用

1.png

2.png

注意:如何調用微信接口,需要調用微信接口(可以參考企業微信官方教程,下面2點都是網上摘抄)

①需要調用微信接口的憑證:access_token

通過:CropID、Secret才能獲取到access_token,但是獲取到的token有效期爲兩分鐘。

微信企業號調用工具傳送門:

http://qydev.weixin.qq.com/debug

打開上面url,使用上面monitops企業號的monit-manager管理組的CorpID和Secret號獲得調用接口憑證access_token,如下

3.png

注意:corpid在如下

4.png

注意:corpsecret如下:

5.png

6.png

②在http://qydev.weixin.qq.com/debug裏面進行測試,點擊下面的檢查問題

7.png

8.png

二、創建腳本Python

1、創建腳本

(本人網上搜的教程)

#!/usr/bin/python

#_*_coding:utf-8 _*_

import urllib,urllib2

import json

import sys

import simplejson

reload(sys)

sys.setdefaultencoding('utf-8')

def gettoken(corpid,corpsecret):

    gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret

    print  gettoken_url

    try:

        token_file = urllib2.urlopen(gettoken_url)

    except urllib2.HTTPError as e:

        print e.code

        print e.read().decode("utf8")

        sys.exit()

    token_data = token_file.read().decode('utf-8')

    token_json = json.loads(token_data)

    token_json.keys()

    token = token_json['access_token']

    return token

def senddata(access_token,user,subject,content):

    send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token

    send_values = {

        "touser":"用戶名",

        "toparty":"1",

        "msgtype":"text",

        "agentid":"1000002",

        "text":{

            "content":subject + '\n' + content

           },

        "safe":"0"

        }

#    send_data = json.dumps(send_values, ensure_ascii=False)

    send_data = simplejson.dumps(send_values, ensure_ascii=False).encode('utf-8')

    send_request = urllib2.Request(send_url, send_data)

    response = json.loads(urllib2.urlopen(send_request).read())

    print str(response)

if __name__ == '__main__':

    user = str(sys.argv[1])

    subject = str(sys.argv[2])

    content = str(sys.argv[3])

    corpid =  'ww6006201xxxxxc8ca628'

    corpsecret = 'K455gQ63CmgFfVyxbkNdeQFLv4KJUO1RqAxxxxnck0o-U'

    accesstoken = gettoken(corpid,corpsecret)

senddata(accesstoken,user,subject,content)

2、設置腳本路徑

(1)把微信腳本放到alertscripts路徑下如,/usr/local/zabbix-3.4.4/share/zabbix/alertscripts

注意:假如不確定alertscripts這個在哪裏,可以find / -name alertscripts

 

# find / -name alertscripts

/usr/local/zabbix-3.4.4/share/zabbix/alertscripts

# cd /usr/local/ zabbix-3.4.4/share/zabbix/alertscripts

三、python腳本調用

注意:以下2點都是根據網上教程摘抄,僅供參考,謝謝

1、下載及安裝使用

#wget https://pypi.python.org/packages/f0/07/26b519e6ebb03c2a74989f7571e6ae6b82e9d7d81b8de6fcdbfc643c7b58/simplejson-3.8.2.tar.gz

# tar zxvf simplejson-3.8.2.tar.gz && cd simplejson-3.8.2

# python setup.py build

# python setup.py install

注意:在上面buildinstall過程中,如果出現下面的WARNING提示,不用管,不影響結果!

WARNING: The C extension could not be compiled, speedups are not enabled.
Plain-Python installation succeeded.
*********************************************************************************

2、把weixin.py腳本導入

# cd /usr/local/zabbix-3.4.4/share/zabbix/alertscripts

#  chmod 755 weixin.py

#  chown zabbix:zabbix weixin.py

3、更改腳本內容

更改如下6項內容:

touser、toparty、msqtype、agentid、corpid、corpsecret

(1)touser爲ShuoLe,如下圖

9.png

(2)toparty爲1,如下圖

10.png

(3)msqtype爲text

(4)agentid爲1000002,如下圖

11.png

(5)corpid爲ww6006201c8ca36288,如下圖

12.png

(6)corpsecret爲K455gQ63CmgFfVyxbkNdeQFLv4KJUO1RqAGnc9k0o-U

如下圖,

13.png

注意事項:

14.png

15.png

4、測試

(1)# ./weixin.py test one two

16.png

(2)企業微信提示如下,

17.png

測試成功。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章