zabbix_manager 5.1.5 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +55 -9
  3. data/LICENSE +1 -1
  4. data/README.md +241 -24
  5. data/lib/zabbix_manager/classes/actions.rb +19 -17
  6. data/lib/zabbix_manager/classes/applications.rb +11 -22
  7. data/lib/zabbix_manager/classes/configurations.rb +20 -10
  8. data/lib/zabbix_manager/classes/discovery_rules.rb +23 -0
  9. data/lib/zabbix_manager/classes/errors.rb +18 -27
  10. data/lib/zabbix_manager/classes/events.rb +8 -3
  11. data/lib/zabbix_manager/classes/graphs.rb +48 -37
  12. data/lib/zabbix_manager/classes/host_groups.rb +64 -0
  13. data/lib/zabbix_manager/classes/host_interfaces.rb +261 -0
  14. data/lib/zabbix_manager/classes/hosts.rb +232 -76
  15. data/lib/zabbix_manager/classes/http_tests.rb +32 -0
  16. data/lib/zabbix_manager/classes/items.rb +206 -72
  17. data/lib/zabbix_manager/classes/maintenance.rb +8 -3
  18. data/lib/zabbix_manager/classes/media_types.rb +18 -0
  19. data/lib/zabbix_manager/classes/problems.rb +51 -62
  20. data/lib/zabbix_manager/classes/proxies.rb +45 -14
  21. data/lib/zabbix_manager/classes/proxy_groups.rb +16 -0
  22. data/lib/zabbix_manager/classes/roles.rb +46 -41
  23. data/lib/zabbix_manager/classes/screens.rb +36 -24
  24. data/lib/zabbix_manager/classes/scripts.rb +16 -12
  25. data/lib/zabbix_manager/classes/server.rb +8 -1
  26. data/lib/zabbix_manager/classes/templates.rb +26 -51
  27. data/lib/zabbix_manager/classes/triggers.rb +251 -67
  28. data/lib/zabbix_manager/classes/user_groups.rb +63 -0
  29. data/lib/zabbix_manager/classes/user_macros.rb +110 -0
  30. data/lib/zabbix_manager/classes/users.rb +16 -23
  31. data/lib/zabbix_manager/classes/value_maps.rb +52 -0
  32. data/lib/zabbix_manager/client.rb +183 -137
  33. data/lib/zabbix_manager/configuration.rb +107 -0
  34. data/lib/zabbix_manager/http_transport.rb +240 -0
  35. data/lib/zabbix_manager/log_sanitizer.rb +58 -0
  36. data/lib/zabbix_manager/monitoring/device.rb +393 -0
  37. data/lib/zabbix_manager/monitoring/expressions.rb +36 -0
  38. data/lib/zabbix_manager/monitoring/line.rb +175 -0
  39. data/lib/zabbix_manager/monitoring/line_items.rb +110 -0
  40. data/lib/zabbix_manager/monitoring/line_lifecycle.rb +169 -0
  41. data/lib/zabbix_manager/monitoring/line_plan.rb +113 -0
  42. data/lib/zabbix_manager/monitoring/thresholds.rb +182 -0
  43. data/lib/zabbix_manager/monitoring/traffic_items.rb +132 -0
  44. data/lib/zabbix_manager/monitoring/validation.rb +92 -0
  45. data/lib/zabbix_manager/monitoring.rb +265 -0
  46. data/lib/zabbix_manager/resource.rb +235 -0
  47. data/lib/zabbix_manager/traffic.rb +238 -0
  48. data/lib/zabbix_manager/version.rb +1 -1
  49. data/lib/zabbix_manager.rb +110 -145
  50. data/zabbix_manager.gemspec +12 -15
  51. metadata +67 -30
  52. data/lib/zabbix_manager/basic/basic_alias.rb +0 -20
  53. data/lib/zabbix_manager/basic/basic_func.rb +0 -84
  54. data/lib/zabbix_manager/basic/basic_init.rb +0 -35
  55. data/lib/zabbix_manager/basic/basic_logic.rb +0 -195
  56. data/lib/zabbix_manager/classes/drules.rb +0 -41
  57. data/lib/zabbix_manager/classes/hostgroups.rb +0 -20
  58. data/lib/zabbix_manager/classes/hostinterfaces.rb +0 -29
  59. data/lib/zabbix_manager/classes/httptests.rb +0 -40
  60. data/lib/zabbix_manager/classes/mediatypes.rb +0 -79
  61. data/lib/zabbix_manager/classes/unusable.rb +0 -11
  62. data/lib/zabbix_manager/classes/usergroups.rb +0 -53
  63. data/lib/zabbix_manager/classes/usermacros.rb +0 -136
  64. data/lib/zabbix_manager/classes/valuemaps.rb +0 -32
