zabbix_manager 5.1.0 → 5.1.1.pre.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +10 -2
  3. data/{LICENSE.txt → LICENSE} +6 -6
  4. data/README.md +3 -2
  5. data/lib/zabbix_manager/basic/basic_alias.rb +6 -8
  6. data/lib/zabbix_manager/basic/basic_func.rb +17 -18
  7. data/lib/zabbix_manager/basic/basic_init.rb +21 -18
  8. data/lib/zabbix_manager/basic/basic_logic.rb +143 -139
  9. data/lib/zabbix_manager/classes/actions.rb +1 -1
  10. data/lib/zabbix_manager/classes/applications.rb +8 -7
  11. data/lib/zabbix_manager/classes/configurations.rb +2 -2
  12. data/lib/zabbix_manager/classes/drules.rb +6 -6
  13. data/lib/zabbix_manager/classes/errors.rb +1 -1
  14. data/lib/zabbix_manager/classes/graphs.rb +6 -6
  15. data/lib/zabbix_manager/classes/hostgroups.rb +0 -34
  16. data/lib/zabbix_manager/classes/hostinterfaces.rb +26 -0
  17. data/lib/zabbix_manager/classes/hosts.rb +35 -153
  18. data/lib/zabbix_manager/classes/httptests.rb +3 -3
  19. data/lib/zabbix_manager/classes/items.rb +51 -49
  20. data/lib/zabbix_manager/classes/mediatypes.rb +5 -5
  21. data/lib/zabbix_manager/classes/problems.rb +43 -61
  22. data/lib/zabbix_manager/classes/proxies.rb +4 -24
  23. data/lib/zabbix_manager/classes/roles.rb +7 -7
  24. data/lib/zabbix_manager/classes/screens.rb +3 -3
  25. data/lib/zabbix_manager/classes/templates.rb +17 -33
  26. data/lib/zabbix_manager/classes/triggers.rb +31 -73
  27. data/lib/zabbix_manager/classes/usergroups.rb +6 -6
  28. data/lib/zabbix_manager/classes/usermacros.rb +38 -37
  29. data/lib/zabbix_manager/classes/users.rb +4 -4
  30. data/lib/zabbix_manager/classes/valuemaps.rb +8 -7
  31. data/lib/zabbix_manager/client.rb +70 -45
  32. data/lib/zabbix_manager/version.rb +2 -1
  33. data/lib/zabbix_manager.rb +18 -38
  34. data/zabbix_manager.gemspec +7 -11
  35. metadata +22 -66
  36. data/CODE_OF_CONDUCT.md +0 -84
  37. data/lib/zabbix_manager/basic/basic_extend.rb +0 -40
@@ -19,7 +19,7 @@ class ZabbixManager
19
19
  # Get full/extended Action object data from API
20
20
  #
21
21
  # @param data [Hash] Should include object's id field name (identify) and id value
22
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
22
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
23
23
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
24
24
  # @return [Hash]
25
25
  def get_full_data(data)
@@ -19,24 +19,25 @@ class ZabbixManager
19
19
  # Get or Create Application object using Zabbix API
20
20
  #
21
21
  # @param data [Hash] Needs to include name and hostid to properly identify Applications via Zabbix API
22
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
22
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
23
23
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
24
- # @return [Integer] Zabbix object id
24
+ # @return [Integer, TrueClass, FalseClass] Zabbix object id
25
25
  def get_or_create(data)
26
26
  log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
27
27
 
28
- unless (id = get_id(name: data[:name], hostid: data[:hostid]))
29
- id = create(data)
28
+ if (id = get_id(name: data[:name], hostid: data[:hostid]))
29
+ id
30
+ else
31
+ create(data)
30
32
  end
31
- id
32
33
  end
33
34
 
34
35
  # Create or update Application object using Zabbix API
35
36
  #
36
37
  # @param data [Hash] Needs to include name and hostid to properly identify Applications via Zabbix API
37
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
38
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
38
39
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
39
- # @return [Integer] Zabbix object id
40
+ # @return [Integer,TrueClass, FalseClass] Zabbix object id
40
41
  def create_or_update(data)
41
42
  applicationid = get_id(name: data[:name], hostid: data[:hostid])
42
43
  applicationid ? update(data.merge(applicationid: applicationid)) : create(data)
@@ -24,7 +24,7 @@ class ZabbixManager
24
24
  # Export configuration data using Zabbix API
25
25
  #
26
26
  # @param data [Hash]
