zabbix_manager 5.1.6 → 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 (58) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +31 -1
  3. data/README.md +69 -46
  4. data/lib/zabbix_manager/classes/actions.rb +3 -11
  5. data/lib/zabbix_manager/classes/applications.rb +2 -8
  6. data/lib/zabbix_manager/classes/configurations.rb +4 -9
  7. data/lib/zabbix_manager/classes/{drules.rb → discovery_rules.rb} +2 -8
  8. data/lib/zabbix_manager/classes/errors.rb +3 -0
  9. data/lib/zabbix_manager/classes/events.rb +2 -1
  10. data/lib/zabbix_manager/classes/graphs.rb +19 -15
  11. data/lib/zabbix_manager/classes/{hostgroups.rb → host_groups.rb} +5 -11
  12. data/lib/zabbix_manager/classes/host_interfaces.rb +261 -0
  13. data/lib/zabbix_manager/classes/hosts.rb +135 -89
  14. data/lib/zabbix_manager/classes/{httptests.rb → http_tests.rb} +2 -8
  15. data/lib/zabbix_manager/classes/items.rb +91 -87
  16. data/lib/zabbix_manager/classes/maintenance.rb +2 -1
  17. data/lib/zabbix_manager/classes/{mediatypes.rb → media_types.rb} +2 -7
  18. data/lib/zabbix_manager/classes/problems.rb +11 -40
  19. data/lib/zabbix_manager/classes/proxies.rb +5 -15
  20. data/lib/zabbix_manager/classes/proxy_groups.rb +16 -0
  21. data/lib/zabbix_manager/classes/roles.rb +13 -19
  22. data/lib/zabbix_manager/classes/screens.rb +11 -30
  23. data/lib/zabbix_manager/classes/scripts.rb +2 -8
  24. data/lib/zabbix_manager/classes/server.rb +1 -0
  25. data/lib/zabbix_manager/classes/templates.rb +2 -65
  26. data/lib/zabbix_manager/classes/triggers.rb +42 -45
  27. data/lib/zabbix_manager/classes/user_groups.rb +63 -0
  28. data/lib/zabbix_manager/classes/user_macros.rb +110 -0
  29. data/lib/zabbix_manager/classes/users.rb +15 -54
  30. data/lib/zabbix_manager/classes/value_maps.rb +52 -0
  31. data/lib/zabbix_manager/client.rb +148 -277
  32. data/lib/zabbix_manager/configuration.rb +107 -0
  33. data/lib/zabbix_manager/http_transport.rb +145 -97
  34. data/lib/zabbix_manager/log_sanitizer.rb +9 -19
  35. data/lib/zabbix_manager/monitoring/device.rb +393 -0
  36. data/lib/zabbix_manager/monitoring/expressions.rb +36 -0
  37. data/lib/zabbix_manager/monitoring/line.rb +175 -0
  38. data/lib/zabbix_manager/monitoring/line_items.rb +110 -0
  39. data/lib/zabbix_manager/monitoring/line_lifecycle.rb +169 -0
  40. data/lib/zabbix_manager/monitoring/line_plan.rb +113 -0
  41. data/lib/zabbix_manager/monitoring/thresholds.rb +182 -0
  42. data/lib/zabbix_manager/monitoring/traffic_items.rb +132 -0
  43. data/lib/zabbix_manager/monitoring/validation.rb +92 -0
  44. data/lib/zabbix_manager/monitoring.rb +216 -642
  45. data/lib/zabbix_manager/resource.rb +235 -0
  46. data/lib/zabbix_manager/traffic.rb +238 -0
  47. data/lib/zabbix_manager/version.rb +1 -1
  48. data/lib/zabbix_manager.rb +66 -179
  49. data/zabbix_manager.gemspec +6 -22
  50. metadata +41 -190
  51. data/lib/zabbix_manager/basic/basic_alias.rb +0 -42
  52. data/lib/zabbix_manager/basic/basic_func.rb +0 -55
  53. data/lib/zabbix_manager/basic/basic_init.rb +0 -50
  54. data/lib/zabbix_manager/basic/basic_logic.rb +0 -239
  55. data/lib/zabbix_manager/classes/hostinterfaces.rb +0 -189
  56. data/lib/zabbix_manager/classes/usergroups.rb +0 -75
  57. data/lib/zabbix_manager/classes/usermacros.rb +0 -222
  58. data/lib/zabbix_manager/classes/valuemaps.rb +0 -26
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ZabbixManager
4
- class Templates < Basic
4
+ # 按模板技术名称解析 ID;返回引用供主机或模板关联操作使用。
5
+ class Templates < Resource
5
6
  # 返回 Zabbix API 中模板对象的方法名前缀。
