Recently I have been playing with zabbix api with golang, so I decided to document the process for future reference, but maybe this can help someone.
I'm using this: https://github.com/AlekSi/zabbix package of Go providing access to zabbix api and works very well with zabbix server 2.0.10 (the only that I have tested) but probably works fine with newer versions.
Authentication
First make sure to have one user with right permissions to read and write on group of servers that you want change. Or you can just create one user on admin group.
To authenticate on zabbix server we need to use the zabbix.NewAPI method to create an instance of api passing the server url as parameter.
import "github.com/AlekSi/zabbix" func main() { server := "http://zabbix.vm" user := "admin" pass := "zabbix" api := zabbix.NewAPI(server + "/api_jsonrpc.php") api.Login(user, pass) }
Without api_jsonrpc.php in the URL, requests to server won't work!
Host
To get a list of all hosts on zabbix with hostid and name we use HostsGet method to make a request and the type Params to pass parameters as a map of interfaces.
# Make a request to server res, _ := api.HostsGet(zabbix.Params{"output": "extend"}) # Loop through the list of servers and show server name and id. for _, server := range res { fmt.Println("Name:", server.Name, "ID:", server.HostId) }
To get a list of hosts by group we can use HostsGetByHostGroupIds, passing the group id as a slice of strings as parameter.
res, _ := api.HostsGetByHostGroupIds([]string{"2", "10"}) for _, server := range res { fmt.Println("name:", server.Name, "id:", server.HostId) }
To create a host we use HostsCreate method, but is a litle complex because we have to create a HostInterface and add on a slice of HostInterfaces, then we need to use HostGroupId to define the group that the host will belong and add on a slice of HostGroupIds. Finally we use Host to define the Host with the HostInferfaces and HostGroupIds previously defined.
Looks hard but in fact is not! Let's see on code
// Host Interface i := zabbix.HostInterface{DNS: "", IP: "192.168.0.90", Main: true, Port: "10050", Type: 1, UseIP: true} interfaces := zabbix.HostInterfaces{i} // group id g := zabbix.HostGroupId{GroupId: "2"} groups := zabbix.HostGroupIds{g} // Host h := zabbix.Host{Host: "Super Server", Available: 1, GroupIds: groups, Name: "Super Server", Status: 0, Interfaces: interfaces} hosts := zabbix.Hosts{h} res := api.HostsCreate(hosts) fmt.Println(res)
This will create the host based on the information that we give on HostInterface and HostGroupId.
But for me this approach have one problem that is, the host will be created with no templates linked to it.
So to create a host with templates linked we have to use the Call method with allow us to create a more custom request to zabbix api, so we pass "host.create" as first parameter followed by the custom parameters that we want, that will contain the templates.
First we need to define the HostTemplate struct and a slice of HostTemplates to define the templates that will be linked to host, then we define the HostInterface and HostGroupId the same way we did before and for last we make the request with the Call method.
type HostTemplate struct { TemplateId string `json:"templateid"` } type Templates []HostTemplate t := HostTemplate{TemplateId: "10001"} templates := Templates{t} // host interface i := zabbix.HostInterface{DNS: "", IP: "192.168.0.90", Main: true, Port: "10050", Type: 1, UseIP: true} interfaces := zabbix.HostInterfaces{i} // group id g := zabbix.HostGroupId{GroupId: "2"} groups := zabbix.HostGroupIds{g} res, _ := api.Call("host.create", zabbix.Params{"host": "Super Server", "interfaces": interfaces, "groups": groups, "templates": templates}) fmt.Println(res)
Very straightforward we define the HostTemplate struct and just add the TemplateId on a slice of Templates, creating HostInterface, HostGroupId as before and passing all this as parameters.
All TemplatesIds that are added to slice of Templates will be linked to the host.
If we want to update a Host we use the Call method to.
Let's say we want to disable or enable the host monitoring status, we do like this.
res, _ := api.Call("host.update", zabbix.Params{"hostid": "10085", "status": "0"}) fmt.Println(res)
Status 0 for monitored and 1 for unmonitored.
To delete a host we use HostsDelete method with a slice of Hosts as parameter.
The Host need contain the HostId field.
// Host h := zabbix.Host{HostId: "10091"} // create a slice of hosts hosts := zabbix.Hosts{h} res := api.HostsDelete(hosts) fmt.Println(res)
HostGroup
To get a list of all Groups with name and groupid the method HostGroupsGet is what we use.
res, _ := api.HostGroupsGet(zabbix.Params{"output": "extend"}) for _, group := range res { fmt.Println("group:", group.Name, "id:", group.GroupId) }
Templates
Getting the ID of a Template with their name is possible using the Call method, just pass template.get as first parameter and the name of template as host.
// parameters with the template name parameters := map[string]string{"host": "Template OS Linux"} res, _ := api.Call("template.get", zabbix.Params{ "filter": parameters , "output": "shorten"}) // A little of type assertions to get the id. f := res.Result.([]interface{}) d := f[0].(map[string]interface{}) fmt.Println(d["templateid"])
We need to use type assertions on the Response because the Response.Result is of type interface{}.
Useful links:
http://godoc.org/github.com/AlekSi/zabbix
https://www.zabbix.com/documentation/2.0/manpages