Zabbix API

Once you have your Zabbix server up and running, you would probably like to integrate it in the rest of your infrastructure. This is where the Zabbix API comes into picture. By using the API, we can extend Zabbix and integrate it with our other solutions. In this chapter, we will show you how to connect to the API and explain you the basics to interact with it.

Getting ready

In this recipe, we only need our Zabbix server with the super administration account.

How to do it...

  1. Make sure you have curl on your system. It should be there when you installed your system. If not run:
    yum install curl
    
  2. Run the following command on your Zabbix server's prompt or from another machine but then replace the IP:
    curl -s -i -X POST -H 'Content-Type: application/json-rpc' -d '{ "params": { "user": "<user>", "password": "<password>" }, "jsonrpc": "2.0", "method": "user.login", "id": 0 }' 'http://127.0.0.1/zabbix/api_jsonrpc.php'
    

    The output should look more or less like the following lines:

    HTTP/1.1 200 OK
    Date: Sat, 27 Dec 2014 12:43:31 GMT
    Server: Apache/2.2.15 (CentOS)
    X-Powered-By: PHP/5.3.3
    Access-Control-Allow-Origin: *
    Access-Control-Allow-Headers: Content-Type
    Access-Control-Allow-Methods: POST
    Access-Control-Max-Age: 1000
    Content-Length: 68
    Connection: close
    Content-Type: application/json
    
    {"jsonrpc":"2.0","result":"b58610b7bc18ea8579e8d03e38dee665"," id":0}
    

How it works…

We made use of curl to send a simplified JSON request to the Zabbix API. To be successful in our request, we had to specify some parameters. First, we specified the protocol: "jsonrpc": "2.0". We also had to specify the "method" parameter. This parameter will tell Zabbix what we want to do, for example, create a host or an item or add a template; we made use of the user.login option. With the params option we were able to specify our login and password to log into the API. The id parameter is a field that is being used to tie a JSON request to its response so that each response will have the same ID.

From Zabbix, we then received the authentication information back in a JSON format. "result": the authentication token that we can use to identify us in our next tasks. And the "id" parameter that belongs to this response. The id is an arbitrary identifier of the request that was made by us.

There's more…

The Zabbix API was added to Zabbix since version 1.8 and has evolved since then in every version. Since Zabbix 2.0, we can say that the API has been stable. So it's very important that if you write some code, you know what version of Zabbix you are using. Make sure you check what has been changed in the API before you upgrade Zabbix as things may be broken after the upgrade.

If you make use of a Zabbix server where frontend, database, and backend are split, then remember that the Zabbix API runs on the frontend.

A reference of all methods that can be used when programming can be found here: https://www.zabbix.com/documentation/2.4/manual/api/reference.

Tip

It's probably wise to create a special user for the Zabbix API and to remember that it's better to use HTTPS than HTTP, else passwords and logins will be send unencrypted.

To make life more easy when programming the API, Zabbix provides us a list of third-party libraries that can work with the API. Those libraries can be used in Python, PHP, Ruby, and so on. Check See also for a URL with a full list of libraries.

Zabbix adds for every method, examples to show you how to use the API. Those are based on the PHP language.

For example, here we see how to add a host to Zabbix by making use of the host.create method.

https://www.zabbix.com/documentation/2.4/manual/api/reference/host/create.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.183.150