zabbix_manager 5.0.4 → 5.0.7

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.
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 +76 -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 +52 -50
  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 +41 -37
  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,24 +1,26 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class Roles < Basic
3
5
  # The method name used for interacting with Role via Zabbix API
4
6
  #
5
7
  # @return [String]
6
8
  def method_name
7
- 'role'
9
+ "role"
8
10
  end
9
11
 
10
12
  # The key field name used for Role objects via Zabbix API
11
13
  #
12
14
  # @return [String]
13
15
  def key
14
- 'roleid'
16
+ "roleid"
15
17
  end
16
18
 
17
19
  # The id field name used for identifying specific Role 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 rules(data)
31
33
  rules = data[:rules] || 2
32
34
  result = @client.api_request(
33
- method: 'role.update',
35
+ method: "role.update",
34
36
  params: {
35
37
  roleid: data[:roleid],
36
38
  rules: 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,10 +62,10 @@ class ZabbixManager
60
62
  log "[DEBUG] Call dump_by_id with parameters: #{data.inspect}"
61
63
 
62
64
  @client.api_request(
63
- method: 'role.get',
65
+ method: "role.get",
64
66
  params: {
65
- output: 'extend',
66
- selectRules: 'extend',
67
+ output: "extend",
68
+ selectRules: "extend",
67
69
  roleids: data[:id]
68
70
  }
69
71
  )
@@ -77,18 +79,18 @@ class ZabbixManager
77
79
  # @return [Array] Returns array of Graph ids
78
80
  def get_ids_by_name(data)
79
81
  result = @client.api_request(
80
- method: 'role.get',
82
+ method: "role.get",
81
83
  params: {
82
84
  filter: {
83
85
  name: data[:name]
84
86
  },
85
- output: 'extend'
87
+ output: "extend"
86
88
  }
87
89
  )
88
90
 
89
- result.map do |rule|
90
- rule['roleid']
91
- end.compact
91
+ result.filter_map do |rule|
92
+ rule["roleid"]
93
+ end
92
94
  end
93
95
 
94
96
  # Update users in Userroles using Zabbix API
@@ -101,14 +103,14 @@ class ZabbixManager
101
103
  user_groups = data[:usrgrpids].map do |t|
102
104
  {
103
105
  usrgrpid: t,
104
- userids: data[:userids],
106
+ userids: data[:userids]
105
107
  }
106
108
  end
107
109
  result = @client.api_request(
108
- method: 'usergroup.update',
109
- params: user_groups,
110
+ method: "usergroup.update",
111
+ params: user_groups
110
112
  )
111
- result ? result['usrgrpids'][0].to_i : nil
113
+ result ? result["usrgrpids"][0].to_i : nil
112
114
  end
113
115
  end
114
116
  end
@@ -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
- filter: {
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)
@@ -105,16 +110,15 @@ class ZabbixManager
105
110
  # 测试数据
106
111
  def mojo_data
107
112
  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",
113
+ comments: "MOJO1",
114
+ opdata: "MOJO_OPDATA",
115
+ priority: 1,
116
+ description: "MOJO1",
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",
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,17 +126,17 @@ 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
- comments: data["comments"],
128
- priority: data["priority"],
129
- description: data["description"],
130
- expression: data["expression"],
131
+ comments: data["comments"],
132
+ priority: data["priority"],
133
+ description: data["description"],
134
+ expression: data["expression"],
131
135
  recovery_expression: data["recovery_expression"],
132
- opdata: data["opdata"],
133
- recovery_mode: 1,
134
- type: 0,
135
- manual_close: 1,
136
+ opdata: data["opdata"],
137
+ recovery_mode: 1,
138
+ type: 0,
139
+ manual_close: 1
136
140
  }
137
141
  )
138
142
  # 检查是是否存在
@@ -144,21 +148,21 @@ 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,
150
- comments: data["comments"],
151
- priority: data["priority"],
152
- description: data["description"],
153
- expression: data["expression"],
153
+ triggerid: triggerid,
154
+ comments: data["comments"],
155
+ priority: data["priority"],
156
+ description: data["description"],
157
+ expression: data["expression"],
154
158
  recovery_expression: data["recovery_expression"],
155
- opdata: data["opdata"],
156
- recovery_mode: 1,
157
- type: 1,
158
- manual_close: 1,
159
+ opdata: data["opdata"],
160
+ recovery_mode: 1,
161
+ type: 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
@@ -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