@@ -1,52 +1,72 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ZabbixManager
4
- class Roles < Basic
5
- # 与 Zabbix API 交互的角色方法名称
4
+ # 角色查询与权限规则更新;权限内容由调用方定义,服务端负责权限校验。
5
+ class Roles < Resource
6
+ # 返回 Zabbix API 中角色对象的方法名前缀。
7
+ #
8
+ # @return [String] 角色对象的方法名前缀
6
9
  def method_name
7
10
  "role"
8
11
  end
9
12
 
10
- # 识别特定角色对象的 id 字段名称
11
- def identify
12
- "name"
13
- end
14
-
15
- # 使用 Zabbix API 为用户组设置权限
13
+ # 更新指定角色的权限规则。
14
+ #
15
+ # @param data [Hash] 包含 roleid 和 rules 的角色规则数据
16
+ # @raise [ApiError] Zabbix API 返回业务错误时抛出
17
+ # @raise [TransportError] Zabbix 服务返回非成功 HTTP 状态时抛出
18
+ # @return [Integer] 已更新角色的 ID
16
19
  def rules(data)
17
- rules = data[:rules] || 2
20
+ attributes = data.deep_symbolize_keys
21
+ raise Invalid, "roleid is required" if attributes[:roleid].blank?
22
+ raise Invalid, "rules is required" if attributes[:rules].blank?
23
+
24
+ normalized_ids([attributes[:roleid]])
25
+
18
26
  result = @client.api_request(
19
27
  method: "role.update",
20
28
  params: {
21
- roleid: data[:roleid],
22
- rules: data[:hostgroupids].map { |t| { permission: permission, id: t } }
29
+ roleid: attributes[:roleid],
30
+ rules: attributes[:rules]
23
31
  }
24
32
  )
25
- result ? result["usrgrpids"][0].to_i : nil
33
+ response_id(result, expected: [attributes[:roleid]])
26
34
  end
27
35
 
28
- # 使用 Zabbix API 将用户添加到用户组
29
- def add_user(data)
30
- update_users(data)
31
- end
32
-
33
- # 从 Zabbix API 中根据指定键和值获取角色对象的数据
36
+ # 按角色 ID 获取角色详情及完整规则。
37
+ #
38
+ # @param data [Hash] 包含 roleid 的查询参数;旧 id 引用仍可使用,但不得矛盾
39
+ # @raise [ApiError] Zabbix API 返回业务错误时抛出
40
+ # @raise [TransportError] Zabbix 服务返回非成功 HTTP 状态时抛出
41
+ # @return [Array<Hash>] 匹配的角色详情
34
42
  def dump_by_id(data)
35
- log "[DEBUG] 调用 dump_by_id 方法,参数: #{data.inspect}"
43
+ attributes = data.deep_symbolize_keys
44
+ if attributes.key?(:roleid) && attributes.key?(:id) &&
45
+ normalized_ids([attributes[:roleid]]) != normalized_ids([attributes[:id]])
46
+ raise Invalid, "roleid and id disagree"
47
+ end
36
48
 
