Zabbix API at FISL12 by Takanori Suzuki


Published on

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Zabbix API at FISL12 by Takanori Suzuki

  1. 1. Zabbix API and related tools - How to interface with Zabbix- Takanori Suzuki
  2. 2. Takanori Suzuki <ul><li>Member of Zabbix-JP, Zabbix community in Japan.
  3. 3. Reported bugs and Posted patches. </li><ul><li>Crashing bug patch
  4. 4. Zabbix proxy stop sending data bug patch
  5. 5. Disk monitoring improvement patch for LVM </li></ul></ul>
  6. 7. Zabbix <ul><li>An enterprise-class open source distributed monitoring solution. </li><ul><li>auto-discovery of servers and network devices
  7. 8. distributed monitoring with centralised WEB administration
  8. 9. server software for many Un*x
  9. 10. native high performance agents for almost all famous OS
  10. 11. agent-less monitoring
  11. 12. web-based interface
  12. 13. etc… </li></ul></ul>
  13. 14. Zabbix API
  14. 15. Zabbix API <ul><li>Zabbix supports Zabbix API from 1.8
  15. 16. Zabbix API enables us to control Zabbix by program.
  16. 17. Though API spec is not fixed as far as 1.8.x release, some users started to use API </li><ul><li>Before Zabbix API </li><ul><li>No command line tool to control Zabbix monitoring settings
  17. 18. There is no way to control Zabbix from external programs except directly executing DB query. </li></ul><li>After Zabbix API </li><ul><li>We can control Zabbix easily from programs.
  18. 19. Non official command line tools are available.
  19. 20. Mobile phone applications for Zabbix are released </li></ul></ul></ul>
  20. 21. Zabbix API <ul><li>Zabbix API Refernce </li><ul><li>URL: </li></ul><li>Functions </li><ul><li>Get, create, delete, update are supported on hosts, items, triggers, actions and many other objects. </li></ul><li>Now there are many useful tools </li><ul><li>zabcon, command line zabbix managing tool
  21. 22. Libraries for API, easy to use from perl, php, ruby and python. </li></ul></ul>
  22. 23. zabcon <ul><li>One of the earliest programs using Zabbix API
  23. 24. Enables controlking Zabbix by command line and interactive shell
  24. 25. Installable by Ruby gem
  25. 26. Site URL: </li><ul><ul><li> </li></ul></ul></ul>
  26. 27. zabcon: How to use <ul><li>Executing ./zabcon.rb it works as interactive shell
  27. 28. By redirecting Zabbix commands, it returns result. </li></ul>$ echo get host|./zabcon.rb hostid,host 10017,Zabbix Server $ echo 'get item hostids=10017'|./zabcon.rb itemid,description,key_ 18435,Ping to the server (TCP), 18436,Version of zabbix_agent(d) running,agent.version 18438,Maximum number of opened files,kernel.maxfiles 18439,Maximum number of processes,kernel.maxproc … ...
  28. 29. Using Zabbix API <ul><li>Zabbix API is based on JSON RPC 2.0
  29. 30. If you can send JSON in POST request, you can use Zabbix API </li><ul><li>Curl is an easy way to test.
  30. 31. Let's go to the demo! </li></ul></ul>
  31. 32. Using Zabbix API //get auth id $ curl -d '{&quot;auth&quot;:null,&quot;method&quot;:&quot;user.authenticate&quot;,&quot;id&quot;:1,&quot;params&quot;:{&quot;password&quot;:&quot;zabbix&quot;,&quot;user&quot;:&quot;api_user&quot;},&quot;jsonrpc&quot;:&quot;2.0&quot;}' -H &quot;Content-Type: application/json-rpc&quot; http://localhost/zabbix185/api_jsonrpc.php //get item in &quot;Zabbix Server&quot; $ curl -d '{&quot;jsonrpc&quot;:&quot;2.0&quot;,&quot;method&quot;:&quot;item.get&quot;,&quot;params&quot;:{&quot;output&quot;:&quot;shorten&quot;,&quot;host&quot;:&quot;Zabbix Server&quot;,&quot;limit&quot;:0},&quot;auth&quot;:&quot;PUT_HERE_AUTH_ID&quot;,&quot;id&quot;:1}' -H &quot;Content-Type: application/json-rpc&quot; http://localhost/zabbix185/api_jsonrpc.php
  32. 33. Zabbix API libraries <ul><li>Writing JSON directly and setting auth id every time is a pain.
  33. 34. There are Zabbix API libraries for php, ruby, python and perl. </li><ul><li>PHP:
  34. 35. Ruby:
  35. 36. Python:
  36. 37. Perl: </li></ul></ul>
  37. 38. Zabbix API libraries: it is easy to make tools <ul><li>Getting item description list by server name </li></ul>#!/usr/bin/env python from zabbix_api import ZabbixAPI import sys server=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 ={&quot;filter&quot;:{&quot;host&quot;:host_name}})[0][&quot;hostid&quot;] items = zapi.item.get({ 'hostids' : (hostid), 'output' : 'extend'}) for i in items: print i['description']
  38. 39. Zabbix web interface is also using Zabbix API internally <ul><li>Internally, Zabbix web interface is also using Zabbix API functions.
  39. 40. Zabbix web interface access functions without JSON, but directly access same functions.
  40. 41. If Zabbix web frontend is installed, we can use it by including 'include/' . </li></ul>
  41. 42. Zabbix web interface is also using Zabbix API internally <ul><li>Sample code </li><ul><li>http://localhost/zabbix185/test.php </li></ul></ul><?php require_once('include/'); $params = array( 'host' => &quot;Zabbix Server&quot;, 'output' => API_OUTPUT_EXTEND, 'limit' => 0 ); $items = CItem::get($params); foreach ($items as $i){ print $i['description'] . &quot;<br>&quot;; } ?>
  42. 43. Other useful way to interface with Zabbix
  43. 44. Other useful way to interface with Zabbix <ul><li>Programs can use Zabbix API for controlling Zabbix.
  44. 45. If we want to send or get data to Zabbix, we can use other Zabbix protocols. </li><ul><li>Zabbix get protocol
  45. 46. Zabbix Sender protocol
  46. 47. Zabbix Agent (Active) protocol </li></ul></ul>
  47. 48. Zabbix get protocol <ul><li>Just sending character data to 10050 port </li></ul>$ telnet localhost 10050 Trying Connected to localhost. Escape character is '^]'. agent.version ZBXD1.8.2Connection closed by foreign host.
  48. 49. Zabbix get protocol <ul><li>Data is returned with header “ZBXD1” and the following data length. </li></ul>
  49. 50. Zabbix get protocol <ul><li>It's so easy a protocol! we can write zabbix_get even by bash. </li><ul><li> </li></ul><li>&quot; top &quot; by zabbix (demo) </li><ul><li> </li></ul></ul>
  50. 51. Zabbix sender protocol Zabbix Agent(Active) protocol <ul><li>data format is JSON with header. </li></ul>
  51. 52. Zabbix sender protocol Zabbix Agent(Active) protocol <ul><li>JSON data format (send data) </li></ul>{ &quot;data&quot;: [ { &quot;host&quot;:&quot;HostA&quot;, &quot;value&quot;:&quot;sent data&quot;, &quot;key&quot;:&quot;AppX_Logger&quot; } ], &quot;request&quot;:&quot;sender data&quot; <--- or“agent data” }
  52. 53. Zabbix sender protocol Zabbix Agent(Active) protocol <ul><li>JSON data format (receive data) </li></ul>{ &quot;response&quot;:&quot;success&quot;, &quot;info&quot;:&quot;Processed 2 Failed 0 Total 2 Seconds spent 0.000103&quot; }
  53. 54. Zabbix sender protocol Zabbix Agent(Active) protocol <ul><li>If you impliment Zabbix sender protocol, the program directly sends data to Zabbix without Zabbix Agent or Zabbix Sender
  54. 55. Zabbix Sender library for Python (demo) </li><ul><ul><li> </li></ul></ul></ul>from ZabbixSender import ZabbixSender sender = ZabbixSender(u'') sender.AddData(u'HostA', u'AppX_Logger', u'data1') sender.AddData(u'HostA', u'AppX_Logger', u'data2') result=sender.Send() sender.ClearData()
  55. 56. <ul>Zabbix is user friendly. Using Zabbix API and Zabbix protocols, it also become program-friendly software. </ul>
  56. 57. Questions
  57. 58. Thank you The original artwork by orngjce223 is distributed under CC BY license (