Skip to content

Zabbix API Introduction and Examples

Video Lecture

Zabbix API Introduction and Examples

Description

Note

This lesson was from a previous version of my course, which I've now made this video available to view for free.

The Zabbix Server also has an application programming interface (API). This allows you to programmatically configure and retrieve data from the Zabbix Server.

The reason for using the API is that you either want to create your own custom interface for your Zabbix Server, e.g., a new web interface, IOS or Android app, or integrate into another monitoring system. The Grafana tutorials from earlier are an example of using the Zabbix API to read the data and create custom dashboards.

In this lesson, we will connect to our API first using the Linux cURL commands, the simple API testing tool, and then we try and example using Python.

From the examples, you will have enough background information to know how to retrieve, add, delete and modify data in the Zabbix server.

When making calls to the API you need to supply a user authentication token. The first step is to get one.

Get Authentication Token

method : user.login

data :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{
  "jsonrpc": "2.0",
  "method": "user.login",
  "params": {
    "user": "Admin",
    "password": "password"
  },
  "id": 1,
  "auth": null
}

Example cURL

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"jsonrpc": "2.0", "method": "user.login", "params": {"user": "Admin", "password": "password"}, "id": 1, "auth": null}' \
  "https://example.com/zabbix/api_jsonrpc.php"

Example response {"jsonrpc":"2.0","result":"186f9f0a1d77c843d0c36641fafb1c7c","id":1}

The authentication token is in the result value above, and you will need it for all other calls to the API.

With the token, we can now make another call to retrieve a list of all the hosts and see some info about them.

List all Hosts

Method : host.get

Data :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{
  "jsonrpc": "2.0",
  "method": "host.get",
  "params": {
    "output": ["hostid", "host"],
    "selectInterfaces": ["interfaceid", "ip"]
  },
  "id": 2,
  "auth": "186f9f0a1d77c843d0c36641fafb1c7c"
}

Example cURL

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"jsonrpc": "2.0", "method": "host.get", "params": {"output": ["hostid", "host"], "selectInterfaces": ["interfaceid", "ip"]}, "id": 2, "auth": "186f9f0a1d77c843d0c36641fafb1c7c"}' \
  "https://example.com/zabbix/api_jsonrpc.php"

Sample response,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
{
  "jsonrpc": "2.0",
  "result": [
    {
      "hostid": "10084",
      "host": "Zabbix server",
      "interfaces": [
        {
          "interfaceid": "1",
          "ip": "127.0.0.1"
        }
      ]
    },
    {
      "hostid": "10336",
      "host": "Switch",
      "interfaces": [
        {
          "interfaceid": "32",
          "ip": "192.168.1.1"
        }
      ]
    },
    {
      "hostid": "10338",
      "host": "HP790128.home",
      "interfaces": [
        {
          "interfaceid": "33",
          "ip": "192.168.1.81"
        }
      ]
    }
  ],
  "id": 2
}

For more information on host api methods, https://www.zabbix.com/documentation/current/manual/api/reference/host

Create an item

Method : item.create

Data :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
{
  "jsonrpc": "2.0",
  "method": "item.create",
  "params": {
    "name": "Free disk space on $1",
    "key_": "vfs.fs.size[/home/seanwasere/,free]",
    "hostid": "10084",
    "type": 0,
    "value_type": 3,
    "interfaceid": "1",
    "delay": 30
  },
  "id": 2,
  "auth": "186f9f0a1d77c843d0c36641fafb1c7c"
}

Example cURL

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"jsonrpc": "2.0", "method": "item.create", "params": { "name": "Free disk space on $1", "key_": "vfs.fs.size[/home/seanwasere/,free]", "hostid": "10084", "type": 0, "value_type": 3, "interfaceid": "1", "delay": 30 }, "id": 2, "auth": "186f9f0a1d77c843d0c36641fafb1c7c"}' \
  "https://example.com/zabbix/api_jsonrpc.php"

Update an Item

Method : item.update

Data :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{
  "jsonrpc": "2.0",
  "method": "item.update",
  "params": {
    "itemid": "33249",
    "key_": "vfs.fs.size[/home/seanwasere_2/,free]"
  },
  "id": 2,
  "auth": "186f9f0a1d77c843d0c36641fafb1c7c"
}

Example cURL

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"jsonrpc": "2.0", "method": "item.update", "params": { "itemid": "33249", "key_": "vfs.fs.size[/home/seanwasere_2/,free]"}, "id": 2, "auth": "186f9f0a1d77c843d0c36641fafb1c7c"}' \
  "https://example.com/zabbix/api_jsonrpc.php"

Delete an Item

Method : item.delete

Data :

1
2
3
4
5
6
7
8
9
{
  "jsonrpc": "2.0",
  "method": "item.delete",
  "params": {
    "itemid": "33249"
  },
  "id": 2,
  "auth": "186f9f0a1d77c843d0c36641fafb1c7c"
}

Example cURL

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"jsonrpc": "2.0", "method": "item.delete", "params": { "itemid": "33249"}, "id": 2, "auth": "186f9f0a1d77c843d0c36641fafb1c7c"}' \
  "https://example.com/zabbix/api_jsonrpc.php"

For more information in the item api methods, https://www.zabbix.com/documentation/current/manual/api/reference/item

View a list of problems

Method : problem.get

Data :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
{
  "jsonrpc": "2.0",
  "method": "problem.get",
  "params": {
    "output": "extend",
    "selectAcknowledges": "extend",
    "recent": "true",
    "sortfield": ["eventid"],
    "sortorder": "DESC"
  },
  "id": 2,
  "auth": "186f9f0a1d77c843d0c36641fafb1c7c"
}

Example cURL

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"jsonrpc": "2.0", "method": "problem.get", "params": {"output": "extend", "selectAcknowledges": "extend", "recent": "true", "sortfield": ["eventid"], "sortorder": "DESC"}, "id": 2, "auth": "186f9f0a1d77c843d0c36641fafb1c7c"}' \
  "https://example.com/zabbix/api_jsonrpc.php"

For more information in the problem api methods, https://www.zabbix.com/documentation/current/manual/api/reference/problem

User Logout

method : user.logout

params :

1
2
3
4
5
6
7
{
  "jsonrpc": "2.0",
  "method": "user.logout",
  "params": {},
  "id": 2,
  "auth": "186f9f0a1d77c843d0c36641fafb1c7c"
}

Example cURL

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"jsonrpc": "2.0", "method": "user.logout", "params": {}, "id": 1, "auth": "186f9f0a1d77c843d0c36641fafb1c7c"}' \
  "https://example.com/zabbix/api_jsonrpc.php"
user.logout

For more information in the user API methods, https://www.zabbix.com/documentation/current/manual/api/reference/user

For more information on All API methods, https://www.zabbix.com/documentation/current/manual/api/reference

Comments