zabbixapi_mgx 5.0.0.pre.alpha1
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 +7 -0
- data/CHANGELOG.md +58 -0
- data/LICENSE.md +20 -0
- data/README.md +98 -0
- data/lib/zabbixapi/basic/basic_alias.rb +37 -0
- data/lib/zabbixapi/basic/basic_func.rb +103 -0
- data/lib/zabbixapi/basic/basic_init.rb +46 -0
- data/lib/zabbixapi/basic/basic_logic.rb +181 -0
- data/lib/zabbixapi/classes/actions.rb +41 -0
- data/lib/zabbixapi/classes/applications.rb +43 -0
- data/lib/zabbixapi/classes/configurations.rb +42 -0
- data/lib/zabbixapi/classes/drules.rb +55 -0
- data/lib/zabbixapi/classes/errors.rb +28 -0
- data/lib/zabbixapi/classes/events.rb +17 -0
- data/lib/zabbixapi/classes/graphs.rb +111 -0
- data/lib/zabbixapi/classes/hostgroups.rb +24 -0
- data/lib/zabbixapi/classes/hosts.rb +80 -0
- data/lib/zabbixapi/classes/httptests.rb +54 -0
- data/lib/zabbixapi/classes/items.rb +83 -0
- data/lib/zabbixapi/classes/maintenance.rb +17 -0
- data/lib/zabbixapi/classes/mediatypes.rb +109 -0
- data/lib/zabbixapi/classes/problems.rb +101 -0
- data/lib/zabbixapi/classes/proxies.rb +48 -0
- data/lib/zabbixapi/classes/roles.rb +114 -0
- data/lib/zabbixapi/classes/screens.rb +94 -0
- data/lib/zabbixapi/classes/scripts.rb +35 -0
- data/lib/zabbixapi/classes/server.rb +16 -0
- data/lib/zabbixapi/classes/templates.rb +106 -0
- data/lib/zabbixapi/classes/triggers.rb +105 -0
- data/lib/zabbixapi/classes/unusable.rb +8 -0
- data/lib/zabbixapi/classes/usergroups.rb +73 -0
- data/lib/zabbixapi/classes/usermacros.rb +228 -0
- data/lib/zabbixapi/classes/users.rb +64 -0
- data/lib/zabbixapi/classes/valuemaps.rb +50 -0
- data/lib/zabbixapi/client.rb +167 -0
- data/lib/zabbixapi/version.rb +3 -0
- data/lib/zabbixapi.rb +194 -0
- data/zabbixapi.gemspec +29 -0
- metadata +124 -0
@@ -0,0 +1,228 @@
|
|
1
|
+
class ZabbixApi
|
2
|
+
class Usermacros < Basic
|
3
|
+
# The id field name used for identifying specific User macro objects via Zabbix API
|
4
|
+
#
|
5
|
+
# @return [String]
|
6
|
+
def identify
|
7
|
+
'macro'
|
8
|
+
end
|
9
|
+
|
10
|
+
# The method name used for interacting with User macros via Zabbix API
|
11
|
+
#
|
12
|
+
# @return [String]
|
13
|
+
def method_name
|
14
|
+
'usermacro'
|
15
|
+
end
|
16
|
+
|
17
|
+
# Get User macro object id from Zabbix API based on provided data
|
18
|
+
#
|
19
|
+
# @param data [Hash] Needs to include macro to properly identify user macros via Zabbix API
|
20
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call or missing object's id field name (identify).
|
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_id(data)
|
24
|
+
log "[DEBUG] Call get_id with parameters: #{data.inspect}"
|
25
|
+
|
26
|
+
# symbolize keys if the user used string keys instead of symbols
|
27
|
+
data = symbolize_keys(data) if data.key?(identify)
|
28
|
+
# raise an error if identify name was not supplied
|
29
|
+
name = data[identify.to_sym]
|
30
|
+
raise ApiError.new("#{identify} not supplied in call to get_id") if name.nil?
|
31
|
+
|
32
|
+
result = request(data, 'usermacro.get', 'hostmacroid')
|
33
|
+
|
34
|
+
!result.empty? && result[0].key?('hostmacroid') ? result[0]['hostmacroid'].to_i : nil
|
35
|
+
end
|
36
|
+
|
37
|
+
# Get Global macro object id from Zabbix API based on provided data
|
38
|
+
#
|
39
|
+
# @param data [Hash] Needs to include macro to properly identify global macros via Zabbix API
|
40
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call or missing object's id field name (identify).
|
41
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
42
|
+
# @return [Integer] Zabbix object id
|
43
|
+
def get_id_global(data)
|
44
|
+
log "[DEBUG] Call get_id_global with parameters: #{data.inspect}"
|
45
|
+
|
46
|
+
# symbolize keys if the user used string keys instead of symbols
|
47
|
+
data = symbolize_keys(data) if data.key?(identify)
|
48
|
+
# raise an error if identify name was not supplied
|
49
|
+
name = data[identify.to_sym]
|
50
|
+
raise ApiError.new("#{identify} not supplied in call to get_id_global") if name.nil?
|
51
|
+
|
52
|
+
result = request(data, 'usermacro.get', 'globalmacroid')
|
53
|
+
|
54
|
+
!result.empty? && result[0].key?('globalmacroid') ? result[0]['globalmacroid'].to_i : nil
|
55
|
+
end
|
56
|
+
|
57
|
+
# Get full/extended User macro data from Zabbix API
|
58
|
+
#
|
59
|
+
# @param data [Hash] Should include object's id field name (identify) and id value
|
60
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
61
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
62
|
+
# @return [Hash]
|
63
|
+
def get_full_data(data)
|
64
|
+
log "[DEBUG] Call get_full_data with parameters: #{data.inspect}"
|
65
|
+
|
66
|
+
request(data, 'usermacro.get', 'hostmacroid')
|
67
|
+
end
|
68
|
+
|
69
|
+
# Get full/extended Global macro data from Zabbix API
|
70
|
+
#
|
71
|
+
# @param data [Hash] Should include object's id field name (identify) and id value
|
72
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
73
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
74
|
+
# @return [Hash]
|
75
|
+
def get_full_data_global(data)
|
76
|
+
log "[DEBUG] Call get_full_data_global with parameters: #{data.inspect}"
|
77
|
+
|
78
|
+
request(data, 'usermacro.get', 'globalmacroid')
|
79
|
+
end
|
80
|
+
|
81
|
+
# Create new User macro object using Zabbix API (with defaults)
|
82
|
+
#
|
83
|
+
# @param data [Hash] Needs to include hostid, macro, and value to create User macro via Zabbix API
|
84
|
+
# @raise [ApiError] 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] The object id if a single object is created
|
87
|
+
# @return [Boolean] True/False if multiple objects are created
|
88
|
+
def create(data)
|
89
|
+
request(data, 'usermacro.create', 'hostmacroids')
|
90
|
+
end
|
91
|
+
|
92
|
+
# Create new Global macro object using Zabbix API (with defaults)
|
93
|
+
#
|
94
|
+
# @param data [Hash] Needs to include hostid, macro, and value to create Global macro via Zabbix API
|
95
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
96
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
97
|
+
# @return [Integer] The object id if a single object is created
|
98
|
+
# @return [Boolean] True/False if multiple objects are created
|
99
|
+
def create_global(data)
|
100
|
+
request(data, 'usermacro.createglobal', 'globalmacroids')
|
101
|
+
end
|
102
|
+
|
103
|
+
# Delete User macro object using Zabbix API
|
104
|
+
#
|
105
|
+
# @param data [Hash] Should include hostmacroid's of User macros to delete
|
106
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
107
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
108
|
+
# @return [Integer] The object id if a single object is deleted
|
109
|
+
# @return [Boolean] True/False if multiple objects are deleted
|
110
|
+
def delete(data)
|
111
|
+
data_delete = [data]
|
112
|
+
request(data_delete, 'usermacro.delete', 'hostmacroids')
|
113
|
+
end
|
114
|
+
|
115
|
+
# Delete Global macro object using Zabbix API
|
116
|
+
#
|
117
|
+
# @param data [Hash] Should include hostmacroid's of Global macros to delete
|
118
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
119
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
120
|
+
# @return [Integer] The object id if a single object is deleted
|
121
|
+
# @return [Boolean] True/False if multiple objects are deleted
|
122
|
+
def delete_global(data)
|
123
|
+
data_delete = [data]
|
124
|
+
request(data_delete, 'usermacro.deleteglobal', 'globalmacroids')
|
125
|
+
end
|
126
|
+
|
127
|
+
# Update User macro object using Zabbix API
|
128
|
+
#
|
129
|
+
# @param data [Hash] Should include object's id field name (identify), id value, and fields to update
|
130
|
+
# @param force [Boolean] Whether to force an object update even if provided data matches Zabbix
|
131
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
132
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
133
|
+
# @return [Integer] The object id if a single object is created
|
134
|
+
# @return [Boolean] True/False if multiple objects are created
|
135
|
+
def update(data)
|
136
|
+
request(data, 'usermacro.update', 'hostmacroids')
|
137
|
+
end
|
138
|
+
|
139
|
+
# Update Global macro object using Zabbix API
|
140
|
+
#
|
141
|
+
# @param data [Hash] Should include object's id field name (identify), id value, and fields to update
|
142
|
+
# @param force [Boolean] Whether to force an object update even if provided data matches Zabbix
|
143
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
144
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
145
|
+
# @return [Integer] The object id if a single object is created
|
146
|
+
# @return [Boolean] True/False if multiple objects are created
|
147
|
+
def update_global(data)
|
148
|
+
request(data, 'usermacro.updateglobal', 'globalmacroids')
|
149
|
+
end
|
150
|
+
|
151
|
+
# Get or Create User macro object using Zabbix API
|
152
|
+
#
|
153
|
+
# @param data [Hash] Needs to include macro and hostid to properly identify User macros via Zabbix API
|
154
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
155
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
156
|
+
# @return [Integer] Zabbix object id
|
157
|
+
def get_or_create(data)
|
158
|
+
log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
|
159
|
+
|
160
|
+
unless (id = get_id(macro: data[:macro], hostid: data[:hostid]))
|
161
|
+
id = create(data)
|
162
|
+
end
|
163
|
+
id
|
164
|
+
end
|
165
|
+
|
166
|
+
# Get or Create Global macro object using Zabbix API
|
167
|
+
#
|
168
|
+
# @param data [Hash] Needs to include macro and hostid to properly identify Global macros via Zabbix API
|
169
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
170
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
171
|
+
# @return [Integer] Zabbix object id
|
172
|
+
def get_or_create_global(data)
|
173
|
+
log "[DEBUG] Call get_or_create_global with parameters: #{data.inspect}"
|
174
|
+
|
175
|
+
unless (id = get_id_global(macro: data[:macro], hostid: data[:hostid]))
|
176
|
+
id = create_global(data)
|
177
|
+
end
|
178
|
+
id
|
179
|
+
end
|
180
|
+
|
181
|
+
# Create or update User macro object using Zabbix API
|
182
|
+
#
|
183
|
+
# @param data [Hash] Needs to include macro and hostid to properly identify User macros via Zabbix API
|
184
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
185
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
186
|
+
# @return [Integer] Zabbix object id
|
187
|
+
def create_or_update(data)
|
188
|
+
hostmacroid = get_id(macro: data[:macro], hostid: data[:hostid])
|
189
|
+
hostmacroid ? update(data.merge(hostmacroid: hostmacroid)) : create(data)
|
190
|
+
end
|
191
|
+
|
192
|
+
# Create or update Global macro object using Zabbix API
|
193
|
+
#
|
194
|
+
# @param data [Hash] Needs to include macro and hostid to properly identify Global macros via Zabbix API
|
195
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
196
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
197
|
+
# @return [Integer] Zabbix object id
|
198
|
+
def create_or_update_global(data)
|
199
|
+
globalmacroid = get_id_global(macro: data[:macro], hostid: data[:hostid])
|
200
|
+
globalmacroid ? update_global(data.merge(globalmacroid: globalmacroid)) : create_global(data)
|
201
|
+
end
|
202
|
+
|
203
|
+
private
|
204
|
+
|
205
|
+
# Custom request method to handle both User and Global macros in one
|
206
|
+
#
|
207
|
+
# @param data [Hash] Needs to include macro and hostid to properly identify Global macros via Zabbix API
|
208
|
+
# @param method [String] Zabbix API method to use for the request
|
209
|
+
# @param result_key [String] Which key to use for parsing results based on User vs Global macros
|
210
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
211
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
212
|
+
# @return [Integer] Zabbix object id
|
213
|
+
def request(data, method, result_key)
|
214
|
+
# Zabbix has different result formats for gets vs updates
|
215
|
+
if method.include?('.get')
|
216
|
+
if result_key.include?('global')
|
217
|
+
@client.api_request(method: method, params: { globalmacro: true, filter: data })
|
218
|
+
else
|
219
|
+
@client.api_request(method: method, params: { filter: data })
|
220
|
+
end
|
221
|
+
else
|
222
|
+
result = @client.api_request(method: method, params: data)
|
223
|
+
|
224
|
+
result.key?(result_key) && !result[result_key].empty? ? result[result_key][0].to_i : nil
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
class ZabbixApi
|
2
|
+
class Users < Basic
|
3
|
+
# The method name used for interacting with Users via Zabbix API
|
4
|
+
#
|
5
|
+
# @return [String]
|
6
|
+
def method_name
|
7
|
+
'user'
|
8
|
+
end
|
9
|
+
|
10
|
+
# The keys field name used for User objects via Zabbix API
|
11
|
+
#
|
12
|
+
# @return [String]
|
13
|
+
def keys
|
14
|
+
'userids'
|
15
|
+
end
|
16
|
+
|
17
|
+
# The key field name used for User objects via Zabbix API
|
18
|
+
#
|
19
|
+
# @return [String]
|
20
|
+
def key
|
21
|
+
'userid'
|
22
|
+
end
|
23
|
+
|
24
|
+
# The id field name used for identifying specific User objects via Zabbix API
|
25
|
+
#
|
26
|
+
# @return [String]
|
27
|
+
def identify
|
28
|
+
'alias'
|
29
|
+
end
|
30
|
+
|
31
|
+
def medias_helper(data, action)
|
32
|
+
result = @client.api_request(
|
33
|
+
method: "user.#{action}",
|
34
|
+
params: data[:userids].map do |t|
|
35
|
+
{
|
36
|
+
userid: t,
|
37
|
+
user_medias: data[:media],
|
38
|
+
}
|
39
|
+
end,
|
40
|
+
)
|
41
|
+
result ? result['userids'][0].to_i : nil
|
42
|
+
end
|
43
|
+
|
44
|
+
# Add media to users using Zabbix API
|
45
|
+
#
|
46
|
+
# @param data [Hash] Needs to include userids and media to mass add media to users
|
47
|
+
# @raise [ApiError] 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 (media)
|
50
|
+
def add_medias(data)
|
51
|
+
medias_helper(data, 'update')
|
52
|
+
end
|
53
|
+
|
54
|
+
# Update media for users using Zabbix API
|
55
|
+
#
|
56
|
+
# @param data [Hash] Needs to include userids and media to mass update media for users
|
57
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
58
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
59
|
+
# @return [Integer] Zabbix object id (user)
|
60
|
+
def update_medias(data)
|
61
|
+
medias_helper(data, 'update')
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
class ZabbixApi
|
2
|
+
class ValueMaps < Basic
|
3
|
+
# The method name used for interacting with ValueMaps via Zabbix API
|
4
|
+
#
|
5
|
+
# @return [String]
|
6
|
+
def method_name
|
7
|
+
'valuemap'
|
8
|
+
end
|
9
|
+
|
10
|
+
# The key field name used for ValueMap objects via Zabbix API
|
11
|
+
#
|
12
|
+
# @return [String]
|
13
|
+
def key
|
14
|
+
'valuemapid'
|
15
|
+
end
|
16
|
+
|
17
|
+
# The id field name used for identifying specific ValueMap objects via Zabbix API
|
18
|
+
#
|
19
|
+
# @return [String]
|
20
|
+
def identify
|
21
|
+
'name'
|
22
|
+
end
|
23
|
+
|
24
|
+
# Get or Create ValueMap object using Zabbix API
|
25
|
+
#
|
26
|
+
# @param data [Hash] Needs to include valuemapids [List] to properly identify ValueMaps via Zabbix API
|
27
|
+
# @raise [ApiError] 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] Zabbix object id
|
30
|
+
def get_or_create(data)
|
31
|
+
log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
|
32
|
+
|
33
|
+
unless (id = get_id(valuemapids: data[:valuemapids]))
|
34
|
+
id = create(data)
|
35
|
+
end
|
36
|
+
id
|
37
|
+
end
|
38
|
+
|
39
|
+
# Create or update Item object using Zabbix API
|
40
|
+
#
|
41
|
+
# @param data [Hash] Needs to include valuemapids to properly identify ValueMaps via Zabbix API
|
42
|
+
# @raise [ApiError] 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 [Integer] Zabbix object id
|
45
|
+
def create_or_update(data)
|
46
|
+
valuemapid = get_id(name: data[:name])
|
47
|
+
valuemapid ? update(data.merge(valuemapids: [:valuemapid])) : create(data)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,167 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
require 'openssl'
|
4
|
+
|
5
|
+
class ZabbixApi
|
6
|
+
class Client
|
7
|
+
# @param (see ZabbixApi::Client#initialize)
|
8
|
+
# @return [Hash]
|
9
|
+
attr_reader :options
|
10
|
+
|
11
|
+
# @return [Integer]
|
12
|
+
def id
|
13
|
+
rand(100_000)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Returns the API version from the Zabbix Server
|
17
|
+
#
|
18
|
+
# @return [String]
|
19
|
+
def api_version
|
20
|
+
@api_version ||= api_request(method: 'apiinfo.version', params: {})
|
21
|
+
@api_version
|
22
|
+
end
|
23
|
+
|
24
|
+
# Log in to the Zabbix Server and generate an auth token using the API
|
25
|
+
#
|
26
|
+
# @return [Hash]
|
27
|
+
def auth
|
28
|
+
api_request(
|
29
|
+
method: 'user.login',
|
30
|
+
params: {
|
31
|
+
user: @options[:user],
|
32
|
+
password: @options[:password]
|
33
|
+
}
|
34
|
+
)
|
35
|
+
end
|
36
|
+
|
37
|
+
# ZabbixApi::Basic.log does not like @client.options[:debug]
|
38
|
+
#
|
39
|
+
# @return [boolean]
|
40
|
+
def debug?
|
41
|
+
return ! @options || @options[:debug]
|
42
|
+
end
|
43
|
+
|
44
|
+
# Log out from the Zabbix Server
|
45
|
+
#
|
46
|
+
# @return [Boolean]
|
47
|
+
#
|
48
|
+
def logout
|
49
|
+
api_request(
|
50
|
+
:method => 'user.logout',
|
51
|
+
:params => []
|
52
|
+
)
|
53
|
+
end
|
54
|
+
|
55
|
+
# Initializes a new Client object
|
56
|
+
#
|
57
|
+
# @param options [Hash]
|
58
|
+
# @option opts [String] :url The url of zabbixapi(example: 'http://localhost/zabbix/api_jsonrpc.php')
|
59
|
+
# @option opts [String] :user
|
60
|
+
# @option opts [String] :password
|
61
|
+
# @option opts [String] :http_user A user for basic auth.(optional)
|
62
|
+
# @option opts [String] :http_password A password for basic auth.(optional)
|
63
|
+
# @option opts [Integer] :timeout Set timeout for requests in seconds.(default: 60)
|
64
|
+
#
|
65
|
+
# @return [ZabbixApi::Client]
|
66
|
+
def initialize(options = {})
|
67
|
+
@options = options
|
68
|
+
if !ENV['http_proxy'].nil? && options[:no_proxy] != true
|
69
|
+
@proxy_uri = URI.parse(ENV['http_proxy'])
|
70
|
+
@proxy_host = @proxy_uri.host
|
71
|
+
@proxy_port = @proxy_uri.port
|
72
|
+
@proxy_user, @proxy_pass = @proxy_uri.userinfo.split(/:/) if @proxy_uri.userinfo
|
73
|
+
end
|
74
|
+
unless api_version =~ %r{^5.[0|2]\.\d+$}
|
75
|
+
message = "Zabbix API version: #{api_version} is not supported by this version of zabbixapi"
|
76
|
+
if @options[:ignore_version]
|
77
|
+
puts "[WARNING] #{message}" if @options[:debug]
|
78
|
+
else
|
79
|
+
raise ZabbixApi::ApiError.new(message)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
@auth_hash = auth
|
84
|
+
end
|
85
|
+
|
86
|
+
# Convert message body to JSON string for the Zabbix API
|
87
|
+
#
|
88
|
+
# @param body [Hash]
|
89
|
+
# @return [String]
|
90
|
+
def message_json(body)
|
91
|
+
message = {
|
92
|
+
method: body[:method],
|
93
|
+
params: body[:params],
|
94
|
+
id: id,
|
95
|
+
jsonrpc: '2.0'
|
96
|
+
}
|
97
|
+
|
98
|
+
message[:auth] = @auth_hash unless body[:method] == 'apiinfo.version' || body[:method] == 'user.login'
|
99
|
+
|
100
|
+
JSON.generate(message)
|
101
|
+
end
|
102
|
+
|
103
|
+
# @param body [String]
|
104
|
+
# @return [String]
|
105
|
+
def http_request(body)
|
106
|
+
uri = URI.parse(@options[:url])
|
107
|
+
|
108
|
+
# set the time out the default (60) or to what the user passed
|
109
|
+
timeout = @options[:timeout].nil? ? 60 : @options[:timeout]
|
110
|
+
puts "[DEBUG] Timeout for request set to #{timeout} seconds" if @options[:debug]
|
111
|
+
|
112
|
+
http =
|
113
|
+
if @proxy_uri
|
114
|
+
Net::HTTP.Proxy(@proxy_host, @proxy_port, @proxy_user, @proxy_pass).new(uri.host, uri.port)
|
115
|
+
else
|
116
|
+
Net::HTTP.new(uri.host, uri.port)
|
117
|
+
end
|
118
|
+
|
119
|
+
if uri.scheme == 'https'
|
120
|
+
http.use_ssl = true
|
121
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
122
|
+
end
|
123
|
+
|
124
|
+
http.open_timeout = timeout
|
125
|
+
http.read_timeout = timeout
|
126
|
+
|
127
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
128
|
+
request.basic_auth @options[:http_user], @options[:http_password] if @options[:http_user]
|
129
|
+
request.add_field('Content-Type', 'application/json-rpc')
|
130
|
+
request.body = body
|
131
|
+
|
132
|
+
response = http.request(request)
|
133
|
+
|
134
|
+
raise HttpError.new("HTTP Error: #{response.code} on #{@options[:url]}", response) unless response.code == '200'
|
135
|
+
|
136
|
+
puts "[DEBUG] Get answer: #{response.body}" if @options[:debug]
|
137
|
+
response.body
|
138
|
+
end
|
139
|
+
|
140
|
+
# @param body [String]
|
141
|
+
# @return [Hash, String]
|
142
|
+
def _request(body)
|
143
|
+
puts "[DEBUG] Send request: #{body}" if @options[:debug]
|
144
|
+
result = JSON.parse(http_request(body))
|
145
|
+
raise ApiError.new("Server answer API error\n #{JSON.pretty_unparse(result['error'])}\n on request:\n #{pretty_body(body)}", result) if result['error']
|
146
|
+
|
147
|
+
result['result']
|
148
|
+
end
|
149
|
+
|
150
|
+
def pretty_body(body)
|
151
|
+
parsed_body = JSON.parse(body)
|
152
|
+
|
153
|
+
# If password is in body hide it
|
154
|
+
parsed_body['params']['password'] = '***' if parsed_body['params'].is_a?(Hash) && parsed_body['params'].key?('password')
|
155
|
+
|
156
|
+
JSON.pretty_unparse(parsed_body)
|
157
|
+
end
|
158
|
+
|
159
|
+
# Execute Zabbix API requests and return response
|
160
|
+
#
|
161
|
+
# @param body [Hash]
|
162
|
+
# @return [Hash, String]
|
163
|
+
def api_request(body)
|
164
|
+
_request message_json(body)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|