6
7
  #
7
8
  # @return [String] 模板对象的方法名前缀
@@ -16,17 +17,6 @@ class ZabbixManager
16
17
  "host"
17
18
  end
18
19
 
19
- # 删除指定模板并返回首个已删除模板的 ID。
20
- #
21
- # @param data [Array] 要删除的 templateid 数组
22
- # @raise [ApiError] Zabbix API 返回业务错误时抛出
23
- # @raise [TransportError] Zabbix 服务返回非成功 HTTP 状态时抛出
24
- # @return [Integer, nil] 已删除模板的 ID,无结果时返回 nil
25
- def delete(data)
26
- result = @client.api_request(method: "template.delete", params: [data])
27
- result.empty? ? nil : result["templateids"][0].to_i
28
- end
29
-
30
20
  # 根据查询条件获取模板 ID 列表。
31
21
  #
32
22
  # @param data [Hash] Zabbix template.get 查询参数
@@ -39,59 +29,6 @@ class ZabbixManager
39
29
  end
40
30
  end
41
31
 
42
- # 批量更新主机与模板的关联关系。
43
- #
44
- # @param data [Hash] 包含 hosts_id 和 templates_id 数组
45
- # @raise [ApiError] Zabbix API 返回业务错误时抛出
46
- # @raise [TransportError] Zabbix 服务返回非成功 HTTP 状态时抛出
47
- # @return [Boolean] API 是否返回有效结果
48
- def mass_update(data)
49
- result = @client.api_request(
50
- method: "template.massUpdate",
51
- params: {
52
- hosts: data[:hosts_id].map { |t| { hostid: t } },
53
- templates: data[:templates_id].map { |t| { templateid: t } }
54
- }
55
- )
56
- result.empty? ? false : true
57
- end
58
-
59
- # 批量为主机添加模板关联。
60
- #
61
- # @param data [Hash] 包含 hosts_id 和 templates_id 数组
62
- # @raise [ApiError] Zabbix API 返回业务错误时抛出
63
- # @raise [TransportError] Zabbix 服务返回非成功 HTTP 状态时抛出
64
- # @return [Boolean] API 是否返回有效结果
65
- def mass_add(data)
66
- result = @client.api_request(
67
- method: "template.massAdd",
68
- params: {
69
- hosts: data[:hosts_id].map { |t| { hostid: t } },
70
- templates: data[:templates_id].map { |t| { templateid: t } }
71
- }
72
- )
73
- result.empty? ? false : true
74
- end
75
-
76
- # 批量移除主机的模板或主机组关联。
77
- #
78
- # @param data [Hash] 包含 hosts_id、templates_id 及可选 group_id
79
- # @raise [ApiError] Zabbix API 返回业务错误时抛出
80
- # @raise [TransportError] Zabbix 服务返回非成功 HTTP 状态时抛出
81
- # @return [Boolean] API 是否返回有效结果
82
- def mass_remove(data)
83
- result = @client.api_request(
84
- method: "template.massRemove",
85
- params: {
86
- hostids: data[:hosts_id],
87
- templateids: data[:templates_id],
88
- groupids: data[:group_id],
89
- force: 1
90
- }
91
- )
92
- result.empty? ? false : true
93
- end
94
-
95
32
  # 按模板技术名称查询,并返回可直接用于主机或模板更新的引用对象。
96
33
  #