27
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
27
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
28
28
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
29
29
  # @return [Hash]
30
30
  def export(data)
@@ -34,7 +34,7 @@ class ZabbixManager
34
34
  # Import configuration data using Zabbix API
35
35
  #
36
36
  # @param data [Hash]
37
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
37
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
38
38
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
39
39
  # @return [Hash]
40
40
  def import(data)
@@ -21,17 +21,17 @@ class ZabbixManager
21
21
  # @return [Hash]
22
22
  def default_options
23
23
  {
24
- name: nil,
24
+ name: nil,
25
25
  iprange: nil,
26
- delay: 3600,
27
- status: 0
26
+ delay: 3600,
27
+ status: 0
28
28
  }
29
29
  end
30
30
 
31
31
  # Get or Create Drule object using Zabbix API
32
32
  #
33
33
  # @param data [Hash] Needs to include name to properly identify Drule via Zabbix API
34
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
34
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
35
35
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
36
36
  # @return [Integer] Zabbix object id
37
37
  def get_or_create(data)
@@ -46,9 +46,9 @@ class ZabbixManager
46
46
  # Create or update Drule object using Zabbix API
47
47
  #
48
48
  # @param data [Hash] Needs to include name to properly identify Drules via Zabbix API
49
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
49
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
50
50
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
51
- # @return [Integer] Zabbix object id
51
+ # @return [Integer, TrueClass, FalseClass] Zabbix object id
52
52
  def create_or_update(data)
53
53
  druleid = get_id(name: data[:name])
54
54
  druleid ? update(data.merge(druleid: druleid)) : create(data)
@@ -21,7 +21,7 @@ class ZabbixManager
21
21
  end
22
22
  end
23
23
 
24
- class ManagerError < BaseError
24
+ class ZbxError < BaseError
25
25
  end
26
26
 
27
27
  class HttpError < BaseError
@@ -19,7 +19,7 @@ class ZabbixManager
19
19
  # Get full/extended Graph data from Zabbix API
20
20
  #
21
21
  # @param data [Hash] Should include object's id field name (identify) and id value
22
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
22
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
23
23
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
24
24
  # @return [Hash]
25
25
  def get_full_data(data)
@@ -39,7 +39,7 @@ class ZabbixManager
39
39
  # Get Graph ids for Host from Zabbix API
40
40
  #
41
41
  # @param data [Hash] Should include host value to query for matching graphs
42
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
42
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
43
43
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
44
44
  # @return [Array] Returns array of Graph ids
45
45
  def get_ids_by_host(data)
@@ -65,7 +65,7 @@ class ZabbixManager
65
65
  # Get Graph Item object using Zabbix API
66
66
  #
67
67
  # @param data [Hash] Needs to include graphids to properly identify Graph Items via Zabbix API
68
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
68
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
69
69
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
70
70
  # @return [Hash]
71
71
  def get_items(data)
@@ -81,7 +81,7 @@ class ZabbixManager
81
81
  # Get or Create Graph object using Zabbix API
82
82
  #
83
83
  # @param data [Hash] Needs to include name and templateid to properly identify Graphs via Zabbix API
84
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
84
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
85
85
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
86
86
  # @return [Integer] Zabbix object id
87
87
  def get_or_create(data)
@@ -97,9 +97,9 @@ class ZabbixManager
97
97
  # Create or update Graph object using Zabbix API
98
98
  #
99
99
  # @param data [Hash] Needs to include name and templateid to properly identify Graphs via Zabbix API
100
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
100
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
101
101
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
102
- # @return [Integer] Zabbix object id
102
+ # @return [Integer, TrueClass, FalseClass] Zabbix object id
103
103
  def create_or_update(data)
104
104
  graphid = get_id(name: data[:name], templateid: data[:templateid])
105
105
  graphid ? _update(data.merge(graphid: graphid)) : create(data)
@@ -22,39 +22,5 @@ class ZabbixManager
22
22
  def key
23
23
  "groupid"
24
24
  end