37
- get_raw(
38
- {
39
- output: "extend",
49
+ id = normalized_ids([attributes.fetch(:roleid) { attributes[:id] }]).first
50
+ @client.api_request(
51
+ method: "role.get",
52
+ params: {
53
+ output: "extend",
40
54
  selectRules: "extend",
41
- roleids: data[:id]
55
+ roleids: id
42
56
  }
43
57
  )
44
58
  end
45
59
 
46
- # 通过角色名称从 Zabbix API 获取角色 id
60
+ # 按角色名称查询所有匹配的角色 ID。
61
+ #
62
+ # @param data [Hash] 包含 name 的角色查询参数
63
+ # @raise [ApiError] Zabbix API 返回业务错误时抛出
64
+ # @raise [TransportError] Zabbix 服务返回非成功 HTTP 状态时抛出
65
+ # @return [Array<String>] 匹配的角色 ID 数组
47
66
  def get_ids_by_name(data)
48
- result = get_raw(
49
- {
67
+ result = @client.api_request(
68
+ method: "role.get",
69
+ params: {
50
70
  filter: {
51
71
  name: data[:name]
52
72
  },
@@ -58,20 +78,5 @@ class ZabbixManager
58
78
  rule["roleid"]
59
79
  end
60
80
  end
61
-
62
- # 使用 Zabbix API 更新用户角色中的用户
63
- def update_users(data)
64
- user_groups = data[:usrgrpids].map do |t|
65
- {
66
- usrgrpid: t,
67
- userids: data[:userids]
68
- }
69
- end
70
- result = @client.api_request(
71
- method: "usergroup.update",
72
- params: user_groups
73
- )
74
- result ? result["usrgrpids"][0].to_i : nil
75
- end
76
81
  end
77
82
  end
@@ -1,8 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ZabbixManager
4
- class Screens < Basic
5
- # extracted from frontends/php/include/defines.inc.php
4
+ # 旧版聚合图形的网格创建入口;目标服务器须提供 screen API。
5
+ class Screens < Resource
6
+ # 以下资源类型取自 frontends/php/include/defines.inc.php。
6
7
  # SCREEN_RESOURCE_GRAPH => 0,
7
8
  # SCREEN_RESOURCE_SIMPLE_GRAPH => 1,
8
9
  # SCREEN_RESOURCE_MAP => 2,
@@ -21,45 +22,43 @@ class ZabbixManager
21
22
  # SCREEN_RESOURCE_SYSTEM_STATUS => 15,
22
23
  # SCREEN_RESOURCE_HOST_TRIGGERS => 16
23
24
 
24
- # 与 Zabbix API 交互的屏幕方法名称
25
+ # 返回 Zabbix API 中聚合图形对象的方法名前缀。
26
+ #
27
+ # @return [String] 聚合图形对象的方法名前缀
25
28
  def method_name
26
29
  "screen"
27
30
  end
28
31
 
29
- # 用于通过 Zabbix API 标识特定屏幕对象的 ID 字段名称
30
- def identify
31
- "name"
32
- end
33
-
34
- # 删除 Zabbix API 中的屏幕对象
35
- def delete(data)
36
- result = @client.api_request(method: "screen.delete", params: [data])
37
- result.empty? ? nil : result["screenids"][0].to_i
38
- end
39
-
40
- # 使用 Zabbix API 为主机获取或创建屏幕对象
32
+ # 按名称获取聚合图形,不存在时按图形 ID 网格化创建。
33
+ #
34
+ # @param data [Hash] 包含 screen_name、graphids 及可选布局参数
35
+ # @raise [ApiError] Zabbix API 返回业务错误时抛出
36
+ # @raise [TransportError] Zabbix 服务返回非成功 HTTP 状态时抛出
37
+ # @return [Integer] 聚合图形 ID
41
38
  def get_or_create_for_host(data)
42
39
  screen_name = data[:screen_name]
43
- graphids = data[:graphids]
44
- screenitems = []
45
- hsize = data[:hsize] || 3
40
+ graphids = Array(data[:graphids])
41
+ raise Invalid, "screen_name is required" if screen_name.blank?
42
+ raise Invalid, "graphids must contain at least one graph" if graphids.empty?
43
+
44
+ hsize = positive_hsize(data.fetch(:hsize, 3))
45
+
46
46
  valign = data[:valign] || 2
47
47
  halign = data[:halign] || 2
48
48
  rowspan = data[:rowspan] || 1
49
49
  colspan = data[:colspan] || 1
50
50
  height = data[:height] || 320
51
51
  width = data[:width] || 200
52
- vsize = data[:vsize] || [1, (graphids.size / hsize).to_i].max
52
+ vsize = data[:vsize] || graphids.length.fdiv(hsize).ceil
53
53
  screenid = get_id(name: screen_name)
54
54
 
55
55
  unless screenid
56
- # 创建屏幕
57
- graphids.each_with_index do |graphid, index|
58
- screenitems << {
56
+ screenitems = graphids.each_with_index.map do |graphid, index|
57
+ {
59
58
  resourcetype: 0,
60
59
  resourceid: graphid,
61
- x: (index % hsize).to_i,
62
- y: (index % graphids.size / hsize).to_i,
60
+ x: index % hsize,
61
+ y: index / hsize,
63
62
  valign: valign,
64
63
  halign: halign,
65
64
  rowspan: rowspan,
@@ -78,5 +77,18 @@ class ZabbixManager
78
77
  end
79
78
  screenid
80
79
  end
80
+
81
+ private
82
+
83
+ # 将横向格数转换为正整数,非法值直接拒绝。
84
+ #
85
+ # @param value [Object] 待校验的横向格数
86
+ # @return [Integer] 正整数横向格数
87
+ def positive_hsize(value)
88
+ size = integer_attribute(value, "hsize")
89
+ raise Invalid, "hsize must be a positive integer" unless size.positive?
90
+
91
+ size
92
+ end
81
93
  end
82
94
  end
@@ -1,21 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ZabbixManager
4
- class Scripts < Basic
5
- # 与 Zabbix API 交互的方法名称
4
+ # 远端脚本执行与可用脚本查询;执行请求可能产生 API 之外的设备副作用。
5
+ class Scripts < Resource
6
+ # 返回 Zabbix API 中脚本对象的方法名前缀。
7
+ #
8
+ # @return [String] 脚本对象的方法名前缀
6
9
  def method_name
7
10
  "script"
8
11
  end
9
12
 
10
- # 用于通过 Zabbix API 标识特定脚本对象的 ID 字段名称
11
- def identify
12
- "name"
13
- end
14
-
15
- # 执行 Zabbix API 中特定主机上的脚本
13
+ # 在指定主机上执行 Zabbix 脚本。
16
14
  #
17
- # 示例:
18
- # execute({ scriptid: '12', hostid: '32' })
15
+ # 示例:
16
+ # execute({ scriptid: '12', hostid: '32 })
17
+ #
18
+ # @param data [Hash] 包含 scriptid 和 hostid 的执行参数
19
+ # @return [Hash] Zabbix API 返回的脚本执行结果
19
20
  def execute(data)
20
21
  @client.api_request(
21
22
  method: "script.execute",
@@ -26,8 +27,11 @@ class ZabbixManager
26
27
  )
27
28
  end
28
29
 
29
- # 使用 Zabbix API 通过主机获取脚本
30
- def get_scripts_by_host(data)
30
+ # 查询指定主机可用的脚本。
31
+ #
32
+ # @param data [Hash] Zabbix scripts.getscriptsbyhosts 查询参数
33
+ # @return [Array<Hash>] 主机可用脚本列表
34
+ def getscriptsbyhost(data)
31
35
  @client.api_request(method: "script.getscriptsbyhosts", params: data)
32
36
  end
33
37
  end
@@ -1,10 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ZabbixManager
4
+ # 已连接服务器的版本快照;复用 Client 初始化结果,不发起额外版本查询。
4
5
  class Server
6
+ # 返回连接对应的 Zabbix API 版本。
7
+ #
8
+ # @return [String] Zabbix API 版本号
5
9
  attr_reader :version
6
10
 
7
- # 初始化一个新的 Server 对象,使用 ZabbixManager 客户端,并获取 Zabbix 服务器 API 版本
11
+ # 使用客户端初始化服务信息,并立即读取 Zabbix API 版本。
12
+ #
13
+ # @param client [ZabbixManager::Client] 已建立配置的 Zabbix 客户端
14
+ # @return [Server] 已初始化的服务信息对象
8
15
  def initialize(client)
9
16
  @client = client
10
17
  @version = @client.api_version
@@ -1,75 +1,50 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ZabbixManager
4
- class Templates < Basic
5
- # Zabbix API 中与模板交互的方法名称
4
+ # 按模板技术名称解析 ID;返回引用供主机或模板关联操作使用。
5
+ class Templates < Resource
6
+ # 返回 Zabbix API 中模板对象的方法名前缀。
7
+ #
8
+ # @return [String] 模板对象的方法名前缀
6
9
  def method_name
7
10
  "template"
8
11
  end
9
12
 
10
- # 用于通过 Zabbix API 标识特定模板对象的 id 字段名称
13
+ # 返回用于唯一识别模板的业务字段名。
14
+ #
15
+ # @return [String] 模板对象的业务标识字段名
11
16
  def identify
12
17
  "host"
13
18
  end
14
19
 
15
- # 通过 Zabbix API 删除模板对象
16
- def delete(data)
17
- result = @client.api_request(method: "template.delete", params: [data])
18
- result.empty? ? nil : result["templateids"][0].to_i
19
- end
20
-
21
- # 从 Zabbix API 获取主机的模板 id
20
+ # 根据查询条件获取模板 ID 列表。
21
+ #
22
+ # @param data [Hash] Zabbix template.get 查询参数
23
+ # @raise [ApiError] Zabbix API 返回业务错误时抛出
24
+ # @raise [TransportError] Zabbix 服务返回非成功 HTTP 状态时抛出
25
+ # @return [Array<String>] 模板 ID 数组
22
26
  def get_ids_by_host(data)
23
27
  @client.api_request(method: "template.get", params: data).map do |tmpl|
24
28
  tmpl["templateid"]
25
29
  end
26
30
  end
27
31
 
28
- # 通过 Zabbix API 获取或创建模板对象
29
- # def get_or_create(data)
30
- # if (templateid = get_id(host: data[:host]))
31
- # templateid
32
- # else
33
- # create(data)
34
- # end
35
- # end
36
-
37
- # 通过 Zabbix API 批量更新主机的模板
38
- def mass_update(data)
39
- result = @client.api_request(
40
- method: "template.massUpdate",
41
- params: {
42
- hosts: data[:hosts_id].map { |t| { hostid: t } },
43
- templates: data[:templates_id].map { |t| { templateid: t } }
44
- }
45
- )
46
- result.empty? ? false : true
47
- end
48
-
49
- # 通过 Zabbix API 批量添加模板到主机
50
- def mass_add(data)
32
+ # 按模板技术名称查询,并返回可直接用于主机或模板更新的引用对象。
33
+ #
34
+ # @param data [String, Array<String>] 一个或多个模板技术名称
35
+ # @return [Array<Hash>, nil] templateid 引用数组,未找到时返回 nil
36
+ def get_template_ids(data)
51
37
  result = @client.api_request(
52
- method: "template.massAdd",
38
+ method: "template.get",
53
39
  params: {
54
- hosts: data[:hosts_id].map { |t| { hostid: t } },
55
- templates: data[:templates_id].map { |t| { templateid: t } }
40
+ output: "extend",
41
+ filter: {
42
+ host: Array(data)
43
+ }
56
44
  }
57
- )
58
- result.empty? ? false : true
59
- end
45
+ ).map { |template| { templateid: template.fetch("templateid") } }
60
46
 
61
- # 通过 Zabbix API 批量从主机移除模板
62
- def mass_remove(data)
63
- result = @client.api_request(
64
- method: "template.massRemove",
65
- params: {
66
- hostids: data[:hosts_id],
67
- templateids: data[:templates_id],
68
- groupids: data[:group_id],
69
- force: 1
70
- }
71
- )
72
- result.empty? ? false : true
47
+ result.presence
73
48
  end
74
49
  end
75
50
  end