zabbix_manager 5.0.1 → 5.0.2

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