最近有同学向我索要 golang 版的 zabbix 对接 钉钉 报警的程序,实现后在当前博客进行记录
源码
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
)
func dingToInfo(a string, s string) bool {
content, data := make(map[string]string), make(map[string]interface{})
content["content"] = s
data["msgtype"] = "text"
data["text"] = content
b, _ := json.Marshal(data)
resp, err := http.Post(a,
"application/json",
bytes.NewBuffer(b))
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
return true
}
func main() {
apiServer := os.Args[1]
dingContent := os.Args[2]
dingToInfo(apiServer, dingContent)
}
执行文件 - 已在 Centos7.6 平台进行测试 - 配置 zabbix 联动
# 确认脚本位置
[root@localhost ~]# vim /etc/zabbix/zabbix_server.conf
# 进入目录下载
[root@localhost ~]# cd /usr/lib/zabbix/alertscripts
[root@localhost ~]# wget https://cloudmessage.top/upload/2022/01/dingdinggo-701f67afa7934cca9d0e67acf47891c7. --no-check-certificate
# 修改权限
[root@localhost ~]# chmod a+x dingdinggo
[root@localhost ~]# chown zabbix.zabbix dingdinggo
评论区