zabbixapi 3.1.0 → 3.2.1

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 (64) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +9 -0
  3. data/CHANGELOG.md +5 -0
  4. data/{LICENSE → LICENSE.md} +1 -1
  5. data/README.md +53 -569
  6. data/lib/zabbixapi.rb +102 -65
  7. data/lib/zabbixapi/basic/basic_alias.rb +21 -4
  8. data/lib/zabbixapi/basic/basic_func.rb +56 -23
  9. data/lib/zabbixapi/basic/basic_init.rb +21 -4
  10. data/lib/zabbixapi/basic/basic_logic.rb +75 -18
  11. data/lib/zabbixapi/classes/actions.rb +8 -4
  12. data/lib/zabbixapi/classes/applications.rb +20 -6
  13. data/lib/zabbixapi/classes/configurations.rb +23 -17
  14. data/lib/zabbixapi/classes/errors.rb +2 -4
  15. data/lib/zabbixapi/classes/graphs.rb +65 -15
  16. data/lib/zabbixapi/classes/hostgroups.rb +12 -4
  17. data/lib/zabbixapi/classes/hosts.rb +36 -10
  18. data/lib/zabbixapi/classes/httptests.rb +24 -4
  19. data/lib/zabbixapi/classes/items.rb +24 -5
  20. data/lib/zabbixapi/classes/maintenance.rb +8 -4
  21. data/lib/zabbixapi/classes/mediatypes.rb +20 -13
  22. data/lib/zabbixapi/classes/proxies.rb +29 -8
  23. data/lib/zabbixapi/classes/screens.rb +41 -25
  24. data/lib/zabbixapi/classes/server.rb +8 -4
  25. data/lib/zabbixapi/classes/templates.rb +46 -43
  26. data/lib/zabbixapi/classes/triggers.rb +43 -16
  27. data/lib/zabbixapi/classes/unusable.rb +0 -2
  28. data/lib/zabbixapi/classes/usergroups.rb +33 -26
  29. data/lib/zabbixapi/classes/usermacros.rb +137 -31
  30. data/lib/zabbixapi/classes/users.rb +32 -10
  31. data/lib/zabbixapi/classes/valuemaps.rb +50 -0
  32. data/lib/zabbixapi/client.rb +61 -22
  33. data/lib/zabbixapi/version.rb +1 -1
  34. data/zabbixapi.gemspec +26 -23
  35. metadata +25 -73
  36. data/.gitignore +0 -7
  37. data/.rspec +0 -1
  38. data/.travis.yml +0 -41
  39. data/Gemfile +0 -3
  40. data/Gemfile.lock +0 -36
  41. data/Rakefile +0 -1
  42. data/json-1.x.Gemfile +0 -4
  43. data/json-1.x.Gemfile.lock +0 -28
  44. data/spec/action.rb +0 -89
  45. data/spec/application.rb +0 -83
  46. data/spec/basic_func.rb +0 -4
  47. data/spec/configuration.rb +0 -122
  48. data/spec/graph.rb +0 -135
  49. data/spec/host.rb +0 -176
  50. data/spec/hostgroup.rb +0 -55
  51. data/spec/httptest.rb +0 -136
  52. data/spec/item.rb +0 -134
  53. data/spec/maintenance.rb +0 -81
  54. data/spec/mediatype.rb +0 -50
  55. data/spec/query.rb +0 -18
  56. data/spec/screen.rb +0 -88
  57. data/spec/script.rb +0 -123
  58. data/spec/server.rb +0 -15
  59. data/spec/spec_helper.rb +0 -21
  60. data/spec/template.rb +0 -148
  61. data/spec/trigger.rb +0 -103
  62. data/spec/user.rb +0 -116
  63. data/spec/usergroup.rb +0 -85
  64. data/spec/usermacro.rb +0 -190
@@ -1,43 +1,65 @@
1
1
  class ZabbixApi
2
2
  class Users < Basic
3
-
3
+ # The method name used for interacting with Users via Zabbix API
4
+ #
5
+ # @return [String]
4
6
  def method_name
5
- "user"
7
+ 'user'
6
8
  end
7
9
 
10
+ # The keys field name used for User objects via Zabbix API
11
+ #
12
+ # @return [String]
8
13
  def keys
9
- "userids"
14
+ 'userids'
10
15
  end
