zabbix_manager 5.0.1 → 5.0.2

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 (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,109 @@
1
+ class ZabbixManager
2
+ class Mediatypes < Basic
3
+ # The method name used for interacting with MediaTypes via Zabbix API
4
+ #
5
+ # @return [String]
6
+ def method_name
7
+ 'mediatype'
8
+ end
9
+
10
+ # The id field name used for identifying specific MediaType objects via Zabbix API
11
+ #
12
+ # @return [String]
13
+ def identify
14
+ 'name'
15
+ end
16
+
17
+ # The default options used when creating MediaType objects via Zabbix API
18
+ #
19
+ # @return [Hash]
20
+ def default_options
21
+ {
22
+ name: '', # Name
23
+ description: '', # Description
24
+ type: 0, # 0 - Email, 1 - External script, 2 - SMS, 3 - Jabber, 100 - EzTexting
25
+ smtp_server: '',
26
+ smtp_helo: '',
27
+ smtp_email: '', # Email address of Zabbix server
28
+ exec_path: '', # Name of external script
29
+ gsm_modem: '', # Serial device name of GSM modem
30
+ username: '', # Jabber user name used by Zabbix server
31
+ passwd: '' # Jabber password used by Zabbix server
32
+ }
33
+ end
34
+
35
+ # def log(message)
36
+ # STDERR.puts
37
+ # STDERR.puts message.to_s
38
+ # STDERR.puts
39
+ # end
40
+
41
+ # Update MediaType object using API
42
+ #
43
+ # @param data [Hash] Should include object's id field name (identify) and id value
44
+ # @param force [Boolean] Whether to force an object update even if provided data matches Zabbix
45
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
46
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
47
+ # @return [Integer] The object id if a single object is created
48
+ # @return [Boolean] True/False if multiple objects are created
49
+ def update(data, force = false)
50
+ log "[DEBUG] Call update with parameters: #{data.inspect}"
51
+ if data[key.to_sym].nil?
52
+ data[key.to_sym] = get_id(data)
53
+ log "[DEBUG] Enriched data with id: #{data.inspect}"
54
+ end
55
+ dump = {}
56
+ dump_by_id(key.to_sym => data[key.to_sym]).each do |item|
57
+ dump = symbolize_keys(item) if item[key].to_i == data[key.to_sym].to_i
58
+ end
59
+ if hash_equals?(dump, data) && !force
60
+ log "[DEBUG] Equal keys #{dump} and #{data}, skip update"
61
+ data[key.to_sym].to_i
62
+ else
63
+ data_update = [data]
64
+ result = @client.api_request(method: "#{method_name}.update", params: data_update)
65
+ parse_keys result
66
+ end
67
+ end
68
+
69
+ # Get MediaType object id from API based on provided data
70
+ #
71
+ # @param data [Hash]
72
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call or missing object's id field name (identify).
73
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
74
+ # @return [Integer] Zabbix object id
75
+ def get_id(data)
76
+ log "[DEBUG] Call get_id with parameters: #{data.inspect}"
77
+ # symbolize keys if the user used string keys instead of symbols
78
+ data = symbolize_keys(data) if data.key?(identify)
79
+ # raise an error if identify name was not supplied
80
+ name = data[identify.to_sym]
81
+ raise ManagerError.new("#{identify} not supplied in call to get_id, #{data} (#{method_name})") if name.nil?
82
+
83
+ result = @client.api_request(
84
+ method: "#{method_name}.get",
85
+ params: {
86
+ filter: {name: name},
87
+ output: [key, identify]
88
+ }
89
+ )
90
+ id = nil
91
+ result.each { |item| id = item[key].to_i if item[identify] == data[identify.to_sym] }
92
+ id
93
+ end
94
+
95
+ # Create or update MediaType object using API
96
+ #
97
+ # @param data [Hash] Should include object's id field name (identify) and id value
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] The object id if a single object is created
101
+ # @return [Boolean] True/False if multiple objects are created
102
+ def create_or_update(data)
103
+ log "[DEBUG] Call create_or_update with parameters: #{data.inspect}"
104
+
105
+ id = get_id(identify.to_sym => data[identify.to_sym])
106
+ id ? update(data.merge(key.to_sym => id.to_s)) : create(data)
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,133 @@
1
+ class ZabbixManager
2
+ class Problems < Basic
3
+ # The method name used for interacting with Hosts via Zabbix API
4
+ #
5
+ # @return [String]
6
+ def method_name
7
+ 'problem'
8
+ end
9
+
10
+ # The id field name used for identifying specific Problem objects via Zabbix API
11
+ #
12
+ # @return [String]
13
+ def identify
14
+ 'name'
15
+ end
16
+
17
+ # The key field name used for Problem objects via Zabbix API
18
+ # However, Problem object does not have a unique identifier
19
+ #
20
+ # @return [String]
21
+ def key
22
+ 'problemid'
23
+ end
24
+
25
+ # Returns the object's plural id field name (identify) based on key
26
+ # However, Problem object does not have a unique identifier
27
+ #
28
+ # @return [String]
29
+ def keys
30
+ 'problemids'
31
+ end
32
+
33
+ # Dump Problem object data by key from Zabbix API
34
+ #
35
+ # @param data [Hash] Should include desired object's key and value
36
+ # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
37
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
38
+ # @return [Hash]
39
+ def dump_by_id(data)
40
+ log "[DEBUG] Call dump_by_id with parameters: #{data.inspect}"
41
+
42
+ @client.api_request(
43
+ method: 'problem.get',
44
+ params: {
45
+ filter: {
46
+ identify.to_sym => data[identify.to_sym]
47
+ },
48
+ output: 'extend'
49
+ }
50
+ )
51
+ end
52
+
53
+ # Get full/extended Problem data from Zabbix API
54
+ #
55
+ # @param data [Hash] Should include object's id field name (identify) and id value
56
+ # @raise [ManagerError] 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 get_full_data(data)
60
+ log "[DEBUG] Call get_full_data with parameters: #{data.inspect}"
61
+
62
+ data = symbolize_keys(data)
63
+
64
+ @client.api_request(
65
+ method: "#{method_name}.get",
66
+ params: {
67
+ filter: {
68
+ identify.to_sym => data[identify.to_sym]
69
+ },
70
+ eventids: data[:eventids] || nil,
71
+ groupids: data[:groupids] || nil,
72
+ hostids: data[:hostids] || nil,
73
+ objectids: data[:objectids] || nil,
74
+ applicationids: data[:applicationids] || nil,
75
+ tags: data[:tags] || nil,
76
+ time_from: data[:time_from] || nil,
77
+ time_till: data[:time_till] || nil,
78
+ eventid_from: data[:eventid_from] || nil,
79
+ eventid_till: data[:eventid_till] || nil,
80
+ recent: data[:recent] || false,
81
+ sortfield: data[:sortfield] || ['eventid'],
82
+ sortorder: data[:sortorder] || 'DESC',
83
+ countOutput: data[:countOutput] || nil,
84
+ output: 'extend',
85
+ selectAcknowledges: 'extend',
86
+ selectTags: 'extend',
87
+ selectSuppressionData: 'extend'
88
+ }
89
+ )
90
+ end
91
+
92
+ # Get full/extended Zabbix data for Problem objects from API
93
+ #
94
+ # @raise [ManagerError] 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 [Array<Hash>] Array of matching objects
97
+ def all
98
+ get_full_data({})
99
+ end
100
+
101
+ def remove_problem
102
+ # 设置时间区间
103
+ time_from = 180.days.ago.at_beginning_of_day.to_i
104
+ time_till = 14.days.ago.at_beginning_of_day.to_i
105
+
106
+ # 抓取制定区间的数据
107
+ data = get_full_data(time_from: time_from, time_till: time_till)
108
+ event_ids = []
109
+
110
+ # 收集所有的 eventid
111
+ data.each do |item|
112
+ event_ids << item["eventid"]
113
+ end
114
+
115
+ # 返回 event_ids
116
+ ack_event event_ids
117
+ end
118
+
119
+ def ack_event(eventids)
120
+ # 请求后端
121
+ result = @client.api_request(
122
+ method: "event.acknowledge",
123
+ params: {
124
+ eventids: eventids,
125
+ action: 2,
126
+ message: "由 RUBY SCRIPT 自动关闭"
127
+ }
128
+ )
129
+ # 返回运行结果
130
+ result
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,68 @@
1
+ class ZabbixManager
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 [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 [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 [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 [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 [ManagerError] 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
+
48
+ # 新增代理服务器查询接口
49
+ def get_proxy_id(proxy)
50
+ # 边界条件判断
51
+ return nil if proxy.nil?
52
+
53
+ # 请求后端接口,只支持单个代理节点
54
+ result = @client.api_request(
55
+ method: 'proxy.get',
56
+ params: {
57
+ output: "extend",
58
+ filter: {
59
+ host: proxy
60
+ }
61
+ }
62
+ )
63
+
64
+ # 返回代理节点
65
+ result.empty? ? nil : result[0]["proxyid"]
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,114 @@
1
+ class ZabbixManager
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 [ManagerError] 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 [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 (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 [ManagerError] 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 [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 [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 [ManagerError] 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 ZabbixManager
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 [ManagerError] 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 [ManagerError] 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 ZabbixManager
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 ZabbixManager
2
+ class Server
3
+ # @return [String]
4
+ attr_reader :version
5
+
6
+ # Initializes a new Server object with ZabbixManager Client and fetches Zabbix Server API version
7
+ #
8
+ # @param client [ZabbixManager::Client]
9
+ # @return [ZabbixManager::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,123 @@
1
+ class ZabbixManager
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 [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 [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 [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 [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 [ManagerError] 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 [ManagerError] 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 [ManagerError] 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 [ManagerError] 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
+
106
+ # 新增 get_template_id 方法,使用列表 flatten 功能拉平数组对象
107
+ def get_template_ids(data)
108
+ # 直接调后端接口
109
+ result = @client.api_request(
110
+ method: 'template.get',
111
+ params: {
112
+ output: "extend",
113
+ filter: {
114
+ host: [data].flatten
115
+ }
116
+ }
117
+ ).map { |temp| [templateid: temp['templateid']]}
118
+
119
+ # 返回 template 模板数组对象
120
+ result.empty? ? nil : result.flatten
121
+ end
122
+ end
123
+ end