25
-
26
- # 新增 get_hostgroup_ids 方法,使用列表 flatten 功能拉平属组对象
27
- def get_hostgroup_ids(data)
28
- result = @client.api_request(
29
- method: "hostgroup.get",
30
- params: {
31
- output: "extend",
32
- filter: {
33
- name: [data].flatten
34
- }
35
- }
36
- ).map { |item| { groupid: item["groupid"] } }
37
-
38
- # 检查是是否存在
39
- result.empty? ? nil : result.flatten
40
- end
41
-
42
- # 新增 get_or_create_hostgroups 方法,查询或创建新的对象
43
- def get_or_create_hostgroups(data)
44
- # 是否存在设备属组,不存在则新建
45
- [data].flatten.each do |item|
46
- result = get_hostgroup_ids(item)
47
- if result.nil?
48
- @client.api_request(
49
- method: "hostgroup.create",
50
- params: {
51
- name: item
52
- }
53
- )
54
- end
55
- rescue StandardError => e
56
- ap e
57
- end
58
- end
59
25
  end
60
26
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ZabbixManager
4
+ class HostInterfaces < Basic
5
+ # The method name used for interacting with HostInterfaces via Zabbix API
6
+ #
7
+ # @return [String]
8
+ def method_name
9
+ "hostinterface"
10
+ end
11
+
12
+ # The id field name used for identifying specific HostInterface objects via Zabbix API
13
+ #
14
+ # @return [String]
15
+ def identify
16
+ "hostids"
17
+ end
18
+
19
+ # The key field name used for HostInterface objects via Zabbix API
20
+ #
21
+ # @return [String]
22
+ def key
23
+ "interfaceid"
24
+ end
25
+ end
26
+ end
@@ -19,45 +19,46 @@ class ZabbixManager
19
19
  # Dump Host object data by key from Zabbix API
20
20
  #
21
21
  # @param data [Hash] Should include desired object's key and value
22
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
22
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
23
23
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
24
24
  # @return [Hash]
25
25
  def dump_by_id(data)
26
26
  log "[DEBUG] Call dump_by_id with parameters: #{data.inspect}"