11
16
 
17
+ # The key field name used for User objects via Zabbix API
18
+ #
19
+ # @return [String]
12
20
  def key
13
- "userid"
21
+ 'userid'
14
22
  end
15
23
 
24
+ # The id field name used for identifying specific User objects via Zabbix API
25
+ #
26
+ # @return [String]
16
27
  def indentify
17
- "alias"
28
+ 'alias'
18
29
  end
19
30
 
31
+ # Add media to users using Zabbix API
32
+ #
33
+ # @param data [Hash] Needs to include userids and media to mass add media to users
34
+ # @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
35
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
36
+ # @return [Integer] Zabbix object id (media)
20
37
  def add_medias(data)
21
38
  result = @client.api_request(
22
- :method => "user.addMedia",
39
+ :method => 'user.addMedia',
23
40
  :params => {
24
41
  :users => data[:userids].map { |t| {:userid => t} },
25
- :medias => data[:media]
42
+ :medias => data[:media],
26
43
  }
27
44
  )
28
45
  result ? result['mediaids'][0].to_i : nil
29
46
  end
30
47
 
48
+ # Update media for users using Zabbix API
49
+ #
50
+ # @param data [Hash] Needs to include userids and media to mass update media for users
51
+ # @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
52
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
53
+ # @return [Integer] Zabbix object id (user)
31
54
  def update_medias(data)
32
55
  result = @client.api_request(
33
- :method => "user.updateMedia",
56
+ :method => 'user.updateMedia',
34
57
  :params => {
35
58
  :users => data[:userids].map { |t| {:userid => t} },
36
- :medias => data[:media]
59
+ :medias => data[:media],
37
60
  }
38
61
  )
39
62
  result ? result['userids'][0].to_i : nil
40
63
  end
41
-
42
64
  end
43
65
  end
@@ -0,0 +1,50 @@
1
+ class ZabbixApi
2
+ class ValueMaps < Basic
3
+ # The method name used for interacting with ValueMaps via Zabbix API
4
+ #
5
+ # @return [String]
6
+ def method_name
7
+ 'valuemap'
8
+ end
9
+
10
+ # The key field name used for ValueMap objects via Zabbix API
11
+ #
12
+ # @return [String]
13
+ def key
14
+ 'valuemapid'
15
+ end
16
+
17
+ # The id field name used for identifying specific ValueMap objects via Zabbix API
18
+ #
19
+ # @return [String]
20
+ def indentify
21
+ 'name'
22
+ end
23
+
24
+ # Get or Create ValueMap object using Zabbix API
25
+ #
26
+ # @param data [Hash] Needs to include valuemapids [List] to properly identify ValueMaps via Zabbix API
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
30
+ def get_or_create(data)
31
+ log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
32
+
33
+ unless (id = get_id(:valuemapids => data[:valuemapids]))
34
+ id = create(data)
35
+ end
36
+ id
37
+ end
38
+
39
+ # Create or update Item object using Zabbix API
40
+ #
41
+ # @param data [Hash] Needs to include valuemapids to properly identify ValueMaps via Zabbix API
42
+ # @raise [ApiError] Error returned when there is a problem with the Zabbix API call.
43
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
44
+ # @return [Integer] Zabbix object id
45
+ def create_or_update(data)
46
+ valuemapid = get_id(:name => data[:name])
47
+ valuemapid ? update(data.merge(:valuemapids => [:valuemapid])) : create(data)
48
+ end
49
+ end
50
+ end
@@ -1,20 +1,27 @@
1
- require 'json'
2
- require 'net/https'
3
1
  require 'net/http'
2
+ require 'json'
4
3
 
5
4
  class ZabbixApi
6
5
  class Client
6
+ # @param (see ZabbixApi::Client#initialize)
7
+ # @return [Hash]
8
+ attr_reader :options
7
9
 
8
- attr :options
9
-
10
+ # @return [Integer]
10
11
  def id
11
- rand(100000)
12
+ rand(100_000)
12
13
  end
13
14
 
15
+ # Returns the API version from the Zabbix Server
16
+ #
17
+ # @return [String]
14
18
  def api_version
15
- @version ||= api_request(:method => "apiinfo.version", :params => {})
19
+ @version ||= api_request(:method => 'apiinfo.version', :params => {})
16
20
  end
17
21
 
