📜 ⬆️ ⬇️

Using Zabbix API

Why do you need it

During the implementation of zabbix for infrastructure monitoring, it became necessary to massively add sensors and triggers.
The use of the web interface for this purpose did not bring pleasure, it was a painfully large amount of monotonous work and little speed on the way to happiness. Behind this drew attention to the presence of zabbix api. For the purposes of the mass addition of sensors and other delights of life, it seemed the most.

A brief analysis of the tools

A brief study showed there are libraries in ruby, python and php.
PHP was dropped immediately, because of ignorance of it, from the remaining languages, the choice fell on ruby. It can be considered a personal predilection, but in the course of work I use puppet written in ruby ​​tightly, therefore I am somewhat familiar with this language.
Zabbix API uses JSON and JSON-RPC for integration with third-party utilities and services.
As it turned out, this beast is not terrible, and on closer examination it turned out to be quite simple to understand.
In order not to worry too much, a ready-made library zabcon was found .
What it is, you can see the link.
Go directly to the recipe.

Initial task

There is a certain number of servers. On all servers there is a zabbix-agent. Sensors are required to monitor the disk space, these sensors also require the addition of triggers. You can use templates, but for me this option does not seem to be quite flexible, again done through the web interface, to which, after a couple of days of dense work, there is a steady disgust.

It is better to lose the whole day, then fly for 5 minutes.

To solve the problem, we will use the library provided by the zabcon utility.
Download, install dependencies, unzip.

Next, draw a script on ruby ​​of the following content.

require './zabbixapi.rb'

zbx=ZbxAPI. new ( 'http://zabbix.server.com')
zbx.login( 'login','password')

# trigger


# return hostid
def hostid_from_hostname(hostname,zabbix)
for host in zabbix.host. get ({ "extendoutput" => true , 'pattern'=>"#{hostname}"})
return host[ 'hostid']

def add_disk_check(hostname,zabbix)
# , ssh
ssh=`ssh -o "StrictHostKeyChecking no" -q #{hostname} 'df -l -P -h | tail -n +2' `

for l in ssh
disk=l.split[5].gsub( "/" , "\/" )
name= "API Free diskspace on #{disk}"

item_p = {

uid = zabbix.item.create(item_p)
p "cant create item on #{hostname} disk #{disk}" if uid.nil?

expression= "{#{hostname}:vfs.fs.size[#{disk},pfree].last(0)}<10"
description= "API Free diskspace on {HOSTNAME} volume #{disk}"

'hostid' => hostid,

uid = zabbix.trigger.create(item_t)
p "cant create trigger on #{hostname} disk #{disk}" if uid.nil?


# zabbix
for host in zbx.host. get ({ "extendoutput" => true })
id=host[ 'hostid']
hostname=host[ 'host']
puts "#{id} -- #{hostname}"

# the end

* This source code was highlighted with Source Code Highlighter .

In the course of activity, I had to tweak zabbixapi.rb, in the original version the script fell out if the required sensor or trigger was already present.
In the do_request function, we are looking for the following piece of code:
if !resp[ "error" ].nil?
raise ZbxAPI_GeneralError, resp[ "error" ] #

return resp
rescue Redirect #
retry if redirects<=5
raise ZbxAPI_GeneralError, "Too many redirects"

* This source code was highlighted with Source Code Highlighter .

Comment the specified lines.


The work on surfing the web interface of the zabbix server was replaced with research activities, along with it was drawn a toolkit that allows in a short time to provide a fine-tuning of monitoring for their own needs.
This example shows the flexibility and resourcefulness of a person in an attempt to get rid of routine work.

Source: https://habr.com/ru/post/104387/

All Articles