go调用zabbix-API获取应用集

/*
filename: application-get.go
author: gaohaixiang
writetime:20200416
*/
package main
import (
   "encoding/json"
   "fmt"
   "zabbix"   //自定义库zabbix.go
)

//从api返回的数据中提取需要的关键字段及数据,api返回的数据如下
//  {2.0 {0  } [map[applicationid:732 flags:0 hostid:10169 name:Classes templateids:[]] ..........
type ApplicationGet struct {
   Applicationid string `json:"applicationid"`
   Hostid string `json:"hostid"`
   Name string `json:"name"`
}

//发送到api的数据,对应的json数据如下
/*
{
    "jsonrpc": "2.0",
    "method": "application.get",
    "params": {
        "output": "extend",
        "hostids": "10256",
        "sortfield": "name"
    },
    "auth": "038e1d7b1735c6a5436ee9eae095879e",
    "id": 1
}
*/
func ApplicationData() (string,map[string]interface{}) {
   //请求数据
   method := "application.get"
   //api.Host  method:= "get"
   data := map[string]interface{}{
      "output": "extend",
      //"hostids": "10265",
      "sortfield": "name",
   }
   return method,data
}

func main(){
   fmt.Println("start main")

   //定义url,zabbix登陆使用的用户名及密码
   url := "http://192.168.73.11/api_jsonrpc.php"
   user := "admin"
   password := "zabbix"

   //传输数据到api,并返回数据res
   method,data := ApplicationData()
   res := zabbix.ZabbixLogIn(url,user,password,method,data)

   //从res中提取需要的关键数据
   var resultData []ApplicationGet
   err := json.Unmarshal(res, &resultData)
   //fmt.Println(&resultData)
   if err != nil {
      return
   }
   //fmt.Println(resultData)
   for j := range resultData {
      //fmt.Println(j)
      fmt.Println("value=",resultData[j].Applicationid,resultData[j].Hostid,resultData[j].Name)
      content := resultData[j].Applicationid+"\t"+resultData[j].Hostid+"\t"+resultData[j].Name+"\n"
      //fmt.Println(content)

      //获取数据存放的文件位置及名称
      filename := "zabbix/zabbix-restult.txt"
      zabbix.FileWrite(content,filename)
   }
}

你可能感兴趣的:(go,zabbix,zabbix-API)