-
Notifications
You must be signed in to change notification settings - Fork 4
/
zabbix.js
139 lines (127 loc) · 4.24 KB
/
zabbix.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var weworkApp = {
touser: null,
message: null,
corpid: null,
corpsecret: null,
agentid: null,
proxy: null,
openai_url: null,
zakey: null,
msgtype: 'text',
// 获取企业微信 AccessToken
getAccessToken: function () {
var url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + weworkApp.corpid + '&corpsecret=' + weworkApp.corpsecret,
request = new CurlHttpRequest();
// 设置代理
if (weworkApp.proxy) {
request.setProxy(weworkApp.proxy);
}
request.AddHeader('Content-Type: application/json');
response = request.Get(url);
try {
response = JSON.parse(response);
return response.access_token;
}
catch (error) {
response = null;
}
if (request.Status() !== 200 || response.errcode !== 0) {
if (typeof response.errmsg === 'string') {
throw response.errmsg;
}
else {
throw 'Unknown error. Check debug log for more information.'
}
}
},
askOpenAI: function (quesion) {
// OpenAI Proxy On AirCode
var url = weworkApp.openai_url,
request = new CurlHttpRequest(),
data = {
"zakey": weworkApp.zakey,
"quesion": quesion
};
// 设置代理
if (weworkApp.proxy) {
request.setProxy(weworkApp.proxy);
}
request.AddHeader('Content-Type: application/json');
response = request.Post(url, JSON.stringify(data));
try {
response = JSON.parse(response);
}
catch (error) {
response = { message: "" };
}
return response.message;
},
// 发消息
sendMessage: function (access_token) {
var params = {
touser: weworkApp.touser,
agentid: weworkApp.agentid,
msgtype: weworkApp.msgtype,
text: {
content: weworkApp.message
},
enable_duplicate_check: 1,
duplicate_check_interval: 20
},
data,
response,
request = new CurlHttpRequest(),
url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token;
// 设置代理
if (weworkApp.proxy) {
request.setProxy(weworkApp.proxy);
}
request.AddHeader('Content-Type: application/json');
data = JSON.stringify(params);
// 在日志中不展示 AccessToken
Zabbix.Log(4, '[weworkApp Webhook] URL: ' + url.replace(weworkApp.access_token, '<ACCESS_TOKEN>'));
Zabbix.Log(4, '[weworkApp Webhook] params: ' + data);
response = request.Post(url, data);
Zabbix.Log(4, '[weworkApp Webhook] HTTP code: ' + request.Status());
try {
response = JSON.parse(response);
}
catch (error) {
response = null;
}
if (request.Status() !== 200 || response.errcode !== 0) {
if (typeof response.errmsg === 'string') {
throw response.errmsg;
}
else {
throw 'Unknown error. Check debug log for more information.'
}
}
}
}
try {
var params = JSON.parse(value);
if (params.HTTPProxy) {
weworkApp.proxy = params.HTTPProxy;
}
weworkApp.agentid = params.Agentid;
weworkApp.corpid = params.Corpid;
weworkApp.corpsecret = params.Corpsecret;
weworkApp.touser = params.To;
weworkApp.openai_url = params.Openaiurl;
weworkApp.zakey = params.Zakey;
var access_token = weworkApp.getAccessToken();
weworkApp.message = params.Subject + '\n' + params.Message;
//如果是故障类型的通知,就问一下 OpenAI
if (params.Subject.indexOf("故障恢复") == -1) {
var aiAnswer = weworkApp.askOpenAI(params.Subject);
weworkApp.message = weworkApp.message + '\n --- \n OpenAI 砖家建议:\n ' + aiAnswer;
}
// 发企业微信应用通知
weworkApp.sendMessage(access_token);
return '200 OK';
}
catch (error) {
Zabbix.Log(4, '[weworkApp Webhook] notification failed: ' + error);
throw 'Sending failed: ' + error + '.';
}