zabbix_manager 5.0.5 → 5.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +2 -4
  3. data/LICENSE.txt +1 -1
  4. data/README.md +48 -21
  5. data/lib/zabbix_manager/basic/basic_alias.rb +2 -0
  6. data/lib/zabbix_manager/basic/basic_extend.rb +11 -9
  7. data/lib/zabbix_manager/basic/basic_func.rb +9 -5
  8. data/lib/zabbix_manager/basic/basic_init.rb +6 -4
  9. data/lib/zabbix_manager/basic/basic_logic.rb +7 -5
  10. data/lib/zabbix_manager/classes/actions.rb +6 -4
  11. data/lib/zabbix_manager/classes/applications.rb +4 -2
  12. data/lib/zabbix_manager/classes/configurations.rb +6 -4
  13. data/lib/zabbix_manager/classes/drules.rb +5 -3
  14. data/lib/zabbix_manager/classes/errors.rb +10 -9
  15. data/lib/zabbix_manager/classes/events.rb +4 -3
  16. data/lib/zabbix_manager/classes/graphs.rb +13 -11
  17. data/lib/zabbix_manager/classes/hostgroups.rb +13 -11
  18. data/lib/zabbix_manager/classes/hosts.rb +36 -34
  19. data/lib/zabbix_manager/classes/httptests.rb +4 -2
  20. data/lib/zabbix_manager/classes/items.rb +34 -28
  21. data/lib/zabbix_manager/classes/maintenance.rb +4 -2
  22. data/lib/zabbix_manager/classes/mediatypes.rb +15 -13
  23. data/lib/zabbix_manager/classes/problems.rb +31 -30
  24. data/lib/zabbix_manager/classes/proxies.rb +9 -7
  25. data/lib/zabbix_manager/classes/roles.rb +19 -17
  26. data/lib/zabbix_manager/classes/screens.rb +6 -4
  27. data/lib/zabbix_manager/classes/scripts.rb +6 -4
  28. data/lib/zabbix_manager/classes/server.rb +3 -1
  29. data/lib/zabbix_manager/classes/templates.rb +18 -16
  30. data/lib/zabbix_manager/classes/triggers.rb +35 -17
  31. data/lib/zabbix_manager/classes/unusable.rb +2 -0
  32. data/lib/zabbix_manager/classes/usergroups.rb +11 -9
  33. data/lib/zabbix_manager/classes/usermacros.rb +38 -37
  34. data/lib/zabbix_manager/classes/users.rb +11 -9
  35. data/lib/zabbix_manager/classes/valuemaps.rb +5 -3
  36. data/lib/zabbix_manager/client.rb +40 -30
  37. data/lib/zabbix_manager/version.rb +3 -1
  38. data/lib/zabbix_manager.rb +36 -35
  39. data/zabbix_manager.gemspec +43 -0
  40. metadata +143 -19
  41. data/.idea/workspace.xml +0 -69
  42. data/.rubocop.yml +0 -13
  43. data/CODE_OF_CONDUCT.md +0 -84
  44. data/Gemfile +0 -10
  45. data/Rakefile +0 -8
  46. data/bin/console +0 -15
  47. data/bin/setup +0 -8
  48. data/zabbix_manager-5.0.1.gem +0 -0
  49. data/zabbix_manager-5.0.2.gem +0 -0
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class Screens < Basic
3
5
  # extracted from frontends/php/include/defines.inc.php
@@ -23,14 +25,14 @@ class ZabbixManager
23
25
  #
24
26
  # @return [String]
25
27
  def method_name
26
- 'screen'
28
+ "screen"
27
29
  end
28
30
 
29
31
  # The id field name used for identifying specific Screen objects via Zabbix API
30
32
  #
31
33
  # @return [String]
32
34
  def identify
33
- 'name'
35
+ "name"
34
36
  end
35
37
 
36
38
  # Delete Screen object using Zabbix API
@@ -40,8 +42,8 @@ class ZabbixManager
40
42
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
41
43
  # @return [Integer] Zabbix object id
