zabbix_manager 5.1.4 → 5.1.6
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 +25 -9
- data/LICENSE +1 -1
- data/README.md +202 -8
- data/lib/zabbix_manager/basic/basic_alias.rb +25 -3
- data/lib/zabbix_manager/basic/basic_func.rb +32 -61
- data/lib/zabbix_manager/basic/basic_init.rb +28 -13
- data/lib/zabbix_manager/basic/basic_logic.rb +172 -128
- data/lib/zabbix_manager/classes/actions.rb +21 -11
- data/lib/zabbix_manager/classes/applications.rb +12 -17
- data/lib/zabbix_manager/classes/configurations.rb +20 -5
- data/lib/zabbix_manager/classes/drules.rb +11 -23
- data/lib/zabbix_manager/classes/errors.rb +16 -28
- data/lib/zabbix_manager/classes/events.rb +6 -2
- data/lib/zabbix_manager/classes/graphs.rb +41 -34
- data/lib/zabbix_manager/classes/hostgroups.rb +53 -3
- data/lib/zabbix_manager/classes/hostinterfaces.rb +172 -12
- data/lib/zabbix_manager/classes/hosts.rb +197 -68
- data/lib/zabbix_manager/classes/httptests.rb +18 -20
- data/lib/zabbix_manager/classes/items.rb +199 -72
- data/lib/zabbix_manager/classes/maintenance.rb +6 -2
- data/lib/zabbix_manager/classes/mediatypes.rb +7 -63
- data/lib/zabbix_manager/classes/problems.rb +75 -57
- data/lib/zabbix_manager/classes/proxies.rb +48 -7
- data/lib/zabbix_manager/classes/roles.rb +47 -36
- data/lib/zabbix_manager/classes/screens.rb +45 -14
- data/lib/zabbix_manager/classes/scripts.rb +17 -7
- data/lib/zabbix_manager/classes/server.rb +7 -1
- data/lib/zabbix_manager/classes/templates.rb +59 -21
- data/lib/zabbix_manager/classes/triggers.rb +253 -66
- data/lib/zabbix_manager/classes/usergroups.rb +28 -6
- data/lib/zabbix_manager/classes/usermacros.rb +119 -33
- data/lib/zabbix_manager/classes/users.rb +39 -7
- data/lib/zabbix_manager/classes/valuemaps.rb +13 -19
- data/lib/zabbix_manager/client.rb +311 -135
- data/lib/zabbix_manager/http_transport.rb +192 -0
- data/lib/zabbix_manager/log_sanitizer.rb +68 -0
- data/lib/zabbix_manager/monitoring.rb +691 -0
- data/lib/zabbix_manager/version.rb +1 -1
- data/lib/zabbix_manager.rb +118 -40
- data/zabbix_manager.gemspec +21 -8
- metadata +208 -22
- data/lib/zabbix_manager/classes/unusable.rb +0 -11
|
@@ -2,78 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
class ZabbixManager
|
|
4
4
|
class Mediatypes < Basic
|
|
5
|
-
#
|
|
5
|
+
# 返回媒介类型对象对应的 Zabbix API 方法前缀。
|
|
6
|
+
# @return [String] API 方法前缀
|
|
6
7
|
def method_name
|
|
7
8
|
"mediatype"
|
|
8
9
|
end
|
|
9
10
|
|
|
10
|
-
#
|
|
11
|
+
# 返回媒介类型用于业务识别的字段名。
|
|
12
|
+
# @return [String] 识别字段名
|
|
11
13
|
def identify
|
|
12
14
|
"name"
|
|
13
15
|
end
|
|
14
16
|
|
|
15
|
-
#
|
|
17
|
+
# 返回媒介类型的最小默认属性;端点和凭据等版本相关字段由调用方显式提供。
|
|
18
|
+
# @return [Hash] 媒介类型默认属性
|
|
16
19
|
def default_options
|
|
17
|
-
{
|
|
18
|
-
name: "", # 媒体类型名称
|
|
19
|
-
description: "", # 描述
|
|
20
|
-
type: 0, # 0 - 电子邮件,1 - 外部脚本,2 - 短信,3 - Jabber,100 - EzTexting
|
|
21
|
-
smtp_server: "",
|
|
22
|
-
smtp_helo: "",
|
|
23
|
-
smtp_email: "", # Zabbix 服务器的电子邮件地址
|
|
24
|
-
exec_path: "", # 外部脚本的名称
|
|
25
|
-
gsm_modem: "", # GSM modem 的串口设备名称
|
|
26
|
-
username: "", # Zabbix 服务器使用的 Jabber 用户名
|
|
27
|
-
passwd: "" # Zabbix 服务器使用的 Jabber 密码
|
|
28
|
-
}
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
# 使用 API 更新媒体类型对象
|
|
32
|
-
def update(data, force = false)
|
|
33
|
-
log "[DEBUG] 调用 update 方法,参数: #{data.inspect}"
|
|
34
|
-
if data[key.to_sym].nil?
|
|
35
|
-
data[key.to_sym] = get_id(data)
|
|
36
|
-
log "[DEBUG] 用 ID 丰富数据: #{data.inspect}"
|
|
37
|
-
end
|
|
38
|
-
dump = {}
|
|
39
|
-
dump_by_id(key.to_sym => data[key.to_sym]).each do |item|
|
|
40
|
-
dump = symbolize_keys(item) if item[key].to_i == data[key.to_sym].to_i
|
|
41
|
-
end
|
|
42
|
-
if hash_equals?(dump, data) && !force
|
|
43
|
-
log "[DEBUG] 键 #{dump} 和 #{data} 相等,跳过更新"
|
|
44
|
-
data[key.to_sym].to_i
|
|
45
|
-
else
|
|
46
|
-
data_update = [data]
|
|
47
|
-
result = @client.api_request(method: "#{method_name}.update", params: data_update)
|
|
48
|
-
parse_keys result
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
# 从 API 获取媒体类型对象的 id
|
|
53
|
-
def get_id(data)
|
|
54
|
-
log "[DEBUG] 调用 get_id 方法,参数: #{data.inspect}"
|
|
55
|
-
data = symbolize_keys(data) if data.key?(identify)
|
|
56
|
-
name = data[identify.to_sym]
|
|
57
|
-
raise ZbxError, "未提供 #{identify} 参数,#{data} (#{method_name})" if name.nil?
|
|
58
|
-
|
|
59
|
-
result = @client.api_request(
|
|
60
|
-
method: "#{method_name}.get",
|
|
61
|
-
params: {
|
|
62
|
-
filter: { name: name },
|
|
63
|
-
output: [key, identify]
|
|
64
|
-
}
|
|
65
|
-
)
|
|
66
|
-
id = nil
|
|
67
|
-
result.each { |item| id = item[key].to_i if item[identify] == data[identify.to_sym] }
|
|
68
|
-
id
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
# 创建或更新媒体类型对象
|
|
72
|
-
def create_or_update(data)
|
|
73
|
-
log "[DEBUG] 调用 create_or_update 方法,参数: #{data.inspect}"
|
|
74
|
-
|
|
75
|
-
id = get_id(identify.to_sym => data[identify.to_sym])
|
|
76
|
-
id ? update(data.merge(key.to_sym => id.to_s)) : create(data)
|
|
20
|
+
{ type: 0 }
|
|
77
21
|
end
|
|
78
22
|
end
|
|
79
23
|
end
|
|
@@ -2,87 +2,105 @@
|
|
|
2
2
|
|
|
3
3
|
class ZabbixManager
|
|
4
4
|
class Problems < Basic
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
# 与 Zabbix API 交互的问题方法名称
|
|
5
|
+
# 返回问题对象对应的 Zabbix API 方法前缀。
|
|
6
|
+
#
|
|
7
|
+
# @return [String]
|
|
9
8
|
def method_name
|
|
10
9
|
"problem"
|
|
11
10
|
end
|
|
12
11
|
|
|
13
|
-
#
|
|
12
|
+
# 返回问题对象用于业务识别的字段名。
|
|
13
|
+
#
|
|
14
|
+
# @return [String]
|
|
14
15
|
def identify
|
|
15
16
|
"name"
|
|
16
17
|
end
|
|
17
18
|
|
|
18
|
-
#
|
|
19
|
+
# 返回问题对象的兼容 ID 字段名;问题对象本身没有独立的唯一标识。
|
|
20
|
+
#
|
|
21
|
+
# @return [String]
|
|
22
|
+
def key
|
|
23
|
+
"problemid"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# 返回问题对象的复数兼容 ID 字段名。
|
|
27
|
+
#
|
|
28
|
+
# @return [String]
|
|
29
|
+
def keys
|
|
30
|
+
"problemids"
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# 按识别字段查询问题对象的完整数据。
|
|
34
|
+
#
|
|
35
|
+
# @param data [Hash] 包含问题识别字段及其值的查询条件
|
|
36
|
+
# @raise [ApiError] Zabbix API 返回业务错误时抛出
|
|
37
|
+
# @raise [TransportError] Zabbix 服务端返回非成功 HTTP 状态时抛出
|
|
38
|
+
# @return [Hash] 匹配的问题数据
|
|
19
39
|
def dump_by_id(data)
|
|
20
|
-
log "[DEBUG]
|
|
40
|
+
log "[DEBUG] Call dump_by_id with parameters: #{data.inspect}"
|
|
21
41
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
42
|
+
@client.api_request(
|
|
43
|
+
method: "problem.get",
|
|
44
|
+
params: {
|
|
45
|
+
filter: {
|
|
46
|
+
identify.to_sym => data[identify.to_sym]
|
|
47
|
+
},
|
|
48
|
+
output: "extend"
|
|
49
|
+
}
|
|
50
|
+
)
|
|
28
51
|
end
|
|
29
52
|
|
|
30
|
-
#
|
|
53
|
+
# 获取问题及其确认、标签和抑制信息,并支持附加查询参数。
|
|
54
|
+
#
|
|
55
|
+
# @param data [Hash] 问题过滤条件及附加 API 参数
|
|
56
|
+
# @raise [ApiError] Zabbix API 返回业务错误时抛出
|
|
57
|
+
# @raise [TransportError] Zabbix 服务端返回非成功 HTTP 状态时抛出
|
|
58
|
+
# @return [Hash] 匹配的问题完整数据
|
|
31
59
|
def get_full_data(data)
|
|
32
|
-
log "[DEBUG]
|
|
60
|
+
log "[DEBUG] Call get_full_data with parameters: #{data.inspect}"
|
|
33
61
|
|
|
34
|
-
data =
|
|
62
|
+
data = data.deep_symbolize_keys
|
|
63
|
+
params = {
|
|
64
|
+
recent: false,
|
|
65
|
+
sortfield: ["eventid"],
|
|
66
|
+
sortorder: "DESC",
|
|
67
|
+
output: "extend",
|
|
68
|
+
selectAcknowledges: "extend",
|
|
69
|
+
selectTags: "extend",
|
|
70
|
+
selectSuppressionData: "extend"
|
|
71
|
+
}.merge(data.except(:name).compact)
|
|
72
|
+
params[:filter] = { identify.to_sym => data[identify.to_sym] } if data[identify.to_sym].present?
|
|
35
73
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
eventids: data[:eventids] || nil,
|
|
41
|
-
groupids: data[:groupids] || nil,
|
|
42
|
-
hostids: data[:hostids] || nil,
|
|
43
|
-
objectids: data[:objectids] || nil,
|
|
44
|
-
applicationids: data[:applicationids] || nil,
|
|
45
|
-
tags: data[:tags] || nil,
|
|
46
|
-
time_from: data[:time_from] || nil,
|
|
47
|
-
time_till: data[:time_till] || nil,
|
|
48
|
-
eventid_from: data[:eventid_from] || nil,
|
|
49
|
-
eventid_till: data[:eventid_till] || nil,
|
|
50
|
-
recent: data[:recent] || false,
|
|
51
|
-
sortfield: data[:sortfield] || ["eventid"],
|
|
52
|
-
sortorder: data[:sortorder] || "DESC",
|
|
53
|
-
countOutput: data[:countOutput] || nil,
|
|
54
|
-
output: "extend",
|
|
55
|
-
selectAcknowledges: "extend",
|
|
56
|
-
selectTags: "extend",
|
|
57
|
-
selectSuppressionData: "extend"
|
|
58
|
-
})
|
|
74
|
+
@client.api_request(
|
|
75
|
+
method: "#{method_name}.get",
|
|
76
|
+
params: params
|
|
77
|
+
)
|
|
59
78
|
end
|
|
60
79
|
|
|
61
|
-
#
|
|
80
|
+
# 获取全部问题对象的完整数据。
|
|
81
|
+
#
|
|
82
|
+
# @raise [ApiError] Zabbix API 返回业务错误时抛出
|
|
83
|
+
# @raise [TransportError] Zabbix 服务端返回非成功 HTTP 状态时抛出
|
|
84
|
+
# @return [Array<Hash>] 匹配的问题对象列表
|
|
62
85
|
def all
|
|
63
86
|
get_full_data({})
|
|
64
87
|
end
|
|
65
88
|
|
|
66
|
-
#
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
end
|
|
75
|
-
return if event_ids.empty?
|
|
89
|
+
# 批量确认事件,可指定确认动作和附加消息。
|
|
90
|
+
# @param eventids [Array<String, Integer>, String, Integer] 待确认的事件 ID
|
|
91
|
+
# @param action [Integer] Zabbix 确认动作位掩码
|
|
92
|
+
# @param message [String, nil] 可选确认消息
|
|
93
|
+
# @return [Hash] 事件确认结果
|
|
94
|
+
def acknowledge_events(eventids, action: 2, message: nil)
|
|
95
|
+
ids = Array(eventids).map(&:to_s).reject(&:blank?).uniq
|
|
96
|
+
raise Invalid, "eventids are required" if ids.empty?
|
|
76
97
|
|
|
77
|
-
|
|
98
|
+
@client.api_request(
|
|
78
99
|
method: "event.acknowledge",
|
|
79
|
-
params: {
|
|
80
|
-
eventids: event_ids,
|
|
81
|
-
action: 2,
|
|
82
|
-
message: "本次告警符合自动刷新规则,已通过接口处理且刷新时间为:#{Time.now}"
|
|
83
|
-
}
|
|
100
|
+
params: { eventids: ids, action: action, message: message }.compact
|
|
84
101
|
)
|
|
85
|
-
result.empty? ? nil : result.map { |i| { eventids: i["eventids"] } }
|
|
86
102
|
end
|
|
103
|
+
|
|
104
|
+
alias ack_event acknowledge_events
|
|
87
105
|
end
|
|
88
106
|
end
|
|
@@ -2,30 +2,71 @@
|
|
|
2
2
|
|
|
3
3
|
class ZabbixManager
|
|
4
4
|
class Proxies < Basic
|
|
5
|
-
#
|
|
5
|
+
# 返回 Zabbix API 中代理对象的方法名前缀。
|
|
6
|
+
#
|
|
7
|
+
# @return [String] 代理对象的方法名前缀
|
|
6
8
|
def method_name
|
|
7
9
|
"proxy"
|
|
8
10
|
end
|
|
9
11
|
|
|
10
|
-
#
|
|
12
|
+
# 返回用于唯一识别代理对象的字段名。
|
|
13
|
+
#
|
|
14
|
+
# @return [String] 代理对象的标识字段名
|
|
11
15
|
def identify
|
|
12
16
|
"host"
|
|
13
17
|
end
|
|
14
18
|
|
|
15
|
-
#
|
|
19
|
+
# 通过 Zabbix API 删除指定代理,并返回首个已删除代理的 ID。
|
|
20
|
+
#
|
|
21
|
+
# @param data [Array] 要删除的 proxyid 数组
|
|
22
|
+
# @raise [ApiError] Zabbix API 返回业务错误时抛出
|
|
23
|
+
# @raise [TransportError] Zabbix 服务返回非成功 HTTP 状态时抛出
|
|
24
|
+
# @return [Integer, nil] 已删除代理的 ID,无结果时返回 nil
|
|
16
25
|
def delete(data)
|
|
17
26
|
result = @client.api_request(method: "proxy.delete", params: data)
|
|
18
27
|
result.empty? ? nil : result["proxyids"][0].to_i
|
|
19
28
|
end
|
|
20
29
|
|
|
21
|
-
#
|
|
22
|
-
|
|
30
|
+
# 检查当前凭证是否可读取指定代理。
|
|
31
|
+
#
|
|
32
|
+
# @param data [Array] 要检查的 proxyid 数组
|
|
33
|
+
# @raise [ApiError] Zabbix API 返回业务错误时抛出
|
|
34
|
+
# @raise [TransportError] Zabbix 服务返回非成功 HTTP 状态时抛出
|
|
35
|
+
# @return [Boolean] 指定代理是否可读
|
|
36
|
+
def isreadable(data)
|
|
23
37
|
@client.api_request(method: "proxy.isreadable", params: data)
|
|
24
38
|
end
|
|
25
39
|
|
|
26
|
-
#
|
|
27
|
-
|
|
40
|
+
# 检查当前凭证是否可写入指定代理。
|
|
41
|
+
#
|
|
42
|
+
# @param data [Array] 要检查的 proxyid 数组
|
|
43
|
+
# @raise [ApiError] Zabbix API 返回业务错误时抛出
|
|
44
|
+
# @raise [TransportError] Zabbix 服务返回非成功 HTTP 状态时抛出
|
|
45
|
+
# @return [Boolean] 指定代理是否可写
|
|
46
|
+
def iswritable(data)
|
|
28
47
|
@client.api_request(method: "proxy.iswritable", params: data)
|
|
29
48
|
end
|
|
49
|
+
|
|
50
|
+
# 按代理名称查询唯一代理 ID,空名称返回 nil,重名时拒绝选择。
|
|
51
|
+
#
|
|
52
|
+
# @param proxy [String] 代理名称
|
|
53
|
+
# @return [String, nil] 唯一代理 ID,未找到时返回 nil
|
|
54
|
+
def get_proxy_id(proxy)
|
|
55
|
+
return nil if proxy.blank?
|
|
56
|
+
|
|
57
|
+
# 请求后端接口,只支持单个代理节点
|
|
58
|
+
result = @client.api_request(
|
|
59
|
+
method: "proxy.get",
|
|
60
|
+
params: {
|
|
61
|
+
output: %w[proxyid host],
|
|
62
|
+
filter: {
|
|
63
|
+
host: proxy
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
)
|
|
67
|
+
raise Conflict, "proxy lookup is ambiguous for #{proxy}" if result.length > 1
|
|
68
|
+
|
|
69
|
+
result.first&.fetch("proxyid", nil)
|
|
70
|
+
end
|
|
30
71
|
end
|
|
31
72
|
end
|
|
@@ -2,51 +2,77 @@
|
|
|
2
2
|
|
|
3
3
|
class ZabbixManager
|
|
4
4
|
class Roles < Basic
|
|
5
|
-
#
|
|
5
|
+
# 返回 Zabbix API 中角色对象的方法名前缀。
|
|
6
|
+
#
|
|
7
|
+
# @return [String] 角色对象的方法名前缀
|
|
6
8
|
def method_name
|
|
7
9
|
"role"
|
|
8
10
|
end
|
|
9
11
|
|
|
10
|
-
#
|
|
12
|
+
# 返回角色对象的主键字段名。
|
|
13
|
+
#
|
|
14
|
+
# @return [String] 角色对象的主键字段名
|
|
15
|
+
def key
|
|
16
|
+
"roleid"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# 返回用于唯一识别角色的业务字段名。
|
|
20
|
+
#
|
|
21
|
+
# @return [String] 角色对象的业务标识字段名
|
|
11
22
|
def identify
|
|
12
23
|
"name"
|
|
13
24
|
end
|
|
14
25
|
|
|
15
|
-
#
|
|
26
|
+
# 更新指定角色的权限规则。
|
|
27
|
+
#
|
|
28
|
+
# @param data [Hash] 包含 roleid 和 rules 的角色规则数据
|
|
29
|
+
# @raise [ApiError] Zabbix API 返回业务错误时抛出
|
|
30
|
+
# @raise [TransportError] Zabbix 服务返回非成功 HTTP 状态时抛出
|
|
31
|
+
# @return [Integer] 已更新角色的 ID
|
|
16
32
|
def rules(data)
|
|
17
|
-
|
|
33
|
+
attributes = data.deep_symbolize_keys
|
|
34
|
+
raise Invalid, "roleid is required" if attributes[:roleid].blank?
|
|
35
|
+
raise Invalid, "rules is required" if attributes[:rules].blank?
|
|
36
|
+
|
|
18
37
|
result = @client.api_request(
|
|
19
38
|
method: "role.update",
|
|
20
39
|
params: {
|
|
21
|
-
roleid:
|
|
22
|
-
rules:
|
|
40
|
+
roleid: attributes[:roleid],
|
|
41
|
+
rules: attributes[:rules]
|
|
23
42
|
}
|
|
24
43
|
)
|
|
25
|
-
result
|
|
44
|
+
result.fetch("roleids").first.to_i
|
|
26
45
|
end
|
|
27
46
|
|
|
28
|
-
#
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
#
|
|
47
|
+
# 按角色 ID 获取角色详情及完整规则。
|
|
48
|
+
#
|
|
49
|
+
# @param data [Hash] 包含 id 的查询参数
|
|
50
|
+
# @raise [ApiError] Zabbix API 返回业务错误时抛出
|
|
51
|
+
# @raise [TransportError] Zabbix 服务返回非成功 HTTP 状态时抛出
|
|
52
|
+
# @return [Array<Hash>] 匹配的角色详情
|
|
34
53
|
def dump_by_id(data)
|
|
35
|
-
log "[DEBUG]
|
|
54
|
+
log "[DEBUG] Call dump_by_id with parameters: #{data.inspect}"
|
|
36
55
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
56
|
+
@client.api_request(
|
|
57
|
+
method: "role.get",
|
|
58
|
+
params: {
|
|
59
|
+
output: "extend",
|
|
40
60
|
selectRules: "extend",
|
|
41
|
-
roleids:
|
|
61
|
+
roleids: data[:id]
|
|
42
62
|
}
|
|
43
63
|
)
|
|
44
64
|
end
|
|
45
65
|
|
|
46
|
-
#
|
|
66
|
+
# 按角色名称查询所有匹配的角色 ID。
|
|
67
|
+
#
|
|
68
|
+
# @param data [Hash] 包含 name 的角色查询参数
|
|
69
|
+
# @raise [ApiError] Zabbix API 返回业务错误时抛出
|
|
70
|
+
# @raise [TransportError] Zabbix 服务返回非成功 HTTP 状态时抛出
|
|
71
|
+
# @return [Array<String>] 匹配的角色 ID 数组
|
|
47
72
|
def get_ids_by_name(data)
|
|
48
|
-
result =
|
|
49
|
-
|
|
73
|
+
result = @client.api_request(
|
|
74
|
+
method: "role.get",
|
|
75
|
+
params: {
|
|
50
76
|
filter: {
|
|
51
77
|
name: data[:name]
|
|
52
78
|
},
|
|
@@ -58,20 +84,5 @@ class ZabbixManager
|
|
|
58
84
|
rule["roleid"]
|
|
59
85
|
end
|
|
60
86
|
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
87
|
end
|
|
77
88
|
end
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
class ZabbixManager
|
|
4
4
|
class Screens < Basic
|
|
5
|
-
#
|
|
5
|
+
# 以下资源类型取自 frontends/php/include/defines.inc.php。
|
|
6
6
|
# SCREEN_RESOURCE_GRAPH => 0,
|
|
7
7
|
# SCREEN_RESOURCE_SIMPLE_GRAPH => 1,
|
|
8
8
|
# SCREEN_RESOURCE_MAP => 2,
|
|
@@ -21,45 +21,61 @@ class ZabbixManager
|
|
|
21
21
|
# SCREEN_RESOURCE_SYSTEM_STATUS => 15,
|
|
22
22
|
# SCREEN_RESOURCE_HOST_TRIGGERS => 16
|
|
23
23
|
|
|
24
|
-
#
|
|
24
|
+
# 返回 Zabbix API 中聚合图形对象的方法名前缀。
|
|
25
|
+
#
|
|
26
|
+
# @return [String] 聚合图形对象的方法名前缀
|
|
25
27
|
def method_name
|
|
26
28
|
"screen"
|
|
27
29
|
end
|
|
28
30
|
|
|
29
|
-
#
|
|
31
|
+
# 返回用于唯一识别聚合图形的业务字段名。
|
|
32
|
+
#
|
|
33
|
+
# @return [String] 聚合图形的业务标识字段名
|
|
30
34
|
def identify
|
|
31
35
|
"name"
|
|
32
36
|
end
|
|
33
37
|
|
|
34
|
-
#
|
|
38
|
+
# 删除指定聚合图形并返回首个已删除对象的 ID。
|
|
39
|
+
#
|
|
40
|
+
# @param data [String, Array] 要删除的聚合图形 ID
|
|
41
|
+
# @raise [ApiError] Zabbix API 返回业务错误时抛出
|
|
42
|
+
# @raise [TransportError] Zabbix 服务返回非成功 HTTP 状态时抛出
|
|
43
|
+
# @return [Integer, nil] 已删除聚合图形的 ID,无结果时返回 nil
|
|
35
44
|
def delete(data)
|
|
36
45
|
result = @client.api_request(method: "screen.delete", params: [data])
|
|
37
46
|
result.empty? ? nil : result["screenids"][0].to_i
|
|
38
47
|
end
|
|
39
48
|
|
|
40
|
-
#
|
|
49
|
+
# 按名称获取聚合图形,不存在时按图形 ID 网格化创建。
|
|
50
|
+
#
|
|
51
|
+
# @param data [Hash] 包含 screen_name、graphids 及可选布局参数
|
|
52
|
+
# @raise [ApiError] Zabbix API 返回业务错误时抛出
|
|
53
|
+
# @raise [TransportError] Zabbix 服务返回非成功 HTTP 状态时抛出
|
|
54
|
+
# @return [Integer] 聚合图形 ID
|
|
41
55
|
def get_or_create_for_host(data)
|
|
42
56
|
screen_name = data[:screen_name]
|
|
43
|
-
graphids = data[:graphids]
|
|
44
|
-
|
|
45
|
-
|
|
57
|
+
graphids = Array(data[:graphids])
|
|
58
|
+
raise Invalid, "screen_name is required" if screen_name.blank?
|
|
59
|
+
raise Invalid, "graphids must contain at least one graph" if graphids.empty?
|
|
60
|
+
|
|
61
|
+
hsize = positive_hsize(data.fetch(:hsize, 3))
|
|
62
|
+
|
|
46
63
|
valign = data[:valign] || 2
|
|
47
64
|
halign = data[:halign] || 2
|
|
48
65
|
rowspan = data[:rowspan] || 1
|
|
49
66
|
colspan = data[:colspan] || 1
|
|
50
67
|
height = data[:height] || 320
|
|
51
68
|
width = data[:width] || 200
|
|
52
|
-
vsize = data[:vsize] ||
|
|
69
|
+
vsize = data[:vsize] || graphids.length.fdiv(hsize).ceil
|
|
53
70
|
screenid = get_id(name: screen_name)
|
|
54
71
|
|
|
55
72
|
unless screenid
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
screenitems << {
|
|
73
|
+
screenitems = graphids.each_with_index.map do |graphid, index|
|
|
74
|
+
{
|
|
59
75
|
resourcetype: 0,
|
|
60
76
|
resourceid: graphid,
|
|
61
|
-
x:
|
|
62
|
-
y:
|
|
77
|
+
x: index % hsize,
|
|
78
|
+
y: index / hsize,
|
|
63
79
|
valign: valign,
|
|
64
80
|
halign: halign,
|
|
65
81
|
rowspan: rowspan,
|
|
@@ -78,5 +94,20 @@ class ZabbixManager
|
|
|
78
94
|
end
|
|
79
95
|
screenid
|
|
80
96
|
end
|
|
97
|
+
|
|
98
|
+
private
|
|
99
|
+
|
|
100
|
+
# 将横向格数转换为正整数,非法值直接拒绝。
|
|
101
|
+
#
|
|
102
|
+
# @param value [Object] 待校验的横向格数
|
|
103
|
+
# @return [Integer] 正整数横向格数
|
|
104
|
+
def positive_hsize(value)
|
|
105
|
+
size = Integer(value)
|
|
106
|
+
raise Invalid unless size.positive?
|
|
107
|
+
|
|
108
|
+
size
|
|
109
|
+
rescue ArgumentError, TypeError
|
|
110
|
+
raise Invalid, "hsize must be a positive integer"
|
|
111
|
+
end
|
|
81
112
|
end
|
|
82
113
|
end
|
|
@@ -2,20 +2,27 @@
|
|
|
2
2
|
|
|
3
3
|
class ZabbixManager
|
|
4
4
|
class Scripts < Basic
|
|
5
|
-
#
|
|
5
|
+
# 返回 Zabbix API 中脚本对象的方法名前缀。
|
|
6
|
+
#
|
|
7
|
+
# @return [String] 脚本对象的方法名前缀
|
|
6
8
|
def method_name
|
|
7
9
|
"script"
|
|
8
10
|
end
|
|
9
11
|
|
|
10
|
-
#
|
|
12
|
+
# 返回用于唯一识别脚本的业务字段名。
|
|
13
|
+
#
|
|
14
|
+
# @return [String] 脚本对象的业务标识字段名
|
|
11
15
|
def identify
|
|
12
16
|
"name"
|
|
13
17
|
end
|
|
14
18
|
|
|
15
|
-
#
|
|
19
|
+
# 在指定主机上执行 Zabbix 脚本。
|
|
16
20
|
#
|
|
17
|
-
#
|
|
18
|
-
# execute({ scriptid: '12', hostid: '32
|
|
21
|
+
# 示例:
|
|
22
|
+
# execute({ scriptid: '12', hostid: '32 })
|
|
23
|
+
#
|
|
24
|
+
# @param data [Hash] 包含 scriptid 和 hostid 的执行参数
|
|
25
|
+
# @return [Hash] Zabbix API 返回的脚本执行结果
|
|
19
26
|
def execute(data)
|
|
20
27
|
@client.api_request(
|
|
21
28
|
method: "script.execute",
|
|
@@ -26,8 +33,11 @@ class ZabbixManager
|
|
|
26
33
|
)
|
|
27
34
|
end
|
|
28
35
|
|
|
29
|
-
#
|
|
30
|
-
|
|
36
|
+
# 查询指定主机可用的脚本。
|
|
37
|
+
#
|
|
38
|
+
# @param data [Hash] Zabbix scripts.getscriptsbyhosts 查询参数
|
|
39
|
+
# @return [Array<Hash>] 主机可用脚本列表
|
|
40
|
+
def getscriptsbyhost(data)
|
|
31
41
|
@client.api_request(method: "script.getscriptsbyhosts", params: data)
|
|
32
42
|
end
|
|
33
43
|
end
|
|
@@ -2,9 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
class ZabbixManager
|
|
4
4
|
class Server
|
|
5
|
+
# 返回连接对应的 Zabbix API 版本。
|
|
6
|
+
#
|
|
7
|
+
# @return [String] Zabbix API 版本号
|
|
5
8
|
attr_reader :version
|
|
6
9
|
|
|
7
|
-
#
|
|
10
|
+
# 使用客户端初始化服务信息,并立即读取 Zabbix API 版本。
|
|
11
|
+
#
|
|
12
|
+
# @param client [ZabbixManager::Client] 已建立配置的 Zabbix 客户端
|
|
13
|
+
# @return [Server] 已初始化的服务信息对象
|
|
8
14
|
def initialize(client)
|
|
9
15
|
@client = client
|
|
10
16
|
@version = @client.api_version
|