侧边栏壁纸
博主头像
汪洋

即使慢,驰而不息,纵会落后,纵会失败,但一定可以达到他所向的目标。 - 鲁迅

  • 累计撰写 190 篇文章
  • 累计创建 74 个标签
  • 累计收到 108 条评论

zabbix - Golang 实现钉钉报警

汪洋
2022-01-13 / 0 评论 / 12 点赞 / 816 阅读 / 1,044 字

最近有同学向我索要 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

0

评论区