42
44
  def delete(data)
43
- result = @client.api_request(method: 'screen.delete', params: [data])
44
- result.empty? ? nil : result['screenids'][0].to_i
45
+ result = @client.api_request(method: "screen.delete", params: [data])
46
+ result.empty? ? nil : result["screenids"][0].to_i
45
47
  end
46
48
 
47
49
  # Get or Create Screen object for Host using Zabbix API
@@ -1,14 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class Scripts < Basic
3
5
  def method_name
4
- 'script'
6
+ "script"
5
7
  end
6
8
 
7
9
  # The id field name used for identifying specific Screen objects via Zabbix API
8
10
  #
9
11
  # @return [String]
10
12
  def identify
11
- 'name'
13
+ "name"
12
14
  end
13
15
 
14
16
  # Submits a request to the zabbix api
@@ -20,7 +22,7 @@ class ZabbixManager
20
22
  # Returns nothing
21
23
  def execute(data)
22
24
  @client.api_request(
23
- method: 'script.execute',
25
+ method: "script.execute",
24
26
  params: {
25
27
  scriptid: data[:scriptid],
26
28
  hostid: data[:hostid]
@@ -29,7 +31,7 @@ class ZabbixManager
29
31
  end
30
32
 
31
33
  def getscriptsbyhost(data)
32
- @client.api_request(method: 'script.getscriptsbyhosts', params: data)
34
+ @client.api_request(method: "script.getscriptsbyhosts", params: data)
33
35
  end
34
36
  end
35
37
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class Server
3
5
  # @return [String]
@@ -10,7 +12,7 @@ class ZabbixManager
10
12
  # @return [String] Zabbix API version number
11
13
  def initialize(client)
12
14
  @client = client
13
- @version = @client.api_version()
15
+ @version = @client.api_version
14
16
  end
15
17
  end
16
18
  end
@@ -1,17 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class Templates < Basic
3
5
  # The method name used for interacting with Templates via Zabbix API
4
6
  #
5
7
  # @return [String]
6
8
  def method_name
7
- 'template'
9
+ "template"
8
10
  end
9
11
 
10
12
  # The id field name used for identifying specific Template objects via Zabbix API
11
13
  #
12
14
  # @return [String]
13
15
  def identify
14
- 'host'
16
+ "host"
15
17
  end
16
18
 
17
19
  # Delete Template object using Zabbix API
@@ -21,8 +23,8 @@ class ZabbixManager
21
23
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
22
24
  # @return [Integer] The Template object id that was deleted
23
25
  def delete(data)
24
- result = @client.api_request(method: 'template.delete', params: [data])
25
- result.empty? ? nil : result['templateids'][0].to_i
26
+ result = @client.api_request(method: "template.delete", params: [data])
27
+ result.empty? ? nil : result["templateids"][0].to_i
26
28
  end
27
29
 
28
30
  # Get Template ids for Host from Zabbix API
@@ -32,8 +34,8 @@ class ZabbixManager
32
34
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
33
35
  # @return [Array] Returns array of Template ids
34
36
  def get_ids_by_host(data)
35
- @client.api_request(method: 'template.get', params: data).map do |tmpl|
36
- tmpl['templateid']
37
+ @client.api_request(method: "template.get", params: data).map do |tmpl|
38
+ tmpl["templateid"]
37
39
  end
38
40
  end
39
41
 
@@ -58,9 +60,9 @@ class ZabbixManager
58
60
  # @return [Boolean]
59
61
  def mass_update(data)
60
62
  result = @client.api_request(
61
- method: 'template.massUpdate',
63
+ method: "template.massUpdate",
62
64
  params: {
63
- hosts: data[:hosts_id].map { |t| { hostid: t } },
65
+ hosts: data[:hosts_id].map { |t| { hostid: t } },
64
66
  templates: data[:templates_id].map { |t| { templateid: t } }
65
67
  }
66
68
  )
@@ -75,9 +77,9 @@ class ZabbixManager
75
77
  # @return [Boolean]
76
78
  def mass_add(data)
77
79
  result = @client.api_request(
78
- method: 'template.massAdd',
80
+ method: "template.massAdd",
79
81
  params: {
80
- hosts: data[:hosts_id].map { |t| { hostid: t } },
82
+ hosts: data[:hosts_id].map { |t| { hostid: t } },
81
83
  templates: data[:templates_id].map { |t| { templateid: t } }
82
84
  }
83
85
  )
@@ -92,12 +94,12 @@ class ZabbixManager
92
94
  # @return [Boolean]
93
95
  def mass_remove(data)
94
96
  result = @client.api_request(
95
- method: 'template.massRemove',
97
+ method: "template.massRemove",
96
98
  params: {
97
- hostids: data[:hosts_id],
99
+ hostids: data[:hosts_id],
98
100
  templateids: data[:templates_id],
99
- groupids: data[:group_id],
100
- force: 1
101
+ groupids: data[:group_id],
102
+ force: 1
101
103
  }
102
104
  )
103
105
  result.empty? ? false : true
@@ -107,14 +109,14 @@ class ZabbixManager
107
109
  def get_template_ids(data)
108
110
  # 直接调后端接口
109
111
  result = @client.api_request(
110
- method: 'template.get',
112
+ method: "template.get",
111
113
  params: {
112
114
  output: "extend",
113
115
  filter: {
114
116
  host: [data].flatten
115
117
  }
116
118
  }
117
- ).map { |temp| [templateid: temp['templateid']]}
119
+ ).map { |temp| [templateid: temp["templateid"]] }
118
120
 
119
121
  # 返回 template 模板数组对象
120
122
  result.empty? ? nil : result.flatten
@@ -1,17 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class Triggers < Basic
3
5
  # The method name used for interacting with Triggers via Zabbix API
4
6
  #
5
7
  # @return [String]
6
8
  def method_name
7
- 'trigger'
9
+ "trigger"
8
10
  end
9
11
 
10
12
  # The id field name used for identifying specific Trigger objects via Zabbix API
11
13
  #
12
14
  # @return [String]
13
15
  def identify
14
- 'description'
16
+ "description"
15
17
  end
16
18
 
17
19
  # Dump Trigger object data by key from Zabbix API
@@ -24,14 +26,14 @@ class ZabbixManager
24
26
  log "[DEBUG] Call dump_by_id with parameters: #{data.inspect}"
25
27
 
26
28
  @client.api_request(
27
- method: 'trigger.get',
29
+ method: "trigger.get",
28
30
  params: {
29
31
  filter: {
30
32
  keys.to_sym => data[keys.to_sym]
31
33
  },
32
- output: 'extend',
33
- select_items: 'extend',
34
- select_functions: 'extend'
34
+ output: "extend",
35
+ select_items: "extend",
36
+ select_functions: "extend"
35
37
  }
36
38
  )
37
39
  end
@@ -51,13 +53,13 @@ class ZabbixManager
51
53
  dump = symbolize_keys(item) if item[key].to_i == data[key.to_sym].to_i
52
54
  end
53
55
 
54
- expression = dump[:items][0][:key_] + '.' + dump[:functions][0][:function] + '(' + dump[:functions][0][:parameter] + ')'
56
+ expression = "#{dump[:items][0][:key_]}.#{dump[:functions][0][:function]}(#{dump[:functions][0][:parameter]})"
55
57
  dump[:expression] = dump[:expression].gsub(/\{(\d*)\}/, "{#{expression}}") # TODO: ugly regexp
56
58
  dump.delete(:functions)
57
59
  dump.delete(:items)
58
60
 
59
61
  old_expression = data[:expression]
60
- data[:expression] = data[:expression].gsub(/\{.*\:/, '{') # TODO: ugly regexp
62
+ data[:expression] = data[:expression].gsub(/\{.*:/, "{") # TODO: ugly regexp
61
63
  data.delete(:templateid)
62
64
 
63
65
  log "[DEBUG] expression: #{dump[:expression]}\n data: #{data[:expression]}"
@@ -68,7 +70,10 @@ class ZabbixManager
68
70
  else
69
71
  data[:expression] = old_expression
70
72
  # disable old trigger
71
- log '[DEBUG] disable :' + @client.api_request(method: "#{method_name}.update", params: [{ triggerid: data[:triggerid], status: '1' }]).inspect
73
+ log "[DEBUG] disable :" + @client.api_request(method: "#{method_name}.update",
74
+ params: [{
75
+ triggerid: data[:triggerid], status: "1"
76
+ }]).inspect
72
77
  # create new trigger
73
78
  data.delete(:triggerid)
74
79
  create(data)
@@ -110,11 +115,10 @@ class ZabbixManager
110
115
  priority: 1,
111
116
  description: "MOJO1",
112
117
  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",
118
+ 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
119
  }
115
120
 
116
121
  create_trigger data.stringify_keys!
117
-
118
122
  end
119
123
 
120
124
  # 设置触发器
@@ -122,7 +126,7 @@ class ZabbixManager
122
126
  data = data.with_indifferent_access
123
127
  # 请求生成触发器
124
128
  result = @client.api_request(
125
- method: 'trigger.create',
129
+ method: "trigger.create",
126
130
  params: {
127
131
  comments: data["comments"],
128
132
  priority: data["priority"],
@@ -132,7 +136,7 @@ class ZabbixManager
132
136
  opdata: data["opdata"],
133
137
  recovery_mode: 1,
134
138
  type: 0,
135
- manual_close: 1,
139
+ manual_close: 1
136
140
  }
137
141
  )
138
142
  # 检查是是否存在
@@ -144,9 +148,9 @@ class ZabbixManager
144
148
  data = data.with_indifferent_access
145
149
  # 请求生成触发器
146
150
  result = @client.api_request(
147
- method: 'trigger.update',
151
+ method: "trigger.update",
148
152
  params: {
149
- triggerid: triggerid.to_i,
153
+ triggerid: triggerid,
150
154
  comments: data["comments"],
151
155
  priority: data["priority"],
152
156
  description: data["description"],
@@ -155,12 +159,26 @@ class ZabbixManager
155
159
  opdata: data["opdata"],
156
160
  recovery_mode: 1,
157
161
  type: 1,
158
- manual_close: 1,
162
+ manual_close: 1
159
163
  }
160
164
  )
161
- #ap result
165
+ # ap result
162
166
  # 检查是是否存在
163
167
  result.empty? ? nil : result["triggerids"]
164
168
  end
169
+
170
+ # 添加触发器依赖项
171
+ def add_trigger_dependency(triggerid, depend_on_trigger_id)
172
+ log "[DEBUG] Call add_trigger_dependency with parameters: #{triggerid} #{depend_on_trigger_id}"
173
+ # 请求生成触发器
174
+ result = @client.api_request(
175
+ method: "trigger.adddependencies",
176
+ params: {
177
+ triggerid: triggerid,
178
+ dependsOnTriggerid: depend_on_trigger_id
179
+ }
180
+ )
181
+ result.empty? ? nil : result["triggerids"]
182
+ end
165
183
  end
166
184
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class Triggers < Basic
3
5
  def create_or_update(data)
@@ -1,24 +1,26 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class Usergroups < Basic
3
5
  # The method name used for interacting with Usergroups via Zabbix API
4
6
  #
5
7
  # @return [String]
6
8
  def method_name
7
- 'usergroup'
9
+ "usergroup"
8
10
  end
9
11
 
10
12
  # The key field name used for Usergroup objects via Zabbix API
11
13
  #
12
14
  # @return [String]
13
15
  def key
14
- 'usrgrpid'
16
+ "usrgrpid"
15
17
  end
16
18
 
17
19
  # The id field name used for identifying specific Usergroup objects via Zabbix API
18
20
  #
19
21
  # @return [String]
20
22
  def identify
21
- 'name'
23
+ "name"
22
24
  end
23
25
 
24
26
  # Set permissions for usergroup using Zabbix API
@@ -30,13 +32,13 @@ class ZabbixManager
30
32
  def permissions(data)
31
33
  permission = data[:permission] || 2
32
34
  result = @client.api_request(
33
- method: 'usergroup.update',
35
+ method: "usergroup.update",
34
36
  params: {
35
37
  usrgrpid: data[:usrgrpid],
36
38
  rights: data[:hostgroupids].map { |t| { permission: permission, id: t } }
37
39
  }
38
40
  )
39
- result ? result['usrgrpids'][0].to_i : nil
41
+ result ? result["usrgrpids"][0].to_i : nil
40
42
  end
41
43
 
42
44
  # Add users to usergroup using Zabbix API
@@ -60,14 +62,14 @@ class ZabbixManager
60
62
  user_groups = data[:usrgrpids].map do |t|
61
63
  {
62
64
  usrgrpid: t,
63
- userids: data[:userids],
65
+ userids: data[:userids]
64
66
  }
65
67
  end
66
68
  result = @client.api_request(
67
- method: 'usergroup.update',
68
- params: user_groups,
69
+ method: "usergroup.update",
70
+ params: user_groups
69
71
  )
70
- result ? result['usrgrpids'][0].to_i : nil
72
+ result ? result["usrgrpids"][0].to_i : nil
71
73
  end
72
74
  end
73
75
  end
@@ -1,17 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class Usermacros < Basic
3
5
  # The id field name used for identifying specific User macro objects via Zabbix API
4
6
  #
5
7
  # @return [String]
6
8
  def identify
7
- 'macro'
9
+ "macro"
8
10
  end
9
11
 
10
12
  # The method name used for interacting with User macros via Zabbix API
11
13
  #
12
14
  # @return [String]
13
15
  def method_name
14
- 'usermacro'
16
+ "usermacro"
15
17
  end
16
18
 
17
19
  # Get User macro object id from Zabbix API based on provided data
@@ -27,11 +29,11 @@ class ZabbixManager
27
29
  data = symbolize_keys(data) if data.key?(identify)
28
30
  # raise an error if identify name was not supplied
29
31
  name = data[identify.to_sym]
30
- raise ManagerError.new("#{identify} not supplied in call to get_id") if name.nil?
32
+ raise ManagerError, "#{identify} not supplied in call to get_id" if name.nil?
31
33
 
32
- result = request(data, 'usermacro.get', 'hostmacroid')
34
+ result = request(data, "usermacro.get", "hostmacroid")
33
35
 
34
- !result.empty? && result[0].key?('hostmacroid') ? result[0]['hostmacroid'].to_i : nil
36
+ !result.empty? && result[0].key?("hostmacroid") ? result[0]["hostmacroid"].to_i : nil
35
37
  end
36
38
 
37
39
  # Get Global macro object id from Zabbix API based on provided data
@@ -47,11 +49,11 @@ class ZabbixManager
47
49
  data = symbolize_keys(data) if data.key?(identify)
48
50
  # raise an error if identify name was not supplied
49
51
  name = data[identify.to_sym]
50
- raise ManagerError.new("#{identify} not supplied in call to get_id_global") if name.nil?
52
+ raise ManagerError, "#{identify} not supplied in call to get_id_global" if name.nil?
51
53
 
52
- result = request(data, 'usermacro.get', 'globalmacroid')
54
+ result = request(data, "usermacro.get", "globalmacroid")
53
55
 
54
- !result.empty? && result[0].key?('globalmacroid') ? result[0]['globalmacroid'].to_i : nil
56
+ !result.empty? && result[0].key?("globalmacroid") ? result[0]["globalmacroid"].to_i : nil
55
57
  end
56
58
 
57
59
  # Get full/extended User macro data from Zabbix API
@@ -63,7 +65,7 @@ class ZabbixManager
63
65
  def get_full_data(data)
64
66
  log "[DEBUG] Call get_full_data with parameters: #{data.inspect}"
65
67
 
66
- request(data, 'usermacro.get', 'hostmacroid')
68
+ request(data, "usermacro.get", "hostmacroid")
67
69
  end
68
70
 
69
71
  # Get full/extended Global macro data from Zabbix API
@@ -75,7 +77,7 @@ class ZabbixManager
75
77
  def get_full_data_global(data)
76
78
  log "[DEBUG] Call get_full_data_global with parameters: #{data.inspect}"
77
79
 
78
- request(data, 'usermacro.get', 'globalmacroid')
80
+ request(data, "usermacro.get", "globalmacroid")
79
81
  end
80
82
 
81
83
  # Create new User macro object using Zabbix API (with defaults)
@@ -86,7 +88,7 @@ class ZabbixManager
86
88
  # @return [Integer] The object id if a single object is created
87
89
  # @return [Boolean] True/False if multiple objects are created
88
90
  def create(data)
89
- request(data, 'usermacro.create', 'hostmacroids')
91
+ request(data, "usermacro.create", "hostmacroids")
90
92
  end
91
93
 
92
94
  # Create new Global macro object using Zabbix API (with defaults)
@@ -97,7 +99,7 @@ class ZabbixManager
97
99
  # @return [Integer] The object id if a single object is created
98
100
  # @return [Boolean] True/False if multiple objects are created
99
101
  def create_global(data)
100
- request(data, 'usermacro.createglobal', 'globalmacroids')
102
+ request(data, "usermacro.createglobal", "globalmacroids")
101
103
  end
102
104
 
103
105
  # Delete User macro object using Zabbix API
@@ -109,7 +111,7 @@ class ZabbixManager
109
111
  # @return [Boolean] True/False if multiple objects are deleted
110
112
  def delete(data)
111
113
  data_delete = [data]
112
- request(data_delete, 'usermacro.delete', 'hostmacroids')
114
+ request(data_delete, "usermacro.delete", "hostmacroids")
113
115
  end
114
116
 
115
117
  # Delete Global macro object using Zabbix API
@@ -121,7 +123,7 @@ class ZabbixManager
121
123
  # @return [Boolean] True/False if multiple objects are deleted
122
124
  def delete_global(data)
123
125
  data_delete = [data]
124
- request(data_delete, 'usermacro.deleteglobal', 'globalmacroids')
126
+ request(data_delete, "usermacro.deleteglobal", "globalmacroids")
125
127
  end
126
128
 
127
129
  # Update User macro object using Zabbix API
@@ -133,7 +135,7 @@ class ZabbixManager
133
135
  # @return [Integer] The object id if a single object is created
134
136
  # @return [Boolean] True/False if multiple objects are created
135
137
  def update(data)
136
- request(data, 'usermacro.update', 'hostmacroids')
138
+ request(data, "usermacro.update", "hostmacroids")
137
139
  end
138
140
 
139
141
  # Update Global macro object using Zabbix API
@@ -145,7 +147,7 @@ class ZabbixManager
145
147
  # @return [Integer] The object id if a single object is created
146
148
  # @return [Boolean] True/False if multiple objects are created
147
149
  def update_global(data)
148
- request(data, 'usermacro.updateglobal', 'globalmacroids')
150
+ request(data, "usermacro.updateglobal", "globalmacroids")
149
151
  end
150
152
 
151
153
  # Get or Create User macro object using Zabbix API
@@ -200,29 +202,28 @@ class ZabbixManager
200
202
  globalmacroid ? update_global(data.merge(globalmacroid: globalmacroid)) : create_global(data)
201
203
  end
202
204
 
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 })
205
+ private
206
+ # Custom request method to handle both User and Global macros in one
207
+ #
208
+ # @param data [Hash] Needs to include macro and hostid to properly identify Global macros via Zabbix API
209
+ # @param method [String] Zabbix API method to use for the request
210
+ # @param result_key [String] Which key to use for parsing results based on User vs Global macros
211
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
212
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
213
+ # @return [Integer] Zabbix object id
214
+ def request(data, method, result_key)
215
+ # Zabbix has different result formats for gets vs updates
216
+ if method.include?(".get")
217
+ if result_key.include?("global")
218
+ @client.api_request(method: method, params: { globalmacro: true, filter: data })
219
+ else
220
+ @client.api_request(method: method, params: { filter: data })
221
+ end
218
222
  else