22
+ # Log in to the Zabbix Server and generate an auth token using the API
23
+ #
24
+ # @return [Hash]
18
25
  def auth
19
26
  api_request(
20
27
  :method => 'user.login',
@@ -25,9 +32,20 @@ class ZabbixApi
25
32
  )
26
33
  end
27
34
 
35
+ # Initializes a new Client object
36
+ #
37
+ # @param options [Hash]
38
+ # @option opts [String] :url The url of zabbixapi(example: 'http://localhost/zabbix/api_jsonrpc.php')
39
+ # @option opts [String] :user
40
+ # @option opts [String] :password
41
+ # @option opts [String] :http_user A user for basic auth.(optional)
42
+ # @option opts [String] :http_password A password for basic auth.(optional)
43
+ # @option opts [Integer] :timeout Set timeout for requests in seconds.(default: 60)
44
+ #
45
+ # @return [ZabbixApi::Client]
28
46
  def initialize(options = {})
29
47
  @options = options
30
- if ENV['http_proxy'] != nil && options[:no_proxy] != true
48
+ if !ENV['http_proxy'].nil? && options[:no_proxy] != true
31
49
  @proxy_uri = URI.parse(ENV['http_proxy'])
32
50
  @proxy_host = @proxy_uri.host
33
51
  @proxy_port = @proxy_uri.port
@@ -39,39 +57,44 @@ class ZabbixApi
39
57
  @auth_hash = auth
40
58
  end
41
59
 
60
+ # Convert message body to JSON string for the Zabbix API
61
+ #
62
+ # @param body [Hash]
63
+ # @return [String]
42
64
  def message_json(body)
43
65
  message = {
44
66
  :method => body[:method],
45
67
  :params => body[:params],
46
68
  :id => id,
47
- :jsonrpc => '2.0'
69
+ :jsonrpc => '2.0',
48
70
  }
49
71
 
50
- message[:auth] = @auth_hash unless (body[:method] == 'apiinfo.version' or body[:method] == 'user.login')
72
+ message[:auth] = @auth_hash unless body[:method] == 'apiinfo.version' || body[:method] == 'user.login'
51
73
 
52
74
  JSON.generate(message)
53
75
  end
54
76
 
77
+ # @param body [String]
78
+ # @return [String]
55
79
  def http_request(body)
56
80
  uri = URI.parse(@options[:url])
81
+
57
82
  # set the time out the default (60) or to what the user passed
58
- @options[:timeout] == nil ? timeout = 60 : timeout = @options[:timeout]
83
+ timeout = @options[:timeout].nil? ? 60 : @options[:timeout]
59
84
  puts "[DEBUG] Timeout for request set to #{timeout} seconds" if @options[:debug]
60
85
 
61
- unless @proxy_uri.nil?
86
+ if @proxy_uri
62
87
  http = Net::HTTP.Proxy(@proxy_host, @proxy_port, @proxy_user, @proxy_pass).new(uri.host, uri.port)
63
-
64
- if uri.scheme == 'https'
65
- http.use_ssl = true
66
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
67
- end
68
88
  else
69
89
  http = Net::HTTP.new(uri.host, uri.port)
70
- if uri.scheme == 'https'
71
- http.use_ssl = true
72
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
73
- end
74
90
  end
91
+
92
+ if uri.scheme == 'https'
93
+ http.use_ssl = true
94
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
95
+ end
96
+
97
+ http.open_timeout = timeout
75
98
  http.read_timeout = timeout
76
99
 
77
100
  request = Net::HTTP::Post.new(uri.request_uri)
@@ -79,21 +102,37 @@ class ZabbixApi
79
102
  request.add_field('Content-Type', 'application/json-rpc')
80
103
  request.body = body
81
104
  response = http.request(request)
105
+
82
106
  raise HttpError.new("HTTP Error: #{response.code} on #{@options[:url]}", response) unless response.code == '200'
107
+
83
108
  puts "[DEBUG] Get answer: #{response.body}" if @options[:debug]
84
109
  response.body
85
110
  end
86
111
 
112
+ # @param body [String]
113
+ # @return [Hash, String]
87
114
  def _request(body)
88
115
  puts "[DEBUG] Send request: #{body}" if @options[:debug]
89
116
  result = JSON.parse(http_request(body))
