Zabbix (agent) framework - accessing API

Zabbix (agent) framework 1.2

New version of Zabbix (agent) framework was released. Let us look for some interresting features. At first, it seems that it is much more Zabbix framework than Zabbix agent framework because today we can access API calls, communicate with server and do more interresting things.


There are three options available.

  • Download deb package
  • Make own deb package based on your parameters
  • Install packageless
  • Please do not mix packaged and unpackaged versions! Zaf will not install if misconfigured. Do 'zaf self-remove' before installing deb package!

Installing deb package

Three are three packages available. zaf-1.2.deb is simple package without git. Installation of plugins is based on https url. This is simplest way. zaf-1.2-git.deb is with git enabled repo. It means that actual version of zaf-plugins will be embeded and 'zaf update' will work natively. zaf-1.2-all.deb is package with all plugins embedded into and git disabled. In theory, differencies are only virtual. You can reconfigure every kind of package to be other one later.

dpkg -i zaf-1.2.deb

Creating your own package

There is contrib script inside git, which shows how to create packages. Any time, you can create package by yourself with your parameters like this example. This example will create zaf package with git disabled, zaf plugin will be installed automaticaly after deb install and fsx plugin will be embedded into package. Nex to this, zabbix API url, user and pass will be set so it will be available for zaf to access it. Remember that you will include your secrets into deb package! Take care!

git clone \
 && cd zaf \
 && git checkout 1.2 \
 && git clone plugins \
 && make deb \
  PLUGINS="./plugins/fsx" \
  IPLUGINS="zaf" \
  ZAF_OPTIONS="ZAF_GIT=0 ZAF_ZBXAPI_URL=https://yourserver/api_jsonrpc.php ZAF_ZBXAPI_USER=zaf ZAF_ZBXAPI_PASS=somepassword" \
  AGENT_OPTIONS="Z_Server=zabbix.server Z_ServerActive=zabbix.server Z_StartAgents=8"

Next to this, distribute deb file to desired hosts and install.

Downloading unpackaged version

If you do not want to take care about packaging, you can use unpackaged version and configure it by yourself:

curl -k | sh -s auto

First access to the API

Why we need API calls? In theory, agent can work without communication to API. But in some cases we need to get or set some extra informations and we need to reach zabbix frontend. It is probably not good idea to deploy some sensitive user and password informations on all systems. In our case, we configured zabbix API manualy directly on zabbix server to be able to backup things. See bellow.

zaf api
zaf: Zabbix Api parameters not set! Set ZAF_ZBXAPI_URL, ZAF_ZBXAPI_USER and ZAF_ZBXAPI_PASS and try again.

so we have to define url, user and password. It is good idea to create zaf user using Zabbix web frontend, create some password and give read-only permissions for testing. Next, we have to define config variables for zaf in /etc/zaf.conf. Add next lines:

# Url to jsonrpc
# Username defined via zabbix admin
# Password
# If you use http authentication, please switch to 'http'. Default is 'internal'

OK, we are there!

zaf api

/usr/bin/zaf api command [parameters]
get-host-id host                        Get host id                                                                     
get-byid-host id [property]             Get host property from id. Leave empty property for JSON                        
get-template-id template                Get template id                                                                 
get-byid-template id [property]         Get template property from id. Leave empty property for JSON                    
get-map-id map                          Get map id                                                                      
get-byid-map id [property]              Get map property from id. Leave empty property for JSON                         
get-inventory host [fields]             Get inventory fields [or all fields]                                            
export-hosts dir [hg]                   Backup all hosts [in group hg] (get their config from zabbix and save to dir/hostname.xml)
export-host host                        Backup host (get config from zabbix to stdout)                                  
import-template {plugin|file}           Import template for plugin or from file                                         
export-template name                    Export template to stdout                                                       
export-templates dir                    Export all template to dir    

Backup everything

I thing that every zabbix admin wants to backup zabbix configuration. There are lot of options and checks inside zabbix. If you miss it, it can take couple of hours to get it back from database dump. For example, if you deleted host and you want to "undelete" , it is really hard job. Yes, we have backup options directly in web frontend. But it takes time and it is not automatic. So we have zaf.

mkdir hosts-backup
ZAF_DEBUG=1 ./zaf api export-hosts ./hosts-backup/
zaf: Will backup this hosts: 10084 10114 10116 10122 10123
zaf: Output dir: ./hosts-backup/
zaf: Exporting host limohost(10084)...
zaf: Exporting host AP-TPlink(10114)...
zaf: Exporting host el-pc(10116)...
zaf: Exporting host turris(10122)...
zaf: Exporting host turris-primo(10123)...

mkdir templates-backup
ZAF_DEBUG=1 ./zaf api export-templates ./templates-backup
zaf: Will backup this templates: 10001 10047 10048 10050 10060 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10081 10082 10083 10088 10089 10091 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10106 10107 10108 10109 10110 10111 10112 10124 10125 10126 10127 10128 10129 10130 10131 10132
zaf: Output dir: ./hosts-backup/
zaf: Exporting template Template OS Linux(10001)...
zaf: Exporting template Template App Zabbix Server(10047)...
zaf: Exporting template Template App Zabbix Proxy(10048)...
zaf: Exporting template Template App Zabbix Agent(10050)...
zaf: Exporting template Template SNMP Interfaces(10060)...

There are all servers and templates (file per template, file per host) saved in selected directories. You can use them selectively if you want to backup something. Yes, you can use cron for this job and have everything backuped in regullar intervals automaticaly.



Poslední komentáře

Bez komentářů.

Kdo je nový

  • radimk
  • limo