Go调用企业微信API发送zabbix告警信息脚本

栏目: 服务器 · 发布时间: 7年前

package main

import (
    "bytes"
    "encoding/json"
    "io/ioutil"
    "net/http"
    "os"
)

type JSON struct {
    Access_token string `json:"access_token"`
}

type MESSAGES struct {
    Touser string `json:"touser"`
    Toparty string `json:"toparty"`
    Msgtype string `json:"msgtype"`
    Agentid int `json:"agentid"`
    Text struct {
        //Subject string `json:"subject"`
        Content string `json:"content"`
    } `json:"text"`
    Safe int `json:"safe"`
}

func Get_AccessToken(corpid,corpsecret string) string {
    gettoken_url := "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + corpid + "&corpsecret=" + corpsecret
    //print(gettoken_url)
    client := &http.Client{}
    req, _ := client.Get(gettoken_url)
    defer req.Body.Close()
    body, _ := ioutil.ReadAll(req.Body)
    //fmt.Printf("\n%q",string(body))
    var json_str JSON
    json.Unmarshal([]byte(body), &json_str)
    //fmt.Printf("\n%q",json_str.Access_token)
    return json_str.Access_token
}

func Send_Message(access_token,msg string) {
    send_url := "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + access_token
    //print(send_url)
    client := &http.Client{}
    req, _ := http.NewRequest("POST", send_url, bytes.NewBuffer([]byte(msg)))
    req.Header.Set("Content-Type", "application/json")
    req.Header.Set("charset","UTF-8")
    resp, err := client.Do(req)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()
    //fmt.Println("response Status:", resp.Status)
    //body, _ := ioutil.ReadAll(resp.Body)
    //fmt.Println("response Body:", string(body))
}

func messages(touser string,toparty string,agentid int,content string) string{
    msg := MESSAGES{
        Touser: touser,
        Toparty: toparty,
        Msgtype: "text",
        Agentid: agentid,
        Safe: 0,
        Text: struct {
            //Subject string `json:"subject"`
            Content string `json:"content"`
        }{Content: content},
    }
    sed_msg, _ := json.Marshal(msg)
    //fmt.Printf("%s",string(sed_msg))
    return string(sed_msg)
}

func main(){
    touser := "BigBoss"  //企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
    toparty := "2"       //企业号中的部门id。
    agentid:= 1000002    //企业号中的应用id。
    corpid := "xxxxxxxxxxxxxxxxx"  //企业号的标识
    corpsecret := "exxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  ///企业号中的应用的Secret
    access_token := Get_AccessToken(corpid,corpsecret)

    subject := os.Args[2] //获取zabbix传进来的第一个参数
    content := subject  + "\n" + os.Args[3]  //获取zabbix传进来的第二个参数

    msg := messages(touser,toparty,agentid,content)
    //fmt.Println(msg)
    Send_Message(access_token,msg)
    //fmt.Println("\n")

}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

算法引论

算法引论

[美]乌迪·曼博(Udi Manber) / 黄林鹏、谢瑾奎、陆首博、等 / 电子工业出版社 / 2010-1 / 36.00元

本书是国际算法大师乌迪·曼博(Udi Manber)博士撰写的一本享有盛誉的著作。全书共分12章:第1章到第4章为介绍性内容,涉及数学归纳法、算法分析、数据结构等内容;第5章提出了与归纳证明进行类比的算法设计思想;第6章到第9章分别给出了4个领域的算法,如序列和集合的算法、图算法、几何算法、代数和数值算法;第10章涉及归约,也是第11章的序幕,而后者涉及NP完全问题;第12章则介绍了并行算法;最后......一起来看看 《算法引论》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具