zabbix_manager 5.0.1 → 5.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/lib/zabbix_manager/basic/basic_alias.rb +37 -0
  3. data/lib/zabbix_manager/basic/basic_extend.rb +38 -0
  4. data/lib/zabbix_manager/basic/basic_func.rb +103 -0
  5. data/lib/zabbix_manager/basic/basic_init.rb +46 -0
  6. data/lib/zabbix_manager/basic/basic_logic.rb +227 -0
  7. data/lib/zabbix_manager/classes/actions.rb +41 -0
  8. data/lib/zabbix_manager/classes/applications.rb +43 -0
  9. data/lib/zabbix_manager/classes/configurations.rb +42 -0
  10. data/lib/zabbix_manager/classes/drules.rb +55 -0
  11. data/lib/zabbix_manager/classes/errors.rb +28 -0
  12. data/lib/zabbix_manager/classes/events.rb +18 -0
  13. data/lib/zabbix_manager/classes/graphs.rb +111 -0
  14. data/lib/zabbix_manager/classes/hostgroups.rb +58 -0
  15. data/lib/zabbix_manager/classes/hosts.rb +218 -0
  16. data/lib/zabbix_manager/classes/httptests.rb +54 -0
  17. data/lib/zabbix_manager/classes/items.rb +145 -0
  18. data/lib/zabbix_manager/classes/maintenance.rb +17 -0
  19. data/lib/zabbix_manager/classes/mediatypes.rb +109 -0
  20. data/lib/zabbix_manager/classes/problems.rb +133 -0
  21. data/lib/zabbix_manager/classes/proxies.rb +68 -0
  22. data/lib/zabbix_manager/classes/roles.rb +114 -0
  23. data/lib/zabbix_manager/classes/screens.rb +94 -0
  24. data/lib/zabbix_manager/classes/scripts.rb +35 -0
  25. data/lib/zabbix_manager/classes/server.rb +16 -0
  26. data/lib/zabbix_manager/classes/templates.rb +123 -0
  27. data/lib/zabbix_manager/classes/triggers.rb +166 -0
  28. data/lib/zabbix_manager/classes/unusable.rb +8 -0
  29. data/lib/zabbix_manager/classes/usergroups.rb +73 -0
  30. data/lib/zabbix_manager/classes/usermacros.rb +228 -0
  31. data/lib/zabbix_manager/classes/users.rb +64 -0
  32. data/lib/zabbix_manager/classes/valuemaps.rb +50 -0
  33. data/lib/zabbix_manager/client.rb +158 -0
  34. data/lib/zabbix_manager/version.rb +1 -1
  35. data/zabbix_manager-5.0.1.gem +0 -0
  36. metadata +77 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5788062827f30e986041b5b75fecbc6bee2da108612326d2cbd5d9e7308bc086
4
- data.tar.gz: af67f5fcf2a6bfdff721caaa72f23ed16d76c563d7ed6b975af6fb912065ab2c
3
+ metadata.gz: baddfe1c19c7d70234e9faaf1327b7fb5a440083229dfc417dd2bc3b34ecdf75
4
+ data.tar.gz: 19e8933f296db4a00f48df300d3684f5c11fdae8f1f4f0921e41a357ade03466
5
5
  SHA512:
