前面有提到通过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="的一串字符串。
添加完成后,我们就可以测试下
然后添加下默认告警媒介
最终效果
未美化效果
写在最后
这种方式就摆脱了格式问题,可以实现开箱即用,而且无需装额外的环境即可实现,快去试试吧。最后感谢官方的模板,有借鉴部分。
原文链接:https://www.toutiao.com/i6965679632477848101/