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,48 @@
|
|
1
|
+
class ZabbixApi
|
2
|
+
class Proxies < Basic
|
3
|
+
# The method name used for interacting with Proxies via Zabbix API
|
4
|
+
#
|
5
|
+
# @return [String]
|
6
|
+
def method_name
|
7
|
+
'proxy'
|
8
|
+
end
|
9
|
+
|
10
|
+
# The id field name used for identifying specific Proxy objects via Zabbix API
|
11
|
+
#
|
12
|
+
# @return [String]
|
13
|
+
def identify
|
14
|
+
'host'
|
15
|
+
end
|
16
|
+
|
17
|
+
# Delete Proxy object using Zabbix API
|
18
|
+
#
|
19
|
+
# @param data [Array] Should include array of proxyid's
|
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 [Integer] The Proxy object id that was deleted
|
23
|
+
def delete(data)
|
24
|
+
result = @client.api_request(method: 'proxy.delete', params: data)
|
25
|
+
result.empty? ? nil : result['proxyids'][0].to_i
|
26
|
+
end
|
27
|
+
|
28
|
+
# Check if a Proxy object is readable using Zabbix API
|
29
|
+
#
|
30
|
+
# @param data [Array] Should include array of proxyid's
|
31
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
32
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
33
|
+
# @return [Boolean] Returns true if the given proxies are readable
|
34
|
+
def isreadable(data)
|
35
|
+
@client.api_request(method: 'proxy.isreadable', params: data)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Check if a Proxy object is writable using Zabbix API
|
39
|
+
#
|
40
|
+
# @param data [Array] Should include array of proxyid's
|
41
|
+
# @raise [ApiError] 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 [Boolean] Returns true if the given proxies are writable
|
44
|
+
def iswritable(data)
|
45
|
+
@client.api_request(method: 'proxy.iswritable', params: data)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
class ZabbixApi
|
2
|
+
class Roles < Basic
|
3
|
+
# The method name used for interacting with Role via Zabbix API
|
4
|
+
#
|
5
|
+
# @return [String]
|
6
|
+
def method_name
|
7
|
+
'role'
|
8
|
+
end
|
9
|
+
|
10
|
+
# The key field name used for Role objects via Zabbix API
|
11
|
+
#
|
12
|
+
# @return [String]
|
13
|
+
def key
|
14
|
+
'roleid'
|
15
|
+
end
|
16
|
+
|
17
|
+
# The id field name used for identifying specific Role 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 [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)
|
30
|
+
def rules(data)
|
31
|
+
rules = data[:rules] || 2
|
32
|
+
result = @client.api_request(
|
33
|
+
method: 'role.update',
|
34
|
+
params: {
|
35
|
+
roleid: data[:roleid],
|
36
|
+
rules: 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 [ApiError] 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
|
+
# Dump Role object data by key from Zabbix API
|
54
|
+
#
|
55
|
+
# @param data [Hash] Should include desired object's key and value
|
56
|
+
# @raise [ApiError] 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 [Hash]
|
59
|
+
def dump_by_id(data)
|
60
|
+
log "[DEBUG] Call dump_by_id with parameters: #{data.inspect}"
|
61
|
+
|
62
|
+
@client.api_request(
|
63
|
+
method: 'role.get',
|
64
|
+
params: {
|
65
|
+
output: 'extend',
|
66
|
+
selectRules: 'extend',
|
67
|
+
roleids: data[:id]
|
68
|
+
}
|
69
|
+
)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Get Role ids by Role Name from Zabbix API
|
73
|
+
#
|
74
|
+
# @param data [Hash] Should include host value to query for matching graphs
|
75
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
76
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
77
|
+
# @return [Array] Returns array of Graph ids
|
78
|
+
def get_ids_by_name(data)
|
79
|
+
result = @client.api_request(
|
80
|
+
method: 'role.get',
|
81
|
+
params: {
|
82
|
+
filter: {
|
83
|
+
name: data[:name]
|
84
|
+
},
|
85
|
+
output: 'extend'
|
86
|
+
}
|
87
|
+
)
|
88
|
+
|
89
|
+
result.map do |rule|
|
90
|
+
rule['roleid']
|
91
|
+
end.compact
|
92
|
+
end
|
93
|
+
|
94
|
+
# Update users in Userroles using Zabbix API
|
95
|
+
#
|
96
|
+
# @param data [Hash] Needs to include userids and usrgrpids to mass update users in groups
|
97
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
98
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
99
|
+
# @return [Integer] Zabbix object id (usergroup)
|
100
|
+
def update_users(data)
|
101
|
+
user_groups = data[:usrgrpids].map do |t|
|
102
|
+
{
|
103
|
+
usrgrpid: t,
|
104
|
+
userids: data[:userids],
|
105
|
+
}
|
106
|
+
end
|
107
|
+
result = @client.api_request(
|
108
|
+
method: 'usergroup.update',
|
109
|
+
params: user_groups,
|
110
|
+
)
|
111
|
+
result ? result['usrgrpids'][0].to_i : nil
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
class ZabbixApi
|
2
|
+
class Screens < Basic
|
3
|
+
# extracted from frontends/php/include/defines.inc.php
|
4
|
+
# SCREEN_RESOURCE_GRAPH => 0,
|
5
|
+
# SCREEN_RESOURCE_SIMPLE_GRAPH => 1,
|
6
|
+
# SCREEN_RESOURCE_MAP => 2,
|
7
|
+
# SCREEN_RESOURCE_PLAIN_TEXT => 3,
|
8
|
+
# SCREEN_RESOURCE_HOSTS_INFO => 4,
|
9
|
+
# SCREEN_RESOURCE_TRIGGERS_INFO => 5,
|
10
|
+
# SCREEN_RESOURCE_SERVER_INFO => 6,
|
11
|
+
# SCREEN_RESOURCE_CLOCK => 7,
|
12
|
+
# SCREEN_RESOURCE_SCREEN => 8,
|
13
|
+
# SCREEN_RESOURCE_TRIGGERS_OVERVIEW => 9,
|
14
|
+
# SCREEN_RESOURCE_DATA_OVERVIEW => 10,
|
15
|
+
# SCREEN_RESOURCE_URL => 11,
|
16
|
+
# SCREEN_RESOURCE_ACTIONS => 12,
|
17
|
+
# SCREEN_RESOURCE_EVENTS => 13,
|
18
|
+
# SCREEN_RESOURCE_HOSTGROUP_TRIGGERS => 14,
|
19
|
+
# SCREEN_RESOURCE_SYSTEM_STATUS => 15,
|
20
|
+
# SCREEN_RESOURCE_HOST_TRIGGERS => 16
|
21
|
+
|
22
|
+
# The method name used for interacting with Screens via Zabbix API
|
23
|
+
#
|
24
|
+
# @return [String]
|
25
|
+
def method_name
|
26
|
+
'screen'
|
27
|
+
end
|
28
|
+
|
29
|
+
# The id field name used for identifying specific Screen objects via Zabbix API
|
30
|
+
#
|
31
|
+
# @return [String]
|
32
|
+
def identify
|
33
|
+
'name'
|
34
|
+
end
|
35
|
+
|
36
|
+
# Delete Screen object using Zabbix API
|
37
|
+
#
|
38
|
+
# @param data [String, Array] Should include id's of the screens to delete
|
39
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
40
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
41
|
+
# @return [Integer] Zabbix object id
|
42
|
+
def delete(data)
|
43
|
+
result = @client.api_request(method: 'screen.delete', params: [data])
|
44
|
+
result.empty? ? nil : result['screenids'][0].to_i
|
45
|
+
end
|
46
|
+
|
47
|
+
# Get or Create Screen object for Host using Zabbix API
|
48
|
+
#
|
49
|
+
# @param data [Hash] Needs to include screen_name and graphids to properly identify Screens via Zabbix API
|
50
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
51
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
52
|
+
# @return [Integer] Zabbix object id
|
53
|
+
def get_or_create_for_host(data)
|
54
|
+
screen_name = data[:screen_name]
|
55
|
+
graphids = data[:graphids]
|
56
|
+
screenitems = []
|
57
|
+
hsize = data[:hsize] || 3
|
58
|
+
valign = data[:valign] || 2
|
59
|
+
halign = data[:halign] || 2
|
60
|
+
rowspan = data[:rowspan] || 1
|
61
|
+
colspan = data[:colspan] || 1
|
62
|
+
height = data[:height] || 320 # default 320
|
63
|
+
width = data[:width] || 200 # default 200
|
64
|
+
vsize = data[:vsize] || [1, (graphids.size / hsize).to_i].max
|
65
|
+
screenid = get_id(name: screen_name)
|
66
|
+
|
67
|
+
unless screenid
|
68
|
+
# Create screen
|
69
|
+
graphids.each_with_index do |graphid, index|
|
70
|
+
screenitems << {
|
71
|
+
resourcetype: 0,
|
72
|
+
resourceid: graphid,
|
73
|
+
x: (index % hsize).to_i,
|
74
|
+
y: (index % graphids.size / hsize).to_i,
|
75
|
+
valign: valign,
|
76
|
+
halign: halign,
|
77
|
+
rowspan: rowspan,
|
78
|
+
colspan: colspan,
|
79
|
+
height: height,
|
80
|
+
width: width
|
81
|
+
}
|
82
|
+
end
|
83
|
+
|
84
|
+
screenid = create(
|
85
|
+
name: screen_name,
|
86
|
+
hsize: hsize,
|
87
|
+
vsize: vsize,
|
88
|
+
screenitems: screenitems
|
89
|
+
)
|
90
|
+
end
|
91
|
+
screenid
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class ZabbixApi
|
2
|
+
class Scripts < Basic
|
3
|
+
def method_name
|
4
|
+
'script'
|
5
|
+
end
|
6
|
+
|
7
|
+
# The id field name used for identifying specific Screen objects via Zabbix API
|
8
|
+
#
|
9
|
+
# @return [String]
|
10
|
+
def identify
|
11
|
+
'name'
|
12
|
+
end
|
13
|
+
|
14
|
+
# Submits a request to the zabbix api
|
15
|
+
# data - A Hash containing the scriptid and hostid
|
16
|
+
#
|
17
|
+
# Example:
|
18
|
+
# execute({ scriptid: '12', hostid: '32 })
|
19
|
+
#
|
20
|
+
# Returns nothing
|
21
|
+
def execute(data)
|
22
|
+
@client.api_request(
|
23
|
+
method: 'script.execute',
|
24
|
+
params: {
|
25
|
+
scriptid: data[:scriptid],
|
26
|
+
hostid: data[:hostid]
|
27
|
+
}
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
def getscriptsbyhost(data)
|
32
|
+
@client.api_request(method: 'script.getscriptsbyhosts', params: data)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class ZabbixApi
|
2
|
+
class Server
|
3
|
+
# @return [String]
|
4
|
+
attr_reader :version
|
5
|
+
|
6
|
+
# Initializes a new Server object with ZabbixApi Client and fetches Zabbix Server API version
|
7
|
+
#
|
8
|
+
# @param client [ZabbixApi::Client]
|
9
|
+
# @return [ZabbixApi::Client]
|
10
|
+
# @return [String] Zabbix API version number
|
11
|
+
def initialize(client)
|
12
|
+
@client = client
|
13
|
+
@version = @client.api_version()
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
class ZabbixApi
|
2
|
+
class Templates < Basic
|
3
|
+
# The method name used for interacting with Templates via Zabbix API
|
4
|
+
#
|
5
|
+
# @return [String]
|
6
|
+
def method_name
|
7
|
+
'template'
|
8
|
+
end
|
9
|
+
|
10
|
+
# The id field name used for identifying specific Template objects via Zabbix API
|
11
|
+
#
|
12
|
+
# @return [String]
|
13
|
+
def identify
|
14
|
+
'host'
|
15
|
+
end
|
16
|
+
|
17
|
+
# Delete Template object using Zabbix API
|
18
|
+
#
|
19
|
+
# @param data [Array] Should include array of templateid's
|
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 [Integer] The Template object id that was deleted
|
23
|
+
def delete(data)
|
24
|
+
result = @client.api_request(method: 'template.delete', params: [data])
|
25
|
+
result.empty? ? nil : result['templateids'][0].to_i
|
26
|
+
end
|
27
|
+
|
28
|
+
# Get Template ids for Host from Zabbix API
|
29
|
+
#
|
30
|
+
# @param data [Hash] Should include host value to query for matching templates
|
31
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
32
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
33
|
+
# @return [Array] Returns array of Template ids
|
34
|
+
def get_ids_by_host(data)
|
35
|
+
@client.api_request(method: 'template.get', params: data).map do |tmpl|
|
36
|
+
tmpl['templateid']
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Get or Create Template object using Zabbix API
|
41
|
+
#
|
42
|
+
# @param data [Hash] Needs to include host to properly identify Templates via Zabbix API
|
43
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
44
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
45
|
+
# @return [Integer] Zabbix object id
|
46
|
+
def get_or_create(data)
|
47
|
+
unless (templateid = get_id(host: data[:host]))
|
48
|
+
templateid = create(data)
|
49
|
+
end
|
50
|
+
templateid
|
51
|
+
end
|
52
|
+
|
53
|
+
# Mass update Templates for Hosts using Zabbix API
|
54
|
+
#
|
55
|
+
# @param data [Hash] Should include hosts_id array and templates_id array
|
56
|
+
# @raise [ApiError] 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 [Boolean]
|
59
|
+
def mass_update(data)
|
60
|
+
result = @client.api_request(
|
61
|
+
method: 'template.massUpdate',
|
62
|
+
params: {
|
63
|
+
hosts: data[:hosts_id].map { |t| { hostid: t } },
|
64
|
+
templates: data[:templates_id].map { |t| { templateid: t } }
|
65
|
+
}
|
66
|
+
)
|
67
|
+
result.empty? ? false : true
|
68
|
+
end
|
69
|
+
|
70
|
+
# Mass add Templates to Hosts using Zabbix API
|
71
|
+
#
|
72
|
+
# @param data [Hash] Should include hosts_id array and templates_id array
|
73
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
74
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
75
|
+
# @return [Boolean]
|
76
|
+
def mass_add(data)
|
77
|
+
result = @client.api_request(
|
78
|
+
method: 'template.massAdd',
|
79
|
+
params: {
|
80
|
+
hosts: data[:hosts_id].map { |t| { hostid: t } },
|
81
|
+
templates: data[:templates_id].map { |t| { templateid: t } }
|
82
|
+
}
|
83
|
+
)
|
84
|
+
result.empty? ? false : true
|
85
|
+
end
|
86
|
+
|
87
|
+
# Mass remove Templates to Hosts using Zabbix API
|
88
|
+
#
|
89
|
+
# @param data [Hash] Should include hosts_id array and templates_id array
|
90
|
+
# @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
|
91
|
+
# @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
|
92
|
+
# @return [Boolean]
|
93
|
+
def mass_remove(data)
|
94
|
+
result = @client.api_request(
|
95
|
+
method: 'template.massRemove',
|
96
|
+
params: {
|
97
|
+
hostids: data[:hosts_id],
|
98
|
+
templateids: data[:templates_id],
|
99
|
+
groupids: data[:group_id],
|
100
|
+
force: 1
|
101
|
+
}
|
102
|
+
)
|
103
|
+
result.empty? ? false : true
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
class ZabbixApi
|
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 [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]
|
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
|
+
key.to_sym => data[key.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 [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 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 [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
|
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 [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
|
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
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
class ZabbixApi
|
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 [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)
|
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 [ApiError] 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 [ApiError] 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
|