API

Overview

zabbix的API功能允许你用编程的方式检索数据.修改zabbix的配置.和提供访问历数据的权限,它被广泛应用到以下方面:

  • 创建一个新的应用集到zabbix上

  • 集成在第三方软件上

  • 执行自动化任务上

Zabbix API是一个基于web的API,并作为web前端的一部分,它使用JSON-RPC 2.0协议,并由以下两种方式实现

  • API是一种独立的方法

  • 客户端的请求和响应之间的API使用JSON格式编码

结构

API包括许多名义上的方法将其分成单独的API,每个方法都会执行一个特定的任务,例如host.creat方法属于host的API用来创建新的host主机,当然API在历史上有时也会被称作为"class"(类)

Tips:

大多数的API至少要包含四种方法:get,create,upeate,delete为检索,创建,更新和删除数据,但是有一些 API可以提供完全不同的方法

执行请求

Once you've set up the frontend, you can use remote HTTP requests to call theAPI. To do that you need to send HTTP POST requests to the api_jsonrpc.php file located in the frontend directory. For example, if your Zabbix frontend is installed under http://company.com/zabbix, the HTTP request to call the apiinfo.version method may look like this

一旦你已经设置好了前端,你就可以使用远程http请求去调用你所定义的API.要实现调用你需要发送http的post请求

到api_jsonrpc.php文件,它位于前端的目录下.例如,如果你的zabbix前端已经安装在了http://company.com/zabbix下,你的http请求就会调用你的apiinfo.version,方法如下所示:

POST http://company.com/zabbix/api_jsonrpc.php HTTP/1.1
Content-Type: application/json-rpc

{"jsonrpc":"2.0","method":"apiinfo.version","id":1,"auth":null,"params":{}}

The request must have the Content-Type header set to one of these values:

这些请求的Content-Type头必须是以下这几种值:

application/json-rpc, application/json or application/jsonrequest.

工作示例

The following section will walk you through some usage examples in more detail.

以下这些区域将会更详细的介绍用法

Authentication(认证)

Before you can access any data inside of Zabbix you'll need to log in and obtain an authentication token. This can be done using the user.login method. Let us suppose that you want to log in as a standard Zabbix Admin user. Then your JSON request will look like this:

在你可以访问zabbix任何内部数据之前,你需要登录并获得身份验证令牌.这个可以通过user.login方法来完成,让我们假设你用zabbix管理员来登录,那么你的json请求看起来将会是这样:

{
    "jsonrpc": "2.0",
    "method": "user.login",
    "params": {
        "user": "Admin",
        "password": "zabbix"
               },
    "id": 1,
    "auth": null
 }


让我们仔细来看看请求的对象,它将会有如下的特性:

jsonrpc - "2.0"-这是标准的JSON RPC参数以标示协议版本。

method -API的调用方法

params - 参数将会传递给定义的api方法

id - 对应的请求的标示符

auth - 用户身份验证令牌,因为我们还没有定义一个,所以这里设置为空


If you provided the credentials correctly, the response returned by theAPIwill contain the user authentication token:

如果你提供了正确的凭证,这个API将会返回的响应包含用户身份验证令牌:

{
    "jsonrpc": "2.0",
    "result": "0424bd59b807674191e7d77572075f33",
    "id": 1
}

The response object in turn contains the following properties:

响应的对象反过来包含以下的特性:

jsonrpc - JSON-RPC协议的版本;

result - API方法返回的数据

id - 对应请求的标示符

获取主机

我们现在有一个有效的用户身份验证令牌,可用于在Zabbix中访问数据

For example, let's use the host.get method to retrieve the IDs, host names and interfaces of all configured hosts:

例如.用host.get方法在hosts配置文件中检索id,主机名和IP

{
    "jsonrpc": "2.0",
    "method": "host.get",
    "params": {
        "output": [
            "hostid",
            "host"
        ],
        "selectInterfaces": [
            "interfaceid",
            "ip"
        ]
               },
    "id": 2,
    "auth": "0424bd59b807674191e7d77572075f33"
 }