97
34
  # @param data [String, Array<String>] 一个或多个模板技术名称
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ZabbixManager
4
- class Triggers < Basic
4
+ # 触发器与依赖对账;受管身份使用标签,创建结果不明时只回读、不重放。
5
+ class Triggers < Resource
5
6
  DEFAULT_UNCERTAIN_WRITE_DELAYS = [0, 0.25, 1, 2].freeze
7
+ MAX_MANAGED_KEY_LENGTH = 200
6
8
  # 返回触发器对应的 Zabbix API 模块名。
7
9
  #
8
10
  # @return [String]
@@ -19,13 +21,11 @@ class ZabbixManager
19
21
 
20
22
  # 按触发器 ID 查询完整对象及关联项和函数。
21
23
  #
22
- # @param data [Hash] Should include desired object's key and value
23
- # @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
24
- # @raise [TransportError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
25
- # @return [Hash]
24
+ # @param data [Hash] 包含目标触发器 ID 字段及其值
25
+ # @raise [ApiError] Zabbix API 明确返回业务错误
26
+ # @raise [TransportError] HTTP 或网络请求失败,可能无法确认远端结果
27
+ # @return [Array<Hash>] 匹配的触发器及其关联对象
26
28
  def dump_by_id(data)
27
- log "[DEBUG] Call dump_by_id with parameters: #{data.inspect}"
28
-
29
29
  @client.api_request(
30
30
  method: "trigger.get",
31
31
  params: {
@@ -74,6 +74,9 @@ class ZabbixManager
74
74
  end
75
75
 
76
76
  # 按管理标签查询唯一触发器。
77
+ # 展开表达式并读取依赖,供不确定写入后的完整期望状态核对。
78
+ # @param hostid [Integer, String] 触发器所属主机
79
+ # @param managed_key [String] 稳定、非秘密的受管身份
77
80
  # @return [Hash, nil]
78
81
  def find_managed_for_host(hostid:, managed_key:)
79
82
  result = @client.api_request(
@@ -81,7 +84,9 @@ class ZabbixManager
81
84
  params: {
82
85
  hostids: hostid,
83
86
  tags: [{ tag: "zabbix_manager_id", value: managed_key, operator: 1 }],
84
- output: ["triggerid", "description"],
87
+ output: "extend",
88
+ expandExpression: true,
89
+ selectDependencies: ["triggerid"],
85
90
  selectTags: "extend"
86
91
  }
87
92
  )
@@ -114,13 +119,14 @@ class ZabbixManager
114
119
  # 批量启用或停用触发器。
115
120
  # @return [Array<Integer>]
116
121
  def set_status(hostid:, triggerids:, enabled:)
122
+ validate_boolean!(enabled, "enabled")
117
123
  ids = normalized_ids(triggerids)
118
124
  verify_host_ownership!(hostid, ids)
119
125
  result = @client.api_request(
120
126
  method: "trigger.update",
121
127
  params: ids.map { |triggerid| { triggerid: triggerid, status: enabled ? 0 : 1 } }
122
128
  )
123
- Array(result.fetch("triggerids")).map(&:to_i)
129
+ response_ids(result, expected: ids)
124
130
  end
125
131
 
126
132
  # 批量删除明确指定的触发器。
@@ -129,13 +135,14 @@ class ZabbixManager
129
135
  ids = normalized_ids(triggerids)
130
136
  verify_host_ownership!(hostid, ids)
131
137
  result = @client.api_request(method: "trigger.delete", params: ids)
132
- Array(result.fetch("triggerids")).map(&:to_i)
138
+ response_ids(result, expected: ids)
133
139
  end
134
140
 
135
141
  # 使用 trigger.update 完整替换一个触发器的依赖集合。
136
142
  # 传入空集合可清除依赖,返回被更新的触发器 ID。
137
143
  # @return [Integer]
138
144
  def replace_dependencies(hostid:, triggerid:, depends_on:, allow_cross_host_dependencies: false)
145
+ validate_boolean!(allow_cross_host_dependencies, "allow_cross_host_dependencies")
139
146
  id = normalized_ids([triggerid]).first
140
147
  dependency_ids = normalized_ids(depends_on, allow_empty: true)
141
148
  dependencies = dependency_ids.map { |value| { triggerid: value } }
@@ -143,13 +150,14 @@ class ZabbixManager
143
150
 
144
151
  verify_host_ownership!(hostid, [id])
145
152
  verify_host_ownership!(hostid, dependency_ids) if dependency_ids.any? && !allow_cross_host_dependencies
146
- update_dependencies(id, dependencies)
153
+ @client.with_upsert_lock("trigger-dependencies:#{id}") { update_dependencies(id, dependencies) }
147
154
  end
148
155
 
149
156
  # 在保留现有依赖的前提下,为触发器追加一个或多个依赖。
150
157
  # 读改写过程按触发器 ID 加锁,跨进程可通过 Client 的 upsert_lock 协调。
151
158
  # @return [Integer]
152
159
  def add_dependencies(hostid:, triggerid:, depends_on:, allow_cross_host_dependencies: false)
160
+ validate_boolean!(allow_cross_host_dependencies, "allow_cross_host_dependencies")
153
161
  id = normalized_ids([triggerid]).first
154
162
  additions = normalized_ids(depends_on)
155
163
  raise Invalid, "trigger cannot depend on itself" if additions.include?(id)
@@ -177,26 +185,18 @@ class ZabbixManager
177
185
  attributes = data.deep_symbolize_keys
178
186
  hostid = attributes.delete(:hostid)
179
187
  managed_key = attributes.delete(:managed_key)&.to_s
180
- legacy_identity_tags = attributes.delete(:legacy_identity_tags)
181
188
  raise Invalid, "hostid is required" if hostid.blank?
182
189
  raise Invalid, "description is required" if attributes[:description].blank?
183
190
  raise Invalid, "expression is required" if attributes[:expression].blank?
184
191
 
185
192
  if managed_key.present?
186
- raise Invalid, "managed_key is too long" if managed_key.length > 200
193
+ raise Invalid, "managed_key is too long" if managed_key.length > MAX_MANAGED_KEY_LENGTH
187
194
 
188
195
  attributes[:tags] = Array(attributes[:tags]).reject do |tag|
189
196
  tag[:tag].to_s == "zabbix_manager_id" || tag["tag"].to_s == "zabbix_manager_id"
190
197
  end
191
198
  attributes[:tags] << { tag: "zabbix_manager_id", value: managed_key }
192
199
  current = find_managed_for_host(hostid: hostid, managed_key: managed_key)
193
- if current.nil? && legacy_identity_tags.present?
194
- current = find_for_host(
195
- hostid: hostid,
196
- description: attributes[:description],
197
- tags: legacy_identity_tags
198
- )
199
- end
200
200
  else
201
201
  ownership_tags = Array(attributes[:tags]).select do |tag|
202
202
  tag[:tag].to_s == "managed_by" || tag["tag"].to_s == "managed_by"
@@ -204,50 +204,57 @@ class ZabbixManager
204
204
  current = find_for_host(hostid: hostid, description: attributes[:description], tags: ownership_tags)
205
205
  end
206
206
  if current
207
- triggerid = current.fetch("triggerid")
207
+ triggerid = current["triggerid"]
208
+ response_identifier(triggerid)
208
209
  attributes[:tags] = merge_tags(current["tags"], attributes[:tags]) if attributes[:tags]
209
- @client.api_request(method: "trigger.update", params: attributes.merge(triggerid: triggerid))
210
- triggerid.to_i
210
+ result = @client.api_request(method: "trigger.update", params: attributes.merge(triggerid: triggerid))
211
+ response_id(result, expected: [triggerid])
211
212
  else
212
213
  create_managed_trigger(hostid, managed_key, attributes)
213
214
  end
214
215
  end
215
216
 
216
- # 创建触发器;若响应丢失或并发冲突则按管理键回读收敛。
217
+ # 创建触发器;响应丢失时按管理键回读并核对期望状态。
217
218
  # @return [Integer]
218
219
  # @api private
219
220
  private def create_managed_trigger(hostid, managed_key, attributes)
220
221
  result = @client.api_request(method: "trigger.create", params: attributes)
221
- result.fetch("triggerids").first.to_i
222
+ response_id(result)
222
223
  rescue TransportError => original_error
223
- current = recover_created_trigger(hostid: hostid, managed_key: managed_key) if managed_key.present?
224
- return current.fetch("triggerid").to_i if current
224
+ if managed_key.present?
225
+ current = recover_created_trigger(hostid: hostid, managed_key: managed_key, attributes: attributes)
226
+ end
227
+ return response_identifier(current["triggerid"]) if current
225
228
 
226
229
  raise ResultUnknown,
227
230
  "trigger create result is unknown; inspect managed key #{managed_key.inspect} before retrying: " \
228
231
  "#{original_error.message}"
229
- rescue ApiError => original_error
230
- current = recover_created_trigger(hostid: hostid, managed_key: managed_key) if managed_key.present?
231
- return current.fetch("triggerid").to_i if current
232
-
233
- raise original_error
234
232
  end
235
233
 
236
234
  # 对结果不确定的创建执行短暂退避回读,不重放写请求。
237
235
  # @return [Hash, nil]
238
236
  # @api private
239
- private def recover_created_trigger(hostid:, managed_key:)
237
+ private def recover_created_trigger(hostid:, managed_key:, attributes:)
240
238
  delays = @client.options.fetch(:uncertain_write_delays, DEFAULT_UNCERTAIN_WRITE_DELAYS)
241
239
  delays.each do |delay|
242
240
  sleep(delay) if delay.positive?
243
241
  current = find_managed_for_host(hostid: hostid, managed_key: managed_key)
244
- return current if current
242
+ if current && recovered_trigger_matches?(current, attributes)
243
+ response_identifier(current["triggerid"])
244
+ return current
245
+ end
245
246
  rescue ApiError, TransportError
246
247
  next
247
248
  end
248
249
  nil
249
250
  end
250
251
 
252
+ private def recovered_trigger_matches?(current, attributes)
253
+ desired_tags = Array(attributes[:tags]).map(&:deep_symbolize_keys)
254
+ actual_tags = Array(current["tags"]).map(&:deep_symbolize_keys)
255
+ attributes_match?(current, attributes.except(:tags)) && (desired_tags - actual_tags).empty?
256
+ end
257
+
251
258
  # 合并受管标签,同时保留调用方未覆盖的运维标签。
252
259
  # @return [Array<Hash>]
253
260
  # @api private
@@ -260,16 +267,6 @@ class ZabbixManager
260
267
  retained + desired
261
268
  end
262
269
 
263
- # 统一触发器 ID 并拒绝空集合。
264
- # @return [Array<String>]
265
- # @api private
266
- private def normalized_ids(values, allow_empty: false)
267
- ids = Array(values).filter_map { |value| value.to_s.strip.presence }.uniq
268
- raise Invalid, "triggerids are required" if ids.empty? && !allow_empty
269
-
270
- ids
271
- end
272
-
273
270
  # 回查触发器归属,阻止共享高权限令牌跨主机修改。
274
271
  private def verify_host_ownership!(hostid, ids)
275
272
  raise Invalid, "hostid is required" if hostid.blank?
@@ -287,7 +284,7 @@ class ZabbixManager
287
284
  result = @client.api_request(
288
285
  method: "trigger.update", params: { triggerid: triggerid, dependencies: dependencies }
289
286
  )
290
- result.fetch("triggerids").first.to_i
287
+ response_id(result, expected: [triggerid])
291
288
  end
292
289
  end
293
290
  end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ZabbixManager
4
+ # 用户组成员和主机组权限替换;空数组表示清空,省略字段不参与更新。
5
+ class UserGroups < Resource
6
+ # @return [String] 原生用户组 API 前缀
7
+ def method_name
8
+ "usergroup"
9
+ end
10
+
11
+ # @return [String] 用户组 ID 字段
12
+ def key
13
+ "usrgrpid"
14
+ end
15
+
16
+ # 完整替换主机组权限;空数组明确清空,Zabbix 6.2 起保留独立的模板组权限。
17
+ # @param user_group_id [Integer, String] 目标用户组 ID
18
+ # @param host_group_ids [Array<Integer, String>] 完整期望主机组集合
19
+ # @param permission [Integer, String] 0 拒绝、2 只读、3 读写
20
+ # @return [Integer] 回执确认的用户组 ID
21
+ def replace_host_group_permissions(user_group_id:, host_group_ids:, permission: 2)
22
+ group_id = normalized_ids([user_group_id]).first
23
+ host_ids = collection_ids(host_group_ids, "host_group_ids", allow_empty: true)
24
+ access = integer_attribute(permission, "permission")
25
+ raise Invalid, "permission must be 0, 2, or 3" unless [0, 2, 3].include?(access)
26
+
27
+ rights_field = api_version >= Gem::Version.new("6.2") ? :hostgroup_rights : :rights
28
+ result = @client.api_request(
29
+ method: "usergroup.update",
30
+ params: { usrgrpid: group_id, rights_field => host_ids.map { |id| { id: id, permission: access } } }
31
+ )
32
+ response_id(result, expected: [group_id])
33
+ end
34
+
35
+ # 完整替换各用户组成员;空 user_ids 明确移除现有成员。
36
+ # @param user_group_ids [Array<Integer, String>] 待更新用户组 ID
37
+ # @param user_ids [Array<Integer, String>] 每组的完整期望用户集合
38
+ # @return [Array<Integer>] 回执确认的全部用户组 ID
39
+ def replace_users(user_group_ids:, user_ids:)
40
+ group_ids = collection_ids(user_group_ids, "user_group_ids")
41
+ users = collection_ids(user_ids, "user_ids", allow_empty: true)
42
+ membership = api_version >= Gem::Version.new("6.0") ?
43
+ { users: users.map { |id| { userid: id } } } : { userids: users }
44
+ result = @client.api_request(
45
+ method: "usergroup.update",
46
+ params: group_ids.map { |id| membership.merge(usrgrpid: id) }
47
+ )
48
+ response_ids(result, expected: group_ids)
49
+ end
50
+
51
+ private
52
+
53
+ def api_version
54
+ Gem::Version.new(@client.api_version)
55
+ end
56
+
57
+ def collection_ids(values, name, allow_empty: false)
58
+ raise Invalid, "#{name} must be an Array" unless values.is_a?(Array)
59
+
60
+ normalized_ids(values, allow_empty: allow_empty)
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ZabbixManager
4
+ # 主机宏由 hostid + macro 唯一定位,全局宏由 macro 唯一定位。
5
+ class UserMacros < Resource
6
+ # @return [String] 宏业务标识
7
+ def identify
8
+ "macro"
9
+ end
10
+
11
+ # @return [String] API 模块名
12
+ def method_name
13
+ "usermacro"
14
+ end
15
+
16
+ # @return [String] 主机宏 ID 字段
17
+ def key
18
+ "hostmacroid"
19
+ end
20
+
21
+ # @return [Hash] 主机宏复合身份
22
+ def identity_filter(data)
23
+ attributes = data.deep_symbolize_keys
24
+ { macro: attributes.fetch(:macro), hostid: attributes.fetch(:hostid) }
25
+ end
26
+
27
+ # @return [Integer, nil] 唯一主机宏 ID
28
+ def get_id(data)
29
+ attributes = identity_filter(data)
30
+ unique_macro_id(get_full_data(attributes), "hostmacroid")
31
+ end
32
+
33
+ # @return [Integer, nil] 唯一全局宏 ID
34
+ def get_id_global(data)
35
+ attributes = data.deep_symbolize_keys
36
+ unique_macro_id(get_full_data_global(macro: attributes.fetch(:macro)), "globalmacroid")
37
+ end
38
+
39
+ # @return [Array<Hash>] 主机宏查询结果
40
+ def get_full_data(data)
41
+ attributes = data.deep_symbolize_keys
42
+ params = { filter: attributes.except(:hostid) }
43
+ params[:hostids] = attributes[:hostid] if attributes.key?(:hostid)
44
+ get_raw(params)
45
+ end
46
+
47
+ # @return [Array<Hash>] 全局宏查询结果
48
+ def get_full_data_global(data)
49
+ get_raw(globalmacro: true, filter: data.deep_symbolize_keys)
50
+ end
51
+
52
+ # @return [Integer, nil] 新建主机宏 ID
53
+ def create(data)
54
+ write_macro("create", data, "hostmacroids")
55
+ end
56
+
57
+ # @return [Integer, nil] 新建全局宏 ID
58
+ def create_global(data)
59
+ write_macro("createglobal", data, "globalmacroids")
60
+ end
61
+
62
+ # @return [Integer, nil] 删除主机宏 ID
63
+ def delete(ids)
64
+ requested = normalized_ids(ids)
65
+ write_macro("delete", requested, "hostmacroids", expected: requested)
66
+ end
67
+
68
+ # @return [Integer, nil] 删除全局宏 ID
69
+ def delete_global(ids)
70
+ requested = normalized_ids(ids)
71
+ write_macro("deleteglobal", requested, "globalmacroids", expected: requested)
72
+ end
73
+
74
+ # @return [Integer, nil] 更新主机宏 ID
75
+ def update(data)
76
+ write_macro("update", data, "hostmacroids", expected: [data.with_indifferent_access[:hostmacroid]])
77
+ end
78
+
79
+ # @return [Integer, nil] 更新全局宏 ID
80
+ def update_global(data)
81
+ write_macro("updateglobal", data, "globalmacroids", expected: [data.with_indifferent_access[:globalmacroid]])
82
+ end
83
+
84
+ # @return [Integer, nil] 已存在或新建全局宏 ID
85
+ def get_or_create_global(data)
86
+ get_id_global(data) || create_global(data)
87
+ end
88
+
89
+ # @return [Integer, nil] 新建或更新全局宏 ID
90
+ def create_or_update_global(data)
91
+ attributes = data.deep_symbolize_keys
92
+ id = get_id_global(attributes)
93
+ id ? update_global(attributes.merge(globalmacroid: id)) : create_global(attributes)
94
+ end
95
+
96
+ private
97
+
98
+ def unique_macro_id(result, id_field)
99
+ raise Conflict, "multiple macros match the requested identity" if result.length > 1
100
+
101
+ response_identifier(result.first[id_field], id_field) if result.first
102
+ end
103
+
104
+ def write_macro(operation, params, id_field, expected: nil)
105
+ normalized_ids(expected) if expected
106
+ result = @client.api_request(method: "usermacro.#{operation}", params: params)
107
+ expected ? response_ids(result, id_field, expected: expected).first : response_id(result, id_field)
108
+ end
109
+ end
110
+ end
@@ -1,71 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ZabbixManager
4
- class Users < Basic
5
- # 返回 Zabbix API 中用户对象的方法名前缀。
6
- #
7
- # @return [String] 用户对象的方法名前缀
4
+ # 用户资源与媒介配置替换;字段名称按服务器版本转换。
5
+ class Users < Resource
6
+ # @return [String] API 模块名
8
7
  def method_name
9
8
  "user"
10
9
  end
11
10
 
12
- # 返回用户对象批量结果中的 ID 字段名。
13
- #
14
- # @return [String] 用户 ID 复数字段名
15
- def keys
16
- "userids"
17
- end
18
-
19
- # 返回用户对象的主键字段名。
20
- #
21
- # @return [String] 用户对象的主键字段名
22
- def key
23
- "userid"
24
- end
25
-
26
- # 返回用于唯一识别用户的业务字段名。
27
- #
28
- # @return [String] 用户对象的业务标识字段名
11
+ # @return [String] 用户名字段
29
12
  def identify
30
- "alias"
13
+ Gem::Version.new(@client.api_version) >= Gem::Version.new("5.4") ? "username" : "alias"
31
14
  end
32
15
 
33
- # 按指定动作批量更新用户媒介配置。
34
- #
16
+ # 替换指定用户的完整媒介配置。
35
17
  # @param data [Hash] 包含 userids 和 media
36
- # @param action [String] user API 的动作名
37
- # @return [Integer, nil] 首个已更新用户的 ID
38
- def medias_helper(data, action)
18
+ # @return [Integer, nil] 首个更新用户的 ID
19
+ def update_medias(data)
20
+ attributes = data.deep_symbolize_keys
21
+ ids = normalized_ids(attributes.fetch(:userids))
22
+ media_field = Gem::Version.new(@client.api_version) >= Gem::Version.new("5.4") ? :medias : :user_medias
39
23
  result = @client.api_request(
40
- method: "user.#{action}",
41
- params: data[:userids].map do |t|
42
- {
43
- userid: t,
44
- user_medias: data[:media]
45
- }
24
+ method: "user.update",
25
+ params: attributes.fetch(:userids).map do |userid|
26
+ { userid: userid, media_field => attributes.fetch(:media) }
46
27
  end
47
28
  )
48
- result ? result["userids"][0].to_i : nil
49
- end
50
-
51
- # 为指定用户添加媒介配置,当前通过 user.update 完成。
52
- #
53
- # @param data [Hash] 包含 userids 和 media
54
- # @raise [ApiError] Zabbix API 返回业务错误时抛出
55
- # @raise [TransportError] Zabbix 服务返回非成功 HTTP 状态时抛出
56
- # @return [Integer, nil] 首个已更新用户的 ID
57
- def add_medias(data)
58
- medias_helper(data, "update")
59
- end
60
-
61
- # 批量替换指定用户的媒介配置。
62
- #
63
- # @param data [Hash] 包含 userids 和 media
64
- # @raise [ApiError] Zabbix API 返回业务错误时抛出
65
- # @raise [TransportError] Zabbix 服务返回非成功 HTTP 状态时抛出
66
- # @return [Integer, nil] 首个已更新用户的 ID
67
- def update_medias(data)
68
- medias_helper(data, "update")
29
+ response_ids(result, expected: ids).first
69
30
  end
70
31
  end
71
32
  end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ZabbixManager
4
+ # 原生值映射资源;所属范围与映射规则由调用方提供。
5
+ class ValueMaps < Resource
6
+ # 返回 Zabbix API 中值映射对象的方法名前缀。
7
+ #
8
+ # @return [String] 值映射对象的方法名前缀
9
+ def method_name
10
+ "valuemap"
11
+ end
12
+
13
+ # 返回用于唯一识别值映射的业务字段名。
14
+ #
15
+ # @return [String] 值映射对象的业务标识字段名
16
+ def identify
17
+ "name"
18
+ end
19
+
20
+ # 5.4 起值映射属于主机或模板;旧版仍按全局名称定位。
21
+ # @return [Hash] 带版本边界的精确身份条件
22
+ def identity_filter(data)
23
+ attributes = data.deep_symbolize_keys
24
+ identity = super(attributes)
25
+ if host_scoped?
26
+ raise Invalid, "hostid is required for a host-scoped value map" unless attributes.key?(:hostid)
27
+
28
+ identity[:hostid] = normalized_ids([attributes[:hostid]]).first
29
+ end
30
+ identity
31
+ end
32
+
33
+ # 名称查询始终经过完整身份检查,避免跨主机合法重名造成误选。
34
+ # @return [Integer, nil] 唯一值映射 ID
35
+ def get_id(data)
36
+ super(identity_filter(data))
37
+ end
38
+
39
+ # hostid 用于身份查询和创建,不能用于移动已有值映射。
40
+ # @return [Integer] 已确认的值映射 ID
41
+ def update(data)
42
+ attributes = data.deep_symbolize_keys
43
+ super(host_scoped? ? attributes.except(:hostid) : attributes)
44
+ end
45
+
46
+ private
47
+
48
+ def host_scoped?
49
+ Gem::Version.new(@client.api_version) >= Gem::Version.new("5.4")
50
+ end
51
+ end
52
+ end