219
- @client.api_request(method: method, params: { filter: data })
220
- end
221
- else
222
- result = @client.api_request(method: method, params: data)
223
+ result = @client.api_request(method: method, params: data)
223
224
 
224
- result.key?(result_key) && !result[result_key].empty? ? result[result_key][0].to_i : nil
225
+ result.key?(result_key) && !result[result_key].empty? ? result[result_key][0].to_i : nil
226
+ end
225
227
  end
226
- end
227
228
  end
228
229
  end
@@ -1,31 +1,33 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class Users < Basic
3
5
  # The method name used for interacting with Users via Zabbix API
4
6
  #
5
7
  # @return [String]
6
8
  def method_name
7
- 'user'
9
+ "user"
8
10
  end
9
11
 
10
12
  # The keys field name used for User objects via Zabbix API
11
13
  #
12
14
  # @return [String]
13
15
  def keys
14
- 'userids'
16
+ "userids"
15
17
  end
16
18
 
17
19
  # The key field name used for User objects via Zabbix API
18
20
  #
19
21
  # @return [String]
20
22
  def key
21
- 'userid'
23
+ "userid"
22
24
  end
23
25
 
24
26
  # The id field name used for identifying specific User objects via Zabbix API
25
27
  #
26
28
  # @return [String]
27
29
  def identify
