zabbix_manager 5.1.3 → 5.1.5
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/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,23 +2,17 @@
|
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
4
|
class Drules < Basic
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# @return [String]
|
5
|
+
# 用于通过 Zabbix API 与 Drules 交互的方法名称
|
8
6
|
def method_name
|
9
7
|
"drule"
|
10
8
|
end
|
11
9
|
|
12
|
-
#
|
13
|
-
#
|
14
|
-
# @return [String]
|
10
|
+
# 用于通过 Zabbix API 标识特定 Drule 对象的 id 字段名称
|
15
11
|
def identify
|
16
12
|
"name"
|
17
13
|
end
|
18
14
|
|
19
|
-
#
|
20
|
-
#
|
21
|
-
# @return [Hash]
|
15
|
+
# 创建 Drule 对象时使用的默认选项
|
22
16
|
def default_options
|
23
17
|
{
|
24
18
|
name: nil,
|
@@ -28,14 +22,9 @@ class ZabbixManager
|
|
28
22
|
}
|
29
23
|
end
|
30
24
|
|
31
|
-
#
|
32
|
-
#
|
33
|
-
# @param data [Hash] Needs to include name to properly identify Drule via Zabbix API
|
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 [Integer] Zabbix object id
|
25
|
+
# 通过 Zabbix API 获取或创建 Drule 对象
|
37
26
|
def get_or_create(data)
|
38
|
-
log "[DEBUG]
|
27
|
+
log "[DEBUG] 调用 get_or_create,参数为:#{data.inspect}"
|
39
28
|
|
40
29
|
unless (id = get_id(name: data[:name]))
|
41
30
|
id = create(data)
|
@@ -43,12 +32,7 @@ class ZabbixManager
|
|
43
32
|
id
|
44
33
|
end
|
45
34
|
|
46
|
-
#
|
47
|
-
#
|
48
|
-
# @param data [Hash] Needs to include name to properly identify Drules via Zabbix API
|
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, TrueClass, FalseClass] Zabbix object id
|
35
|
+
# 通过 Zabbix API 创建或更新 Drule 对象
|
52
36
|
def create_or_update(data)
|
53
37
|
druleid = get_id(name: data[:name])
|
54
38
|
druleid ? update(data.merge(druleid: druleid)) : create(data)
|
@@ -1,29 +1,39 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
|
+
# BaseError 是 ZabbixManager 模块中处理异常的基类
|
4
5
|
class BaseError < RuntimeError
|
5
|
-
|
6
|
+
# 用于存储响应对象
|
7
|
+
attr_accessor :response
|
8
|
+
# 用于存储 Zabbix API 错误信息
|
9
|
+
attr_accessor :error
|
10
|
+
# 用于存储格式化后的错误消息
|
11
|
+
attr_accessor :error_message
|
6
12
|
|
13
|
+
# 初始化异常对象
|
7
14
|
def initialize(message, response = nil)
|
8
15
|
super(message)
|
9
16
|
@response = response
|
10
|
-
|
11
17
|
set_error! if @response
|
12
18
|
end
|
13
19
|
|
14
20
|
private
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
21
|
+
|
22
|
+
# 从响应中提取 Zabbix API 错误信息
|
23
|
+
def set_error!
|
24
|
+
@error = @response["error"]
|
25
|
+
@error_message = "#{@error["message"]}: #{@error["data"]}"
|
26
|
+
rescue StandardError
|
27
|
+
@error = nil
|
28
|
+
@error_message = nil
|
29
|
+
end
|
22
30
|
end
|
23
31
|
|
32
|
+
# ZbxError 是 ZabbixManager 模块中处理 Zabbix API 异常的类
|
24
33
|
class ZbxError < BaseError
|
25
34
|
end
|
26
35
|
|
36
|
+
# HttpError 是 ZabbixManager 模块中处理 HTTP 异常的类
|
27
37
|
class HttpError < BaseError
|
28
38
|
end
|
29
39
|
end
|
@@ -2,16 +2,12 @@
|
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
4
|
class Events < Basic
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# @return [String]
|
5
|
+
# 用于通过 Zabbix API 与 Events 交互的方法名称
|
8
6
|
def method_name
|
9
7
|
"event"
|
10
8
|
end
|
11
9
|
|
12
|
-
#
|
13
|
-
#
|
14
|
-
# @return [String]
|
10
|
+
# 用于通过 Zabbix API 标识特定 Event 对象的 id 字段名称
|
15
11
|
def identify
|
16
12
|
"name"
|
17
13
|
end
|
@@ -2,32 +2,22 @@
|
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
4
|
class Graphs < Basic
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# @return [String]
|
5
|
+
# 用于通过 Zabbix API 与 Graphs 交互的方法名称
|
8
6
|
def method_name
|
9
7
|
"graph"
|
10
8
|
end
|
11
9
|
|
12
|
-
#
|
13
|
-
#
|
14
|
-
# @return [String]
|
10
|
+
# 用于通过 Zabbix API 标识特定 Graph 对象的 id 字段名称
|
15
11
|
def identify
|
16
12
|
"name"
|
17
13
|
end
|
18
14
|
|
19
|
-
#
|
20
|
-
#
|
21
|
-
# @param data [Hash] Should include object's id field name (identify) and id value
|
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 [Hash]
|
15
|
+
# 从 Zabbix API 获取完整/扩展的 Graph 数据
|
25
16
|
def get_full_data(data)
|
26
|
-
log "[DEBUG]
|
17
|
+
log "[DEBUG] 使用参数调用 get_full_data: #{data.inspect}"
|
27
18
|
|
28
|
-
|
29
|
-
|
30
|
-
params: {
|
19
|
+
get_raw(
|
20
|
+
{
|
31
21
|
search: {
|
32
22
|
identify.to_sym => data[identify.to_sym]
|
33
23
|
},
|
@@ -36,16 +26,10 @@ class ZabbixManager
|
|
36
26
|
)
|
37
27
|
end
|
38
28
|
|
39
|
-
#
|
40
|
-
#
|
41
|
-
# @param data [Hash] Should include host value to query for matching graphs
|
42
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
43
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
44
|
-
# @return [Array] Returns array of Graph ids
|
29
|
+
# 从 Zabbix API 获取主机的 Graph ids
|
45
30
|
def get_ids_by_host(data)
|
46
|
-
result =
|
47
|
-
|
48
|
-
params: {
|
31
|
+
result = get_raw(
|
32
|
+
{
|
49
33
|
filter: {
|
50
34
|
host: data[:host]
|
51
35
|
},
|
@@ -54,38 +38,28 @@ class ZabbixManager
|
|
54
38
|
)
|
55
39
|
|
56
40
|
result.filter_map do |graph|
|
57
|
-
|
58
|
-
name
|
59
|
-
filter
|
41
|
+
graphid = graph["graphid"]
|
42
|
+
name = graph["name"]
|
43
|
+
filter = data[:filter]
|
60
44
|
|
61
|
-
|
45
|
+
graphid if filter.nil? || name.match?(/#{filter}/)
|
62
46
|
end
|
63
47
|
end
|
64
48
|
|
65
|
-
#
|
66
|
-
#
|
67
|
-
# @param data [Hash] Needs to include graphids to properly identify Graph Items via Zabbix API
|
68
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
69
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
70
|
-
# @return [Hash]
|
49
|
+
# 通过 Zabbix API 获取 Graph Item 对象
|
71
50
|
def get_items(data)
|
72
51
|
@client.api_request(
|
73
52
|
method: "graphitem.get",
|
74
53
|
params: {
|
75
54
|
graphids: [data],
|
76
|
-
output:
|
55
|
+
output: "extend"
|
77
56
|
}
|
78
57
|
)
|
79
58
|
end
|
80
59
|
|
81
|
-
#
|
82
|
-
#
|
83
|
-
# @param data [Hash] Needs to include name and templateid to properly identify Graphs via Zabbix API
|
84
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
85
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
86
|
-
# @return [Integer] Zabbix object id
|
60
|
+
# 通过 Zabbix API 获取或创建 Graph 对象
|
87
61
|
def get_or_create(data)
|
88
|
-
log "[DEBUG]
|
62
|
+
log "[DEBUG] 使用参数调用 get_or_create: #{data.inspect}"
|
89
63
|
|
90
64
|
unless (id = get_id(name: data[:name], templateid: data[:templateid]))
|
91
65
|
id = create(data)
|
@@ -94,12 +68,7 @@ class ZabbixManager
|
|
94
68
|
id
|
95
69
|
end
|
96
70
|
|
97
|
-
#
|
98
|
-
#
|
99
|
-
# @param data [Hash] Needs to include name and templateid to properly identify Graphs via Zabbix API
|
100
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
101
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
102
|
-
# @return [Integer, TrueClass, FalseClass] Zabbix object id
|
71
|
+
# 通过 Zabbix API 创建或更新 Graph 对象
|
103
72
|
def create_or_update(data)
|
104
73
|
graphid = get_id(name: data[:name], templateid: data[:templateid])
|
105
74
|
graphid ? _update(data.merge(graphid: graphid)) : create(data)
|
@@ -2,23 +2,17 @@
|
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
4
|
class HostGroups < Basic
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# @return [String]
|
5
|
+
# 用于通过 Zabbix API 与 HostGroups 交互的方法名称
|
8
6
|
def method_name
|
9
7
|
"hostgroup"
|
10
8
|
end
|
11
9
|
|
12
|
-
#
|
13
|
-
#
|
14
|
-
# @return [String]
|
10
|
+
# 用于通过 Zabbix API 标识特定 HostGroup 对象的 id 字段名称
|
15
11
|
def identify
|
16
12
|
"name"
|
17
13
|
end
|
18
14
|
|
19
|
-
#
|
20
|
-
#
|
21
|
-
# @return [String]
|
15
|
+
# HostGroup 对象通过 Zabbix API 使用的 key 字段名称
|
22
16
|
def key
|
23
17
|
"groupid"
|
24
18
|
end
|
@@ -2,35 +2,27 @@
|
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
4
|
class HostInterfaces < Basic
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# @return [String]
|
5
|
+
# 用于通过 Zabbix API 与 HostInterfaces 交互的方法名称
|
8
6
|
def method_name
|
9
7
|
"hostinterface"
|
10
8
|
end
|
11
9
|
|
12
|
-
#
|
13
|
-
#
|
14
|
-
# @return [String]
|
10
|
+
# 用于通过 Zabbix API 标识特定 HostInterface 对象的 id 字段名称
|
15
11
|
def identify
|
16
12
|
"hostids"
|
17
13
|
end
|
18
14
|
|
19
|
-
#
|
20
|
-
#
|
21
|
-
# @return [String]
|
15
|
+
# HostInterface 对象通过 Zabbix API 使用的 key 字段名称
|
22
16
|
def key
|
23
17
|
"interfaceid"
|
24
18
|
end
|
25
19
|
|
26
|
-
#
|
20
|
+
# 通过 Zabbix API 基于提供的 hostid 获取 Zabbix interface 的 id
|
27
21
|
# @note 基于 hostid 查询关联的 Interfaceid
|
28
|
-
# @param hostid [Integer, String]
|
29
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call or missing object's id field name (identify).
|
30
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
31
|
-
# @return [Integer, NilClass] Zabbix object id
|
32
22
|
def get_interfaceid(hostid)
|
33
|
-
result = get_raw({ output: key,
|
23
|
+
result = get_raw({ output: key, identify.to_sym => hostid })
|
24
|
+
|
25
|
+
# 如果查询结果不为空,且第一个元素不为空,则继续获取 {interfaceid} 并转换为整数
|
34
26
|
result&.[](0)&.[](key)&.to_i
|
35
27
|
end
|
36
28
|
end
|
@@ -2,44 +2,17 @@
|
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
4
|
class Hosts < Basic
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# @return [String]
|
5
|
+
# 与 Zabbix API 交互的主机方法名称
|
8
6
|
def method_name
|
9
7
|
"host"
|
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 [Hash] Should include desired object's key and value
|
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 [Hash]
|
25
|
-
def dump_by_id(data)
|
26
|
-
log "[DEBUG] Call dump_by_id with parameters: #{data.inspect}"
|
27
|
-
|
28
|
-
@client.api_request(
|
29
|
-
method: "host.get",
|
30
|
-
params: {
|
31
|
-
filter: {
|
32
|
-
key.to_sym => data[key.to_sym]
|
33
|
-
},
|
34
|
-
output: "extend",
|
35
|
-
selectHosts: "shorten"
|
36
|
-
}
|
37
|
-
)
|
38
|
-
end
|
39
|
-
|
40
|
-
# The default options used when creating Host objects via Zabbix API
|
41
|
-
# @note Create Host 缺省的配置参数
|
42
|
-
# @return [Hash]
|
15
|
+
# 在通过 Zabbix API 创建主机对象时使用的默认选项
|
43
16
|
def default_options
|
44
17
|
{
|
45
18
|
host: nil,
|
@@ -63,76 +36,88 @@ class ZabbixManager
|
|
63
36
|
}
|
64
37
|
end
|
65
38
|
|
66
|
-
#
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
templates: data[:templates_id]
|
78
|
-
}
|
39
|
+
# 通过 Zabbix API 从指定的 key 值获取主机对象数据
|
40
|
+
def dump_by_id(data)
|
41
|
+
log "[DEBUG] 调用 dump_by_id 方法,参数: #{data.inspect}"
|
42
|
+
|
43
|
+
get_raw({
|
44
|
+
filter: {
|
45
|
+
key.to_sym => data[key.to_sym]
|
46
|
+
},
|
47
|
+
output: "extend",
|
48
|
+
selectHosts: "shorten"
|
49
|
+
}
|
79
50
|
)
|
80
|
-
result.empty? ? false : true
|
81
51
|
end
|
82
52
|
|
83
|
-
#
|
84
|
-
# @note 基于监控对象索引键(#{identify})查询 #{key}
|
85
|
-
# @param data [String, Array] Needs to include host to properly identify Zabbix Objects 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 [Array, NilClass] Zabbix object id
|
53
|
+
# 通过 Zabbix API 根据名称获取 Zabbix 对象的 ID
|
89
54
|
def get_ids_by_name(data)
|
90
|
-
log "[DEBUG]
|
55
|
+
log "[DEBUG] 调用 #{method_name}.get_ids_by_name 方法,参数: #{data.inspect}"
|
91
56
|
|
92
|
-
result = get_raw(
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
}
|
57
|
+
result = get_raw({
|
58
|
+
filter: { name: [data].flatten },
|
59
|
+
output: [key]
|
60
|
+
}
|
97
61
|
)
|
98
|
-
result.empty? ? nil : result.map { |i| {
|
62
|
+
result.empty? ? nil : result.map { |i| { key.to_sym => i[key] } }
|
99
63
|
end
|
100
64
|
|
101
|
-
#
|
102
|
-
# @note 创建或更新 Host 监控对象,入参为基于符号的哈希对象,返回监控对象 id
|
103
|
-
# @note 重写基类方法 create_or_update,特殊处理 interfaces 对象
|
104
|
-
# @param data [Hash] Should include object's id field name (identify) and id value
|
105
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
106
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
107
|
-
# @return [Integer] The object id if a single object is created
|
108
|
-
# @return [Boolean] True/False if multiple objects are created
|
65
|
+
# 使用 Zabbix API 创建或更新 Zabbix 对象
|
109
66
|
def create_or_update(data)
|
110
|
-
|
67
|
+
# 输出调试日志,显示方法调用和参数信息
|
68
|
+
log "[DEBUG] 调用 #{method_name}.create_or_update 方法,参数: #{data.inspect}"
|
111
69
|
|
70
|
+
# 获取 Zabbix 对象的 ID
|
112
71
|
hostid = get_id(identify.to_sym => data[identify.to_sym])
|
72
|
+
# 如果不存在对应的对象ID,则创建新对象
|
113
73
|
return create(data) unless hostid
|
114
74
|
|
75
|
+
# 获取对象的接口 ID
|
115
76
|
interface_id = get_interfaceid(hostid)
|
116
|
-
|
77
|
+
# 如果接口 ID 不存在,抛出异常
|
78
|
+
raise ZbxError, "#{data.inspect} get_interfaceid 查无记录" unless interface_id
|
79
|
+
|
80
|
+
# 复制数据以避免修改原始数据
|
81
|
+
dump_data = data.dup
|
117
82
|
|
118
|
-
|
119
|
-
|
83
|
+
# 从数据中提取并删除 interfaces,并合并接口 ID
|
84
|
+
interfaces = dump_data.delete(:interfaces)&.merge(interfaceid: interface_id)
|
85
|
+
|
86
|
+
# 构建参数,包括 interfaces 和 Zabbix 对象的键值对
|
87
|
+
params = dump_data.merge(interfaces: interfaces).merge(key.to_sym => hostid.to_s)
|
88
|
+
|
89
|
+
# 更新对象
|
120
90
|
update(params)
|
121
91
|
end
|
122
92
|
|
123
|
-
#
|
124
|
-
# @note 基于 hostid 查询关联的 Interfaceid
|
125
|
-
# @param hostid [Integer, String]
|
126
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call or missing object's id field name (identify).
|
127
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
128
|
-
# @return [Integer, NilClass] Zabbix object id
|
93
|
+
# 从 API 中基于提供的 hostid 获取 Zabbix interface 的 id
|
129
94
|
def get_interfaceid(hostid)
|
95
|
+
log "[DEBUG] 调用 #{method_name}.get_interfaceid 方法,参数: #{hostid.inspect}"
|
96
|
+
|
130
97
|
result = @client.api_request(
|
131
98
|
method: "hostinterface.get",
|
132
99
|
params: { output: "interfaceid", hostids: hostid }
|
133
100
|
)
|
134
|
-
|
101
|
+
|
102
|
+
# 使用 dig 方法获取深层嵌套的值
|
103
|
+
interface_id = result&.dig(0, "interfaceid")&.to_i
|
104
|
+
|
105
|
+
# 如果获取接口 ID 失败,提供更具体的错误信息
|
106
|
+
raise ZbxError, "无法获取 #{hostid} 的接口 ID" if interface_id.nil?
|
107
|
+
|
108
|
+
interface_id
|
109
|
+
end
|
110
|
+
|
111
|
+
# 在 Hosts 中使用 Zabbix API 取消关联/移除模板
|
112
|
+
def unlink_templates(data)
|
113
|
+
result = @client.api_request(
|
114
|
+
method: "host.massRemove",
|
115
|
+
params: {
|
116
|
+
hostids: data[:hosts_id],
|
117
|
+
templates: data[:templates_id]
|
118
|
+
}
|
119
|
+
)
|
120
|
+
result.empty? ? false : true
|
135
121
|
end
|
136
122
|
end
|
137
123
|
end
|
138
|
-
|
@@ -2,39 +2,28 @@
|
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
4
|
class HttpTests < Basic
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# @return [String]
|
5
|
+
# 用于通过 Zabbix API 与 HttpTests 交互的方法名称
|
8
6
|
def method_name
|
9
7
|
"httptest"
|
10
8
|
end
|
11
9
|
|
12
|
-
#
|
13
|
-
#
|
14
|
-
# @return [String]
|
10
|
+
# 用于通过 Zabbix API 标识特定 HttpTest 对象的 id 字段名称
|
15
11
|
def identify
|
16
12
|
"name"
|
17
13
|
end
|
18
14
|
|
19
|
-
#
|
20
|
-
#
|
21
|
-
# @return [Hash]
|
15
|
+
# 通过 Zabbix API 创建 HttpTest 对象时使用的默认选项
|
22
16
|
def default_options
|
23
17
|
{
|
24
18
|
hostid: nil,
|
25
|
-
name:
|
26
|
-
steps:
|
19
|
+
name: nil,
|
20
|
+
steps: []
|
27
21
|
}
|
28
22
|
end
|
29
23
|
|
30
|
-
#
|
31
|
-
#
|
32
|
-
# @param data [Hash] Needs to include name and hostid to properly identify HttpTests via Zabbix API
|
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 [Integer] Zabbix object id
|
24
|
+
# 通过 Zabbix API 获取或创建 HttpTest 对象
|
36
25
|
def get_or_create(data)
|
37
|
-
log "[DEBUG]
|
26
|
+
log "[DEBUG] 使用参数调用 get_or_create: #{data.inspect}"
|
38
27
|
|
39
28
|
unless (id = get_id(name: data[:name], hostid: data[:hostid]))
|
40
29
|
id = create(data)
|
@@ -42,12 +31,7 @@ class ZabbixManager
|
|
42
31
|
id
|
43
32
|
end
|
44
33
|
|
45
|
-
#
|
46
|
-
#
|
47
|
-
# @param data [Hash] Needs to include name and hostid to properly identify HttpTests via Zabbix API
|
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, TrueClass, FalseClass] Zabbix object id
|
34
|
+
# 通过 Zabbix API 创建或更新 HttpTest 对象
|
51
35
|
def create_or_update(data)
|
52
36
|
httptestid = get_id(name: data[:name], hostid: data[:hostid])
|
53
37
|
httptestid ? update(data.merge(httptestid: httptestid)) : create(data)
|