90
- raise ApiError.new("Server answer API error\n #{JSON.pretty_unparse(result['error'])}\n on request:\n #{JSON.pretty_unparse(JSON.parse(body))}", result) if result['error']
117
+ raise ApiError.new("Server answer API error\n #{JSON.pretty_unparse(result['error'])}\n on request:\n #{pretty_body(body)}", result) if result['error']
91
118
  result['result']
92
119
  end
93
120
 
121
+ def pretty_body(body)
122
+ parsed_body = JSON.parse(body)
123
+
124
+ # If password is in body hide it
125
+ parsed_body['params']['password'] = '***' if parsed_body['params'].is_a?(Hash) && parsed_body['params'].key?('password')
126
+
127
+ JSON.pretty_unparse(parsed_body)
128
+ end
129
+
130
+ # Execute Zabbix API requests and return response
131
+ #
132
+ # @param body [Hash]
133
+ # @return [Hash, String]
94
134
  def api_request(body)
95
135
  _request message_json(body)
96
136
  end
97
-
98
137
  end
99
138
  end
@@ -1,3 +1,3 @@
1
1
  class ZabbixApi
2
- VERSION = "3.1.0"
2
+ VERSION = '3.2.1'.freeze
3
3
  end
@@ -1,26 +1,29 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+
3
7
  require 'zabbixapi/version'
4
8
 
5
- Gem::Specification.new do |s|
6
- s.name = "zabbixapi"
7
- s.version = ZabbixApi::VERSION
8
- s.authors = ["Vasiliev D.V.", "Ivan Evtuhovich"]
9
- s.email = %w(vadv.mkn@gmail.com evtuhovich@gmail.com)
10
- s.homepage = "https://github.com/express42/zabbixapi"
11
- s.summary = %q{Realization for Zabbix API.}
12
- s.description = %q{Allows you to work with zabbix api from ruby.}
13
- s.licenses = %w(MIT)
14
-
15
- s.add_runtime_dependency 'json'
16
- s.required_ruby_version = Gem::Requirement.new(">= 2".freeze)
17
- s.add_development_dependency 'rake'
18
- s.add_development_dependency 'rspec'
19
-
20
- s.rubyforge_project = "zabbixapi"
21
-
22
- s.files = `git ls-files`.split("\n")
23
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
24
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
25
- s.require_paths = %w(lib)
9
+ Gem::Specification.new do |spec|
10
+ spec.add_dependency 'http', '~> 2.0'
11
+ spec.add_dependency 'json', '~> 2.0'
12
+ spec.add_development_dependency 'bundler', '~> 1.0'
13
+
14
+ spec.name = 'zabbixapi'
15
+ spec.version = ZabbixApi::VERSION
16
+ spec.authors = ['Vasiliev D.V.', 'Ivan Evtuhovich']
17
+ spec.email = ['vadv.mkn@gmail.com', 'evtuhovich@gmail.com']
18
+
19
+ spec.summary = 'Simple and lightweight ruby module for working with the Zabbix API'
20
+ spec.description = 'Allows you to work with zabbix api from ruby.'
21
+ spec.homepage = 'https://github.com/express42/zabbixapi'
22
+ spec.licenses = 'MIT'
23
+
24
+ spec.rubyforge_project = 'zabbixapi'
25
+
26
+ spec.files = ['.yardopts', 'CHANGELOG.md', 'LICENSE.md', 'README.md', 'zabbixapi.gemspec'] + Dir['lib/**/*.rb']
27
+ spec.require_paths = 'lib'
28
+ spec.required_ruby_version = '>= 2.0.0'
26
29
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zabbixapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vasiliev D.V.
@@ -9,50 +9,50 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-02-23 00:00:00.000000000 Z
12
+ date: 2017-08-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: json
15
+ name: http
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - ">="
18
+ - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: '0'
20
+ version: '2.0'
21
21
  type: :runtime
22
22
  prerelease: false
23
23
  version_requirements: !ruby/object:Gem::Requirement
24
24
  requirements:
25
- - - ">="
25
+ - - "~>"
26
26
  - !ruby/object:Gem::Version
27
- version: '0'
27
+ version: '2.0'
28
28
  - !ruby/object:Gem::Dependency
29
- name: rake
29
+ name: json
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - ">="
32
+ - - "~>"
33
33
  - !ruby/object:Gem::Version
