nagios增加微信监控

2018-07-19

注册微信企业号,手机安装企业微信。

 

1、/usr/local/nagios/etc/objects

commands.cfg增加

define command{
command_name notify-host-by-weixin
command_line /usr/local/nagios/weixin/notify-host-by-weixin.py "$CONTACTALIAS$separator$NOTIFICATIONTYPE$separator$HOSTNAME$separator$HOSTSTATE$separator$HOSTADDRESS$separator$HOSTOUTPUT$separator$LONGDATETIME$"
}

define command{
command_name notify-service-by-weixin
command_line /usr/local/nagios/weixin/notify-service-by-weixin.py "$CONTACTALIAS$separator$NOTIFICATIONTYPE$separator$SERVICEDESC$separator$HOSTALIAS$separator$HOSTADDRESS$separator$SERVICESTATE$separator$LONGDATETIME$separator$SERVICEOUTPUT$"
}

2、templates.cfg

define contact{

service_notification_commands   notify-service-by-weixin,notify-service-by-email; send service notifications via email
host_notification_commands      notify-host-by-weixin,notify-host-by-email       ; send host notifications via email

3、代码

[root@server68 weixin]# cat  notify-host-by-weixin.py  notify-service-by-weixin.py
#!/usr/bin/python3.4

import urllib.request
import json
import sys

Corpid="wwca222222222222"
Corpsecret="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

# get AccessToken
def gettoken(corp_id,corp_secret):
gettoken_url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s" % (Corpid,Corpsecret)
try:
token_file = urllib.request.urlopen(gettoken_url)
except urllib.error.HTTPError as e:
print(e.code)
print(e.read().decode("utf8"))
token_data = token_file.read().decode('utf-8')
token_json = json.loads(token_data)
token_json.keys()
token = token_json['access_token']
return token

#send msg

def senddata(access_token,notify_str):
send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
notifydata = notify_str.split("separator")
user = notifydata[0]
cationtype = notifydata[1]
name = notifydata[2]
state = notifydata[3]
address = notifydata[4]
datatime  =  notifydata[5]
output = notifydata[6]
content = '**Nagios兆维主机** \n\n类型: ' + cationtype + \
'\n主机: ' + name + \
'\n状态: ' + state + \
'\n地址: ' + address + \
'\n时间: ' + datatime + \
'\n日志: ' + output + '\n'
send_values = {
"toparty":"2",
"msgtype":"text",
"agentid":"1000002",
"text":{
"content":content
},
"safe":"0"
}
send_data = json.dumps (send_values, ensure_ascii = False).encode (encoding = 'UTF8')
send_request=urllib.request.Request(send_url, send_data)
response = urllib.request.urlopen(send_request)
#return
msg = response.read()
return msg

if __name__ == '__main__':
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
reload(sys)
sys.setdefaultencoding(default_encoding)

notifystr = str(sys.argv[1])
accesstoken = gettoken(Corpid,Corpsecret)
msg = senddata(accesstoken,notifystr)
print(msg)
#!/usr/bin/python3.4

import urllib.request
import json
import sys

Corpid="wwca222222222222"
Corpsecret="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

def gettoken(corp_id,corp_secret):
gettoken_url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s" % (Corpid,Corpsecret)
try:
token_file = urllib.request.urlopen(gettoken_url)
except urllib.error.HTTPError as e:
print(e.code)
print(e.read().decode("utf8"))
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,notify_str):
send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
notifydata = notify_str.split("separator")
user = notifydata[0]
cationtype = notifydata[1]
desc = notifydata[2]
alias = notifydata[3]
address = notifydata[4]
state = notifydata[5]
datatime = notifydata[6]
output = notifydata[7]
#content ='Nagios Alert \ntype: ' + cationtype + '\nServiceName: ' + desc + '\nHostname: ' + alias + '\nIP: ' + address + '\nStatus: ' + state + '\nTime: ' + datatime + '\n'
content ='**Nagios兆维服务** \n\n类型: ' + cationtype + \
'\n服务: ' + desc + \
'\n主机: ' + alias + \
'\n地址: ' + address + \
'\n状态: ' + state + \
'\n时间: ' + datatime + \
'\n日志:' + output + '\n'
send_values = {
"toparty":"2",
"msgtype":"text",
"agentid":"1000002",
"text":{
"content":content
},
"safe":"0"
}
send_data = json.dumps(send_values, ensure_ascii=False).encode(encoding='UTF8')
send_request = urllib.request.Request(send_url, send_data)
response = urllib.request.urlopen(send_request)
msg = response.read()
return msg
if __name__ == '__main__':
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
reload(sys)
sys.setdefaultencoding(default_encoding)

notifystr = str(sys.argv[1])
accesstoken = gettoken(Corpid,Corpsecret)
msg = senddata(accesstoken,notifystr)
print(msg)

分类:Linux | 标签: |

相关日志

评论被关闭!