zabbix api查看地址:https://www.zabbix.com/documentation/4.0/zh/manual/api
遇到的问题: 创建host的json格式复杂,无法直接定义go的数组格式
这里通过定义复杂json数据格式的方式解决:
//接口数组
type Interfaces struct {
Type int `json:"type"`
Main int `json:"main"`
Useip int `json:"useip"`
Ip string `json:"ip"`
Dns string `json:"dns"`
Port string `json:"port"`
}
//groups 数组
type Groups struct {
Groupid string `json:"groupid"`
}
//模板数组
type Templates struct {
Templateid string `json:"templateid"`
}
type Params struct {
Host string `json:"host"`
Interfaces []interface{} `json:"interfaces"`
Groups []interface{} `json:"groups"`
Templates []interface{} `json:"templates"`
}
type Data struct {
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params Params `json:"params"`
Auth string `json:"auth"`
Id int `json:"id"`
}
创建主机部分方法实现:
//创建HOST
func (api *API) CreateHost(ip, groupname, templatename, hostname string) (bool, error) {
host_id, _ := api.GetHostByName(hostname)
if host_id != "" {
fmt.Println("该主机已存在", host_id)
return true, nil
}
group_id, err := api.GetHostGroupByName(groupname)
if err != nil {
fmt.Println("改主机组不存在")
return false, err
}
template_id, err := api.GetTemplateByName(templatename)
if err != nil {
fmt.Println("该模板不存在")
return false, err
}
auth, err := api.UserLogin()
if err != nil {
fmt.Println("获取auth失败", err)
return false, err
}
//定义json数据
interfaces := &Interfaces{}
interfaces.Type = 1
interfaces.Main = 1
interfaces.Useip = 1
interfaces.Ip = ip
interfaces.Dns = ""
interfaces.Port = "10050"
groups := &Groups{}
groups.Groupid = group_id
templates := &Templates{}
templates.Templateid = template_id
params := &Params{}
params.Host = hostname
params.Templates = make([]interface{}, 1)
params.Templates[0] = *templates
params.Groups = make([]interface{}, 1)
params.Groups[0] = *groups
params.Interfaces = make([]interface{}, 1)
params.Interfaces[0] = *interfaces
data := &Data{}
data.Jsonrpc = "2.0"
data.Method = "host.create"
data.Params = *params
data.Auth = auth
data.Id = 1
response, err := api.Zabbix_Request(*data)
if err != nil {
fmt.Println("创建主机出错", err)
return false, err
}
if response.Error.Code != 0 {
return false, &response.Error
}
return true, nil
}
//根据hostname获取hostid
func (api *API) GetHostByName(name string) (string, error) {
auth, err := api.UserLogin()
if err != nil {
fmt.Println("获取auth失败", err)
return "", err
}
data := map[string]interface{}{
"jsonrpc": "2.0",
"method": "host.get",
"params": map[string]interface{}{
"output": "extend",
"filter": map[string]string{
"host": name,
},
},
"auth": auth,
"id": 1,
}
response, err := api.Zabbix_Request(data)
if err != nil {
fmt.Println("获取主机失败")
return "", err
}
if response.Error.Code != 0 {
return "", &response.Error
}
res, err := json.Marshal(response.Result)
if err != nil {
fmt.Println("数据转化为json格式失败")
return "", err
}
var result []Hosts
err1 := json.Unmarshal(res, &result)
if err1 != nil {
return "", err1
}
var host_id string
for i := range result {
host_id = result[i].HostId
fmt.Println("主机ID是", host_id)
}
return host_id, nil
}
//登录
func (api *API) UserLogin() (string, error) {
data := map[string]interface{}{
"jsonrpc": "2.0",
"method": "user.login",
"params": map[string]interface{}{
"user": zabbix_user,
"password": zabbix_pass,
},
"id": 0,
}
response, err := api.Zabbix_Request(data)
if err != nil {
fmt.Print(err)
return "", err
}
if response.Error.Code != 0 {
return "", &response.Error
}
auth := response.Result.(string)
return auth, nil
}
//获取API请求结果
func (api *API)Zabbix_Request(data interface{}) (Zabbix_Response, error) {
api.url = url
temp, err := json.Marshal(data)
if err != nil {
return Zabbix_Response{}, err
}
request, err := http.NewRequest("POST", api.url, bytes.NewBuffer(temp))
if err != nil {
return Zabbix_Response{}, err
}
request.Header.Add("Content-Type", "application/json-rpc")
response, err := api.Client.Do(request)
if err != nil {
return Zabbix_Response{}, err
}
var result Zabbix_Response
var buf bytes.Buffer
_, err = io.Copy(&buf, response.Body)
defer response.Body.Close()
if err != nil {
return Zabbix_Response{}, err
}
json.Unmarshal(buf.Bytes(), &result)
return result, nil
}