34
- version: '0'
35
- type: :development
34
+ version: '2.0'
35
+ type: :runtime
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - ">="
39
+ - - "~>"
40
40
  - !ruby/object:Gem::Version
41
- version: '0'
41
+ version: '2.0'
42
42
  - !ruby/object:Gem::Dependency
43
- name: rspec
43
+ name: bundler
44
44
  requirement: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - ">="
46
+ - - "~>"
47
47
  - !ruby/object:Gem::Version
48
- version: '0'
48
+ version: '1.0'
49
49
  type: :development
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - ">="
53
+ - - "~>"
54
54
  - !ruby/object:Gem::Version
55
- version: '0'
55
+ version: '1.0'
56
56
  description: Allows you to work with zabbix api from ruby.
57
57
  email:
58
58
  - vadv.mkn@gmail.com
@@ -61,17 +61,10 @@ executables: []
61
61
  extensions: []
62
62
  extra_rdoc_files: []
63
63
  files:
64
- - ".gitignore"
65
- - ".rspec"
66
- - ".travis.yml"
64
+ - ".yardopts"
67
65
  - CHANGELOG.md
68
- - Gemfile
69
- - Gemfile.lock
70
- - LICENSE
66
+ - LICENSE.md
71
67
  - README.md
72
- - Rakefile
73
- - json-1.x.Gemfile
74
- - json-1.x.Gemfile.lock
75
68
  - lib/zabbixapi.rb
76
69
  - lib/zabbixapi/basic/basic_alias.rb
77
70
  - lib/zabbixapi/basic/basic_func.rb
@@ -98,29 +91,9 @@ files:
98
91
  - lib/zabbixapi/classes/usergroups.rb
99
92
  - lib/zabbixapi/classes/usermacros.rb
100
93
  - lib/zabbixapi/classes/users.rb
94
+ - lib/zabbixapi/classes/valuemaps.rb
101
95
  - lib/zabbixapi/client.rb
102
96
  - lib/zabbixapi/version.rb
103
- - spec/action.rb
104
- - spec/application.rb
105
- - spec/basic_func.rb
106
- - spec/configuration.rb
107
- - spec/graph.rb
108
- - spec/host.rb
109
- - spec/hostgroup.rb
110
- - spec/httptest.rb
111
- - spec/item.rb
112
- - spec/maintenance.rb
113
- - spec/mediatype.rb
114
- - spec/query.rb
115
- - spec/screen.rb
116
- - spec/script.rb
117
- - spec/server.rb
118
- - spec/spec_helper.rb
119
- - spec/template.rb
120
- - spec/trigger.rb
121
- - spec/user.rb
122
- - spec/usergroup.rb
123
- - spec/usermacro.rb
124
97
  - zabbixapi.gemspec
125
98
  homepage: https://github.com/express42/zabbixapi
126
99
  licenses:
@@ -134,7 +107,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
134
107
  requirements:
135
108
  - - ">="
136
109
  - !ruby/object:Gem::Version
137
- version: '2'
110
+ version: 2.0.0
138
111
  required_rubygems_version: !ruby/object:Gem::Requirement
139
112
  requirements:
140
113
  - - ">="
@@ -142,29 +115,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
115
  version: '0'
143
116
  requirements: []
144
117
  rubyforge_project: zabbixapi
145
- rubygems_version: 2.6.10
118
+ rubygems_version: 2.6.11
146
119
  signing_key:
147
120
  specification_version: 4
148
- summary: Realization for Zabbix API.
149
- test_files:
150
- - spec/action.rb
151
- - spec/application.rb
152
- - spec/basic_func.rb
153
- - spec/configuration.rb
154
- - spec/graph.rb
155
- - spec/host.rb
156
- - spec/hostgroup.rb
157
- - spec/httptest.rb
158
- - spec/item.rb
159
- - spec/maintenance.rb
160
- - spec/mediatype.rb
161
- - spec/query.rb
162
- - spec/screen.rb
163
- - spec/script.rb
164
- - spec/server.rb
165
- - spec/spec_helper.rb
166
- - spec/template.rb
167
- - spec/trigger.rb
168
- - spec/user.rb
169
- - spec/usergroup.rb
170
- - spec/usermacro.rb
121
+ summary: Simple and lightweight ruby module for working with the Zabbix API
122
+ test_files: []