28
- 'alias'
30
+ "alias"
29
31
  end
30
32
 
31
33
  def medias_helper(data, action)
@@ -34,11 +36,11 @@ class ZabbixManager
34
36
  params: data[:userids].map do |t|
35
37
  {
36
38
  userid: t,
37
- user_medias: data[:media],
39
+ user_medias: data[:media]
38
40
  }
39
- end,
41
+ end
40
42
  )
41
- result ? result['userids'][0].to_i : nil
43
+ result ? result["userids"][0].to_i : nil
42
44
  end
43
45
 
44
46
  # Add media to users using Zabbix API
@@ -48,7 +50,7 @@ class ZabbixManager
48
50
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
49
51
  # @return [Integer] Zabbix object id (media)
50
52
  def add_medias(data)
51
- medias_helper(data, 'update')
53
+ medias_helper(data, "update")
52
54
  end
53
55
 
54
56
  # Update media for users using Zabbix API
@@ -58,7 +60,7 @@ class ZabbixManager
58
60
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
59
61
  # @return [Integer] Zabbix object id (user)
60
62
  def update_medias(data)
61
- medias_helper(data, 'update')
63
+ medias_helper(data, "update")
62
64
  end
63
65
  end
64
66
  end
@@ -1,24 +1,26 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class ValueMaps < Basic
3
5
  # The method name used for interacting with ValueMaps via Zabbix API
4
6
  #
5
7
  # @return [String]
6
8
  def method_name
7
- 'valuemap'
9
+ "valuemap"
8
10
  end
9
11
 
10
12
  # The key field name used for ValueMap objects via Zabbix API
11
13
  #
12
14
  # @return [String]
13
15
  def key
14
- 'valuemapid'
16
+ "valuemapid"
15
17
  end
16
18
 
17
19
  # The id field name used for identifying specific ValueMap objects via Zabbix API
18
20
  #
19
21
  # @return [String]
20
22
  def identify
21
- 'name'
23
+ "name"
22
24
  end
23
25
 
24
26
  # Get or Create ValueMap object using Zabbix API