zabbix api at fisl12 by takanori suzuki

Download Zabbix API at FISL12 by Takanori Suzuki

If you can't read please download the document

Upload: takanori-suzuki

Post on 16-Apr-2017

19.162 views

Category:

Technology


0 download

TRANSCRIPT

Zabbix API and related tools
- How to interface with Zabbix-

Takanori Suzuki

Takanori Suzuki

Member of Zabbix-JP, Zabbix community in Japan.

Reported bugs and Posted patches.Crashing bug patch

Zabbix proxy stop sending data bug patch

Disk monitoring improvement patch for LVM

Zabbix

An enterprise-class open source distributed monitoring solution.auto-discovery of servers and network devices

distributed monitoring with centralised WEB administration

server software for many Un*x

native high performance agents for almost all famous OS

agent-less monitoring

web-based interface

etc

Zabbix API

Zabbix API

Zabbix supports Zabbix API from 1.8

Zabbix API enables us to control Zabbix by program.

Though API spec is not fixed as far as 1.8.x release, some users started to use APIBefore Zabbix APINo command line tool to control Zabbix monitoring settings

There is no way to control Zabbix from external programs except directly executing DB query.

After Zabbix APIWe can control Zabbix easily from programs.

Non official command line tools are available.

Mobile phone applications for Zabbix are released

Zabbix API

Zabbix API RefernceURL: http://www.zabbix.com/documentation/1.8/api

FunctionsGet, create, delete, update are supported on hosts, items, triggers, actions and many other objects.

Now there are many useful toolszabcon, command line zabbix managing tool

Libraries for API, easy to use from perl, php, ruby and python.

zabcon

One of the earliest programs using Zabbix API

Enables controlking Zabbix by command line and interactive shell

Installable by Ruby gem

Site URL: http://trac.red-tux.net/wiki/zbx_api/zabcon

zabcon: How to use

Executing ./zabcon.rb it works as interactive shell

By redirecting Zabbix commands, it returns result.

$ echo get host|./zabcon.rb hostid,host10017,Zabbix Server$ echo 'get item hostids=10017'|./zabcon.rbitemid,description,key_18435,Ping to the server (TCP),agent.ping18436,Version of zabbix_agent(d) running,agent.version18438,Maximum number of opened files,kernel.maxfiles18439,Maximum number of processes,kernel.maxproc...

Using Zabbix API

Zabbix API is based on JSON RPC 2.0

If you can send JSON in POST request, you can use Zabbix APICurl is an easy way to test.

Let's go to the demo!

Using Zabbix API

//get auth id$ curl -d '{"auth":null,"method":"user.authenticate","id":1,"params":{"password":"zabbix","user":"api_user"},"jsonrpc":"2.0"}' -H "Content-Type: application/json-rpc" http://localhost/zabbix185/api_jsonrpc.php

//get item in "Zabbix Server"$ curl -d '{"jsonrpc":"2.0","method":"item.get","params":{"output":"shorten","host":"Zabbix Server","limit":0},"auth":"PUT_HERE_AUTH_ID","id":1}' -H "Content-Type: application/json-rpc" http://localhost/zabbix185/api_jsonrpc.php

Zabbix API libraries

Writing JSON directly and setting auth id every time is a pain.

There are Zabbix API libraries for php, ruby, python and perl.PHP: http://andrewfarley.com/php/zabbix-1-8-api-php-class-v1-0

Ruby: http://trac.red-tux.net/wiki/zbx_api

Python: https://github.com/gescheit/scripts/tree/master/zabbix/

Perl: https://github.com/mikeda/ZabbixAPI

Zabbix API libraries:
it is easy to make tools

Getting item description list by server name

#!/usr/bin/env pythonfrom zabbix_api import ZabbixAPIimport sysserver=sys.argv[1]username=sys.argv[2]password=sys.argv[3]host_name=sys.argv[4]

zapi = ZabbixAPI(server=server)zapi.login(username, password)

hostid = zapi.host.get({"filter":{"host":host_name}})[0]["hostid"]items = zapi.item.get({ 'hostids' : (hostid), 'output' : 'extend'})for i in items:print i['description']

Zabbix web interface is also using Zabbix API internally

Internally, Zabbix web interface is also using Zabbix API functions.

Zabbix web interface access functions without JSON, but directly access same functions.

If Zabbix web frontend is installed, we can use it by including 'include/config.inc.php'.

Zabbix web interface is also using Zabbix API internally

Sample codehttp://localhost/zabbix185/test.php

Other useful way to interface with Zabbix

Other useful way to interface with Zabbix

Programs can use Zabbix API for controlling Zabbix.

If we want to send or get data to Zabbix, we can use other Zabbix protocols.Zabbix get protocol

Zabbix Sender protocol

Zabbix Agent (Active) protocol

Zabbix get protocol

Just sending character data to 10050 port

$ telnet localhost 10050Trying 127.0.0.1...Connected to localhost.Escape character is '^]'.agent.versionZBXD1.8.2Connection closed by foreign host.

Zabbix get protocol

Data is returned with header ZBXD\1 and the following data length.

Zabbix get protocol

It's so easy a protocol! we can write zabbix_get even by bash.https://github.com/BlueSkyDetector/code-snippet/blob/master/bash-zabbix-tools/zabbix_get.sh

"top" by zabbix (demo)https://github.com/BlueSkyDetector/code-snippet/blob/master/bash-zabbix-tools/zabbix_top.sh

Zabbix sender protocol
Zabbix Agent(Active) protocol

data format is JSON with header.

Zabbix sender protocol
Zabbix Agent(Active) protocol

JSON data format (send data)

{ "data": [ { "host":"HostA", "value":"sent data", "key":"AppX_Logger" } ], "request":"sender data"