zabbix_manager 5.1.3 → 5.1.4
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 +16 -47
- 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 +42 -76
- data/lib/zabbix_manager/classes/httptests.rb +7 -23
- data/lib/zabbix_manager/classes/items.rb +12 -68
- 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 +65 -69
- data/lib/zabbix_manager/version.rb +1 -2
- data/lib/zabbix_manager.rb +33 -46
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cebae410e9abb93e18e28e5184aca238514910332b5769d132fc282d163d218c
|
4
|
+
data.tar.gz: f02f9e2cbe167a4358dd89da12f7d04f27d84a2e9869bc9d652ef8526671c514
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c73a3e5b13a7f514508ed7998bf4df6ffc6a5c778e9a8b5eac620842a3df4555fc4c4fca41d76ad8740665c4d5583ceea2440439113ebd7dd3aeeeb40eab32b4
|
7
|
+
data.tar.gz: d3f0d4c6d9e627f36ba0f1ca87098c6060e20f22218586368139e14d0446dcddef640a998dcd711b4c848ef60e9e9f99b10204a5fe0f69fe28dc6c438f256cf1
|
@@ -2,34 +2,17 @@
|
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
4
|
class Basic
|
5
|
-
#
|
6
|
-
# @note 通用的查询方法
|
7
|
-
# @param data [Hash] Should include object's id field name (identify) and id value
|
8
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
9
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
10
|
-
# @return [Hash]
|
5
|
+
# 获取 Zabbix 对象数据
|
11
6
|
def get(data)
|
12
7
|
get_full_data(data)
|
13
8
|
end
|
14
9
|
|
15
|
-
#
|
16
|
-
# @note 通用的新增方法
|
17
|
-
# @param data [Hash]
|
18
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
19
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
20
|
-
# @return [Integer] The object id if a single object is created
|
21
|
-
# @return [Boolean] True/False if multiple objects are created
|
10
|
+
# 新增 Zabbix 对象
|
22
11
|
def add(data)
|
23
12
|
create(data)
|
24
13
|
end
|
25
14
|
|
26
|
-
#
|
27
|
-
# @note 通用的删除方法
|
28
|
-
# @param data [Hash] Should include object's id field name (identify) and id value
|
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] The object id if a single object is deleted
|
32
|
-
# @return [Boolean] True/False if multiple objects are deleted
|
15
|
+
# 删除 Zabbix 对象
|
33
16
|
def destroy(data)
|
34
17
|
delete(data)
|
35
18
|
end
|
@@ -2,31 +2,24 @@
|
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
4
|
class Basic
|
5
|
-
#
|
6
|
-
# @note 开启debug模式,打印日志
|
7
|
-
# @param message [String]
|
5
|
+
# 在调试模式下将日志消息记录到 stdout
|
8
6
|
def log(message)
|
9
7
|
puts message if @client.options[:debug]
|
10
8
|
end
|
11
9
|
|
12
|
-
#
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
# @return [Boolean]
|
17
|
-
def hash_equals?(first_hash, second_hash)
|
18
|
-
normalized_first_hash = normalize_hash(first_hash)
|
19
|
-
normalized_second_hash = normalize_hash(second_hash)
|
10
|
+
# 比较两个哈希是否相等
|
11
|
+
def hash_equals?(hash1, hash2)
|
12
|
+
normalized_hash1 = normalize_hash(hash1)
|
13
|
+
normalized_hash2 = normalize_hash(hash2)
|
20
14
|
|
21
|
-
|
22
|
-
|
23
|
-
|
15
|
+
merged_hash1 = normalized_hash1.merge(normalized_hash2)
|
16
|
+
merged_hash2 = normalized_hash2.merge(normalized_hash1)
|
17
|
+
|
18
|
+
merged_hash1 == merged_hash2
|
24
19
|
end
|
25
20
|
|
26
|
-
#
|
27
|
-
#
|
28
|
-
# @param object [Array, Hash]
|
29
|
-
# @return [Array, Hash]
|
21
|
+
# 将所有哈希/数组键转换为符号
|
22
|
+
# &method(:symbolize_keys) 是 Ruby 中一种将方法转换为可传递给块或其他方法的 Proc 的方式
|
30
23
|
def symbolize_keys(object)
|
31
24
|
case object
|
32
25
|
when Array
|
@@ -38,34 +31,27 @@ class ZabbixManager
|
|
38
31
|
end
|
39
32
|
end
|
40
33
|
|
41
|
-
#
|
42
|
-
# @note 将哈希元素序列化成字符串
|
43
|
-
# @param hash [Hash]
|
44
|
-
# @return [Hash]
|
34
|
+
# 将所有哈希值规范化为字符串
|
45
35
|
def normalize_hash(hash)
|
46
36
|
result = hash.dup
|
47
37
|
|
48
|
-
|
38
|
+
# 移除用于日志记录的 TODO 注释。TemplateID 和 HostID 具有不同的 ID
|
39
|
+
result.delete(:hostid)
|
49
40
|
|
50
|
-
result.
|
51
|
-
|
52
|
-
end
|
41
|
+
result.transform_keys!(&:to_sym)
|
42
|
+
result.transform_values! { |value| value.is_a?(Array) ? normalize_array(value) : value.to_s }
|
53
43
|
|
54
44
|
result
|
55
45
|
end
|
56
46
|
|
57
|
-
#
|
58
|
-
# @note 将数组元素序列化成字符串
|
59
|
-
# @param array [Array]
|
60
|
-
# @return [Array]
|
47
|
+
# 将所有数组值规范化为字符串
|
61
48
|
def normalize_array(array)
|
62
49
|
result = []
|
63
50
|
|
64
51
|
array.each do |e|
|
65
|
-
|
66
|
-
when Array
|
52
|
+
if e.is_a?(Array)
|
67
53
|
result.push(normalize_array(e))
|
68
|
-
|
54
|
+
elsif e.is_a?(Hash)
|
69
55
|
result.push(normalize_hash(e))
|
70
56
|
else
|
71
57
|
result.push(e.to_s)
|
@@ -75,11 +61,7 @@ class ZabbixManager
|
|
75
61
|
result
|
76
62
|
end
|
77
63
|
|
78
|
-
#
|
79
|
-
# @note 提取 zabbix 监控对象 CRUD key 或者 keys
|
80
|
-
# @param data
|
81
|
-
# @return [Integer] The object id if a single object hash is provided with key
|
82
|
-
# @return [Boolean] True/False if multiple class object hash is provided
|
64
|
+
# 解析包含 ID 键或布尔值的数据哈希
|
83
65
|
def parse_keys(data)
|
84
66
|
case data
|
85
67
|
when Hash
|
@@ -93,14 +75,10 @@ class ZabbixManager
|
|
93
75
|
end
|
94
76
|
end
|
95
77
|
|
96
|
-
#
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
# @return [Hash]
|
101
|
-
def merge_params(first_hash, second_hash)
|
102
|
-
new = first_hash.dup
|
103
|
-
new.merge(second_hash)
|
78
|
+
# 合并两个哈希为一个新的哈希
|
79
|
+
def merge_hashes(hash1, hash2)
|
80
|
+
new_hash = hash1.dup
|
81
|
+
new_hash.merge(hash2)
|
104
82
|
end
|
105
83
|
end
|
106
84
|
end
|
@@ -1,49 +1,33 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
|
-
# @author: WENWU YAN
|
5
|
-
# @email: 968828@gmail.com
|
6
|
-
# @date: 2023/3/25下午4:47
|
7
4
|
class Basic
|
8
|
-
#
|
9
|
-
# @note zabbix api 基类
|
10
|
-
# @param client [ZabbixManager::Client]
|
11
|
-
# @return [ZabbixManager::Client]
|
5
|
+
# 使用 ZabbixManager 客户端初始化一个新的 Basic 对象
|
12
6
|
def initialize(client)
|
13
7
|
@client = client
|
14
8
|
end
|
15
9
|
|
16
|
-
#
|
17
|
-
# @note 子类必须实现的方法 method_name
|
18
|
-
# @raise [ZbxError] Basic object does not directly support method_name
|
10
|
+
# 在派生类中应该重写此方法以提供特定的方法名
|
19
11
|
def method_name
|
20
|
-
raise ZbxError, "
|
12
|
+
raise ZbxError, "子类需要自行实现 method_name"
|
21
13
|
end
|
22
14
|
|
23
|
-
#
|
24
|
-
# @note 子类必须实现的方法
|
25
|
-
# @raise [ZbxError] Basic object does not directly support identify
|
15
|
+
# 在派生类中应该重写此方法以提供特定的 ID 字段名
|
26
16
|
def identify
|
27
|
-
raise ZbxError, "
|
17
|
+
raise ZbxError, "子类需要自行实现 identify"
|
28
18
|
end
|
29
19
|
|
30
|
-
#
|
31
|
-
# @note 子类缺省的 options
|
32
|
-
# @return [Hash]
|
20
|
+
# 派生类可以重写此方法以提供特定的默认选项
|
33
21
|
def default_options
|
34
22
|
{}
|
35
23
|
end
|
36
24
|
|
37
|
-
#
|
38
|
-
# @note 默认 key 为 "#{method_name}id"
|
39
|
-
# @return [String]
|
25
|
+
# 根据 method_name + id 返回对象的 ID 字段名(identify)
|
40
26
|
def key
|
41
27
|
"#{method_name}id"
|
42
28
|
end
|
43
29
|
|
44
|
-
#
|
45
|
-
# @note 默认 key 为 "#{method_name}ids"
|
46
|
-
# @return [String]
|
30
|
+
# 根据 key 返回对象的复数 ID 字段名(identify)
|
47
31
|
def keys
|
48
32
|
"#{key}s"
|
49
33
|
end
|
@@ -2,38 +2,25 @@
|
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
4
|
class Basic
|
5
|
-
#
|
6
|
-
# @note 新增 zabbix 监控对象,返回监控对象 id
|
7
|
-
# @param data [Hash]
|
8
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
9
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
10
|
-
# @return [Integer] The object id if a single object is created
|
11
|
-
# @return [Boolean] True/False if multiple objects are created
|
5
|
+
# 使用 API 新增 Zabbix 监控对象(使用默认值)
|
12
6
|
def create(data)
|
13
|
-
log "[DEBUG]
|
7
|
+
log "[DEBUG] 调用 #{method_name}.create,参数: #{data.inspect}"
|
14
8
|
|
15
9
|
params = default_options.merge(data)
|
16
10
|
result = @client.api_request(method: "#{method_name}.create", params: params)
|
17
11
|
parse_keys result
|
18
12
|
end
|
19
13
|
|
20
|
-
#
|
21
|
-
# @note 更新 zabbix 监控对象,返回监控对象 id
|
22
|
-
# @param data [Hash] Should include object's id field name (identify) and id value
|
23
|
-
# @param force [Boolean] Whether to force an object update even if provided data matches Zabbix
|
24
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
25
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
26
|
-
# @return [Integer] The object id if a single object is created
|
27
|
-
# @return [Boolean] True/False if multiple objects are created
|
14
|
+
# 使用 API 更新 Zabbix 监控对象
|
28
15
|
def update(data, force = false)
|
29
|
-
log "[DEBUG]
|
16
|
+
log "[DEBUG] 调用 #{method_name}.update,参数: #{data.inspect}"
|
30
17
|
|
31
18
|
dump = {}
|
32
19
|
dump_by_id(key.to_sym => data[key.to_sym]).each do |item|
|
33
20
|
dump = symbolize_keys(item) if item[key].to_i == data[key.to_sym].to_i
|
34
21
|
end
|
35
22
|
if hash_equals?(dump, data) && !force
|
36
|
-
log "[DEBUG]
|
23
|
+
log "[DEBUG] 系统以有数据 #{dump} 和 待更新的数据 #{data} 相同,跳过本次更新"
|
37
24
|
data[key.to_sym].to_i
|
38
25
|
else
|
39
26
|
result = @client.api_request(method: "#{method_name}.update", params: data)
|
@@ -41,30 +28,18 @@ class ZabbixManager
|
|
41
28
|
end
|
42
29
|
end
|
43
30
|
|
44
|
-
#
|
45
|
-
# @note 删除 zabbix 监控对象,返回监控对象 id
|
46
|
-
# @param data [Hash] Should include object's id field name (identify) and id value
|
47
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
48
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
49
|
-
# @return [Integer] The object id if a single object is deleted
|
50
|
-
# @return [Boolean] True/False if multiple objects are deleted
|
31
|
+
# 使用 API 删除 Zabbix 监控对象
|
51
32
|
def delete(data)
|
52
|
-
log "[DEBUG]
|
33
|
+
log "[DEBUG] 调用 #{method_name}.delete,参数: #{data.inspect}"
|
53
34
|
|
54
35
|
result = @client.api_request(method: "#{method_name}.delete", params: [data].flatten)
|
55
36
|
parse_keys result
|
56
37
|
end
|
57
38
|
|
58
|
-
#
|
59
|
-
# @note 创建或更新 zabbix 监控对象,返回监控对象 id
|
60
|
-
# @param data [Hash] Should include object's id field name (identify) and id value
|
61
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
62
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
63
|
-
# @return [Integer, Array, TrueClass, FalseClass] Zabbix object id
|
39
|
+
# 使用 API 获取或创建 Zabbix 监控对象
|
64
40
|
def get_or_create(data)
|
65
|
-
log "[DEBUG]
|
41
|
+
log "[DEBUG] 调用 #{method_name}.get_or_create,参数: #{data.inspect}"
|
66
42
|
|
67
|
-
# Call get_id return object id if exists
|
68
43
|
if (id = get_id(identify.to_sym => data[identify.to_sym]))
|
69
44
|
id
|
70
45
|
else
|
@@ -72,46 +47,30 @@ class ZabbixManager
|
|
72
47
|
end
|
73
48
|
end
|
74
49
|
|
75
|
-
#
|
76
|
-
# @note 批量创建或更新监控对象并返回 [ { "#{key}": id } ]
|
77
|
-
# @param data [Hash, Array] Should include object's id field name (identify) and id value
|
78
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
79
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
80
|
-
# @return [Array] Zabbix object [{keys: id}]
|
50
|
+
# 使用 API 批量获取或创建 Zabbix 监控对象
|
81
51
|
def get_or_create_keys(data)
|
82
|
-
log "[DEBUG]
|
52
|
+
log "[DEBUG] 调用 #{method_name}.get_or_create_keys,参数: #{data.inspect}"
|
83
53
|
|
84
54
|
ids = []
|
85
55
|
[data].flatten.each do |item|
|
86
56
|
if (id = get_or_create(item))
|
87
|
-
ids << {
|
57
|
+
ids << { key.to_sym => id }
|
88
58
|
end
|
89
59
|
end
|
90
60
|
ids
|
91
61
|
end
|
92
62
|
|
93
|
-
#
|
94
|
-
# @note 创建或更新 zabbix 监控对象,入参为基于符号的哈希对象,返回监控对象 id
|
95
|
-
# @param data [Hash] Should include object's id field name (identify) and id value
|
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] The object id if a single object is created
|
99
|
-
# @return [Boolean] True/False if multiple objects are created
|
63
|
+
# 使用 API 创建或更新 Zabbix 监控对象
|
100
64
|
def create_or_update(data)
|
101
|
-
log "[DEBUG]
|
65
|
+
log "[DEBUG] 调用 #{method_name}.create_or_update,参数: #{data.inspect}"
|
102
66
|
|
103
67
|
id = get_id(identify.to_sym => data[identify.to_sym])
|
104
68
|
id ? update(data.merge(key.to_sym => id.to_s)) : create(data)
|
105
69
|
end
|
106
70
|
|
107
|
-
#
|
108
|
-
# @note 基于 "#{key}" 打印 zabbix 监控对象详情
|
109
|
-
# @param data [Hash] Should include desired object's key and value
|
110
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
111
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
112
|
-
# @return [Hash]
|
71
|
+
# 使用 API 基于 key 打印 Zabbix 监控对象详情
|
113
72
|
def dump_by_id(data)
|
114
|
-
log "[DEBUG]
|
73
|
+
log "[DEBUG] 调用 #{method_name}.dump_by_id,参数: #{data.inspect}"
|
115
74
|
|
116
75
|
get_raw(
|
117
76
|
{
|
@@ -123,21 +82,15 @@ class ZabbixManager
|
|
123
82
|
)
|
124
83
|
end
|
125
84
|
|
126
|
-
#
|
127
|
-
# @note 基于 zabbix "#{identify}" 查询监控对象的 "#{key}"
|
128
|
-
# @param data [Hash]
|
129
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call or missing object's id field name (identify).
|
130
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
131
|
-
# @return [Integer, NilClass] Zabbix object id
|
85
|
+
# 使用 API 基于提供的数据查询 Zabbix 监控对象 id
|
132
86
|
def get_id(data)
|
133
|
-
log "[DEBUG]
|
87
|
+
log "[DEBUG] 调用 #{method_name}.get_id,参数: #{data.inspect}"
|
134
88
|
|
135
|
-
# symbolize keys if the user used string keys instead of symbols
|
136
89
|
data = symbolize_keys(data) if data.key?(identify)
|
137
|
-
# raise an error if identify name was not supplied
|
138
90
|
name = data[identify.to_sym]
|
139
|
-
raise ZbxError, "#{identify}
|
91
|
+
raise ZbxError, "在调用 get_id 时未提供 identify #{identify}" if name.nil?
|
140
92
|
|
93
|
+
# 输出信息截取 key identify
|
141
94
|
result = get_raw(
|
142
95
|
{
|
143
96
|
filter: data,
|
@@ -148,47 +101,59 @@ class ZabbixManager
|
|
148
101
|
result.find { |item| item[identify] == data[identify.to_sym] }&.[](key)&.to_i
|
149
102
|
end
|
150
103
|
|
151
|
-
#
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
104
|
+
# 使用 API 基于提供的数据查询 Zabbix 监控对象 ids
|
105
|
+
def get_ids(data)
|
106
|
+
log "[DEBUG] 调用 #{method_name}.get_ids,参数: #{data.inspect}"
|
107
|
+
|
108
|
+
ids = []
|
109
|
+
[data].flatten.each do |item|
|
110
|
+
if (id = get_id(item))
|
111
|
+
ids << id
|
112
|
+
end
|
113
|
+
end
|
114
|
+
ids
|
115
|
+
end
|
116
|
+
|
117
|
+
# 使用 API 基于提供的数据查询 Zabbix 监控对象 id
|
157
118
|
def get_key_ids(data)
|
158
|
-
log "[DEBUG]
|
119
|
+
log "[DEBUG] 调用 #{method_name}.get_key_ids,参数: #{data.inspect}"
|
159
120
|
|
160
121
|
ids = []
|
161
122
|
[data].flatten.each do |item|
|
162
123
|
if (id = get_id(item))
|
163
|
-
ids << {
|
124
|
+
ids << { key.to_sym => id }
|
164
125
|
end
|
165
126
|
end
|
166
127
|
ids
|
167
128
|
end
|
168
129
|
|
169
|
-
#
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
130
|
+
# 使用 API 基于 name 查询 Zabbix 监控对象 id
|
131
|
+
def get_ids_by_identify(data)
|
132
|
+
log "[DEBUG] 调用 #{method_name}.get_ids_by_identify,参数: #{data.inspect}"
|
133
|
+
|
134
|
+
result = get_raw(
|
135
|
+
{
|
136
|
+
filter: { identify.to_sym => [data].flatten },
|
137
|
+
output: [key]
|
138
|
+
}
|
139
|
+
)
|
140
|
+
result.empty? ? nil : result.map { |i| i[key] }
|
141
|
+
end
|
142
|
+
|
143
|
+
# 使用 API 基于 name 查询 Zabbix 监控对象 id
|
175
144
|
def get_key_ids_by_identify(data)
|
176
|
-
log "[DEBUG]
|
145
|
+
log "[DEBUG] 调用 #{method_name}.get_key_ids_by_identify,参数: #{data.inspect}"
|
177
146
|
|
178
147
|
result = get_raw(
|
179
148
|
{
|
180
|
-
filter: {
|
181
|
-
output: [
|
149
|
+
filter: { identify.to_sym => [data].flatten },
|
150
|
+
output: [key]
|
182
151
|
}
|
183
152
|
)
|
184
|
-
result.empty? ? nil : result.map { |i| {
|
153
|
+
result.empty? ? nil : result.map { |i| { key.to_sym => i[key] } }
|
185
154
|
end
|
186
155
|
|
187
|
-
#
|
188
|
-
# @note 查询 zabbix 监控对象所有的 "#{identify}: #{key}"
|
189
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
190
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
191
|
-
# @return [Hash] Array of matching objects
|
156
|
+
# 使用 API 查询所有 Zabbix 监控对象的详细信息
|
192
157
|
def all
|
193
158
|
result = {}
|
194
159
|
get_raw({ output: "extend" }).each do |item|
|
@@ -197,14 +162,9 @@ class ZabbixManager
|
|
197
162
|
result
|
198
163
|
end
|
199
164
|
|
200
|
-
#
|
201
|
-
# @note 基于 "#{identify}" 过滤 zabbix 监控对象,输出详情
|
202
|
-
# @param data [Hash] Should include object's id field name (identify) and id value
|
203
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
204
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
205
|
-
# @return [Hash]
|
165
|
+
# 使用 API 查询 Zabbix 监控对象的详细信息
|
206
166
|
def get_full_data(data)
|
207
|
-
log "[DEBUG]
|
167
|
+
log "[DEBUG] 调用 #{method_name}.get_full_data,参数: #{data.inspect}"
|
208
168
|
|
209
169
|
get_raw(
|
210
170
|
{
|
@@ -216,18 +176,20 @@ class ZabbixManager
|
|
216
176
|
)
|
217
177
|
end
|
218
178
|
|
219
|
-
#
|
220
|
-
# @note zabbix 监控对象低阶查询方法,静默方法 debug 打印信息
|
221
|
-
# @param data [Hash]
|
222
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
223
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
224
|
-
# @return [Hash]
|
179
|
+
# 使用 API 查询 Zabbix 监控对象的原始数据
|
225
180
|
def get_raw(data)
|
226
|
-
# log "[DEBUG] Call #{method_name}.get_raw with parameters: #{data.inspect}"
|
227
181
|
@client.api_request(
|
228
182
|
method: "#{method_name}.get",
|
229
183
|
params: data
|
230
184
|
)
|
231
185
|
end
|
186
|
+
|
187
|
+
# 使用 API 查询 Zabbix 监控对象的原始数据
|
188
|
+
def request_raw(method, data)
|
189
|
+
@client.api_request(
|
190
|
+
method: method,
|
191
|
+
params: data
|
192
|
+
)
|
193
|
+
end
|
232
194
|
end
|
233
195
|
end
|
@@ -2,40 +2,30 @@
|
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
4
|
class Actions < Basic
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# @return [String]
|
5
|
+
# 用于与 Zabbix API 交互的方法名称
|
8
6
|
def method_name
|
9
7
|
"action"
|
10
8
|
end
|
11
9
|
|
12
|
-
#
|
13
|
-
#
|
14
|
-
# @return [String]
|
10
|
+
# 用于通过 Zabbix API 标识特定 Action 对象的 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
|
+
# 从 API 获取完整/扩展的 Action 对象数据
|
25
16
|
def get_full_data(data)
|
26
|
-
log "[DEBUG]
|
17
|
+
log "[DEBUG] 调用 get_full_data,参数为:#{data.inspect}"
|
27
18
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
filter: {
|
19
|
+
get_raw(
|
20
|
+
{
|
21
|
+
filter: {
|
32
22
|
identify.to_sym => data[identify.to_sym]
|
33
23
|
},
|
34
|
-
output:
|
35
|
-
selectOperations:
|
36
|
-
selectRecoveryOperations:
|
24
|
+
output: "extend",
|
25
|
+
selectOperations: "extend",
|
26
|
+
selectRecoveryOperations: "extend",
|
37
27
|
selectAcknowledgeOperations: "extend",
|
38
|
-
selectFilter:
|
28
|
+
selectFilter: "extend"
|
39
29
|
}
|
40
30
|
)
|
41
31
|
end
|
@@ -2,28 +2,19 @@
|
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
4
|
class Applications < Basic
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# @return [String]
|
5
|
+
# 用于通过 Zabbix API 与 Applications 交互的方法名称
|
8
6
|
def method_name
|
9
7
|
"application"
|
10
8
|
end
|
11
9
|
|
12
|
-
#
|
13
|
-
#
|
14
|
-
# @return [String]
|
10
|
+
# 用于通过 Zabbix API 标识特定 Application 对象的 id 字段名称
|
15
11
|
def identify
|
16
12
|
"name"
|
17
13
|
end
|
18
14
|
|
19
|
-
#
|
20
|
-
#
|
21
|
-
# @param data [Hash] Needs to include name and hostid to properly identify Applications via Zabbix API
|
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, TrueClass, FalseClass] Zabbix object id
|
15
|
+
# 通过 Zabbix API 获取或创建 Application 对象
|
25
16
|
def get_or_create(data)
|
26
|
-
log "[DEBUG]
|
17
|
+
log "[DEBUG] 调用 get_or_create,参数为:#{data.inspect}"
|
27
18
|
|
28
19
|
if (id = get_id(name: data[:name], hostid: data[:hostid]))
|
29
20
|
id
|
@@ -32,12 +23,7 @@ class ZabbixManager
|
|
32
23
|
end
|
33
24
|
end
|
34
25
|
|
35
|
-
#
|
36
|
-
#
|
37
|
-
# @param data [Hash] Needs to include name and hostid to properly identify Applications via Zabbix API
|
38
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
39
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
40
|
-
# @return [Integer,TrueClass, FalseClass] Zabbix object id
|
26
|
+
# 通过 Zabbix API 创建或更新 Application 对象
|
41
27
|
def create_or_update(data)
|
42
28
|
applicationid = get_id(name: data[:name], hostid: data[:hostid])
|
43
29
|
applicationid ? update(data.merge(applicationid: applicationid)) : create(data)
|
@@ -2,41 +2,27 @@
|
|
2
2
|
|
3
3
|
class ZabbixManager
|
4
4
|
class Configurations < Basic
|
5
|
-
#
|
5
|
+
# 用于标识返回结果是否为数组
|
6
6
|
def array_flag
|
7
7
|
true
|
8
8
|
end
|
9
9
|
|
10
|
-
#
|
11
|
-
#
|
12
|
-
# @return [String]
|
10
|
+
# 用于通过 Zabbix API 与 Configurations 交互的方法名称
|
13
11
|
def method_name
|
14
12
|
"configuration"
|
15
13
|
end
|
16
14
|
|
17
|
-
#
|
18
|
-
#
|
19
|
-
# @return [String]
|
15
|
+
# 用于通过 Zabbix API 标识特定 Configuration 对象的 id 字段名称
|
20
16
|
def identify
|
21
17
|
"host"
|
22
18
|
end
|
23
19
|
|
24
|
-
#
|
25
|
-
#
|
26
|
-
# @param data [Hash]
|
27
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
28
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
29
|
-
# @return [Hash]
|
20
|
+
# 使用 Zabbix API 导出配置数据
|
30
21
|
def export(data)
|
31
22
|
@client.api_request(method: "configuration.export", params: data)
|
32
23
|
end
|
33
24
|
|
34
|
-
#
|
35
|
-
#
|
36
|
-
# @param data [Hash]
|
37
|
-
# @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
|
38
|
-
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
39
|
-
# @return [Hash]
|
25
|
+
# 使用 Zabbix API 导入配置数据
|
40
26
|
def import(data)
|
41
27
|
@client.api_request(method: "configuration.import", params: data)
|
42
28
|
end
|