Some integration might require complex operations and data processing. So, at a certain point, if you have never used the API, you might need to use the API to solve some integration issues.

Contents

I. API basics (1:05)
II. The easier way (5:46)

API basics

You can execute remote HTTP requests to call the Zabbix-API by sending HTTP POST requests to the ‘api_jsonrpc.php‘.

Performing requests to call the API

On a technical level, the API is used by the frontend. For instance, you can set up multiple Zabbix frontends with one of them dedicated to API access.

To access data inside of Zabbix, you need to log in first, using the ‘user.login‘ method.

user.login

In return, you’ll get an authentication ID, including the JSON string, the data, and the request identifier, which is similar to a session ID when you log in using the web frontend.

Authentication ID

NOTE. In big installations, where API is used a lot as people use the default libraries for requests, issues arise because the users log in again and again without logging out. Every time they log in, they create a new session, and for the API user, there might be almost 1 million sessions.

For each request from the web frontend without API, for instance, to show the dashboard, your web client will send a request to verify that you are an authenticated user, and this request has to be verified against this 1 million or more sessions. In such a scenario, the responsiveness of the web frontend will become very slow.

With a valid user authentication ID, you can access the data in Zabbix, for instance, using the ‘host.get‘ method to retrieve the IDs, host names and interfaces of all configured hosts.

Retrieving hosts

The easier way

We’ve published Zabbix API library, which is freely available on GitHub, that simplfies the API handling and is also addressing session caching issue.

Zabbix API library

This library supports the following features:

  • Session caching.
  • HTTPS connections with official- and self-signed certificates.
  • Linux and Windows PHP implementations.
  • Multiple concurrent connections with different user accounts and/or different servers.
  • Support of Zabbix 3.0, Zabbix 3.2, Zabbix 3.4 and Zabbix 4.0.

Live presentation

The use case will be providing a list of our hosts, and attaching macros, host names and interface names for web events in a CSV file.

1. Clone the official repository address of the Zabbix API library,

Cloning Zabbix API library

and copy the address to the command line to execute.

Cloning Zabbix API library to your drive

2. Enter the directory ‘zabbixapi-php’.

zabbixapi directory

NOTE. In our example, I’ve created the code for the CSV report in Visual Studio Code,

Here, you have the file of the library (ZabbixApi.php), setup credentials ($zabUrl, $zabUser, and $zabPassword), and login $zbx->login($zabUrl, $zabUser, $zabPassword);

So, you can execute a call.

$result = $zbx->call ('host.get',array("countOutput" => true));

Report in the Visual Studio Code

3. Execute a call with real-life credentials to find out, for instance, Zabbix version and the number of hosts.

Executing a call

4. To get a CSV sheet out of it, we add some more params to the code.

Data for CSV report

The data here is a part of the Zabbix website, where you can find all the options available.

5. Execute the call ‘host.get‘.

host.get

Then in the examples/ directory you should have a new CSV file, where the host names are the same as in Zabbix.

CSV report

NOTE. Zabbix API library is open-source and can be used free of charge. The library is commercially backed up and maintained by IntelliTrend GmbH, an official Zabbix Partner and Zabbix Training company.

 

 

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x