zabbix_manager 5.1.3 → 5.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/zabbix_manager/basic/basic_alias.rb +3 -20
- data/lib/zabbix_manager/basic/basic_func.rb +24 -46
- data/lib/zabbix_manager/basic/basic_init.rb +8 -24
- data/lib/zabbix_manager/basic/basic_logic.rb +66 -104
- data/lib/zabbix_manager/classes/actions.rb +11 -21
- data/lib/zabbix_manager/classes/applications.rb +5 -19
- data/lib/zabbix_manager/classes/configurations.rb +5 -19
- data/lib/zabbix_manager/classes/drules.rb +6 -22
- data/lib/zabbix_manager/classes/errors.rb +19 -9
- data/lib/zabbix_manager/classes/events.rb +2 -6
- data/lib/zabbix_manager/classes/graphs.rb +18 -49
- data/lib/zabbix_manager/classes/hostgroups.rb +3 -9
- data/lib/zabbix_manager/classes/hostinterfaces.rb +7 -15
- data/lib/zabbix_manager/classes/hosts.rb +62 -77
- data/lib/zabbix_manager/classes/httptests.rb +8 -24
- data/lib/zabbix_manager/classes/items.rb +19 -78
- data/lib/zabbix_manager/classes/maintenance.rb +2 -6
- data/lib/zabbix_manager/classes/mediatypes.rb +20 -52
- data/lib/zabbix_manager/classes/problems.rb +38 -66
- data/lib/zabbix_manager/classes/proxies.rb +7 -26
- data/lib/zabbix_manager/classes/roles.rb +18 -57
- data/lib/zabbix_manager/classes/screens.rb +7 -21
- data/lib/zabbix_manager/classes/scripts.rb +7 -10
- data/lib/zabbix_manager/classes/server.rb +1 -6
- data/lib/zabbix_manager/classes/templates.rb +15 -49
- data/lib/zabbix_manager/classes/triggers.rb +15 -49
- data/lib/zabbix_manager/classes/unusable.rb +2 -1
- data/lib/zabbix_manager/classes/usergroups.rb +6 -28
- data/lib/zabbix_manager/classes/usermacros.rb +19 -113
- data/lib/zabbix_manager/classes/users.rb +7 -34
- data/lib/zabbix_manager/classes/valuemaps.rb +5 -26
- data/lib/zabbix_manager/client.rb +66 -69
- data/lib/zabbix_manager/version.rb +1 -2
- data/lib/zabbix_manager.rb +33 -46
- metadata +3 -3
@@ -2,24 +2,20 @@
|
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
4
|
class Scripts < Basic
|
5
|
+
# 与 Zabbix API 交互的方法名称
|
5
6
|
def method_name
|
6
7
|
"script"
|
7
8
|
end
|
8
9
|
|
9
|
-
#
|
10
|
-
#
|
11
|
-
# @return [String]
|
10
|
+
# 用于通过 Zabbix API 标识特定脚本对象的 ID 字段名称
|
12
11
|
def identify
|
13
12
|
"name"
|
14
13
|
end
|
15
14
|
|
16
|
-
#
|
17
|
-
# data - A Hash containing the scriptid and hostid
|
18
|
-
#
|
19
|
-
# Example:
|
20
|
-
# execute({ scriptid: '12', hostid: '32 })
|
15
|
+
# 执行 Zabbix API 中特定主机上的脚本
|
21
16
|
#
|
22
|
-
#
|
17
|
+
# 示例:
|
18
|
+
# execute({ scriptid: '12', hostid: '32' })
|
23
19
|
def execute(data)
|
24
20
|
@client.api_request(
|
25
21
|
method: "script.execute",
|
@@ -30,7 +26,8 @@ class ZabbixManager
|
|
30
26
|
)
|
31
27
|
end
|
32
28
|
|
33
|
-
|
29
|
+
# 使用 Zabbix API 通过主机获取脚本
|
30
|
+
def get_scripts_by_host(data)
|
34
31
|
@client.api_request(method: "script.getscriptsbyhosts", params: data)
|
35
32
|
end
|
36
33
|
end
|
@@ -2,14 +2,9 @@
|
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
4
|
class Server
|
5
|
-
# @return [String]
|
6
5
|
attr_reader :version
|
7
6
|
|
8
|
-
#
|
9
|
-
#
|
10
|
-
# @param client [ZabbixManager::Client]
|
11
|
-
# @return [ZabbixManager::Client]
|
12
|
-
# @return [String] Zabbix API version number
|
7
|
+
# 初始化一个新的 Server 对象,使用 ZabbixManager 客户端,并获取 Zabbix 服务器 API 版本
|
13
8
|
def initialize(client)
|
14
9
|
@client = client
|
15
10
|
@version = @client.api_version
|
@@ -2,63 +2,39 @@
|
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
4
|
class Templates < Basic
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# @return [String]
|
5
|
+
# Zabbix API 中与模板交互的方法名称
|
8
6
|
def method_name
|
9
7
|
"template"
|
10
8
|
end
|
11
9
|
|
12
|
-
#
|
13
|
-
#
|
14
|
-
# @return [String]
|
10
|
+
# 用于通过 Zabbix API 标识特定模板对象的 id 字段名称
|
15
11
|
def identify
|
16
12
|
"host"
|
17
13
|
end
|
18
14
|
|
19
|
-
#
|
20
|
-
#
|
21
|
-
# @param data [Array] Should include array of templateid's
|
22
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
23
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
24
|
-
# @return [Integer, NilClass] The Template object id that was deleted
|
15
|
+
# 通过 Zabbix API 删除模板对象
|
25
16
|
def delete(data)
|
26
17
|
result = @client.api_request(method: "template.delete", params: [data])
|
27
18
|
result.empty? ? nil : result["templateids"][0].to_i
|
28
19
|
end
|
29
20
|
|
30
|
-
#
|
31
|
-
#
|
32
|
-
# @param data [Hash] Should include host value to query for matching templates
|
33
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
34
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
35
|
-
# @return [Array] Returns array of Template ids
|
21
|
+
# 从 Zabbix API 获取主机的模板 id
|
36
22
|
def get_ids_by_host(data)
|
37
23
|
@client.api_request(method: "template.get", params: data).map do |tmpl|
|
38
24
|
tmpl["templateid"]
|
39
25
|
end
|
40
26
|
end
|
41
27
|
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
#
|
46
|
-
#
|
47
|
-
#
|
48
|
-
|
49
|
-
|
50
|
-
templateid
|
51
|
-
else
|
52
|
-
create(data)
|
53
|
-
end
|
54
|
-
end
|
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
|
55
36
|
|
56
|
-
#
|
57
|
-
#
|
58
|
-
# @param data [Hash] Should include hosts_id array and templates_id array
|
59
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
60
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
61
|
-
# @return [Boolean]
|
37
|
+
# 通过 Zabbix API 批量更新主机的模板
|
62
38
|
def mass_update(data)
|
63
39
|
result = @client.api_request(
|
64
40
|
method: "template.massUpdate",
|
@@ -70,12 +46,7 @@ class ZabbixManager
|
|
70
46
|
result.empty? ? false : true
|
71
47
|
end
|
72
48
|
|
73
|
-
#
|
74
|
-
#
|
75
|
-
# @param data [Hash] Should include hosts_id array and templates_id array
|
76
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
77
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
78
|
-
# @return [Boolean]
|
49
|
+
# 通过 Zabbix API 批量添加模板到主机
|
79
50
|
def mass_add(data)
|
80
51
|
result = @client.api_request(
|
81
52
|
method: "template.massAdd",
|
@@ -87,12 +58,7 @@ class ZabbixManager
|
|
87
58
|
result.empty? ? false : true
|
88
59
|
end
|
89
60
|
|
90
|
-
#
|
91
|
-
#
|
92
|
-
# @param data [Hash] Should include hosts_id array and templates_id array
|
93
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
94
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
95
|
-
# @return [Boolean]
|
61
|
+
# 通过 Zabbix API 批量从主机移除模板
|
96
62
|
def mass_remove(data)
|
97
63
|
result = @client.api_request(
|
98
64
|
method: "template.massRemove",
|
@@ -2,24 +2,17 @@
|
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
4
|
class Triggers < Basic
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# @return [String]
|
5
|
+
# 与 Zabbix API 交互的触发器方法名称
|
8
6
|
def method_name
|
9
7
|
"trigger"
|
10
8
|
end
|
11
9
|
|
12
|
-
#
|
13
|
-
#
|
14
|
-
# @return [String]
|
10
|
+
# 用于通过 Zabbix API 标识特定触发器对象的 id 字段名称
|
15
11
|
def identify
|
16
12
|
"description"
|
17
13
|
end
|
18
14
|
|
19
|
-
#
|
20
|
-
# @note 缺省的 trigger 参数
|
21
|
-
# @see https://www.zabbix.com/documentation/current/en/manual/api/reference/trigger/get
|
22
|
-
# @return [Hash]
|
15
|
+
# 创建 Zabbix 触发器的默认选项
|
23
16
|
def default_options
|
24
17
|
{
|
25
18
|
recovery_mode: 1,
|
@@ -28,18 +21,12 @@ class ZabbixManager
|
|
28
21
|
}
|
29
22
|
end
|
30
23
|
|
31
|
-
#
|
32
|
-
#
|
33
|
-
# @param data [Hash] Should include desired object's key and value
|
34
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
35
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
36
|
-
# @return [Hash]
|
24
|
+
# 通过键值对从 Zabbix API 转储触发器对象数据
|
37
25
|
def dump_by_id(data)
|
38
|
-
log "[DEBUG]
|
26
|
+
log "[DEBUG] 调用 dump_by_id 方法,参数: #{data.inspect}"
|
39
27
|
|
40
|
-
|
41
|
-
|
42
|
-
params: {
|
28
|
+
get_raw(
|
29
|
+
{
|
43
30
|
triggerids: data[keys.to_sym],
|
44
31
|
output: "extend",
|
45
32
|
select_items: "extend",
|
@@ -48,14 +35,9 @@ class ZabbixManager
|
|
48
35
|
)
|
49
36
|
end
|
50
37
|
|
51
|
-
#
|
52
|
-
#
|
53
|
-
# @param data [Hash] Needs to include description and hostid to properly identify Triggers via Zabbix API
|
54
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
55
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
56
|
-
# @return [Integer, TrueClass, FalseClass, NilClass] Zabbix object id
|
38
|
+
# 安全地使用 Zabbix API 更新触发器对象,通过删除和替换触发器
|
57
39
|
def safe_update(data)
|
58
|
-
log "[DEBUG]
|
40
|
+
log "[DEBUG] 调用 safe_update 方法,参数: #{data.inspect}"
|
59
41
|
|
60
42
|
dump = {}
|
61
43
|
item_id = data[key.to_sym].to_i
|
@@ -75,7 +57,7 @@ class ZabbixManager
|
|
75
57
|
log "[DEBUG] expression: #{dump[:expression]}\n data: #{data[:expression]}"
|
76
58
|
|
77
59
|
if hash_equals?(dump, data)
|
78
|
-
log "[DEBUG]
|
60
|
+
log "[DEBUG] 键 #{dump} 和 #{data} 相等,跳过 safe_update"
|
79
61
|
item_id
|
80
62
|
else
|
81
63
|
data[:expression] = old_expression
|
@@ -90,14 +72,9 @@ class ZabbixManager
|
|
90
72
|
end
|
91
73
|
end
|
92
74
|
|
93
|
-
#
|
94
|
-
#
|
95
|
-
# @param data [Hash] Needs to include description and hostid to properly identify Triggers via Zabbix API
|
96
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
97
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
98
|
-
# @return [Integer, TrueClass, FalseClass, NilClass] Zabbix object id
|
75
|
+
# 通过 Zabbix API 获取或创建触发器对象
|
99
76
|
def get_or_create(data)
|
100
|
-
log "[DEBUG]
|
77
|
+
log "[DEBUG] 调用 #{method_name}.get_or_create 方法,参数: #{data.inspect}"
|
101
78
|
|
102
79
|
if (id = get_id(description: data[:description], hostid: data[:hostid]))
|
103
80
|
id
|
@@ -106,26 +83,15 @@ class ZabbixManager
|
|
106
83
|
end
|
107
84
|
end
|
108
85
|
|
109
|
-
#
|
110
|
-
#
|
111
|
-
# @param data [Hash] Needs to include description and hostid to properly identify Triggers via Zabbix API
|
112
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
113
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
114
|
-
# @return [Integer, NilClass, TrueClass, FalseClass] Zabbix object id
|
86
|
+
# 使用 Zabbix API 创建或更新触发器对象
|
115
87
|
def create_or_update(data)
|
116
88
|
triggerid = get_id(description: data[:description], hostid: data[:hostid])
|
117
89
|
triggerid ? update(data.merge(triggerid: triggerid)) : create(data)
|
118
90
|
end
|
119
91
|
|
120
|
-
#
|
121
|
-
#
|
122
|
-
# @param triggerid [Integer] identify for trigger via Zabbix API
|
123
|
-
# @param depend_on_trigger_id [Integer] identify for dependsOnTrigger via Zabbix API
|
124
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
125
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
126
|
-
# @return [Integer, NilClass] Zabbix object id
|
92
|
+
# 使用 Zabbix API 添加触发器依赖关系
|
127
93
|
def add_trigger_dependency(triggerid, depend_on_trigger_id)
|
128
|
-
log "[DEBUG]
|
94
|
+
log "[DEBUG] 调用 add_trigger_dependency 方法,参数: #{triggerid} #{depend_on_trigger_id}"
|
129
95
|
|
130
96
|
result = @client.api_request(
|
131
97
|
method: "trigger.adddependencies",
|
@@ -2,8 +2,9 @@
|
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
4
|
class Triggers < Basic
|
5
|
+
# 使用 Zabbix API 创建或更新触发器对象
|
5
6
|
def create_or_update(data)
|
6
|
-
log "[DEBUG]
|
7
|
+
log "[DEBUG] 调用 create_or_update 方法,参数: #{data.inspect}"
|
7
8
|
get_or_create(data)
|
8
9
|
end
|
9
10
|
end
|
@@ -2,33 +2,22 @@
|
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
4
|
class Usergroups < Basic
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# @return [String]
|
5
|
+
# 用于通过 Zabbix API 与 Usergroups 交互的方法名称
|
8
6
|
def method_name
|
9
7
|
"usergroup"
|
10
8
|
end
|
11
9
|
|
12
|
-
#
|
13
|
-
#
|
14
|
-
# @return [String]
|
10
|
+
# 用于 Usergroup 对象通过 Zabbix API 的键字段名称
|
15
11
|
def key
|
16
12
|
"usrgrpid"
|
17
13
|
end
|
18
14
|
|
19
|
-
#
|
20
|
-
#
|
21
|
-
# @return [String]
|
15
|
+
# 用于通过 Zabbix API 识别特定 Usergroup 对象的 id 字段名称
|
22
16
|
def identify
|
23
17
|
"name"
|
24
18
|
end
|
25
19
|
|
26
|
-
#
|
27
|
-
#
|
28
|
-
# @param data [Hash] Needs to include usrgrpids and hostgroupids along with permissions to set
|
29
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
30
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
31
|
-
# @return [Integer, NilClass] Zabbix object id (usergroup)
|
20
|
+
# 使用 Zabbix API 设置用户组的权限
|
32
21
|
def permissions(data)
|
33
22
|
permission = data[:permission] || 2
|
34
23
|
result = @client.api_request(
|
@@ -41,23 +30,12 @@ class ZabbixManager
|
|
41
30
|
result ? result["usrgrpids"][0].to_i : nil
|
42
31
|
end
|
43
32
|
|
44
|
-
#
|
45
|
-
#
|
46
|
-
# @deprecated Zabbix has removed massAdd in favor of update.
|
47
|
-
# @param data [Hash] Needs to include userids and usrgrpids to mass add users to groups
|
48
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
49
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
50
|
-
# @return [Integer, NilClass] Zabbix object id (usergroup)
|
33
|
+
# 使用 Zabbix API 将用户添加到用户组
|
51
34
|
def add_user(data)
|
52
35
|
update_users(data)
|
53
36
|
end
|
54
37
|
|
55
|
-
#
|
56
|
-
#
|
57
|
-
# @param data [Hash] Needs to include userids and usrgrpids to mass update users in groups
|
58
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
59
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
60
|
-
# @return [Integer, NilClass] Zabbix object id (usergroup)
|
38
|
+
# 使用 Zabbix API 更新用户组中的用户
|
61
39
|
def update_users(data)
|
62
40
|
user_groups = data[:usrgrpids].map do |t|
|
63
41
|
{
|
@@ -2,160 +2,89 @@
|
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
4
|
class Usermacros < Basic
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# @return [String]
|
5
|
+
# 用于通过 Zabbix API 识别特定 User 宏对象的 id 字段名称
|
8
6
|
def identify
|
9
7
|
"macro"
|
10
8
|
end
|
11
9
|
|
12
|
-
#
|
13
|
-
#
|
14
|
-
# @return [String]
|
10
|
+
# 用于通过 Zabbix API 与 User 宏交互的方法名称
|
15
11
|
def method_name
|
16
12
|
"usermacro"
|
17
13
|
end
|
18
14
|
|
19
|
-
#
|
20
|
-
#
|
21
|
-
# @param data [Hash] Needs to include macro to properly identify user macros via Zabbix API
|
22
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call or missing object's id field name (identify).
|
23
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
24
|
-
# @return [Integer] Zabbix object id
|
15
|
+
# 通过 Zabbix API 根据提供的数据获取 User 宏对象的 id
|
25
16
|
def get_id(data)
|
26
17
|
log "[DEBUG] Call get_id with parameters: #{data.inspect}"
|
27
18
|
|
28
|
-
# symbolize keys if the user used string keys instead of symbols
|
29
19
|
data = symbolize_keys(data) if data.key?(identify)
|
30
|
-
# raise an error if identify name was not supplied
|
31
20
|
name = data[identify.to_sym]
|
32
21
|
raise ZbxError, "#{identify} not supplied in call to get_id" if name.nil?
|
33
22
|
|
34
23
|
result = request(data, "usermacro.get", "hostmacroid")
|
35
24
|
|
36
|
-
!result
|
25
|
+
!result&.empty? && result[0].key?("hostmacroid") ? result[0]["hostmacroid"].to_i : nil
|
37
26
|
end
|
38
27
|
|
39
|
-
#
|
40
|
-
#
|
41
|
-
# @param data [Hash] Needs to include macro to properly identify global macros via Zabbix API
|
42
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call or missing object's id field name (identify).
|
43
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
44
|
-
# @return [Integer] Zabbix object id
|
28
|
+
# 通过 Zabbix API 根据提供的数据获取 Global 宏对象的 id
|
45
29
|
def get_id_global(data)
|
46
30
|
log "[DEBUG] Call get_id_global with parameters: #{data.inspect}"
|
47
31
|
|
48
|
-
# symbolize keys if the user used string keys instead of symbols
|
49
32
|
data = symbolize_keys(data) if data.key?(identify)
|
50
|
-
# raise an error if identify name was not supplied
|
51
33
|
name = data[identify.to_sym]
|
52
34
|
raise ZbxError, "#{identify} not supplied in call to get_id_global" if name.nil?
|
53
35
|
|
54
36
|
result = request(data, "usermacro.get", "globalmacroid")
|
55
37
|
|
56
|
-
!result
|
38
|
+
!result&.empty? && result[0].key?("globalmacroid") ? result[0]["globalmacroid"].to_i : nil
|
57
39
|
end
|
58
40
|
|
59
|
-
#
|
60
|
-
#
|
61
|
-
# @param data [Hash] Should include object's id field name (identify) and id value
|
62
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
63
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
64
|
-
# @return [Hash]
|
41
|
+
# 通过 Zabbix API 获取完整/扩展的 User 宏数据
|
65
42
|
def get_full_data(data)
|
66
43
|
log "[DEBUG] Call get_full_data with parameters: #{data.inspect}"
|
67
44
|
|
68
45
|
request(data, "usermacro.get", "hostmacroid")
|
69
46
|
end
|
70
47
|
|
71
|
-
#
|
72
|
-
#
|
73
|
-
# @param data [Hash] Should include object's id field name (identify) and id value
|
74
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
75
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
76
|
-
# @return [Hash]
|
48
|
+
# 通过 Zabbix API 获取完整/扩展的 Global 宏数据
|
77
49
|
def get_full_data_global(data)
|
78
50
|
log "[DEBUG] Call get_full_data_global with parameters: #{data.inspect}"
|
79
51
|
|
80
52
|
request(data, "usermacro.get", "globalmacroid")
|
81
53
|
end
|
82
54
|
|
83
|
-
#
|
84
|
-
#
|
85
|
-
# @param data [Hash] Needs to include hostid, macro, and value to create User macro via Zabbix API
|
86
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
87
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
88
|
-
# @return [Integer] The object id if a single object is created
|
89
|
-
# @return [Boolean] True/False if multiple objects are created
|
55
|
+
# 通过 Zabbix API 创建新的 User 宏对象(使用默认值)
|
90
56
|
def create(data)
|
91
57
|
request(data, "usermacro.create", "hostmacroids")
|
92
58
|
end
|
93
59
|
|
94
|
-
#
|
95
|
-
#
|
96
|
-
# @param data [Hash] Needs to include hostid, macro, and value to create Global macro via Zabbix API
|
97
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
98
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
99
|
-
# @return [Integer] The object id if a single object is created
|
100
|
-
# @return [Boolean] True/False if multiple objects are created
|
60
|
+
# 通过 Zabbix API 创建新的 Global 宏对象(使用默认值)
|
101
61
|
def create_global(data)
|
102
62
|
request(data, "usermacro.createglobal", "globalmacroids")
|
103
63
|
end
|
104
64
|
|
105
|
-
#
|
106
|
-
#
|
107
|
-
# @param data [Hash] Should include hostmacroid's of User macros to delete
|
108
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
109
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
110
|
-
# @return [Integer] The object id if a single object is deleted
|
111
|
-
# @return [Boolean] True/False if multiple objects are deleted
|
65
|
+
# 通过 Zabbix API 删除 User 宏对象
|
112
66
|
def delete(data)
|
113
67
|
data_delete = [data]
|
114
68
|
request(data_delete, "usermacro.delete", "hostmacroids")
|
115
69
|
end
|
116
70
|
|
117
|
-
#
|
118
|
-
#
|
119
|
-
# @param data [Hash] Should include hostmacroid's of Global macros to delete
|
120
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
121
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
122
|
-
# @return [Integer] The object id if a single object is deleted
|
123
|
-
# @return [Boolean] True/False if multiple objects are deleted
|
71
|
+
# 通过 Zabbix API 删除 Global 宏对象
|
124
72
|
def delete_global(data)
|
125
73
|
data_delete = [data]
|
126
74
|
request(data_delete, "usermacro.deleteglobal", "globalmacroids")
|
127
75
|
end
|
128
76
|
|
129
|
-
#
|
130
|
-
#
|
131
|
-
# @param data [Hash] Should include object's id field name (identify), id value, and fields to update
|
132
|
-
# @param force [Boolean] Whether to force an object update even if provided data matches Zabbix
|
133
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
134
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
135
|
-
# @return [Integer] The object id if a single object is created
|
136
|
-
# @return [Boolean] True/False if multiple objects are created
|
77
|
+
# 通过 Zabbix API 更新 User 宏对象
|
137
78
|
def update(data)
|
138
79
|
request(data, "usermacro.update", "hostmacroids")
|
139
80
|
end
|
140
81
|
|
141
|
-
#
|
142
|
-
#
|
143
|
-
# @param data [Hash] Should include object's id field name (identify), id value, and fields to update
|
144
|
-
# @param force [Boolean] Whether to force an object update even if provided data matches Zabbix
|
145
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
146
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
147
|
-
# @return [Integer] The object id if a single object is created
|
148
|
-
# @return [Boolean] True/False if multiple objects are created
|
82
|
+
# 通过 Zabbix API 更新 Global 宏对象
|
149
83
|
def update_global(data)
|
150
84
|
request(data, "usermacro.updateglobal", "globalmacroids")
|
151
85
|
end
|
152
86
|
|
153
|
-
#
|
154
|
-
#
|
155
|
-
# @param data [Hash] Needs to include macro and hostid to properly identify User macros via Zabbix API
|
156
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
157
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
158
|
-
# @return [Integer] Zabbix object id
|
87
|
+
# 通过 Zabbix API 获取或创建 User 宏对象
|
159
88
|
def get_or_create(data)
|
160
89
|
log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
|
161
90
|
|
@@ -165,12 +94,7 @@ class ZabbixManager
|
|
165
94
|
id
|
166
95
|
end
|
167
96
|
|
168
|
-
#
|
169
|
-
#
|
170
|
-
# @param data [Hash] Needs to include macro and hostid to properly identify Global macros via Zabbix API
|
171
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
172
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
173
|
-
# @return [Integer] Zabbix object id
|
97
|
+
# 通过 Zabbix API 获取或创建 Global 宏对象
|
174
98
|
def get_or_create_global(data)
|
175
99
|
log "[DEBUG] Call get_or_create_global with parameters: #{data.inspect}"
|
176
100
|
|
@@ -180,23 +104,13 @@ class ZabbixManager
|
|
180
104
|
id
|
181
105
|
end
|
182
106
|
|
183
|
-
#
|
184
|
-
#
|
185
|
-
# @param data [Hash] Needs to include macro and hostid to properly identify User macros via Zabbix API
|
186
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
187
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
188
|
-
# @return [Integer, TrueClass, FalseClass, NilClass] Zabbix object id
|
107
|
+
# 通过 Zabbix API 创建或更新 User 宏对象
|
189
108
|
def create_or_update(data)
|
190
109
|
hostmacroid = get_id(macro: data[:macro], hostid: data[:hostid])
|
191
110
|
hostmacroid ? update(data.merge(hostmacroid: hostmacroid)) : create(data)
|
192
111
|
end
|
193
112
|
|
194
|
-
#
|
195
|
-
#
|
196
|
-
# @param data [Hash] Needs to include macro and hostid to properly identify Global macros via Zabbix API
|
197
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
198
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
199
|
-
# @return [Integer, TrueClass, FalseClass, NilClass] Zabbix object id
|
113
|
+
# 通过 Zabbix API 创建或更新 Global 宏对象
|
200
114
|
def create_or_update_global(data)
|
201
115
|
globalmacroid = get_id_global(macro: data[:macro], hostid: data[:hostid])
|
202
116
|
globalmacroid ? update_global(data.merge(globalmacroid: globalmacroid)) : create_global(data)
|
@@ -204,16 +118,8 @@ class ZabbixManager
|
|
204
118
|
|
205
119
|
private
|
206
120
|
|
207
|
-
#
|
208
|
-
#
|
209
|
-
# @param data [Hash] Needs to include macro and hostid to properly identify Global macros via Zabbix API
|
210
|
-
# @param method [String] Zabbix API method to use for the request
|
211
|
-
# @param result_key [String] Which key to use for parsing results based on User vs Global macros
|
212
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
213
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
214
|
-
# @return [Integer, TrueClass, FalseClass, NilClass] Zabbix object id
|
121
|
+
# 自定义请求方法,以处理 User 和 Global 宏
|
215
122
|
def request(data, method, result_key)
|
216
|
-
# Zabbix has different result formats for gets vs updates
|
217
123
|
if method.include?(".get")
|
218
124
|
if result_key.include?("global")
|
219
125
|
@client.api_request(method: method, params: { globalmacro: true, filter: data })
|
@@ -2,40 +2,23 @@
|
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
4
|
class Users < Basic
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# @return [String]
|
5
|
+
# 用于通过 Zabbix API 与 Users 交互的方法名称
|
8
6
|
def method_name
|
9
7
|
"user"
|
10
8
|
end
|
11
9
|
|
12
|
-
#
|
13
|
-
#
|
14
|
-
# @return [String]
|
15
|
-
def keys
|
16
|
-
"userids"
|
17
|
-
end
|
18
|
-
|
19
|
-
# The key field name used for User objects via Zabbix API
|
20
|
-
#
|
21
|
-
# @return [String]
|
22
|
-
def key
|
23
|
-
"userid"
|
24
|
-
end
|
25
|
-
|
26
|
-
# The id field name used for identifying specific User objects via Zabbix API
|
27
|
-
#
|
28
|
-
# @return [String]
|
10
|
+
# 用于通过 Zabbix API 识别特定 User 对象的 id 字段名称
|
29
11
|
def identify
|
30
12
|
"alias"
|
31
13
|
end
|
32
14
|
|
15
|
+
# 用于辅助处理添加或更新用户媒体信息的方法
|
33
16
|
def medias_helper(data, action)
|
34
17
|
result = @client.api_request(
|
35
18
|
method: "user.#{action}",
|
36
|
-
params: data[:userids].map do |
|
19
|
+
params: data[:userids].map do |i|
|
37
20
|
{
|
38
|
-
userid:
|
21
|
+
userid: i,
|
39
22
|
user_medias: data[:media]
|
40
23
|
}
|
41
24
|
end
|
@@ -43,22 +26,12 @@ class ZabbixManager
|
|
43
26
|
result ? result["userids"][0].to_i : nil
|
44
27
|
end
|
45
28
|
|
46
|
-
#
|
47
|
-
#
|
48
|
-
# @param data [Hash] Needs to include userids and media to mass add media to users
|
49
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
50
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
51
|
-
# @return [Integer, NilClass] Zabbix object id (media)
|
29
|
+
# 通过 Zabbix API 为用户添加媒体信息
|
52
30
|
def add_medias(data)
|
53
31
|
medias_helper(data, "update")
|
54
32
|
end
|
55
33
|
|
56
|
-
#
|
57
|
-
#
|
58
|
-
# @param data [Hash] Needs to include userids and media to mass update media for users
|
59
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
60
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
61
|
-
# @return [Integer, NilClass] Zabbix object id (user)
|
34
|
+
# 通过 Zabbix API 更新用户媒体信息
|
62
35
|
def update_medias(data)
|
63
36
|
medias_helper(data, "update")
|
64
37
|
end
|