这是原厂英文文档的翻译页面. 欢迎帮助我们 完善文档.
2022 Zabbix中国峰会
2022 Zabbix中国峰会

附录 1. 参考说明

注释

数据类型

Zabbix API 支持以下输入数据类型:

数据类型 描述
boolean 布尔值, 只接受 truefalse两种参数。
flag 如果传递的值不等于 nullfalse ,则认为该值为 true
integer 整数。
float 浮点数。
string 文本字符串。
text 长文本字符串。
timestamp Unix 时间戳。
array 有序的值序列,即普通数组。
object 关联数组。
query 用于定义应返回的数据。

可定义为仅返回指定属性的属性名称数组,或者以下预定值之一的数据:
extend - 返回所有对象属性;
count - 返回获取到的记录数量, 仅支持某些子查询。

<note 注意>Zabbix API 始终以字符串或数组格式返回 :::

属性标签

一些对象属性用短标签来描述它们的行为。可使用以下标签:

  • readonly - 属性值是自动设置的,不能被客户端定义或修改;
  • constant - 属性值可以在创建对象时设置,创建后不能被修改。

预留 ID 值 "0"

预留 ID 值 "0" 可以用来过滤元素和删除引用的对象。 例如,从主机中删除一个引用的代理,proxy_hostid 应该设置为 0 ("proxy_hostid": "0") 或者,要过滤被zabbix server监控的主机,proxyids 选项则应该被设置为 0 ("proxyids": "0")。

常用的 "get" 方法参数

所有 get 方法都支持如下参数:

参数 数 类型 描述
countOutput boolean 返回结果中的记录数,而不是实际的数据。
editable boolean 如果设置为 true ,则只返回用户具有写权限的对象。

默认值: false
excludeSearch boolean 返回与在 search 参数中给定的条件不匹配的结果。
filter object 仅返回与给定过滤条件完全匹配的结果。

过滤条件是一个数组,其中键是属性名,值可以是单个值或要匹配的值数组。

不适用于 text 字段。
limit integer 限制返回记录的数据。
output query 要返回的对象属性。

默认值: extend.
preservekeys boolean 在结果数组中,使用 ID 作为键。
search object 返回给定通配符(不区分大小写)匹配到的结果。

接受一个数组,键是属性名,其值是要搜索的字符串。如果没有其他选项,将执行LIKE "%…%"搜索 。

仅适用于 stringtext 字段。
searchByAny boolean 如果设置为 true,则返回在 filtersearch 参数中给出的任何条件匹配的结果,而不是所有条件。

默认值: false
searchWildcardsEnabled boolean 如果设置为 true,则可以在search 中使用 "*" 作为通配符。

默认值: false
sortfield string/array 按给定属性对结果进行排序。有关可用于排序的属性列表,请参考特定的API get方法描述。宏在排序前不会被展开。
sortorder string/array 排序顺序。 如果传递数组,则每个值都将与 sortfield 参数中给定的相应属性匹配。

可选的值为:
ASC - 升序;
DESC - 降序。
startSearch boolean search 参数将比较字段的开始,即执行 LIKE "…%" 搜索。

如果 searchWildcardsEnabled 设置为 true,则忽略。

样例

用户权限检查

用户是否有权限修改以MySQLLinux 开头的主机?

请求:

{
           "jsonrpc": "2.0",
           "method": "host.get",
           "params": {
               "countOutput": true,
               "search": {
                   "host": ["MySQL", "Linux"]
               },
               "editable": true,
               "startSearch": true,
               "searchByAny": true
           },
           "auth": "766b71ee543230a1182ca5c44d353e36",
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": "0",
           "id": 1
       }

结果0,表示没有拥有读/写权限的主机

不匹配统计

统计主机名称中不包含ubuntu的主机数。

请求:

{
           "jsonrpc": "2.0",
           "method": "host.get",
           "params": {
               "countOutput": true,
               "search": {
                   "host": "ubuntu"
               },
               "excludeSearch": true
           },
           "auth": "766b71ee543230a1182ca5c44d353e36",
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": "44",
           "id": 1
       }

使用通配符搜索主机

查找主机名包含server并且接口端口是1005010071的主机。将结果限制在5个并按主机名降序排序。

请求:

{
           "jsonrpc": "2.0",
           "method": "host.get",
           "params": {
               "output": ["hostid", "host"],
               "selectInterfaces": ["port"],
               "filter": {
                   "port": ["10050", "10071"]
               },
               "search": {
                   "host": "*server*"
               },
               "searchWildcardsEnabled": true,
               "searchByAny": true,
               "sortfield": "host",
               "sortorder": "DESC",
               "limit": 5
           },
           "auth": "766b71ee543230a1182ca5c44d353e36",
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": [
               {
                   "hostid": "50003",
                   "host": "WebServer-Tomcat02",
                   "interfaces": [
                       {
                           "port": "10071"
                       }
                   ]
               },
               {
                   "hostid": "50005",
                   "host": "WebServer-Tomcat01",
                   "interfaces": [
                       {
                           "port": "10071"
                       }
                   ]
               },
               {
                   "hostid": "50004",
                   "host": "WebServer-Nginx",
                   "interfaces": [
                       {
                           "port": "10071"
                       }
                   ]
               },
               {
                   "hostid": "99032",
                   "host": "MySQL server 01",
                   "interfaces": [
                       {
                           "port": "10050"
                       }
                   ]
               },
               {
                   "hostid": "99061",
                   "host": "Linux server 01",
                   "interfaces": [
                       {
                           "port": "10050"
                       }
                   ]
               }
           ],
           "id": 1
       }

使用通配符搜索主机并加上PRESERVEKEYS参数

如果将参数preservekeys添加到上一个请求中,返回的结果是一个关联数组,键是对象的id。

请求:

{
           "jsonrpc": "2.0",
           "method": "host.get",
           "params": {
               "output": ["hostid", "host"],
               "selectInterfaces": ["port"],
               "filter": {
                   "port": ["10050", "10071"]
               },
               "search": {
                   "host": "*server*"
               },
               "searchWildcardsEnabled": true,
               "searchByAny": true,
               "sortfield": "host",
               "sortorder": "DESC",
               "limit": 5,
               "preservekeys": true
           },
           "auth": "766b71ee543230a1182ca5c44d353e36",
           "id": 1
       }

响应:

{
           "jsonrpc": "2.0",
           "result": {
               "50003": {
                   "hostid": "50003",
                   "host": "WebServer-Tomcat02",
                   "interfaces": [
                       {
                           "port": "10071"
                       }
                   ]
               },
               "50005": {
                   "hostid": "50005",
                   "host": "WebServer-Tomcat01",
                   "interfaces": [
                       {
                           "port": "10071"
                       }
                   ]
               },        
               "50004": {
                   "hostid": "50004",
                   "host": "WebServer-Nginx",
                   "interfaces": [
                       {
                           "port": "10071"
                       }
                   ]
               },
               "99032": {
                   "hostid": "99032",
                   "host": "MySQL server 01",
                   "interfaces": [
                       {
                           "port": "10050"
                       }
                   ]
               },
               "99061": {
                   "hostid": "99061",
                   "host": "Linux server 01",
                   "interfaces": [
                       {
                           "port": "10050"
                       }
                   ]
               }
           },
           "id": 1
       }