zabbixapi 3.1.0 → 3.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardopts +9 -0
- data/CHANGELOG.md +5 -0
- data/{LICENSE → LICENSE.md} +1 -1
- data/README.md +53 -569
- data/lib/zabbixapi.rb +102 -65
- data/lib/zabbixapi/basic/basic_alias.rb +21 -4
- data/lib/zabbixapi/basic/basic_func.rb +56 -23
- data/lib/zabbixapi/basic/basic_init.rb +21 -4
- data/lib/zabbixapi/basic/basic_logic.rb +75 -18
- data/lib/zabbixapi/classes/actions.rb +8 -4
- data/lib/zabbixapi/classes/applications.rb +20 -6
- data/lib/zabbixapi/classes/configurations.rb +23 -17
- data/lib/zabbixapi/classes/errors.rb +2 -4
- data/lib/zabbixapi/classes/graphs.rb +65 -15
- data/lib/zabbixapi/classes/hostgroups.rb +12 -4
- data/lib/zabbixapi/classes/hosts.rb +36 -10
- data/lib/zabbixapi/classes/httptests.rb +24 -4
- data/lib/zabbixapi/classes/items.rb +24 -5
- data/lib/zabbixapi/classes/maintenance.rb +8 -4
- data/lib/zabbixapi/classes/mediatypes.rb +20 -13
- data/lib/zabbixapi/classes/proxies.rb +29 -8
- data/lib/zabbixapi/classes/screens.rb +41 -25
- data/lib/zabbixapi/classes/server.rb +8 -4
- data/lib/zabbixapi/classes/templates.rb +46 -43
- data/lib/zabbixapi/classes/triggers.rb +43 -16
- data/lib/zabbixapi/classes/unusable.rb +0 -2
- data/lib/zabbixapi/classes/usergroups.rb +33 -26
- data/lib/zabbixapi/classes/usermacros.rb +137 -31
- data/lib/zabbixapi/classes/users.rb +32 -10
- data/lib/zabbixapi/classes/valuemaps.rb +50 -0
- data/lib/zabbixapi/client.rb +61 -22
- data/lib/zabbixapi/version.rb +1 -1
- data/zabbixapi.gemspec +26 -23
- metadata +25 -73
- data/.gitignore +0 -7
- data/.rspec +0 -1
- data/.travis.yml +0 -41
- data/Gemfile +0 -3
- data/Gemfile.lock +0 -36
- data/Rakefile +0 -1
- data/json-1.x.Gemfile +0 -4
- data/json-1.x.Gemfile.lock +0 -28
- data/spec/action.rb +0 -89
- data/spec/application.rb +0 -83
- data/spec/basic_func.rb +0 -4
- data/spec/configuration.rb +0 -122
- data/spec/graph.rb +0 -135
- data/spec/host.rb +0 -176
- data/spec/hostgroup.rb +0 -55
- data/spec/httptest.rb +0 -136
- data/spec/item.rb +0 -134
- data/spec/maintenance.rb +0 -81
- data/spec/mediatype.rb +0 -50
- data/spec/query.rb +0 -18
- data/spec/screen.rb +0 -88
- data/spec/script.rb +0 -123
- data/spec/server.rb +0 -15
- data/spec/spec_helper.rb +0 -21
- data/spec/template.rb +0 -148
- data/spec/trigger.rb +0 -103
- data/spec/user.rb +0 -116
- data/spec/usergroup.rb +0 -85
- data/spec/usermacro.rb +0 -190
@@ -1,32 +1,49 @@
|
|
1
1
|
class ZabbixApi
|
2
2
|
class Triggers < Basic
|
3
|
-
|
3
|
+
# The method name used for interacting with Triggers via Zabbix API
|
4
|
+
#
|
5
|
+
# @return [String]
|
4
6
|
def method_name
|
5
|
-
|
7
|
+
'trigger'
|
6
8
|
end
|
7
9
|
|
10
|
+
# The id field name used for identifying specific Trigger objects via Zabbix API
|
11
|
+
#
|
12
|
+
# @return [String]
|
8
13
|
def indentify
|
9
|
-
|
14
|
+
'description'
|
10
15
|
end
|
11
16
|
|
17
|
+
# Dump Trigger object data by key from Zabbix API
|
18
|
+
#
|
19
|
+
# @param data [Hash] Should include desired object's key and value
|
20
|
+
# @raise [ApiError] 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]
|
12
23
|
def dump_by_id(data)
|
13
24
|
log "[DEBUG] Call dump_by_id with parametrs: #{data.inspect}"
|
14
25
|
|
15
26
|
@client.api_request(
|
16
|
-
:method =>
|
27
|
+
:method => 'trigger.get',
|
17
28
|
:params => {
|
18
29
|
:filter => {
|
19
|
-
key.to_sym => data[key.to_sym]
|
30
|
+
key.to_sym => data[key.to_sym],
|
20
31
|
},
|
21
|
-
:output =>
|
22
|
-
:select_items =>
|
23
|
-
:select_functions =>
|
32
|
+
:output => 'extend',
|
33
|
+
:select_items => 'extend',
|
34
|
+
:select_functions => 'extend',
|
24
35
|
}
|
25
36
|
)
|
26
37
|
end
|
27
38
|
|
39
|
+
# Safely update Trigger object using Zabbix API by deleting and replacing trigger
|
40
|
+
#
|
41
|
+
# @param data [Hash] Needs to include description and hostid to properly identify Triggers 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
|
28
45
|
def safe_update(data)
|
29
|
-
log "[DEBUG] Call
|
46
|
+
log "[DEBUG] Call safe_update with parameters: #{data.inspect}"
|
30
47
|
|
31
48
|
dump = {}
|
32
49
|
item_id = data[key.to_sym].to_i
|
@@ -34,31 +51,36 @@ class ZabbixApi
|
|
34
51
|
dump = symbolize_keys(item) if item[key].to_i == data[key.to_sym].to_i
|
35
52
|
end
|
36
53
|
|
37
|
-
expression = dump[:items][0][:key_]+
|
38
|
-
dump[:expression] = dump[:expression].gsub(/\{(\d*)\}/,"{#{expression}}") #TODO ugly regexp
|
54
|
+
expression = dump[:items][0][:key_] + '.' + dump[:functions][0][:function] + '(' + dump[:functions][0][:parameter] + ')'
|
55
|
+
dump[:expression] = dump[:expression].gsub(/\{(\d*)\}/, "{#{expression}}") # TODO: ugly regexp
|
39
56
|
dump.delete(:functions)
|
40
57
|
dump.delete(:items)
|
41
58
|
|
42
59
|
old_expression = data[:expression]
|
43
|
-
data[:expression] = data[:expression].gsub(/\{.*\:/,
|
60
|
+
data[:expression] = data[:expression].gsub(/\{.*\:/, '{') # TODO: ugly regexp
|
44
61
|
data.delete(:templateid)
|
45
62
|
|
46
63
|
log "[DEBUG] expression: #{dump[:expression]}\n data: #{data[:expression]}"
|
47
64
|
|
48
65
|
if hash_equals?(dump, data)
|
49
|
-
log "[DEBUG] Equal keys #{dump} and #{data}, skip
|
66
|
+
log "[DEBUG] Equal keys #{dump} and #{data}, skip safe_update"
|
50
67
|
item_id
|
51
68
|
else
|
52
69
|
data[:expression] = old_expression
|
53
70
|
# disable old trigger
|
54
|
-
log
|
71
|
+
log '[DEBUG] disable :' + @client.api_request(:method => "#{method_name}.update", :params => [{:triggerid => data[:triggerid], :status => '1'}]).inspect
|
55
72
|
# create new trigger
|
56
73
|
data.delete(:triggerid)
|
57
74
|
create(data)
|
58
75
|
end
|
59
|
-
|
60
76
|
end
|
61
77
|
|
78
|
+
# Get or Create Trigger object using Zabbix API
|
79
|
+
#
|
80
|
+
# @param data [Hash] Needs to include description and hostid to properly identify Triggers via Zabbix API
|
81
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
82
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
83
|
+
# @return [Integer] Zabbix object id
|
62
84
|
def get_or_create(data)
|
63
85
|
log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
|
64
86
|
|
@@ -68,10 +90,15 @@ class ZabbixApi
|
|
68
90
|
id
|
69
91
|
end
|
70
92
|
|
93
|
+
# Create or update Trigger object using Zabbix API
|
94
|
+
#
|
95
|
+
# @param data [Hash] Needs to include description and hostid to properly identify Triggers via Zabbix API
|
96
|
+
# @raise [ApiError] 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] Zabbix object id
|
71
99
|
def create_or_update(data)
|
72
100
|
triggerid = get_id(:description => data[:description], :hostid => data[:hostid])
|
73
101
|
triggerid ? update(data.merge(:triggerid => triggerid)) : create(data)
|
74
102
|
end
|
75
|
-
|
76
103
|
end
|
77
104
|
end
|
@@ -1,69 +1,76 @@
|
|
1
1
|
class ZabbixApi
|
2
2
|
class Usergroups < Basic
|
3
|
-
|
3
|
+
# The method name used for interacting with Usergroups via Zabbix API
|
4
|
+
#
|
5
|
+
# @return [String]
|
4
6
|
def method_name
|
5
|
-
|
7
|
+
'usergroup'
|
6
8
|
end
|
7
9
|
|
10
|
+
# The key field name used for Usergroup objects via Zabbix API
|
11
|
+
#
|
12
|
+
# @return [String]
|
8
13
|
def key
|
9
|
-
|
14
|
+
'usrgrpid'
|
10
15
|
end
|
11
16
|
|
17
|
+
# The id field name used for identifying specific Usergroup objects via Zabbix API
|
18
|
+
#
|
19
|
+
# @return [String]
|
12
20
|
def indentify
|
13
|
-
|
21
|
+
'name'
|
14
22
|
end
|
15
23
|
|
16
|
-
# Set
|
24
|
+
# Set permissions for usergroup using Zabbix API
|
17
25
|
#
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
26
|
+
# @param data [Hash] Needs to include usrgrpids and hostgroupids along with permissions to set
|
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 (usergroup)
|
22
30
|
def set_perms(data)
|
23
31
|
permission = data[:permission] || 2
|
24
32
|
result = @client.api_request(
|
25
|
-
:method =>
|
33
|
+
:method => 'usergroup.massAdd',
|
26
34
|
:params => {
|
27
35
|
:usrgrpids => [data[:usrgrpid]],
|
28
|
-
:rights => data[:hostgroupids].map { |t| {:permission => permission, :id => t} }
|
36
|
+
:rights => data[:hostgroupids].map { |t| {:permission => permission, :id => t} },
|
29
37
|
}
|
30
38
|
)
|
31
39
|
result ? result['usrgrpids'][0].to_i : nil
|
32
40
|
end
|
33
41
|
|
34
|
-
#
|
42
|
+
# Add users to usergroup using Zabbix API
|
35
43
|
#
|
36
|
-
#
|
37
|
-
#
|
38
|
-
#
|
39
|
-
#
|
44
|
+
# @param data [Hash] Needs to include userids and usrgrpids to mass add users to groups
|
45
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
46
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
47
|
+
# @return [Integer] Zabbix object id (usergroup)
|
40
48
|
def add_user(data)
|
41
49
|
result = @client.api_request(
|
42
|
-
:method =>
|
50
|
+
:method => 'usergroup.massAdd',
|
43
51
|
:params => {
|
44
52
|
:usrgrpids => data[:usrgrpids],
|
45
|
-
:userids => data[:userids]
|
53
|
+
:userids => data[:userids],
|
46
54
|
}
|
47
55
|
)
|
48
56
|
result ? result['usrgrpids'][0].to_i : nil
|
49
57
|
end
|
50
58
|
|
51
|
-
# Update
|
59
|
+
# Update users in usergroups using Zabbix API
|
52
60
|
#
|
53
|
-
#
|
54
|
-
#
|
55
|
-
#
|
56
|
-
#
|
61
|
+
# @param data [Hash] Needs to include userids and usrgrpids to mass update users in groups
|
62
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
63
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
64
|
+
# @return [Integer] Zabbix object id (usergroup)
|
57
65
|
def update_users(data)
|
58
66
|
result = @client.api_request(
|
59
|
-
:method =>
|
67
|
+
:method => 'usergroup.massUpdate',
|
60
68
|
:params => {
|
61
69
|
:usrgrpids => data[:usrgrpids],
|
62
|
-
:userids => data[:userids]
|
70
|
+
:userids => data[:userids],
|
63
71
|
}
|
64
72
|
)
|
65
73
|
result ? result['usrgrpids'][0].to_i : nil
|
66
74
|
end
|
67
|
-
|
68
75
|
end
|
69
76
|
end
|
@@ -1,79 +1,159 @@
|
|
1
1
|
class ZabbixApi
|
2
2
|
class Usermacros < Basic
|
3
|
+
# The id field name used for identifying specific User macro objects via Zabbix API
|
4
|
+
#
|
5
|
+
# @return [String]
|
3
6
|
def indentify
|
4
|
-
|
7
|
+
'macro'
|
5
8
|
end
|
6
9
|
|
10
|
+
# The method name used for interacting with User macros via Zabbix API
|
11
|
+
#
|
12
|
+
# @return [String]
|
7
13
|
def method_name
|
8
|
-
|
14
|
+
'usermacro'
|
9
15
|
end
|
10
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 (indentify).
|
21
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
22
|
+
# @return [Integer] Zabbix object id
|
11
23
|
def get_id(data)
|
12
24
|
log "[DEBUG] Call get_id with parameters: #{data.inspect}"
|
13
25
|
|
14
26
|
# symbolize keys if the user used string keys instead of symbols
|
15
27
|
data = symbolize_keys(data) if data.key?(indentify)
|
16
|
-
# raise an error if indentify name was not supplied
|
28
|
+
# raise an error if indentify name was not supplied
|
17
29
|
name = data[indentify.to_sym]
|
18
|
-
raise ApiError.new("#{indentify} not supplied in call to get_id") if name
|
30
|
+
raise ApiError.new("#{indentify} not supplied in call to get_id") if name.nil?
|
19
31
|
|
20
|
-
result = request(data,
|
32
|
+
result = request(data, 'usermacro.get', 'hostmacroid')
|
21
33
|
|
22
|
-
result.
|
34
|
+
!result.empty? && result[0].key?('hostmacroid') ? result[0]['hostmacroid'].to_i : nil
|
23
35
|
end
|
24
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 (indentify).
|
41
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
42
|
+
# @return [Integer] Zabbix object id
|
25
43
|
def get_id_global(data)
|
26
44
|
log "[DEBUG] Call get_id_global with parameters: #{data.inspect}"
|
27
45
|
|
28
46
|
# symbolize keys if the user used string keys instead of symbols
|
29
47
|
data = symbolize_keys(data) if data.key?(indentify)
|
30
|
-
# raise an error if indentify name was not supplied
|
48
|
+
# raise an error if indentify name was not supplied
|
31
49
|
name = data[indentify.to_sym]
|
32
|
-
raise ApiError.new("#{indentify} not supplied in call to get_id") if name
|
50
|
+
raise ApiError.new("#{indentify} not supplied in call to get_id") if name.nil?
|
33
51
|
|
34
|
-
result = request(data,
|
52
|
+
result = request(data, 'usermacro.get', 'globalmacroid')
|
35
53
|
|
36
|
-
result.
|
54
|
+
!result.empty? && result[0].key?('globalmacroid') ? result[0]['globalmacroid'].to_i : nil
|
37
55
|
end
|
38
56
|
|
57
|
+
# Get full/extended User macro data from Zabbix API
|
58
|
+
#
|
59
|
+
# @param data [Hash] Should include object's id field name (indentify) 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]
|
39
63
|
def get_full_data(data)
|
40
64
|
log "[DEBUG] Call get_full_data with parameters: #{data.inspect}"
|
41
65
|
|
42
|
-
request(data,
|
66
|
+
request(data, 'usermacro.get', 'hostmacroid')
|
43
67
|
end
|
44
68
|
|
69
|
+
# Get full/extended Global macro data from Zabbix API
|
70
|
+
#
|
71
|
+
# @param data [Hash] Should include object's id field name (indentify) 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]
|
45
75
|
def get_full_data_global(data)
|
46
76
|
log "[DEBUG] Call get_full_data_global with parameters: #{data.inspect}"
|
47
77
|
|
48
|
-
request(data,
|
78
|
+
request(data, 'usermacro.get', 'globalmacroid')
|
49
79
|
end
|
50
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
|
51
88
|
def create(data)
|
52
|
-
request(data,
|
89
|
+
request(data, 'usermacro.create', 'hostmacroids')
|
53
90
|
end
|
54
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
|
55
99
|
def create_global(data)
|
56
|
-
request(data,
|
100
|
+
request(data, 'usermacro.createglobal', 'globalmacroids')
|
57
101
|
end
|
58
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
|
59
110
|
def delete(data)
|
60
111
|
data_delete = [data]
|
61
|
-
request(data_delete,
|
112
|
+
request(data_delete, 'usermacro.delete', 'hostmacroids')
|
62
113
|
end
|
63
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
|
64
122
|
def delete_global(data)
|
65
123
|
data_delete = [data]
|
66
|
-
request(data_delete,
|
124
|
+
request(data_delete, 'usermacro.deleteglobal', 'globalmacroids')
|
67
125
|
end
|
68
126
|
|
127
|
+
# Update User macro object using Zabbix API
|
128
|
+
#
|
129
|
+
# @param data [Hash] Should include object's id field name (indentify), 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
|
69
135
|
def update(data)
|
70
|
-
request(data,
|
136
|
+
request(data, 'usermacro.update', 'hostmacroids')
|
71
137
|
end
|
72
138
|
|
139
|
+
# Update Global macro object using Zabbix API
|
140
|
+
#
|
141
|
+
# @param data [Hash] Should include object's id field name (indentify), 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
|
73
147
|
def update_global(data)
|
74
|
-
request(data,
|
148
|
+
request(data, 'usermacro.updateglobal', 'globalmacroids')
|
75
149
|
end
|
76
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
|
77
157
|
def get_or_create(data)
|
78
158
|
log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
|
79
159
|
|
@@ -83,6 +163,12 @@ class ZabbixApi
|
|
83
163
|
id
|
84
164
|
end
|
85
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
|
86
172
|
def get_or_create_global(data)
|
87
173
|
log "[DEBUG] Call get_or_create_global with parameters: #{data.inspect}"
|
88
174
|
|
@@ -92,31 +178,51 @@ class ZabbixApi
|
|
92
178
|
id
|
93
179
|
end
|
94
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
|
95
187
|
def create_or_update(data)
|
96
188
|
hostmacroid = get_id(:macro => data[:macro], :hostid => data[:hostid])
|
97
189
|
hostmacroid ? update(data.merge(:hostmacroid => hostmacroid)) : create(data)
|
98
190
|
end
|
99
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
|
100
198
|
def create_or_update_global(data)
|
101
199
|
hostmacroid = get_id_global(:macro => data[:macro], :hostid => data[:hostid])
|
102
200
|
hostmacroid ? update_global(data.merge(:globalmacroid => globalmacroid)) : create_global(data)
|
103
201
|
end
|
104
202
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
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})
|
114
218
|
else
|
115
|
-
|
116
|
-
|
117
|
-
result.key?(result_key) && result[result_key].length > 0 ? result[result_key][0].to_i : nil
|
219
|
+
@client.api_request(:method => method, :params => {:filter => data})
|
118
220
|
end
|
119
|
-
|
221
|
+
else
|
222
|
+
result = @client.api_request(:method => method, :params => data)
|
120
223
|
|
224
|
+
result.key?(result_key) && !result[result_key].empty? ? result[result_key][0].to_i : nil
|
225
|
+
end
|
226
|
+
end
|
121
227
|
end
|
122
228
|
end
|