The open-source Zabbix monitoring solution has a published, simple binary protocol that allows you to send metrics to the Zabbix server without relying on the Zabbix Agent – which makes it very convenient for integration with other parts of your infrastructure.
In this article, I’ll show how to use the go-zabbix package for sending metrics to the Zabbix server. If instead you were looking to manipulate the backend server definitions (host, templates, hostgroups, etc.) using the REST API, then see my other article here.
Prerequisite
Before running this code, you MUST create two custom fields of type “zabbix trapper” on one of your Zabbix host definitions. If your fields are not Trapper type items, then you cannot push values into them and you will get a failure.
In the Zabbix Server web GUI, select Configuration > Hosts, then on the host you want to experiment with, select “items” and it will list the items it is monitoring.
Press “create item”, and use the name “mystr1” of type “zabbix trapper” of data type “text” and press “add”. Then create another item with name “myint1” of type “zabbix trapper” of data type “numeric unsigned” and press “add”.
Run Script
Now we will populate those two trapper items by using my zabbixsender.go script. The instructions below are for compiling on Ubuntu, translate appropriately for other operating systems.
$ cd $GOPATH $ mkdir -p $GOPATH/src/zabbixsender $ cd $GOPATH/src/zabbixsender $ wget https://raw.githubusercontent.com/fabianlee/blogcode/master/golang/zabbixsender/zabbixsender.go $ go get $ go build
Then run it by supplying arguments for the FQDN of the Zabbix server, and then the name of the Zabbix host we want to insert values into:
$ ./zabbixsender --zabbix=zabbixserver --host=myhost
And you should see output similar to below:
ZBXDZ{"response":"success","info":"processed: 2; failed: 0; total: 2; seconds spent: 0.000044"}
If you do not have trapper items on ‘myhost’ with the names “mystr1” and “myint1” (as described in the prerequisites section above), then instead of seeing 2 processed items, you will see 2 failed items.
REFERENCES
https://github.com/blacked/go-zabbix
https://www.zabbix.org/wiki/Docs/api/libraries
https://www.zabbix.com/documentation/2.4/manual/config/items/itemtypes/trapper
https://github.com/fabianlee/blogcode/tree/master/golang/zabbixsender