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.
- checksums.yaml +4 -4
- data/lib/zabbix_manager/basic/basic_alias.rb +37 -0
- data/lib/zabbix_manager/basic/basic_extend.rb +38 -0
- data/lib/zabbix_manager/basic/basic_func.rb +103 -0
- data/lib/zabbix_manager/basic/basic_init.rb +46 -0
- data/lib/zabbix_manager/basic/basic_logic.rb +227 -0
- data/lib/zabbix_manager/classes/actions.rb +41 -0
- data/lib/zabbix_manager/classes/applications.rb +43 -0
- data/lib/zabbix_manager/classes/configurations.rb +42 -0
- data/lib/zabbix_manager/classes/drules.rb +55 -0
- data/lib/zabbix_manager/classes/errors.rb +28 -0
- data/lib/zabbix_manager/classes/events.rb +18 -0
- data/lib/zabbix_manager/classes/graphs.rb +111 -0
- data/lib/zabbix_manager/classes/hostgroups.rb +58 -0
- data/lib/zabbix_manager/classes/hosts.rb +218 -0
- data/lib/zabbix_manager/classes/httptests.rb +54 -0
- data/lib/zabbix_manager/classes/items.rb +145 -0
- data/lib/zabbix_manager/classes/maintenance.rb +17 -0
- data/lib/zabbix_manager/classes/mediatypes.rb +109 -0
- data/lib/zabbix_manager/classes/problems.rb +133 -0
- data/lib/zabbix_manager/classes/proxies.rb +68 -0
- data/lib/zabbix_manager/classes/roles.rb +114 -0
- data/lib/zabbix_manager/classes/screens.rb +94 -0
- data/lib/zabbix_manager/classes/scripts.rb +35 -0
- data/lib/zabbix_manager/classes/server.rb +16 -0
- data/lib/zabbix_manager/classes/templates.rb +123 -0
- data/lib/zabbix_manager/classes/triggers.rb +166 -0
- data/lib/zabbix_manager/classes/unusable.rb +8 -0
- data/lib/zabbix_manager/classes/usergroups.rb +73 -0
- data/lib/zabbix_manager/classes/usermacros.rb +228 -0
- data/lib/zabbix_manager/classes/users.rb +64 -0
- data/lib/zabbix_manager/classes/valuemaps.rb +50 -0
- data/lib/zabbix_manager/client.rb +158 -0
- data/lib/zabbix_manager/version.rb +1 -1
- data/zabbix_manager-5.0.1.gem +0 -0
- metadata +77 -2
@@ -0,0 +1,166 @@
|
|
1
|
+
class ZabbixManager
|
2
|
+
class Triggers < Basic
|
3
|
+
# The method name used for interacting with Triggers via Zabbix API
|
4
|
+
#
|
5
|
+
# @return [String]
|
6
|
+
def method_name
|
7
|
+
'trigger'
|
8
|
+
end
|
9
|
+
|
10
|
+
# The id field name used for identifying specific Trigger objects via Zabbix API
|
11
|
+
#
|
12
|
+
# @return [String]
|
13
|
+
def identify
|
14
|
+
'description'
|
15
|
+
end
|
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 [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 dump_by_id(data)
|
24
|
+
log "[DEBUG] Call dump_by_id with parameters: #{data.inspect}"
|
25
|
+
|
26
|
+
@client.api_request(
|
27
|
+
method: 'trigger.get',
|
28
|
+
params: {
|
29
|
+
filter: {
|
30
|
+
keys.to_sym => data[keys.to_sym]
|
31
|
+
},
|
32
|
+
output: 'extend',
|
33
|
+
select_items: 'extend',
|
34
|
+
select_functions: 'extend'
|
35
|
+
}
|
36
|
+
)
|
37
|
+
end
|
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 [ManagerError] 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 safe_update(data)
|
46
|
+
log "[DEBUG] Call safe_update with parameters: #{data.inspect}"
|
47
|
+
|
48
|
+
dump = {}
|
49
|
+
item_id = data[key.to_sym].to_i
|
50
|
+
dump_by_id(key.to_sym => data[key.to_sym]).each do |item|
|
51
|
+
dump = symbolize_keys(item) if item[key].to_i == data[key.to_sym].to_i
|
52
|
+
end
|
53
|
+
|
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
|
56
|
+
dump.delete(:functions)
|
57
|
+
dump.delete(:items)
|
58
|
+
|
59
|
+
old_expression = data[:expression]
|
60
|
+
data[:expression] = data[:expression].gsub(/\{.*\:/, '{') # TODO: ugly regexp
|
61
|
+
data.delete(:templateid)
|
62
|
+
|
63
|
+
log "[DEBUG] expression: #{dump[:expression]}\n data: #{data[:expression]}"
|
64
|
+
|
65
|
+
if hash_equals?(dump, data)
|
66
|
+
log "[DEBUG] Equal keys #{dump} and #{data}, skip safe_update"
|
67
|
+
item_id
|
68
|
+
else
|
69
|
+
data[:expression] = old_expression
|
70
|
+
# disable old trigger
|
71
|
+
log '[DEBUG] disable :' + @client.api_request(method: "#{method_name}.update", params: [{ triggerid: data[:triggerid], status: '1' }]).inspect
|
72
|
+
# create new trigger
|
73
|
+
data.delete(:triggerid)
|
74
|
+
create(data)
|
75
|
+
end
|
76
|
+
end
|
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 [ManagerError] 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
|
84
|
+
def get_or_create(data)
|
85
|
+
log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
|
86
|
+
|
87
|
+
unless (id = get_id(description: data[:description], hostid: data[:hostid]))
|
88
|
+
id = create(data)
|
89
|
+
end
|
90
|
+
id
|
91
|
+
end
|
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 [ManagerError] 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
|
99
|
+
def create_or_update(data)
|
100
|
+
triggerid = get_id(description: data[:description], hostid: data[:hostid])
|
101
|
+
|
102
|
+
triggerid ? update(data.merge(triggerid: triggerid)) : create(data)
|
103
|
+
end
|
104
|
+
|
105
|
+
# 测试数据
|
106
|
+
def mojo_data
|
107
|
+
data = {
|
108
|
+
comments: "MOJO1",
|
109
|
+
opdata: "MOJO_OPDATA",
|
110
|
+
priority: 1,
|
111
|
+
description: "MOJO1",
|
112
|
+
expression: "{SZX1-ISP-SW7:net.if.in[ifHCInOctets.1].avg(15m)}>=450000000 or {SZX1-ISP-SW7:net.if.out[ifHCOutOctets.1].avg(15m)}>=450000000",
|
113
|
+
recovery_expression: "{SZX1-ISP-SW7:net.if.in[ifHCInOctets.1].avg(15m)}<=350000000 or {SZX1-ISP-SW7:net.if.out[ifHCOutOctets.1].avg(15m)}<=350000000",
|
114
|
+
}
|
115
|
+
|
116
|
+
create_trigger data.stringify_keys!
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
# 设置触发器
|
121
|
+
def create_trigger(data = {})
|
122
|
+
data = data.with_indifferent_access
|
123
|
+
# 请求生成触发器
|
124
|
+
result = @client.api_request(
|
125
|
+
method: 'trigger.create',
|
126
|
+
params: {
|
127
|
+
comments: data["comments"],
|
128
|
+
priority: data["priority"],
|
129
|
+
description: data["description"],
|
130
|
+
expression: data["expression"],
|
131
|
+
recovery_expression: data["recovery_expression"],
|
132
|
+
opdata: data["opdata"],
|
133
|
+
recovery_mode: 1,
|
134
|
+
type: 0,
|
135
|
+
manual_close: 1,
|
136
|
+
}
|
137
|
+
)
|
138
|
+
# 检查是是否存在
|
139
|
+
result.empty? ? nil : result["triggerids"]
|
140
|
+
end
|
141
|
+
|
142
|
+
# 更新触发器
|
143
|
+
def update_trigger(triggerid, data = {})
|
144
|
+
data = data.with_indifferent_access
|
145
|
+
# 请求生成触发器
|
146
|
+
result = @client.api_request(
|
147
|
+
method: 'trigger.update',
|
148
|
+
params: {
|
149
|
+
triggerid: triggerid.to_i,
|
150
|
+
comments: data["comments"],
|
151
|
+
priority: data["priority"],
|
152
|
+
description: data["description"],
|
153
|
+
expression: data["expression"],
|
154
|
+
recovery_expression: data["recovery_expression"],
|
155
|
+
opdata: data["opdata"],
|
156
|
+
recovery_mode: 1,
|
157
|
+
type: 1,
|
158
|
+
manual_close: 1,
|
159
|
+
}
|
160
|
+
)
|
161
|
+
ap result
|
162
|
+
# 检查是是否存在
|
163
|
+
result.empty? ? nil : result["triggerids"]
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
class ZabbixManager
|
2
|
+
class Usergroups < Basic
|
3
|
+
# The method name used for interacting with Usergroups via Zabbix API
|
4
|
+
#
|
5
|
+
# @return [String]
|
6
|
+
def method_name
|
7
|
+
'usergroup'
|
8
|
+
end
|
9
|
+
|
10
|
+
# The key field name used for Usergroup objects via Zabbix API
|
11
|
+
#
|
12
|
+
# @return [String]
|
13
|
+
def key
|
14
|
+
'usrgrpid'
|
15
|
+
end
|
16
|
+
|
17
|
+
# The id field name used for identifying specific Usergroup objects via Zabbix API
|
18
|
+
#
|
19
|
+
# @return [String]
|
20
|
+
def identify
|
21
|
+
'name'
|
22
|
+
end
|
23
|
+
|
24
|
+
# Set permissions for usergroup using Zabbix API
|
25
|
+
#
|
26
|
+
# @param data [Hash] Needs to include usrgrpids and hostgroupids along with permissions to set
|
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] Zabbix object id (usergroup)
|
30
|
+
def permissions(data)
|
31
|
+
permission = data[:permission] || 2
|
32
|
+
result = @client.api_request(
|
33
|
+
method: 'usergroup.update',
|
34
|
+
params: {
|
35
|
+
usrgrpid: data[:usrgrpid],
|
36
|
+
rights: data[:hostgroupids].map { |t| { permission: permission, id: t } }
|
37
|
+
}
|
38
|
+
)
|
39
|
+
result ? result['usrgrpids'][0].to_i : nil
|
40
|
+
end
|
41
|
+
|
42
|
+
# Add users to usergroup using Zabbix API
|
43
|
+
#
|
44
|
+
# @deprecated Zabbix has removed massAdd in favor of update.
|
45
|
+
# @param data [Hash] Needs to include userids and usrgrpids to mass add users to groups
|
46
|
+
# @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
|
47
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
48
|
+
# @return [Integer] Zabbix object id (usergroup)
|
49
|
+
def add_user(data)
|
50
|
+
update_users(data)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Update users in usergroups using Zabbix API
|
54
|
+
#
|
55
|
+
# @param data [Hash] Needs to include userids and usrgrpids to mass update users in groups
|
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] Zabbix object id (usergroup)
|
59
|
+
def update_users(data)
|
60
|
+
user_groups = data[:usrgrpids].map do |t|
|
61
|
+
{
|
62
|
+
usrgrpid: t,
|
63
|
+
userids: data[:userids],
|
64
|
+
}
|
65
|
+
end
|
66
|
+
result = @client.api_request(
|
67
|
+
method: 'usergroup.update',
|
68
|
+
params: user_groups,
|
69
|
+
)
|
70
|
+
result ? result['usrgrpids'][0].to_i : nil
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,228 @@
|
|
1
|
+
class ZabbixManager
|
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 [ManagerError] 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 ManagerError.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 [ManagerError] 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 ManagerError.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 [ManagerError] 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 [ManagerError] 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 [ManagerError] 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 [ManagerError] 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 [ManagerError] 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 [ManagerError] 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 [ManagerError] 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 [ManagerError] 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 [ManagerError] 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 [ManagerError] 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 [ManagerError] 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 [ManagerError] 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 [ManagerError] 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 ZabbixManager
|
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 [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 (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 [ManagerError] 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 ZabbixManager
|
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 [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] 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 [ManagerError] 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
|