Zabbix 5.2 由淺入深之釘釘機器人告警(webhook方式)
作者:IT小白Kasar
實際上釘釘機器人也是通過webhook的方式來實現的,Zabbix 5.2版本原生支持webhook的方式去推送事件。
前面有提到通過python的方式告警,但由于每個人的環境都不一樣,容易出現格式縮進等一些問題,而實際上釘釘機器人也是通過webhook的方式來實現的,Zabbix 5.2版本原生支持webhook的方式去推送事件。
首先貼下代碼,下面的title部分記得修改為自己的
- var dingding = {
- key: null,
- message: null,
- msgtype: "markdown",
- proxy: null,
- sendMessage: function () {
- var params = {
- msgtype: dingding.msgtype,
- markdown: {
- title: "IT小白Kasar",(注意這里可以修改)
- text: dingding.message
- },
- },
- data,
- response,
- request = new CurlHttpRequest(),
- url =
- "https://oapi.dingtalk.com/robot/send?access_token=" +
- dingding.key;
- if (dingding.proxy) {
- request.setProxy(dingding.proxy);
- }
- request.AddHeader("Content-Type: application/json");
- data = JSON.stringify(params);
- // Remove replace() function if you want to see the exposed key in the log file.
- Zabbix.Log(
- 4,
- "[dingding Webhook] URL: " + url.replace(dingding.key, "<BOT KEY>")
- );
- Zabbix.Log(4, "[dingding Webhook] params: " + data);
- response = request.Post(url, data);
- Zabbix.Log(4, "[dingding 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 (typeof params.Key === "undefined") {
- throw 'Incorrect value is given for parameter "Key": parameter is missing';
- }
- dingding.key = params.Key;
- if (params.HTTPProxy) {
- dingding.proxy = params.HTTPProxy;
- }
- dingding.to = params.To;
- dingding.message = params.Subject + "\n" + params.Message;
- dingding.sendMessage();
- return "OK";
- } catch (error) {
- Zabbix.Log(4, "[dingding Webhook] notification failed: " + error);
- throw "Sending failed: " + error + ".";
- }
這個腳本貼在下圖的這個位置,在報警媒介類型下
需要將key部分修改為自己的機器人的token,主要是下圖里的"access_token="的一串字符串。
添加完成后,我們就可以測試下
然后添加下默認告警媒介
最終效果
未美化效果
寫在最后
這種方式就擺脫了格式問題,可以實現開箱即用,而且無需裝額外的環境即可實現,快去試試吧。最后感謝官方的模板,有借鑒部分。
責任編輯:姜華
來源:
今日頭條