6
- metadata.gz: 8c0872a75da753c7b40ced8f269a413889f94742f876b2bf3868ee541b19fc38d8e8de7f2a5cc9729ef2a74eee0c2231d59dee8d01933b558c3c10578e2acb23
7
- data.tar.gz: e647bea7ba69dc30fe7fa4e8cb671739622644ba09bdea4da21c7bd237aca0064c30f8a25e4928c480ad9e270d4dacd28eac13cca934c23890c2a5b31bf03fae
6
+ metadata.gz: e7992694a2eaf74eb663c254e9143f1b1f054dba270bd856f9fce44466c54540f674e587cf76fc4924e7059acd301b04058ccea5ebbca69f538ef1facbc9e4f7
7
+ data.tar.gz: 30ff7ca3f862f7db10361f46337a680a47a79fde932efe280ec756167d2b7de83aa7ea35c30229b756d68cd4492ac020ed4f2f7a3a1dbf832d1398a955c825e4
@@ -0,0 +1,37 @@
1
+ class ZabbixManager
2
+ class Basic
3
+ # Get Zabbix object data from API by id
4
+ #
5
+ # @param data [Hash] Should include object's id field name (identify) and id value
6
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
7
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
8
+ # @return [Hash]
9
+ def get(data)
10
+ get_full_data(data)
11
+ end
12
+
13
+ # Add new Zabbix object using API create
14
+ #
15
+ # @param data [Hash]
16
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
17
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
18
+ # @return [Integer] The object id if a single object is created
19
+ # @return [Boolean] True/False if multiple objects are created
20
+ def add(data)
21
+ create(data)
22
+ end
23
+
24
+ # Destroy Zabbix object using API delete
25
+ #
26
+ # @param data [Hash] Should include object's id field name (identify) and id value
27
+ # @raise [ManagerError] 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 [Integer] The object id if a single object is deleted
30
+ # @return [Boolean] True/False if multiple objects are deleted
31
+ def destroy(data)
32
+ delete(data)
33
+ end
34
+
35
+ def method_name; end
36
+ end
37
+ end
@@ -0,0 +1,38 @@
1
+ class ZabbixManager
2
+ class Basic
3
+ # 新增 get_hostgroup_ids 方法,使用列表 flatten 功能拉平属组对象
4
+ def get_hostgroup_ids(data)
5
+ result = @client.api_request(
6
+ method: 'hostgroup.get',
7
+ params: {
8
+ output: "extend",
9
+ filter: {
10
+ name: [data].flatten
11
+ }
12
+ }
13
+ ).map { |item| { groupid: item['groupid'] } }
14
+
15
+ # 检查是是否存在
16
+ result.empty? ? nil : result.flatten
17
+ end
18
+
19
+ # 新增 get_or_create_hostgroups 方法,查询或创建新的对象
20
+ def get_or_create_hostgroups(data)
21
+ [data].flatten.each do |item|
22
+ # 是否存在设备属组,不存在则新建
23
+ begin
24
+ result = get_hostgroup_ids(item)
25
+ @client.api_request(
26
+ method: 'hostgroup.create',
27
+ params: {
28
+ name: item
29
+ }
30
+ ) if result.nil?
31
+ rescue => e
32
+ ap e
33
+ end
34
+ end
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,103 @@
1
+ class ZabbixManager
2
+ class Basic
3
+ # Log messages to stdout when debugging
4
+ #
5
+ # @param message [String]
6
+ def log(message)
7
+ puts message.to_s if @client.options[:debug]
8
+ end
9
+
10
+ # Compare two hashes for equality
11
+ #
12
+ # @param first_hash [Hash]
13
+ # @param second_hash [Hash]
14
+ # @return [Boolean]
15
+ def hash_equals?(first_hash, second_hash)
16
+ normalized_first_hash = normalize_hash(first_hash)
17
+ normalized_second_hash = normalize_hash(second_hash)
18
+
19
+ hash1 = normalized_first_hash.merge(normalized_second_hash)
20
+ hash2 = normalized_second_hash.merge(normalized_first_hash)
21
+ hash1 == hash2
22
+ end
23
+
24
+ # Convert all hash/array keys to symbols
25
+ #
26
+ # @param object [Array, Hash]
27
+ # @return [Array, Hash]
28
+ def symbolize_keys(object)
29
+ if object.is_a?(Array)
30
+ object.each_with_index do |val, index|
31
+ object[index] = symbolize_keys(val)
32
+ end
33
+ elsif object.is_a?(Hash)
34
+ object.keys.each do |key|
35
+ object[key.to_sym] = symbolize_keys(object.delete(key))
36
+ end
37
+ end
38
+ object
39
+ end
40
+
41
+ # Normalize all hash values to strings
42
+ #
43
+ # @param hash [Hash]
44
+ # @return [Hash]
45
+ def normalize_hash(hash)
46
+ result = hash.dup
47
+
48
+ result.delete(:hostid) # TODO: remove to logig. TemplateID and HostID has different id
49
+
50
+ result.each do |key, value|
51
+ result[key] = value.is_a?(Array) ? normalize_array(value) : value.to_s
52
+ end
53
+
54
+ result
55
+ end
56
+
57
+ # Normalize all array values to strings
58
+ #
59
+ # @param array [Array]
60
+ # @return [Array]
61
+ def normalize_array(array)
62
+ result = []
63
+
64
+ array.each do |e|
65
+ if e.is_a?(Array)
66
+ result.push(normalize_array(e))
67
+ elsif e.is_a?(Hash)
68
+ result.push(normalize_hash(e))
69
+ else
70
+ result.push(e.to_s)
71
+ end
72
+ end
73
+
74
+ result
75
+ end
76
+
77
+ # Parse a data hash for id key or boolean to return
78
+ #
79
+ # @param data [Hash]
80
+ # @return [Integer] The object id if a single object hash is provided with key
81
+ # @return [Boolean] True/False if multiple class object hash is provided
82
+ def parse_keys(data)
83
+ case data
84
+ when Hash
85
+ data.empty? ? nil : data[keys][0].to_i
86
+ when TrueClass
87
+ true
88
+ when FalseClass
89
+ false
90
+ end
91
+ end
92
+
93
+ # Merge two hashes into a single new hash
94
+ #
95
+ # @param first_hash [Hash]
96
+ # @param second_hash [Hash]
97
+ # @return [Hash]
98
+ def merge_params(first_hash, second_hash)
99
+ new = first_hash.dup
100
+ new.merge(second_hash)
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,46 @@
1
+ class ZabbixManager
2
+ class Basic
3
+ # Initializes a new Basic object with ZabbixManager Client
4
+ #
5
+ # @param client [ZabbixManager::Client]
6
+ # @return [ZabbixManager::Client]
7
+ def initialize(client)
8
+ @client = client
9
+ end
10
+
11
+ # Placeholder for inherited objects to provide object-specific method name
12
+ #
13
+ # @raise [ManagerError] Basic object does not directly support method_name
14
+ def method_name
15
+ raise ManagerError.new("Can't call method_name here")
16
+ end
17
+
18
+ # Placeholder for inherited objects to provide default options
19
+ #
20
+ # @return [Hash]
21
+ def default_options
22
+ {}
23
+ end
24
+
25
+ # Returns the object's plural id field name (identify) based on key
26
+ #
27
+ # @return [String]
28
+ def keys
29
+ key + 's'
30
+ end
31
+
32
+ # Returns the object's id field name (identify) based on method_name + id
33
+ #
34
+ # @return [String]
35
+ def key
36
+ method_name + 'id'
37
+ end
38
+
39
+ # Placeholder for inherited objects to provide object-specific id field name
40
+ #
41
+ # @raise [ManagerError] Basic object does not directly support identify
42
+ def identify
43
+ raise ManagerError.new("Can't call identify here")
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,227 @@
1
+ class ZabbixManager
2
+ class Basic
3
+ # Create new Zabbix object using API (with defaults)
4
+ #
5
+ # @param data [Hash]
6
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
7
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
8
+ # @return [Integer] The object id if a single object is created
9
+ # @return [Boolean] True/False if multiple objects are created
10
+ def create(data)
11
+ log "[DEBUG] Call create with parameters: #{data.inspect}"
12
+
13
+ # 判断是否绑定默认选项并重新生成配置
14
+ data_with_default = default_options.empty? ? data : merge_params(default_options, data)
15
+ data_create = [data_with_default]
16
+
17
+ # 调用实例方法
18
+ result = @client.api_request(method: "#{method_name}.create", params: data_create)
19
+ # 判断是否执行成功并返回结果
20
+ parse_keys result
21
+ end
22
+
23
+ # Delete Zabbix object using API
24
+ #
25
+ # @param data [Hash] Should include object's id field name (identify) and id value
26
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
27
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
28
+ # @return [Integer] The object id if a single object is deleted
29
+ # @return [Boolean] True/False if multiple objects are deleted
30
+ def delete(data)
31
+ log "[DEBUG] Call delete with parameters: #{data.inspect}"
32
+
33
+ data_delete = [data]
34
+ result = @client.api_request(method: "#{method_name}.delete", params: data_delete)
35
+ parse_keys result
36
+ end
37
+
38
+ # Create or update Zabbix object using API
39
+ #
40
+ # @param data [Hash] Should include object's id field name (identify) and id value
41
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
42
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
43
+ # @return [Integer] The object id if a single object is created
44
+ # @return [Boolean] True/False if multiple objects are created
45
+ def create_or_update(data)
46
+ log "[DEBUG] Call create_or_update with parameters: #{data.inspect}"
47
+
48
+ id = get_id(identify.to_sym => data[identify.to_sym])
49
+ id ? update(data.merge(key.to_sym => id.to_s)) : create(data)
50
+ end
51
+
52
+ # Update Zabbix object using API
53
+ #
54
+ # @param data [Hash] Should include object's id field name (identify) and id value
55
+ # @param force [Boolean] Whether to force an object update even if provided data matches Zabbix
56
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
57
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
58
+ # @return [Integer] The object id if a single object is created
59
+ # @return [Boolean] True/False if multiple objects are created
60
+ def update(data, force = false)
61
+ log "[DEBUG] Call update with parameters: #{data.inspect}"
62
+ dump = {}
63
+ dump_by_id(key.to_sym => data[key.to_sym]).each do |item|
64
+ dump = symbolize_keys(item) if item[key].to_i == data[key.to_sym].to_i
65
+ end
66
+ if hash_equals?(dump, data) && !force
67
+ log "[DEBUG] Equal keys #{dump} and #{data}, skip update"
68
+ data[key.to_sym].to_i
69
+ else
70
+ data_update = [data]
71
+ result = @client.api_request(method: "#{method_name}.update", params: data_update)
72
+ parse_keys result
73
+ end
74
+ end
75
+
76
+ # mojo_update 请求补丁函数
77
+ def mojo_update(method, data)
78
+ log "[DEBUG] Call update with parameters: #{data.inspect}"
79
+ @client.api_request(method: method, params: data)
80
+ end
81
+
82
+ # MOJO_UPDATE
83
+
84
+ # Get full/extended Zabbix object data from API
85
+ #
86
+ # @param data [Hash] Should include object's id field name (identify) and id value
87
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
88
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
89
+ # @return [Hash]
90
+ def get_full_data(data)
91
+ log "[DEBUG] Call get_full_data with parameters: #{data.inspect}"
92
+
93
+ @client.api_request(
94
+ method: "#{method_name}.get",
95
+ params: {
96
+ filter: {
97
+ identify.to_sym => data[identify.to_sym]
98
+ },
99
+ output: 'extend'
100
+ }
101
+ )
102
+ end
103
+
104
+ # Get raw Zabbix object data from API
105
+ #
106
+ # @param data [Hash]
107
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
108
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
109
+ # @return [Hash]
110
+ def get_raw(data)
111
+ log "[DEBUG] Call get_raw with parameters: #{data.inspect}"
112
+
113
+ @client.api_request(
114
+ method: "#{method_name}.get",
115
+ params: data
116
+ )
117
+ end
118
+
119
+ # Dump Zabbix object data by key from API
120
+ #
121
+ # @param data [Hash] Should include desired object's key and value
122
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
123
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
124
+ # @return [Hash]
125
+ def dump_by_id(data)
126
+ log "[DEBUG] Call dump_by_id with parameters: #{data.inspect}"
127
+
128
+ @client.api_request(
129
+ method: "#{method_name}.get",
130
+ params: {
131
+ filter: {
132
+ key.to_sym => data[key.to_sym]
133
+ },
134
+ output: 'extend'
135
+ }
136
+ )
137
+ end
138
+
139
+ # Get full/extended Zabbix data for all objects of type/class from API
140
+ #
141
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
142
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
143
+ # @return [Array<Hash>] Array of matching objects
144
+ def all
145
+ result = {}
146
+ @client.api_request(
147
+ method: "#{method_name}.get",
148
+ params: { output: 'extend' }
149
+ ).each do |item|
150
+ result[item[identify]] = item[key]
151
+ end
152
+ result
153
+ end
154
+
155
+ # Get Zabbix object id from API based on provided data
156
+ #
157
+ # @param data [Hash]
158
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call or missing object's id field name (identify).
159
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
160
+ # @return [Integer] Zabbix object id
161
+ def get_id(data)
162
+ log "[DEBUG] Call get_id with parameters: #{data.inspect}"
163
+
164
+ # symbolize keys if the user used string keys instead of symbols
165
+ data = symbolize_keys(data) if data.key?(identify)
166
+ # raise an error if identify name was not supplied
167
+ name = data[identify.to_sym]
168
+ raise ManagerError.new("#{identify} not supplied in call to get_id") if name.nil?
169
+
170
+ result = @client.api_request(
171
+ method: "#{method_name}.get",
172
+ params: {
173
+ filter: data,
174
+ output: [key, identify]
175
+ }
176
+ )
177
+ id = nil
178
+ result.each { |item| id = item[key].to_i if item[identify] == data[identify.to_sym] }
179
+ id
180
+ end
181
+
182
+ # Get or Create Zabbix object using API
183
+ #
184
+ # @param data [Hash] Should include object's id field name (identify) and id value
185
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
186
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
187
+ # @return [Integer] Zabbix object id
188
+ def get_or_create(data)
189
+ log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
190
+
191
+ unless (id = get_id(identify.to_sym => data[identify.to_sym]))
192
+ id = create(data)
193
+ end
194
+ id
195
+ end
196
+
197
+ # 根据数据自动创建
198
+ def create_raw(data)
199
+ log "[DEBUG] Call create_raw with parameters: #{data.inspect}"
200
+ # 请求创建数据
201
+ @client.api_request(
202
+ method: "#{method_name}.create",
203
+ params: data
204
+ )
205
+ end
206
+
207
+ # 根据数据自动更新数据
208
+ def update_raw(data)
209
+ log "[DEBUG] Call update_raw with parameters: #{data.inspect}"
210
+ # 请求创建数据
211
+ @client.api_request(
212
+ method: "#{method_name}.update",
213
+ params: data
214
+ )
215
+ end
216
+
217
+ # 根据数据自动删除数据
218
+ def delete_raw(data)
219
+ log "[DEBUG] Call delete_raw with parameters: #{data.inspect}"
220
+ # 请求创建数据
221
+ @client.api_request(
222
+ method: "#{method_name}.delete",
223
+ params: data
224
+ )
225
+ end
226
+ end
227
+ end
@@ -0,0 +1,41 @@
1
+ class ZabbixManager
2
+ class Actions < Basic
3
+ # The method name used for interacting with Actions via Zabbix API
4
+ #
5
+ # @return [String]
6
+ def method_name
7
+ 'action'
8
+ end
9
+
10
+ # The id field name used for identifying specific Action objects via Zabbix API
11
+ #
12
+ # @return [String]
13
+ def identify
14
+ 'name'
15
+ end
16
+
17
+ # Get full/extended Action object data from API
18
+ #
19
+ # @param data [Hash] Should include object's id field name (identify) and id value
20
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
21
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
22
+ # @return [Hash]
23
+ def get_full_data(data)
24
+ log "[DEBUG] Call get_full_data with parameters: #{data.inspect}"
25
+
26
+ @client.api_request(
27
+ method: "#{method_name}.get",
28
+ params: {
29
+ filter: {
30
+ identify.to_sym => data[identify.to_sym]
31
+ },
32
+ output: 'extend',
33
+ selectOperations: "extend",
34
+ selectRecoveryOperations: "extend",
35
+ selectAcknowledgeOperations: "extend",
36
+ selectFilter: "extend",
37
+ }
38
+ )
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,43 @@
1
+ class ZabbixManager
2
+ class Applications < Basic
3
+ # The method name used for interacting with Applications via Zabbix API
4
+ #
5
+ # @return [String]
6
+ def method_name
7
+ 'application'
8
+ end
9
+
10
+ # The id field name used for identifying specific Application objects via Zabbix API
11
+ #
12
+ # @return [String]
13
+ def identify
14
+ 'name'
15
+ end
16
+
17
+ # Get or Create Application object using Zabbix API
18
+ #
19
+ # @param data [Hash] Needs to include name and hostid to properly identify Applications via Zabbix API
20
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
21
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
22
+ # @return [Integer] Zabbix object id
23
+ def get_or_create(data)
24
+ log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
25
+
26
+ unless (id = get_id(name: data[:name], hostid: data[:hostid]))
27
+ id = create(data)
28
+ end
29
+ id
30
+ end
31
+
32
+ # Create or update Application object using Zabbix API
33
+ #
34
+ # @param data [Hash] Needs to include name and hostid to properly identify Applications via Zabbix API
35
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
36
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
37
+ # @return [Integer] Zabbix object id
38
+ def create_or_update(data)
39
+ applicationid = get_id(name: data[:name], hostid: data[:hostid])
40
+ applicationid ? update(data.merge(applicationid: applicationid)) : create(data)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,42 @@
1
+ class ZabbixManager
2
+ class Configurations < Basic
3
+ # @return [Boolean]
4
+ def array_flag
5
+ true
6
+ end
7
+
8
+ # The method name used for interacting with Configurations via Zabbix API
9
+ #
10
+ # @return [String]
11
+ def method_name
12
+ 'configuration'
13
+ end
14
+
15
+ # The id field name used for identifying specific Configuration objects via Zabbix API
16
+ #
17
+ # @return [String]
18
+ def identify
19
+ 'host'
20
+ end
21
+
22
+ # Export configuration data using Zabbix API
23
+ #
24
+ # @param data [Hash]
25
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
26
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
27
+ # @return [Hash]
28
+ def export(data)
29
+ @client.api_request(method: 'configuration.export', params: data)
30
+ end
31
+
32
+ # Import configuration data using Zabbix API
33
+ #
34
+ # @param data [Hash]
35
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
36
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
37
+ # @return [Hash]
38
+ def import(data)
39
+ @client.api_request(method: 'configuration.import', params: data)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,55 @@
1
+ class ZabbixManager
2
+ class Drules < Basic
3
+ # The method name used for interacting with Drules via Zabbix API
4
+ #
5
+ # @return [String]
6
+ def method_name
7
+ 'drule'
8
+ end
9
+
10
+ # The id field name used for identifying specific Drule objects via Zabbix API
11
+ #
12
+ # @return [String]
13
+ def identify
14
+ 'name'
15
+ end
16
+
17
+ # The default options used when creating Drule objects via Zabbix API
18
+ #
19
+ # @return [Hash]
20
+ def default_options
21
+ {
22
+ name: nil,
23
+ iprange: nil,
24
+ delay: 3600,
25
+ status: 0,
26
+ }
27
+ end
28
+
29
+ # Get or Create Drule object using Zabbix API
30
+ #
31
+ # @param data [Hash] Needs to include name to properly identify Drule via Zabbix API
32
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
33
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
34
+ # @return [Integer] Zabbix object id
35
+ def get_or_create(data)
36
+ log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
37
+
38
+ unless (id = get_id(name: data[:name]))
39
+ id = create(data)
40
+ end
41
+ id
42
+ end
43
+
44
+ # Create or update Drule object using Zabbix API
45
+ #
46
+ # @param data [Hash] Needs to include name to properly identify Drules via Zabbix API
47
+ # @raise [ManagerError] 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] Zabbix object id
50
+ def create_or_update(data)
51
+ druleid = get_id(name: data[:name])
52
+ druleid ? update(data.merge(druleid: druleid)) : create(data)
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,28 @@
1
+ class ZabbixManager
2
+ class BaseError < RuntimeError
3
+ attr_accessor :response, :error, :error_message
4
+
5
+ def initialize(message, response = nil)
6
+ super(message)
7
+ @response = response
8
+
9
+ set_error! if @response
10
+ end
11
+
12
+ private
13
+
14
+ def set_error!
15
+ @error = @response['error']
16
+ @error_message = "#{@error['message']}: #{@error['data']}"
17
+ rescue StandardError
18
+ @error = nil
19
+ @error_message = nil
20
+ end
21
+ end
22
+
23
+ class ManagerError < BaseError
24
+ end
25
+
26
+ class HttpError < BaseError
27
+ end
28
+ end