zabbixapi 4.0.0 → 5.0.0.pre.alpha1

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 (40) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +20 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +30 -17
  5. data/lib/zabbixapi.rb +31 -1
  6. data/lib/zabbixapi/basic/basic_alias.rb +2 -2
  7. data/lib/zabbixapi/basic/basic_func.rb +13 -12
  8. data/lib/zabbixapi/basic/basic_init.rb +5 -5
  9. data/lib/zabbixapi/basic/basic_logic.rb +35 -34
  10. data/lib/zabbixapi/classes/actions.rb +25 -1
  11. data/lib/zabbixapi/classes/applications.rb +4 -4
  12. data/lib/zabbixapi/classes/configurations.rb +3 -3
  13. data/lib/zabbixapi/classes/drules.rb +55 -0
  14. data/lib/zabbixapi/classes/errors.rb +5 -2
  15. data/lib/zabbixapi/classes/events.rb +17 -0
  16. data/lib/zabbixapi/classes/graphs.rb +23 -33
  17. data/lib/zabbixapi/classes/hostgroups.rb +1 -1
  18. data/lib/zabbixapi/classes/hosts.rb +20 -20
  19. data/lib/zabbixapi/classes/httptests.rb +7 -7
  20. data/lib/zabbixapi/classes/items.rb +36 -36
  21. data/lib/zabbixapi/classes/maintenance.rb +1 -1
  22. data/lib/zabbixapi/classes/mediatypes.rb +86 -11
  23. data/lib/zabbixapi/classes/problems.rb +101 -0
  24. data/lib/zabbixapi/classes/proxies.rb +4 -4
  25. data/lib/zabbixapi/classes/roles.rb +114 -0
  26. data/lib/zabbixapi/classes/screens.rb +18 -17
  27. data/lib/zabbixapi/classes/scripts.rb +18 -10
  28. data/lib/zabbixapi/classes/server.rb +1 -1
  29. data/lib/zabbixapi/classes/templates.rb +19 -21
  30. data/lib/zabbixapi/classes/triggers.rb +14 -13
  31. data/lib/zabbixapi/classes/unusable.rb +1 -1
  32. data/lib/zabbixapi/classes/usergroups.rb +16 -19
  33. data/lib/zabbixapi/classes/usermacros.rb +24 -24
  34. data/lib/zabbixapi/classes/users.rb +16 -17
  35. data/lib/zabbixapi/classes/valuemaps.rb +4 -4
  36. data/lib/zabbixapi/client.rb +34 -16
  37. data/lib/zabbixapi/version.rb +1 -1
  38. data/zabbixapi.gemspec +2 -2
  39. metadata +16 -14
  40. data/.yardopts +0 -9
@@ -10,7 +10,7 @@ class ZabbixApi
10
10
  # The id field name used for identifying specific Maintenance objects via Zabbix API
11
11
  #
12
12
  # @return [String]
13
- def indentify
13
+ def identify
14
14
  'name'
15
15
  end
16
16
  end
@@ -10,8 +10,8 @@ class ZabbixApi
10
10
  # The id field name used for identifying specific MediaType objects via Zabbix API
11
11
  #
12
12
  # @return [String]
13
- def indentify
14
- 'description'
13
+ def identify
14
+ 'name'
15
15
  end
16
16
 
17
17
  # The default options used when creating MediaType objects via Zabbix API
@@ -19,16 +19,91 @@ class ZabbixApi
19
19
  # @return [Hash]
20
20
  def default_options
21
21
  {
22
- :description => '', # Name
23
- :type => 0, # 0 - Email, 1 - External script, 2 - SMS, 3 - Jabber, 100 - EzTexting
24
- :smtp_server => '',
25
- :smtp_helo => '',
26
- :smtp_email => '', # Email address of Zabbix server
27
- :exec_path => '', # Name of external script
28
- :gsm_modem => '', # Serial device name of GSM modem
29
- :username => '', # Jabber user name used by Zabbix server
30
- :passwd => '' # Jabber password used by Zabbix server
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
31
32
  }
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 [ApiError] 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 [ApiError] 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 ApiError.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 [ApiError] 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
33
108
  end
34
109
  end
@@ -0,0 +1,101 @@
1
+ class ZabbixApi
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 [ApiError] 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 [ApiError] 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 [ApiError] 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
+ end
101
+ end
@@ -10,7 +10,7 @@ class ZabbixApi
10
10
  # The id field name used for identifying specific Proxy objects via Zabbix API
