Zabbix: Zabbix REST API using a Go client

The open-source Zabbix monitoring solution has a REST API that provides the ability for deep integrations with your existing monitoring, logging, and alerting systems.

This fosters development of community-driven modules like Ryan Day’s zabbix Go language package, which is an easy way to automate Zabbix tasks like creating hosts and manipulating other back end structures.

One of the nice things about the Go language is that libraries are generally statically linked into a single executable, so you only need to copy over a single executable.  You don’t have to copy 3rd party jars (Java) or require an internet connection to pypi from production system (Python).

Example

As an example of how you can manipulate Zabbix using the Go programming language, I’ve written a small script that will login and create a host in Zabbix.  This could serve many purposes, including the creation of a monitored host when a new VM or container is brought online in a dynamically scaled environment.

The simple instructions are to download “zabbixhost.go” from my github project, then build and run using go.  Here are instructions for Ubuntu (translate to Windows as needed):

$ mkdir -p $GOPATH/src/zabbixhost
$ cd $GOPATH/src/zabbixhost
$ wget https://raw.githubusercontent.com/fabianlee/blogcode/master/golang/zabbixhost/zabbixhost.go
$ go get
$ go build
$ ./zabbixhost

Since you invoked without any arguments, the program will throw up help on its expected flags.  An example invocation would look something like this:

$ ./zabbixhost --zabbix=http://127.0.0.1 --user=Admin --pass=zabbix --host=myagent1 --IP=192.168.1.2 --template="Template OS Linux" --group="Linux servers"

A successful run would yield output like below:

Going to connect to http://127.0.0.1/zabbix/api_jsonrpc.php as Admin
2.4.8
Connected to API
template id: 10001
hostgroup id: 2
new host myagent1 with id: 10124
Logged out

Which tells us that is connected to a Zabbix server of version “2.4.8”, resolved the template and hostgroup, and finally created a new Zabbix host definition with an id of 10124.

Explanation

Let me call out a few section of the code for more explanation.  First, most of the API calls require an authenticated user session, so this line defining the API object provides that information.  Then a few lines later the actual login occurs.

api, err := zabbix.NewAPI(*zserver + "/zabbix/api_jsonrpc.php", *user, *pass)
...
_, err = api.Login()

But before we can create the Zabbix host definition, we have to resolve the name of our template and hostgroup into the numerical id of the template and hostgroup.

templateId := GetTemplateId(api,*templateName)
...
hostgroupId := GetHostGroupId(api,*hostGroupName)

And finally, armed with this information, we can create the host object:

isCreated := CreateZabbixHost(api,*hostName,templateId,hostgroupId,*IP)

 

REFERENCES

http://www.zabbix.org/wiki/Docs/api/libraries

https://github.com/rday/zabbix

https://github.com/fabianlee/blogcode/tree/master/golang/zabbixhost