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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +55 -9
- data/LICENSE +1 -1
- data/README.md +241 -24
- data/lib/zabbix_manager/classes/actions.rb +19 -17
- data/lib/zabbix_manager/classes/applications.rb +11 -22
- data/lib/zabbix_manager/classes/configurations.rb +20 -10
- data/lib/zabbix_manager/classes/discovery_rules.rb +23 -0
- data/lib/zabbix_manager/classes/errors.rb +18 -27
- data/lib/zabbix_manager/classes/events.rb +8 -3
- data/lib/zabbix_manager/classes/graphs.rb +48 -37
- data/lib/zabbix_manager/classes/host_groups.rb +64 -0
- data/lib/zabbix_manager/classes/host_interfaces.rb +261 -0
- data/lib/zabbix_manager/classes/hosts.rb +232 -76
- data/lib/zabbix_manager/classes/http_tests.rb +32 -0
- data/lib/zabbix_manager/classes/items.rb +206 -72
- data/lib/zabbix_manager/classes/maintenance.rb +8 -3
- data/lib/zabbix_manager/classes/media_types.rb +18 -0
- data/lib/zabbix_manager/classes/problems.rb +51 -62
- data/lib/zabbix_manager/classes/proxies.rb +45 -14
- data/lib/zabbix_manager/classes/proxy_groups.rb +16 -0
- data/lib/zabbix_manager/classes/roles.rb +46 -41
- data/lib/zabbix_manager/classes/screens.rb +36 -24
- data/lib/zabbix_manager/classes/scripts.rb +16 -12
- data/lib/zabbix_manager/classes/server.rb +8 -1
- data/lib/zabbix_manager/classes/templates.rb +26 -51
- data/lib/zabbix_manager/classes/triggers.rb +251 -67
- data/lib/zabbix_manager/classes/user_groups.rb +63 -0
- data/lib/zabbix_manager/classes/user_macros.rb +110 -0
- data/lib/zabbix_manager/classes/users.rb +16 -23
- data/lib/zabbix_manager/classes/value_maps.rb +52 -0
- data/lib/zabbix_manager/client.rb +183 -137
- data/lib/zabbix_manager/configuration.rb +107 -0
- data/lib/zabbix_manager/http_transport.rb +240 -0
- data/lib/zabbix_manager/log_sanitizer.rb +58 -0
- data/lib/zabbix_manager/monitoring/device.rb +393 -0
- data/lib/zabbix_manager/monitoring/expressions.rb +36 -0
- data/lib/zabbix_manager/monitoring/line.rb +175 -0
- data/lib/zabbix_manager/monitoring/line_items.rb +110 -0
- data/lib/zabbix_manager/monitoring/line_lifecycle.rb +169 -0
- data/lib/zabbix_manager/monitoring/line_plan.rb +113 -0
- data/lib/zabbix_manager/monitoring/thresholds.rb +182 -0
- data/lib/zabbix_manager/monitoring/traffic_items.rb +132 -0
- data/lib/zabbix_manager/monitoring/validation.rb +92 -0
- data/lib/zabbix_manager/monitoring.rb +265 -0
- data/lib/zabbix_manager/resource.rb +235 -0
- data/lib/zabbix_manager/traffic.rb +238 -0
- data/lib/zabbix_manager/version.rb +1 -1
- data/lib/zabbix_manager.rb +110 -145
- data/zabbix_manager.gemspec +12 -15
- metadata +67 -30
- data/lib/zabbix_manager/basic/basic_alias.rb +0 -20
- data/lib/zabbix_manager/basic/basic_func.rb +0 -84
- data/lib/zabbix_manager/basic/basic_init.rb +0 -35
- data/lib/zabbix_manager/basic/basic_logic.rb +0 -195
- data/lib/zabbix_manager/classes/drules.rb +0 -41
- data/lib/zabbix_manager/classes/hostgroups.rb +0 -20
- data/lib/zabbix_manager/classes/hostinterfaces.rb +0 -29
- data/lib/zabbix_manager/classes/httptests.rb +0 -40
- data/lib/zabbix_manager/classes/mediatypes.rb +0 -79
- data/lib/zabbix_manager/classes/unusable.rb +0 -11
- data/lib/zabbix_manager/classes/usergroups.rb +0 -53
- data/lib/zabbix_manager/classes/usermacros.rb +0 -136
- data/lib/zabbix_manager/classes/valuemaps.rb +0 -32
|
@@ -1,106 +1,290 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
class ZabbixManager
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
# 触发器与依赖对账;受管身份使用标签,创建结果不明时只回读、不重放。
|
|
5
|
+
class Triggers < Resource
|
|
6
|
+
DEFAULT_UNCERTAIN_WRITE_DELAYS = [0, 0.25, 1, 2].freeze
|
|
7
|
+
MAX_MANAGED_KEY_LENGTH = 200
|
|
8
|
+
# 返回触发器对应的 Zabbix API 模块名。
|
|
9
|
+
#
|
|
10
|
+
# @return [String]
|
|
6
11
|
def method_name
|
|
7
12
|
"trigger"
|
|
8
13
|
end
|
|
9
14
|
|
|
10
|
-
#
|
|
15
|
+
# 使用描述作为通用 CRUD 的业务标识。
|
|
16
|
+
#
|
|
17
|
+
# @return [String]
|
|
11
18
|
def identify
|
|
12
19
|
"description"
|
|
13
20
|
end
|
|
14
21
|
|
|
15
|
-
#
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
22
|
+
# 按触发器 ID 查询完整对象及关联项和函数。
|
|
23
|
+
#
|
|
24
|
+
# @param data [Hash] 包含目标触发器 ID 字段及其值
|
|
25
|
+
# @raise [ApiError] Zabbix API 明确返回业务错误
|
|
26
|
+
# @raise [TransportError] HTTP 或网络请求失败,可能无法确认远端结果
|
|
27
|
+
# @return [Array<Hash>] 匹配的触发器及其关联对象
|
|
28
|
+
def dump_by_id(data)
|
|
29
|
+
@client.api_request(
|
|
30
|
+
method: "trigger.get",
|
|
31
|
+
params: {
|
|
32
|
+
filter: {
|
|
33
|
+
key.to_sym => data[key.to_sym]
|
|
34
|
+
},
|
|
35
|
+
output: "extend",
|
|
36
|
+
selectItems: "extend",
|
|
37
|
+
selectFunctions: "extend"
|
|
38
|
+
}
|
|
39
|
+
)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# 查询主机触发器,并按需返回监控项、函数、依赖和标签。
|
|
43
|
+
# @return [Array<Hash>]
|
|
44
|
+
def for_host(hostid, output: "extend", select_items: nil, select_functions: nil,
|
|
45
|
+
select_dependencies: nil, select_tags: nil, filter: nil)
|
|
46
|
+
raise Invalid, "hostid is required" if hostid.blank?
|
|
47
|
+
|
|
48
|
+
params = { hostids: hostid, output: output }
|
|
49
|
+
params[:selectItems] = select_items if select_items
|
|
50
|
+
params[:selectFunctions] = select_functions if select_functions
|
|
51
|
+
params[:selectDependencies] = select_dependencies if select_dependencies
|
|
52
|
+
params[:selectTags] = select_tags if select_tags
|
|
53
|
+
params[:filter] = filter if filter.present?
|
|
54
|
+
@client.api_request(method: "trigger.get", params: params)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# 按主机、描述和可选标签查询唯一触发器。
|
|
58
|
+
# @return [Hash, nil]
|
|
59
|
+
def find_for_host(hostid:, description:, tags: nil)
|
|
60
|
+
params = {
|
|
61
|
+
hostids: hostid,
|
|
62
|
+
filter: { description: description },
|
|
63
|
+
output: ["triggerid", "description"],
|
|
64
|
+
selectTags: "extend"
|
|
21
65
|
}
|
|
66
|
+
params[:tags] = tags if tags.present?
|
|
67
|
+
result = @client.api_request(
|
|
68
|
+
method: "trigger.get",
|
|
69
|
+
params: params
|
|
70
|
+
)
|
|
71
|
+
raise Conflict, "multiple triggers match #{description} on host #{hostid}" if result.length > 1
|
|
72
|
+
|
|
73
|
+
result.first
|
|
22
74
|
end
|
|
23
75
|
|
|
24
|
-
#
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
76
|
+
# 按管理标签查询唯一触发器。
|
|
77
|
+
# 展开表达式并读取依赖,供不确定写入后的完整期望状态核对。
|
|
78
|
+
# @param hostid [Integer, String] 触发器所属主机
|
|
79
|
+
# @param managed_key [String] 稳定、非秘密的受管身份
|
|
80
|
+
# @return [Hash, nil]
|
|
81
|
+
def find_managed_for_host(hostid:, managed_key:)
|
|
82
|
+
result = @client.api_request(
|
|
83
|
+
method: "trigger.get",
|
|
84
|
+
params: {
|
|
85
|
+
hostids: hostid,
|
|
86
|
+
tags: [{ tag: "zabbix_manager_id", value: managed_key, operator: 1 }],
|
|
87
|
+
output: "extend",
|
|
88
|
+
expandExpression: true,
|
|
89
|
+
selectDependencies: ["triggerid"],
|
|
90
|
+
selectTags: "extend"
|
|
34
91
|
}
|
|
35
92
|
)
|
|
93
|
+
raise Conflict, "multiple triggers use managed key #{managed_key}" if result.length > 1
|
|
94
|
+
|
|
95
|
+
result.first
|
|
36
96
|
end
|
|
37
97
|
|
|
38
|
-
#
|
|
39
|
-
|
|
40
|
-
|
|
98
|
+
# 按触发器 ID 查询唯一对象,并可返回当前依赖集合。
|
|
99
|
+
# @return [Hash, nil]
|
|
100
|
+
def find_by_id(triggerid, select_dependencies: nil)
|
|
101
|
+
id = normalized_ids([triggerid]).first
|
|
102
|
+
params = { triggerids: id, output: "extend" }
|
|
103
|
+
params[:selectDependencies] = select_dependencies if select_dependencies
|
|
104
|
+
result = @client.api_request(method: "trigger.get", params: params)
|
|
105
|
+
raise Conflict, "multiple triggers use triggerid #{id}" if result.length > 1
|
|
41
106
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
107
|
+
result.first
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# 按稳定管理键串行创建或更新主机触发器。
|
|
111
|
+
# 可通过 Client 的 upsert_lock 接入跨进程锁。
|
|
112
|
+
# @return [Integer]
|
|
113
|
+
def upsert_for_host(data)
|
|
114
|
+
attributes = data.deep_symbolize_keys
|
|
115
|
+
lock_key = "trigger:#{attributes[:hostid]}:#{attributes[:managed_key] || attributes[:description]}"
|
|
116
|
+
@client.with_upsert_lock(lock_key) { perform_upsert_for_host(attributes) }
|
|
117
|
+
end
|
|
47
118
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
119
|
+
# 批量启用或停用触发器。
|
|
120
|
+
# @return [Array<Integer>]
|
|
121
|
+
def set_status(hostid:, triggerids:, enabled:)
|
|
122
|
+
validate_boolean!(enabled, "enabled")
|
|
123
|
+
ids = normalized_ids(triggerids)
|
|
124
|
+
verify_host_ownership!(hostid, ids)
|
|
125
|
+
result = @client.api_request(
|
|
126
|
+
method: "trigger.update",
|
|
127
|
+
params: ids.map { |triggerid| { triggerid: triggerid, status: enabled ? 0 : 1 } }
|
|
128
|
+
)
|
|
129
|
+
response_ids(result, expected: ids)
|
|
130
|
+
end
|
|
52
131
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
132
|
+
# 批量删除明确指定的触发器。
|
|
133
|
+
# @return [Array<Integer>]
|
|
134
|
+
def delete_many(hostid:, triggerids:)
|
|
135
|
+
ids = normalized_ids(triggerids)
|
|
136
|
+
verify_host_ownership!(hostid, ids)
|
|
137
|
+
result = @client.api_request(method: "trigger.delete", params: ids)
|
|
138
|
+
response_ids(result, expected: ids)
|
|
139
|
+
end
|
|
56
140
|
|
|
57
|
-
|
|
141
|
+
# 使用 trigger.update 完整替换一个触发器的依赖集合。
|
|
142
|
+
# 传入空集合可清除依赖,返回被更新的触发器 ID。
|
|
143
|
+
# @return [Integer]
|
|
144
|
+
def replace_dependencies(hostid:, triggerid:, depends_on:, allow_cross_host_dependencies: false)
|
|
145
|
+
validate_boolean!(allow_cross_host_dependencies, "allow_cross_host_dependencies")
|
|
146
|
+
id = normalized_ids([triggerid]).first
|
|
147
|
+
dependency_ids = normalized_ids(depends_on, allow_empty: true)
|
|
148
|
+
dependencies = dependency_ids.map { |value| { triggerid: value } }
|
|
149
|
+
raise Invalid, "trigger cannot depend on itself" if dependencies.any? { |item| item[:triggerid] == id }
|
|
58
150
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
151
|
+
verify_host_ownership!(hostid, [id])
|
|
152
|
+
verify_host_ownership!(hostid, dependency_ids) if dependency_ids.any? && !allow_cross_host_dependencies
|
|
153
|
+
@client.with_upsert_lock("trigger-dependencies:#{id}") { update_dependencies(id, dependencies) }
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# 在保留现有依赖的前提下,为触发器追加一个或多个依赖。
|
|
157
|
+
# 读改写过程按触发器 ID 加锁,跨进程可通过 Client 的 upsert_lock 协调。
|
|
158
|
+
# @return [Integer]
|
|
159
|
+
def add_dependencies(hostid:, triggerid:, depends_on:, allow_cross_host_dependencies: false)
|
|
160
|
+
validate_boolean!(allow_cross_host_dependencies, "allow_cross_host_dependencies")
|
|
161
|
+
id = normalized_ids([triggerid]).first
|
|
162
|
+
additions = normalized_ids(depends_on)
|
|
163
|
+
raise Invalid, "trigger cannot depend on itself" if additions.include?(id)
|
|
164
|
+
|
|
165
|
+
verify_host_ownership!(hostid, [id])
|
|
166
|
+
|
|
167
|
+
@client.with_upsert_lock("trigger-dependencies:#{id}") do
|
|
168
|
+
current = find_by_id(id, select_dependencies: ["triggerid"])
|
|
169
|
+
raise ApiError, "Zabbix trigger #{id} was not found" unless current
|
|
170
|
+
|
|
171
|
+
existing = Array(current["dependencies"]).filter_map { |dependency| dependency["triggerid"] }
|
|
172
|
+
dependency_ids = (existing + additions).uniq
|
|
173
|
+
unless allow_cross_host_dependencies
|
|
174
|
+
verify_host_ownership!(hostid, dependency_ids)
|
|
175
|
+
end
|
|
176
|
+
dependencies = dependency_ids.map { |value| { triggerid: value } }
|
|
177
|
+
update_dependencies(id, dependencies)
|
|
72
178
|
end
|
|
73
179
|
end
|
|
74
180
|
|
|
75
|
-
#
|
|
76
|
-
|
|
77
|
-
|
|
181
|
+
# 执行已加锁的触发器幂等写入。
|
|
182
|
+
# @return [Integer]
|
|
183
|
+
# @api private
|
|
184
|
+
private def perform_upsert_for_host(data)
|
|
185
|
+
attributes = data.deep_symbolize_keys
|
|
186
|
+
hostid = attributes.delete(:hostid)
|
|
187
|
+
managed_key = attributes.delete(:managed_key)&.to_s
|
|
188
|
+
raise Invalid, "hostid is required" if hostid.blank?
|
|
189
|
+
raise Invalid, "description is required" if attributes[:description].blank?
|
|
190
|
+
raise Invalid, "expression is required" if attributes[:expression].blank?
|
|
191
|
+
|
|
192
|
+
if managed_key.present?
|
|
193
|
+
raise Invalid, "managed_key is too long" if managed_key.length > MAX_MANAGED_KEY_LENGTH
|
|
78
194
|
|
|
79
|
-
|
|
80
|
-
|
|
195
|
+
attributes[:tags] = Array(attributes[:tags]).reject do |tag|
|
|
196
|
+
tag[:tag].to_s == "zabbix_manager_id" || tag["tag"].to_s == "zabbix_manager_id"
|
|
197
|
+
end
|
|
198
|
+
attributes[:tags] << { tag: "zabbix_manager_id", value: managed_key }
|
|
199
|
+
current = find_managed_for_host(hostid: hostid, managed_key: managed_key)
|
|
81
200
|
else
|
|
82
|
-
|
|
201
|
+
ownership_tags = Array(attributes[:tags]).select do |tag|
|
|
202
|
+
tag[:tag].to_s == "managed_by" || tag["tag"].to_s == "managed_by"
|
|
203
|
+
end
|
|
204
|
+
current = find_for_host(hostid: hostid, description: attributes[:description], tags: ownership_tags)
|
|
205
|
+
end
|
|
206
|
+
if current
|
|
207
|
+
triggerid = current["triggerid"]
|
|
208
|
+
response_identifier(triggerid)
|
|
209
|
+
attributes[:tags] = merge_tags(current["tags"], attributes[:tags]) if attributes[:tags]
|
|
210
|
+
result = @client.api_request(method: "trigger.update", params: attributes.merge(triggerid: triggerid))
|
|
211
|
+
response_id(result, expected: [triggerid])
|
|
212
|
+
else
|
|
213
|
+
create_managed_trigger(hostid, managed_key, attributes)
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
# 创建触发器;响应丢失时按管理键回读并核对期望状态。
|
|
218
|
+
# @return [Integer]
|
|
219
|
+
# @api private
|
|
220
|
+
private def create_managed_trigger(hostid, managed_key, attributes)
|
|
221
|
+
result = @client.api_request(method: "trigger.create", params: attributes)
|
|
222
|
+
response_id(result)
|
|
223
|
+
rescue TransportError => original_error
|
|
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
|
|
228
|
+
|
|
229
|
+
raise ResultUnknown,
|
|
230
|
+
"trigger create result is unknown; inspect managed key #{managed_key.inspect} before retrying: " \
|
|
231
|
+
"#{original_error.message}"
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
# 对结果不确定的创建执行短暂退避回读,不重放写请求。
|
|
235
|
+
# @return [Hash, nil]
|
|
236
|
+
# @api private
|
|
237
|
+
private def recover_created_trigger(hostid:, managed_key:, attributes:)
|
|
238
|
+
delays = @client.options.fetch(:uncertain_write_delays, DEFAULT_UNCERTAIN_WRITE_DELAYS)
|
|
239
|
+
delays.each do |delay|
|
|
240
|
+
sleep(delay) if delay.positive?
|
|
241
|
+
current = find_managed_for_host(hostid: hostid, managed_key: managed_key)
|
|
242
|
+
if current && recovered_trigger_matches?(current, attributes)
|
|
243
|
+
response_identifier(current["triggerid"])
|
|
244
|
+
return current
|
|
245
|
+
end
|
|
246
|
+
rescue ApiError, TransportError
|
|
247
|
+
next
|
|
83
248
|
end
|
|
249
|
+
nil
|
|
84
250
|
end
|
|
85
251
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
+
|
|
258
|
+
# 合并受管标签,同时保留调用方未覆盖的运维标签。
|
|
259
|
+
# @return [Array<Hash>]
|
|
260
|
+
# @api private
|
|
261
|
+
private def merge_tags(existing_tags, desired_tags)
|
|
262
|
+
desired = Array(desired_tags).map(&:deep_symbolize_keys)
|
|
263
|
+
desired_names = desired.map { |tag| tag.fetch(:tag).to_s }
|
|
264
|
+
retained = Array(existing_tags).map(&:deep_symbolize_keys).reject do |tag|
|
|
265
|
+
desired_names.include?(tag[:tag].to_s)
|
|
266
|
+
end
|
|
267
|
+
retained + desired
|
|
90
268
|
end
|
|
91
269
|
|
|
92
|
-
#
|
|
93
|
-
def
|
|
94
|
-
|
|
270
|
+
# 回查触发器归属,阻止共享高权限令牌跨主机修改。
|
|
271
|
+
private def verify_host_ownership!(hostid, ids)
|
|
272
|
+
raise Invalid, "hostid is required" if hostid.blank?
|
|
95
273
|
|
|
96
274
|
result = @client.api_request(
|
|
97
|
-
method: "trigger.
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
275
|
+
method: "trigger.get", params: { hostids: hostid, triggerids: ids, output: ["triggerid"] }
|
|
276
|
+
)
|
|
277
|
+
owned_ids = result.map { |trigger| trigger.fetch("triggerid").to_s }
|
|
278
|
+
foreign_ids = ids - owned_ids
|
|
279
|
+
raise Conflict, "triggers do not belong to host #{hostid}: #{foreign_ids.join(", ")}" if foreign_ids.any?
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
# 使用官方 trigger.update 完整写入依赖集合。
|
|
283
|
+
private def update_dependencies(triggerid, dependencies)
|
|
284
|
+
result = @client.api_request(
|
|
285
|
+
method: "trigger.update", params: { triggerid: triggerid, dependencies: dependencies }
|
|
102
286
|
)
|
|
103
|
-
result
|
|
287
|
+
response_id(result, expected: [triggerid])
|
|
104
288
|
end
|
|
105
289
|
end
|
|
106
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,39 +1,32 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
class ZabbixManager
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
# 用户资源与媒介配置替换;字段名称按服务器版本转换。
|
|
5
|
+
class Users < Resource
|
|
6
|
+
# @return [String] API 模块名
|
|
6
7
|
def method_name
|
|
7
8
|
"user"
|
|
8
9
|
end
|
|
9
10
|
|
|
10
|
-
#
|
|
11
|
+
# @return [String] 用户名字段
|
|
11
12
|
def identify
|
|
12
|
-
"alias"
|
|
13
|
+
Gem::Version.new(@client.api_version) >= Gem::Version.new("5.4") ? "username" : "alias"
|
|
13
14
|
end
|
|
14
15
|
|
|
15
|
-
#
|
|
16
|
-
|
|
16
|
+
# 替换指定用户的完整媒介配置。
|
|
17
|
+
# @param data [Hash] 包含 userids 和 media
|
|
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
|
|
17
23
|
result = @client.api_request(
|
|
18
|
-
method: "user
|
|
19
|
-
params:
|
|
20
|
-
{
|
|
21
|
-
userid: i,
|
|
22
|
-
user_medias: data[:media]
|
|
23
|
-
}
|
|
24
|
+
method: "user.update",
|
|
25
|
+
params: attributes.fetch(:userids).map do |userid|
|
|
26
|
+
{ userid: userid, media_field => attributes.fetch(:media) }
|
|
24
27
|
end
|
|
25
28
|
)
|
|
26
|
-
result
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
# 通过 Zabbix API 为用户添加媒体信息
|
|
30
|
-
def add_medias(data)
|
|
31
|
-
medias_helper(data, "update")
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
# 通过 Zabbix API 更新用户媒体信息
|
|
35
|
-
def update_medias(data)
|
|
36
|
-
medias_helper(data, "update")
|
|
29
|
+
response_ids(result, expected: ids).first
|
|
37
30
|
end
|
|
38
31
|
end
|
|
39
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
|