11
11
  #
12
12
  # @return [String]
13
- def indentify
13
+ def identify
14
14
  'host'
15
15
  end
16
16
 
@@ -21,7 +21,7 @@ class ZabbixApi
21
21
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
22
22
  # @return [Integer] The Proxy object id that was deleted
23
23
  def delete(data)
24
- result = @client.api_request(:method => 'proxy.delete', :params => data)
24
+ result = @client.api_request(method: 'proxy.delete', params: data)
25
25
  result.empty? ? nil : result['proxyids'][0].to_i
26
26
  end
27
27
 
@@ -32,7 +32,7 @@ class ZabbixApi
32
32
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
33
33
  # @return [Boolean] Returns true if the given proxies are readable
34
34
  def isreadable(data)
35
- @client.api_request(:method => 'proxy.isreadable', :params => data)
35
+ @client.api_request(method: 'proxy.isreadable', params: data)
36
36
  end
37
37
 
38
38
  # Check if a Proxy object is writable using Zabbix API
@@ -42,7 +42,7 @@ class ZabbixApi
42
42
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
43
43
  # @return [Boolean] Returns true if the given proxies are writable
44
44
  def iswritable(data)
45
- @client.api_request(:method => 'proxy.iswritable', :params => data)
45
+ @client.api_request(method: 'proxy.iswritable', params: data)
46
46
  end
47
47
  end
48
48
  end
@@ -0,0 +1,114 @@
1
+ class ZabbixApi
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 [ApiError] 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 [ApiError] 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 [ApiError] 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 [ApiError] 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 [ApiError] 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
@@ -29,7 +29,7 @@ class ZabbixApi
29
29
  # The id field name used for identifying specific Screen objects via Zabbix API
30
30
  #
31
31
  # @return [String]
32
- def indentify
32
+ def identify
33
33
  'name'
34
34
  end
35
35
 
@@ -40,7 +40,7 @@ class ZabbixApi
40
40
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
41
41
  # @return [Integer] Zabbix object id
42
42
  def delete(data)
43
- result = @client.api_request(:method => 'screen.delete', :params => [data])
43
+ result = @client.api_request(method: 'screen.delete', params: [data])
44
44
  result.empty? ? nil : result['screenids'][0].to_i
45
45
  end
46
46
 
@@ -62,29 +62,30 @@ class ZabbixApi
62
62
  height = data[:height] || 320 # default 320
63
63
  width = data[:width] || 200 # default 200
64
64
  vsize = data[:vsize] || [1, (graphids.size / hsize).to_i].max
65
- screenid = get_id(:name => screen_name)
65
+ screenid = get_id(name: screen_name)
66
66
 
67
67
  unless screenid
68
68
  # Create screen
69
69
  graphids.each_with_index do |graphid, index|
70
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,
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
81
  }
82
82
  end
83
+
83
84
  screenid = create(
84
- :name => screen_name,
85
- :hsize => hsize,
86
- :vsize => vsize,
87
- :screenitems => screenitems
85
+ name: screen_name,
86
+ hsize: hsize,
87
+ vsize: vsize,
88
+ screenitems: screenitems
88
89
  )
89
90
  end
90
91
  screenid
@@ -1,27 +1,35 @@
1
1
  class ZabbixApi
2
2
  class Scripts < Basic
3
-
4
3
  def method_name
5
- "script"
4
+ 'script'
6
5
  end
7
6
 
8
- def indentify
9
- "name"
7
+ # The id field name used for identifying specific Screen objects via Zabbix API
8
+ #
9
+ # @return [String]
10
+ def identify
11
+ 'name'
10
12
  end
11
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
12
21
  def execute(data)
13
22
  @client.api_request(
14
- :method => "script.execute",
15
- :params => {
16
- :scriptid => data[:scriptid],
17
- :hostid => data[:hostid]
23
+ method: 'script.execute',
24
+ params: {
25
+ scriptid: data[:scriptid],
26
+ hostid: data[:hostid]
18
27
  }
19
28
  )
20
29
  end
21
30
 
22
31
  def getscriptsbyhost(data)
23
- @client.api_request(:method => "script.getscriptsbyhosts", :params => data)
32
+ @client.api_request(method: 'script.getscriptsbyhosts', params: data)
24
33
  end
25
-
26
34
  end
27
35
  end