27
+
27
28
  @client.api_request(
28
29
  method: "host.get",
29
30
  params: {
30
- filter: {
31
+ filter: {
31
32
  key.to_sym => data[key.to_sym]
32
33
  },
33
- output: "extend"
34
- # selectHosts: 'shorten'
34
+ output: "extend",
35
+ selectHosts: "shorten"
35
36
  }
36
37
  )
37
38
  end
38
39
 
39
40
  # The default options used when creating Host objects via Zabbix API
40
- #
41
+ # @note Create Host 缺省的配置参数
41
42
  # @return [Hash]
42
43
  def default_options
43
44
  {
44
- host: nil,
45
- interfaces: {
46
- main: 1,
47
- useip: 1,
48
- type: 2,
49
- ip: nil,
50
- dns: "",
51
- port: 161,
45
+ host: nil,
46
+ interfaces: {
47
+ main: 1,
48
+ useip: 1,
49
+ type: 2,
50
+ ip: nil,
51
+ dns: "",
52
+ port: 161,
52
53
  details: {
53
- version: 2,
54
- community: "transsion"
54
+ version: 2,
55
+ community: "public"
55
56
  }
56
57
  },
57
- status: 0,
58
- available: 1,
59
- groups: [],
60
- proxy_hostid: nil,
58
+ status: 0,
59
+ available: 1,
60
+ groups: [],
61
+ proxy_hostid: nil,
61
62
  inventory_mode: 1
62
63
  }
63
64
  end
@@ -65,156 +66,37 @@ class ZabbixManager
65
66
  # Unlink/Remove Templates from Hosts using Zabbix API
66
67
  #
67
68
  # @param data [Hash] Should include hosts_id array and templates_id array
68
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
69
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
69
70
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
70
71
  # @return [Boolean]
71
72
  def unlink_templates(data)
72
73
  result = @client.api_request(
73
74
  method: "host.massRemove",
74
75
  params: {
75
- hostids: data[:hosts_id],
76
+ hostids: data[:hosts_id],
76
77
  templates: data[:templates_id]
77
78
  }
78
79
  )
79
80
  result.empty? ? false : true
80
81
  end
81
82
 
82
- # Create or update Host object using Zabbix API
83
- #
84
- # @param data [Hash] Needs to include host to properly identify Hosts via Zabbix API
85
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
83
+ # Get Zabbix object ID by name using Zabbix API
84
+ # @note 基于监控对象索引键(#{identify})查询 #{key}
85
+ # @param data [String, Array] Needs to include host to properly identify Zabbix Objects via Zabbix API
86
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
86
87
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
87
- # @return [Integer] Zabbix object id
88
-
89
- # 根据主机 hostid 获取 interfaceid
90
- def get_interface_id(hostid)
91
- log "[DEBUG] Call _get_interface_id with parameters: #{hostid.inspect}"
92
-
93
- # 请求后端接口
94
- result = @client.api_request(
95
- method: "hostinterface.get",
96
- params: {
97
- hostids: hostid
98
- }
99
- )
100
- # 是否为空
101
- result.empty? ? nil : result[0]["interfaceid"]
102
- end
103
-
104
- # 批量删除主机
105
- def mojo_delete(data)
106
- hostid = get_id(host: data[:host])
107
- log "[DEBUG] Call get_id with parameters: #{hostid.inspect}"
108
-
109
- result = @client.api_request(
110
- method: "host.delete",
111
- params: [hostid]
112
- )
113
-
114
- # 是否为空
115
- if result.present?
116
- p "成功移除监控主机 #{data.ip}"
117
- else
118
- p "未能移除监控主机 #{data.ip}"
119
- end
120
- end
121
-
122
- # 测试数据
123
- def update_mojo
124
- data = {
125
- name: "SZX1-16-SW3111111111",
126
- groups: [
127
- {
128
- groupid: 22
129
- }
130
- ],
131
- templates: [
132
- {
133
- templateid: 10_227
134
- }
135
- ],
136
- inventory_mode: 1,
137
- hostid: 15_951
138
- }
139
-
140
- # 请求后端接口
141
- @client.api_request(method: "host.update", params: data)
142
- end
143
-
144
- # 根据主机名查询 hostid
145
- def get_host_id(name)
146
- result = @client.api_request(
147
- method: "host.get",
148
- params: {
149
- output: "extend",
150
- filter: {
151
- host: name
152
- }
153
- }
154
- )
155
-
156
- # 检查是是否存在
157
- result.empty? ? nil : result[0]["hostid"]
158
- end
159
-
160
- # 根据可见名称查询 hostid
161
- def get_hostid_by_name(name)
162
- result = @client.api_request(
163
- method: "host.get",
164
- params: {
165
- output: "extend",
166
- filter: {
167
- name: name
168
- }
88
+ # @return [Array, NilClass] Zabbix object id
89
+ def get_ids_by_name(data)
90
+ log "[DEBUG] Call #{method_name}.get_ids_by_name with parameters: #{data.inspect}"
91
+
92
+ result = get_raw(
93
+ {
94
+ filter: { name: [data].flatten },
95
+ output: ["#{key}"]
169
96
  }
170
97
  )
171
-
172
- # 检查是是否存在
173
- result.empty? ? nil : result[0]["hostid"]
98
+ result.empty? ? nil : result.map { |i| { "#{key}": i[key] } }
174
99
  end
175
100
 
176
- # update host index to serail
177
- def update_host_to_serial(data)
178
- # 检索出监控对象的 hostdid
179
- hostid = get_hostid_by_name(host: data[:name])
180
-
181
- if hostid.present?
182
- p "已存在监控对象,正在更新 #{data[:name]} 的索引数据 #{data[:host]}"
183
- # 数据更新期间不处理模板信息 | TODO 后续需要考虑实现
184
- data.delete(:templates)
185
- mojo_update("host.update", data.merge(hostid: hostid))
186
- else
187
- p "未检索到 #{data[:host]} 监控对象"
188
- end
189
- end
190
-
191
- # create_or_update
192
- def create_or_update(data)
193
- hostid = get_id(host: data[:host])
194
- # 此处更新需要拆分为 hosts 和 interfaces 的数据更新
195
- if hostid.present?
196
- p "已存在监控对象,正在更新 #{data[:host]} 监控数据"
197
-
198
- # 查询接口对象
199
- interfaceid = get_interface_id(hostid)
200
- # 绑定接口属性
201
- interfaces = data.delete(:interfaces)
202
- interfaces[:interfaceid] = interfaceid
203
-
204
- # 数据更新期间不处理模板信息 | TODO 后续需要考虑实现
205
- data.delete(:templates)
206
-
207
- # 分别更新接口和关联信息
208
- mojo_update("hostinterface.update", interfaces)
209
- mojo_update("host.update", data.merge(hostid: hostid))
210
- else
211
- p "正在创建 #{data[:host]} 监控对象"
212
- # 新增监控对象
213
- create data
214
- end
215
- rescue StandardError => e
216
- puts "创建或新增主机 #{data[:host]} 异常,异常信息:#{e}"
217
- # raise NotImplementedError
218
- end
219
101
  end
220
102
  end
@@ -30,7 +30,7 @@ class ZabbixManager
30
30
  # Get or Create HttpTest object using Zabbix API
31
31
  #
32
32
  # @param data [Hash] Needs to include name and hostid to properly identify HttpTests via Zabbix API
33
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
33
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
34
34
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
35
35
  # @return [Integer] Zabbix object id
36
36
  def get_or_create(data)
@@ -45,9 +45,9 @@ class ZabbixManager
45
45
  # Create or update HttpTest object using Zabbix API
46
46
  #
47
47
  # @param data [Hash] Needs to include name and hostid to properly identify HttpTests via Zabbix API
48
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
48
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
49
49
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
50
- # @return [Integer] Zabbix object id
50
+ # @return [Integer, TrueClass, FalseClass] Zabbix object id
51
51
  def create_or_update(data)
52
52
  httptestid = get_id(name: data[:name], hostid: data[:hostid])
53
53
  httptestid ? update(data.merge(httptestid: httptestid)) : create(data)
@@ -59,40 +59,51 @@ class ZabbixManager
59
59
  # Get or Create Item object using Zabbix API
60
60
  #
61
61
  # @param data [Hash] Needs to include name and hostid to properly identify Items via Zabbix API
62
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
62
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
63
63
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
64
- # @return [Integer] Zabbix object id
64
+ # @return [Integer, TrueClass, FalseClass, NilClass] Zabbix object id
65
65
  def get_or_create(data)
66
- log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
66
+ log "[DEBUG] Call #{method_name}.get_or_create with parameters: #{data.inspect}"
67
67
 
68
- unless (id = get_id(name: data[:name], hostid: data[:hostid]))
69
- id = create(data)
68
+ if (id = get_id(name: data[:name], hostid: data[:hostid]))
69
+ id
70
+ else
71
+ create(data)
70
72
  end
71
- id
72
73
  end
73
74
 
74
75
  # Create or update Item object using Zabbix API
75
76
  #
76
77
  # @param data [Hash] Needs to include name and hostid to properly identify Items via Zabbix API
77
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
78
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
78
79
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
79
- # @return [Integer] Zabbix object id
80
+ # @return [Integer, TrueClass, FalseClass, NilClass] Zabbix object id
80
81
  def create_or_update(data)
81
82
  itemid = get_id(name: data[:name], hostid: data[:hostid])
82
83
  itemid ? update(data.merge(itemid: itemid)) : create(data)
83
84
  end
84
85
 
85
- # 根据设备名称和接口名字查询监控项 | 15809 | GigabitEthernet1/0/12
86
+ # Create Dns Monitor object using Zabbix API
87
+ # @note 根据设备名称和接口名字查询监控项:入参 GigabitEthernet1/0/12,出参 15809
88
+ # @example Interface Et0/0(TO_CLOUD): Bits sent, snmpoid 1.3.6.1.2.1.31.1.1.1.10.1
89
+ # @example Interface Et0/0(TO_CLOUD): Operational status, snmpoid 1.3.6.1.2.1.2.2.1.8.1
90
+ # @example Interface Et0/0(TO_CLOUD): Speed, snmpoid 1.3.6.1.2.1.31.1.1.1.15.1
91
+ # @example Interface Et0/0(TO_CLOUD): Bits received, snmpoid 1.3.6.1.2.1.31.1.1.1.6.1
92
+ # @param hostid [String, Integer] Needs to include name and hostid to properly identify Items via Zabbix API
93
+ # @param name [String] Needs to include name and hostid to properly identify Items via Zabbix API
94
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
95
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
96
+ # @return [Integer, TrueClass, FalseClass, NilClass] Zabbix object id
86
97
  def get_interface_items(hostid, name)
98
+ log "[DEBUG] Call #{method_name}.get_interface_items with parameters: #{data.inspect}"
99
+
87
100
  # 自动剔除收尾空白字串
88
- _name = name&.gsub(%r{[^/0-9]}, "")&.strip
101
+ _name = name&.gsub(%r{[^/0-9]}, "").strip
89
102
  iface = "#{_name}("
90
103
 
91
104
  # 模糊查询接口下所有监控项,同时过滤出特定的 snmp_oid
92
- result = @client.api_request(
93
- method: "item.get",
94
- params: {
95
- # output: ["itemid", "name", "snmp_oid", "key_", "triggerids"],
105
+ result = get_raw(
106
+ {
96
107
  output: "extend",
97
108
  hostids: hostid,
98
109
  search: {
@@ -104,49 +115,40 @@ class ZabbixManager
104
115
  }.sort_by {
105
116
  |item| item["key_"]
106
117
  }
107
-
108
- # 检查是是否存在
109
118
  result.empty? ? nil : result
110
119
  end
111
120
 
112
- # 创建单个 dns item
121
+ # The default options used when creating DNS Monitor objects via Zabbix API
122
+ # @note 缺省的 dns 参数
123
+ # @return [Hash]
124
+ def default_dns_params
125
+ {
126
+ type: 0, # 代表 zabbix_agent
127
+ value_type: 1, # 代表字符串
128
+ delay: "1m",
129
+ history: "90d",
130
+ lifetime: "30d",
131
+ timeout: "15s"
132
+ }
133
+ end
134
+
135
+ # Create Dns Monitor object using Zabbix API
136
+ #
137
+ # @param hostid [Hash] Needs to include dns_name and hostid to properly identify Items via Zabbix API
138
+ # @param dns_name [Hash] Needs to include dns_name and hostid to properly identify Items via Zabbix API
139
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
140
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
141
+ # @return [Integer, TrueClass, FalseClass, NilClass] Zabbix object id
113
142
  def create_dns_item(hostid, dns_name)
114
- # 字串内插
115
- item_name = "【DNS域名解析监控】#{dns_name}"
116
- item_key_ = "net.dns.record[,#{dns_name},A,2,2]"
143
+ name = "【DNS域名解析监控】#{dns_name}"
144
+ key = "net.dns.record[,#{dns_name},A,2,2]"
117
145
 
118
- # 请求绑定 dns 监控项到特定的 hostid
146
+ params = default_dns_params.merge(hostid: hostid, name: name, key: key)
119
147
  result = @client.api_request(
120
148
  method: "item.create",
121
- params: {
122
- hostid: hostid,
123
- name: item_name,
124
- key_: item_key_,
125
- # 代表 zabbix_agent
126
- type: 0,
127
- # 代表字符串
128
- value_type: 1,
129
- # 固定参数
130
- delay: "1m",
131
- history: "90d",
132
- lifetime: "30d",
133
- timeout: "3s"
134
- }
135
- )
136
- p "成功创建 dns监控 #{dns_name}"
137
- rescue StandardError
138
- p "创建 dns监控 #{dns_name} 异常"
139
- end
140
-
141
- # 查询某个监控项具体信息
142
- def get_item_info
143
- result = @client.api_request(
144
- method: "item.get",
145
- params: {
146
- output: "extend",
147
- hostids: "16914"
148
- }
149
+ params: params
149
150
  )
151
+ result.empty? ? nil : result
150
152
  end
151
153
  end
152
154
  end
@@ -44,7 +44,7 @@ class ZabbixManager
44
44
  #
45
45
  # @param data [Hash] Should include object's id field name (identify) and id value
46
46
  # @param force [Boolean] Whether to force an object update even if provided data matches Zabbix
47
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
47
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
48
48
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
49
49
  # @return [Integer] The object id if a single object is created
50
50
  # @return [Boolean] True/False if multiple objects are created
@@ -71,16 +71,16 @@ class ZabbixManager
71
71
  # Get MediaType object id from API based on provided data
72
72
  #
73
73
  # @param data [Hash]
74
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call or missing object's id field name (identify).
74
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call or missing object's id field name (identify).
75
75
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
76
- # @return [Integer] Zabbix object id
76
+ # @return [Integer, NilClass] Zabbix object id
77
77
  def get_id(data)
78
78
  log "[DEBUG] Call get_id with parameters: #{data.inspect}"
79
79
  # symbolize keys if the user used string keys instead of symbols
80
80
  data = symbolize_keys(data) if data.key?(identify)
81
81
  # raise an error if identify name was not supplied
82
82
  name = data[identify.to_sym]
83
- raise ManagerError, "#{identify} not supplied in call to get_id, #{data} (#{method_name})" if name.nil?
83
+ raise ZbxError, "#{identify} not supplied in call to get_id, #{data} (#{method_name})" if name.nil?
84
84
 
85
85
  result = @client.api_request(
86
86
  method: "#{method_name}.get",
@@ -97,7 +97,7 @@ class ZabbixManager
97
97
  # Create or update MediaType object using API
98
98
  #
99
99
  # @param data [Hash] Should include object's id field name (identify) and id value
100
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
100
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
101
101
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
102
102
  # @return [Integer] The object id if a single object is created
103
103
  # @return [Boolean] True/False if multiple objects are created