附录 1. 参考说明

注释

数据类型

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

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

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

Zabbix API 始终以字符串或数组格式返回

属性标签

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

  • readonly - 属性值是自动设置的,不能被客户端定义或修改;

  • constant - 属性值可以在创建对象时设置,创建后不能被修改。

预留 ID 值 “0”

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

常用的 “get” 方法参数

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

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

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

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

不适用于 text 字段。
limitinteger限制返回记录的数据。
outputquery要返回的对象属性。

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

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

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

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

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

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

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

样例

用户权限检查

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

请求:

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "host.get",
  4. "params": {
  5. "countOutput": true,
  6. "search": {
  7. "host": ["MySQL", "Linux"]
  8. },
  9. "editable": true,
  10. "startSearch": true,
  11. "searchByAny": true
  12. },
  13. "auth": "766b71ee543230a1182ca5c44d353e36",
  14. "id": 1
  15. }

响应:

  1. {
  2. "jsonrpc": "2.0",
  3. "result": "0",
  4. "id": 1
  5. }

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

不匹配统计

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

请求:

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "host.get",
  4. "params": {
  5. "countOutput": true,
  6. "search": {
  7. "host": "ubuntu"
  8. },
  9. "excludeSearch": true
  10. },
  11. "auth": "766b71ee543230a1182ca5c44d353e36",
  12. "id": 1
  13. }

响应:

  1. {
  2. "jsonrpc": "2.0",
  3. "result": "44",
  4. "id": 1
  5. }

使用通配符搜索主机

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

请求:

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "host.get",
  4. "params": {
  5. "output": ["hostid", "host"],
  6. "selectInterfaces": ["port"],
  7. "filter": {
  8. "port": ["10050", "10071"]
  9. },
  10. "search": {
  11. "host": "*server*"
  12. },
  13. "searchWildcardsEnabled": true,
  14. "searchByAny": true,
  15. "sortfield": "host",
  16. "sortorder": "DESC",
  17. "limit": 5
  18. },
  19. "auth": "766b71ee543230a1182ca5c44d353e36",
  20. "id": 1
  21. }

响应:

  1. {
  2. "jsonrpc": "2.0",
  3. "result": [
  4. {
  5. "hostid": "50003",
  6. "host": "WebServer-Tomcat02",
  7. "interfaces": [
  8. {
  9. "port": "10071"
  10. }
  11. ]
  12. },
  13. {
  14. "hostid": "50005",
  15. "host": "WebServer-Tomcat01",
  16. "interfaces": [
  17. {
  18. "port": "10071"
  19. }
  20. ]
  21. },
  22. {
  23. "hostid": "50004",
  24. "host": "WebServer-Nginx",
  25. "interfaces": [
  26. {
  27. "port": "10071"
  28. }
  29. ]
  30. },
  31. {
  32. "hostid": "99032",
  33. "host": "MySQL server 01",
  34. "interfaces": [
  35. {
  36. "port": "10050"
  37. }
  38. ]
  39. },
  40. {
  41. "hostid": "99061",
  42. "host": "Linux server 01",
  43. "interfaces": [
  44. {
  45. "port": "10050"
  46. }
  47. ]
  48. }
  49. ],
  50. "id": 1
  51. }

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

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

请求:

  1. {
  2. "jsonrpc": "2.0",
  3. "method": "host.get",
  4. "params": {
  5. "output": ["hostid", "host"],
  6. "selectInterfaces": ["port"],
  7. "filter": {
  8. "port": ["10050", "10071"]
  9. },
  10. "search": {
  11. "host": "*server*"
  12. },
  13. "searchWildcardsEnabled": true,
  14. "searchByAny": true,
  15. "sortfield": "host",
  16. "sortorder": "DESC",
  17. "limit": 5,
  18. "preservekeys": true
  19. },
  20. "auth": "766b71ee543230a1182ca5c44d353e36",
  21. "id": 1
  22. }

响应:

  1. {
  2. "jsonrpc": "2.0",
  3. "result": {
  4. "50003": {
  5. "hostid": "50003",
  6. "host": "WebServer-Tomcat02",
  7. "interfaces": [
  8. {
  9. "port": "10071"
  10. }
  11. ]
  12. },
  13. "50005": {
  14. "hostid": "50005",
  15. "host": "WebServer-Tomcat01",
  16. "interfaces": [
  17. {
  18. "port": "10071"
  19. }
  20. ]
  21. },
  22. "50004": {
  23. "hostid": "50004",
  24. "host": "WebServer-Nginx",
  25. "interfaces": [
  26. {
  27. "port": "10071"
  28. }
  29. ]
  30. },
  31. "99032": {
  32. "hostid": "99032",
  33. "host": "MySQL server 01",
  34. "interfaces": [
  35. {
  36. "port": "10050"
  37. }
  38. ]
  39. },
  40. "99061": {
  41. "hostid": "99061",
  42. "host": "Linux server 01",
  43. "interfaces": [
  44. {
  45. "port": "10050"
  46. }
  47. ]
  48. }
  49. },
  50. "id": 1
  51. }