zobbix 0.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9911466813d661136d61be74125c070d5f277e5c
4
+ data.tar.gz: fd1ff95638a274075f3a985516bb9abcdb69646e
5
+ SHA512:
6
+ metadata.gz: 7099349a5a24eea485962b89cd895ce003c98b814433fb4850751ea870bf16eeca01f2fb0c0b16c1dbe915fd0f5a98b14d537b31c7a9f48cf6ca976b7e02985f
7
+ data.tar.gz: cf8dfc3ca569c4ea824a2b7759862e9594783f91121b12656ec7636bcb0d2b6eb3a3025dc02d67067ea45696a5715b7b79751028b8e9e893f3c3bb78c2c340bd
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *.swo
2
+ *.swp
3
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'yard'
data/Gemfile.lock ADDED
@@ -0,0 +1,41 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ zobbix (0.0.1)
5
+ httparty (~> 0.13.7)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ coderay (1.1.0)
11
+ ffaker (2.1.0)
12
+ httparty (0.13.7)
13
+ json (~> 1.8)
14
+ multi_xml (>= 0.5.2)
15
+ json (1.8.3)
16
+ method_source (0.8.2)
17
+ minitest (5.8.1)
18
+ multi_xml (0.5.5)
19
+ pry (0.10.2)
20
+ coderay (~> 1.1.0)
21
+ method_source (~> 0.8.1)
22
+ slop (~> 3.4)
23
+ rake (10.4.2)
24
+ slop (3.6.0)
25
+ vcr (2.9.3)
26
+ yard (0.8.7.6)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ ffaker (~> 2.1)
33
+ minitest (~> 5.8.1)
34
+ pry (~> 0.10.2)
35
+ rake (~> 10.0)
36
+ vcr (~> 2.9.3)
37
+ yard
38
+ zobbix!
39
+
40
+ BUNDLED WITH
41
+ 1.10.6
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # Ruby Zabbix API Wrapper
2
+
3
+ ## Zabbix server requirements
4
+ Zabbix Server version should be 2.2 or 2.4
5
+
6
+ ## Installation
7
+ ```
8
+ gem install zobbix
9
+ ```
10
+
11
+ ## Usage
12
+ ```ruby
13
+ require 'zobbix'
14
+
15
+ zbx = Zobbix.connect(uri: 'http://localhost/zabbix',
16
+ user: 'Admin',
17
+ password: 'zabbix')
18
+
19
+ method = 'trigger.create'
20
+ params = {
21
+ description: 'Name of the trigger',
22
+ expression: 'Linux Template:system.cpu.util[all,idle,avg1].avg(120)}<5'
23
+ }
24
+
25
+ zbx.request(methods, params)
26
+ ```
27
+
28
+ ## Zabbix Server API
29
+
30
+ [API reference (2.2)](https://www.zabbix.com/documentation/2.2/manual/api/reference)
31
+
32
+ [API reference (2.4)](https://www.zabbix.com/documentation/2.4/manual/api/reference)
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ t.test_files = FileList['test/**/*_test.rb']
6
+ t.verbose = false
7
+ t.ruby_opts = ['-rtest_helper']
8
+ end
9
+
10
+ task default: :test
@@ -0,0 +1,47 @@
1
+ require 'httparty'
2
+ require 'json'
3
+ require 'zobbix/api_response'
4
+
5
+ class Zobbix
6
+ class ApiRequest
7
+ include HTTParty
8
+
9
+ headers 'Content-Type' => 'application/json-rpc'
10
+
11
+ def self.default_params
12
+ { 'jsonrpc' => '2.0' }
13
+ end
14
+
15
+ def self.path
16
+ '/api_jsonrpc.php'
17
+ end
18
+
19
+ def self.perform(*args)
20
+ new(*args).perform
21
+ end
22
+
23
+ def initialize(uri, method, params)
24
+ @uri = uri.sub(/\/$/, '')
25
+ @method = method
26
+ @auth = params.delete(:auth)
27
+ @params = params
28
+ end
29
+
30
+ def perform
31
+ ApiResponse.new { self.class.post("#{@uri}#{self.class.path}", body: payload) }
32
+ end
33
+
34
+ private
35
+
36
+ def id
37
+ rand(9999) # Id should be totally random
38
+ end
39
+
40
+ def payload
41
+ JSON.generate(self.class.default_params.merge(method: @method,
42
+ params: @params,
43
+ auth: @auth,
44
+ id: id))
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,52 @@
1
+ class Zobbix
2
+ class ApiResponse
3
+ attr_reader :response, :exception
4
+
5
+ def initialize(&request)
6
+ @response = {}
7
+ @exception = nil
8
+
9
+ @response = request.call
10
+ rescue => ex
11
+ @exception = ex
12
+ end
13
+
14
+ def success?
15
+ @exception.nil? && @response['error'].nil?
16
+ end
17
+
18
+ def error?
19
+ !success?
20
+ end
21
+
22
+ def result
23
+ @response['result']
24
+ end
25
+
26
+ def error_code
27
+ @response['error'] && @response['error']['code']
28
+ end
29
+
30
+ def error_message
31
+ @response['error'] && @response['error']['message']
32
+ end
33
+
34
+ def error_data
35
+ @response['error'] && @response['error']['data']
36
+ end
37
+
38
+ def id
39
+ @response['id']
40
+ end
41
+
42
+ def raise_exception
43
+ return if success?
44
+
45
+ if @exception
46
+ raise @exception
47
+ else
48
+ raise Error.new("API returned error. Code: #{error_code} Message: #{error_message} Data: #{error_data}")
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,11 @@
1
+ require 'zobbix/api_request'
2
+
3
+ class Zobbix
4
+ module Apiinfo
5
+ class VersionRequest < ApiRequest
6
+ def initialize(uri)
7
+ super(uri, 'apiinfo.version', {})
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ unless Hash.method_defined? :symbolize_keys
2
+ class Hash
3
+ # Converts the keys of the hash to symbols.
4
+ #
5
+ # @return [Hash] a copy of the original hash with its keys converted to symbols. Leave nonconvertible keys untouched when symbolizing keys.
6
+ #
7
+ # @example
8
+ # { 'one' => 1, 'two' => 2 }.symbolize_keys #=> { :one => 1, :two => 2 }
9
+ # { 1 => "a" }.symbolize_keys #=> { 1 => "a" }
10
+ def symbolize_keys
11
+ Hash[map { |(k, v)| [(k.to_sym rescue k), v] }]
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,25 @@
1
+ require 'zobbix/core_ext/hash'
2
+
3
+ class Zobbix
4
+ class Credentials
5
+ def initialize(hash)
6
+ @hash = hash.symbolize_keys
7
+ end
8
+
9
+ def uri
10
+ @hash.fetch(:uri)
11
+ end
12
+
13
+ def user
14
+ @hash.fetch(:user)
15
+ end
16
+
17
+ def password
18
+ @hash.fetch(:password)
19
+ end
20
+
21
+ def to_hash
22
+ @hash.dup
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,14 @@
1
+ require 'zobbix/api_request'
2
+
3
+ class Zobbix
4
+ module User
5
+ class LoginRequest < ApiRequest
6
+ def initialize(uri, user, password)
7
+ super(uri,
8
+ 'user.login',
9
+ user: user,
10
+ password: password)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ class Zobbix
2
+ VERSION = '0.0.1'
3
+ end
data/lib/zobbix.rb ADDED
@@ -0,0 +1,136 @@
1
+ require 'zobbix/credentials'
2
+ require 'zobbix/apiinfo/version_request'
3
+ require 'zobbix/user/login_request'
4
+
5
+ require 'rubygems/version'
6
+ require 'rubygems/requirement'
7
+
8
+ class Zobbix
9
+ ZABBIX_VERSION_REQUIREMENT = Gem::Requirement.new('>= 2.2.0', '< 2.5')
10
+
11
+ class Error < StandardError; end
12
+
13
+ class ConnectionError < Error
14
+ def initialize(credentials)
15
+ super "Can't connect to Zabbix Server #{credentials.to_hash.inspect}"
16
+ end
17
+ end
18
+
19
+ class UnsupportedVersionError < Error
20
+ def initialize(version)
21
+ super "Version #{version.inspect} is not supported"
22
+ end
23
+ end
24
+
25
+ class AuthenticationError < Error
26
+ def initialize(credentials)
27
+ super "Can't authenticate on Zabbix Server with #{credentials.to_hash.inspect}"
28
+ end
29
+ end
30
+
31
+ class UnknownMethodError < Error
32
+ def initialize(method)
33
+ super "Unknown method #{method.inspect}"
34
+ end
35
+ end
36
+
37
+ def self.connect(credentials)
38
+ new(credentials)
39
+ .tap(&:check_version!)
40
+ .tap(&:authenticate!)
41
+ end
42
+
43
+ def self.supported_version?(zabbix_version)
44
+ zabbix_version.to_s =~ /^2\.4\./
45
+ end
46
+
47
+ attr_reader :credentials, :auth
48
+
49
+ #
50
+ # @param Zobbix::Credentials
51
+ def initialize(credentials)
52
+ @raise_exceptions = credentials.delete(:raise_exceptions) || false
53
+ @credentials = Credentials.new(credentials)
54
+ @auth = nil
55
+ end
56
+
57
+ # Checks API version
58
+ #
59
+ # @note This method should be called automatically
60
+ # @raise Zobbix::ConnectionError Can't establish connection
61
+ # @raise Zobbix::UnsupportedVersionError Bad API version
62
+ def check_version!
63
+ version = Apiinfo::VersionRequest.perform(credentials.uri).result
64
+
65
+ if version.nil?
66
+ raise ConnectionError.new(credentials)
67
+ end
68
+
69
+ version = Gem::Version.new(version)
70
+ unless ZABBIX_VERSION_REQUIREMENT.satisfied_by?(version)
71
+ raise UnsupportedVersionError.new(version)
72
+ end
73
+ end
74
+
75
+ # Performs authentication
76
+ #
77
+ # @return [String] Auth token
78
+ # @see https://www.zabbix.com/documentation/2.4/manual/api#authentication
79
+ # @note This method should be called automatically
80
+ #
81
+ # @raise Zobbix::AuthenticationError
82
+ def authenticate!
83
+ response = User::LoginRequest.perform(credentials.uri,
84
+ credentials.user,
85
+ credentials.password)
86
+
87
+ unless response.success?
88
+ raise AuthenticationError.new(credentials)
89
+ end
90
+
91
+ @auth = response.result
92
+ end
93
+
94
+ # Makes API request
95
+ #
96
+ # @param [String] request method
97
+ # @param [Hash] request params
98
+ # @return [Zobbix::ApiResponse] response object
99
+ #
100
+ # @see https://www.zabbix.com/documentation/2.4/manual/api/reference
101
+ def request(method, params = {})
102
+ request = resolve_class(method)
103
+
104
+ response =
105
+ if request
106
+ if requires_auth?(method)
107
+ request.perform(credentials.uri, @auth, params)
108
+ else
109
+ request.perform(credentials.uri, params)
110
+ end
111
+ else
112
+ raw_request(method, params)
113
+ end
114
+
115
+ response.raise_exception if @raise_exceptions && response.error?
116
+
117
+ response
118
+ end
119
+
120
+ private
121
+
122
+ def resolve_class(method)
123
+ namespace, mtd = method.split('.').map(&:capitalize)
124
+ self.class.const_get(namespace).const_get("#{mtd}Request")
125
+ rescue NameError
126
+ end
127
+
128
+ def raw_request(method, params)
129
+ ApiRequest.perform(credentials.uri, method, params.merge(auth: @auth))
130
+ end
131
+
132
+ def requires_auth?(method)
133
+ method = method.to_s
134
+ method != 'apiinfo.version' && method != 'user.login'
135
+ end
136
+ end
@@ -0,0 +1,17 @@
1
+ require 'zobbix/api_request'
2
+
3
+ class Zobbix
4
+ class ApiRequestTest < Minitest::Test
5
+ def request(uri, method, params)
6
+ ApiRequest.new(uri, method, params).perform
7
+ end
8
+
9
+ def test_it_makes_successful_request
10
+ VCR.use_cassette('api_request') do
11
+ response = request(ZABBIX_TEST_URI, 'apiinfo.version', {})
12
+ assert_success(response)
13
+ assert_equal(response.result, '2.4.6')
14
+ end
15
+ end
16
+ end
17
+ end
File without changes
@@ -0,0 +1,14 @@
1
+ require 'test_helper'
2
+ require 'zobbix/apiinfo/version_request'
3
+
4
+ class Zobbix
5
+ module Apiinfo
6
+ class VersionRequestTest < Minitest::Test
7
+ def test_it_makes_correct_request
8
+ VCR.use_cassette('apiinfo.version') do
9
+ VersionRequest.perform(ZABBIX_TEST_URI)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,23 @@
1
+ require 'zobbix/credentials'
2
+
3
+ class Zobbix
4
+ class CredentialsTest < Minitest::Test
5
+ def credentials(hash)
6
+ Credentials.new(hash)
7
+ end
8
+
9
+ def it_recognizes(name)
10
+ word = FFaker::Lorem.word
11
+ assert_equal(credentials(name.to_s => word).public_send(name),
12
+ word)
13
+ assert_equal(credentials(name.to_sym => word).public_send(name),
14
+ word)
15
+ end
16
+
17
+ def test_it_recognizes_all_options
18
+ it_recognizes :uri
19
+ it_recognizes :user
20
+ it_recognizes :password
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,40 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://localhost/zabbix/api_jsonrpc.php
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"jsonrpc":"2.0","method":"apiinfo.version","params":{},"auth":null,"id":5157}'
9
+ headers:
10
+ Content-Type:
11
+ - application/json-rpc
12
+ response:
13
+ status:
14
+ code: 200
15
+ message: OK
16
+ headers:
17
+ Date:
18
+ - Fri, 09 Oct 2015 11:05:53 GMT
19
+ Server:
20
+ - Apache/2.4.7 (Ubuntu)
21
+ X-Powered-By:
22
+ - PHP/5.5.9-1ubuntu4.12
23
+ Access-Control-Allow-Origin:
24
+ - '*'
25
+ Access-Control-Allow-Headers:
26
+ - Content-Type
27
+ Access-Control-Allow-Methods:
28
+ - POST
29
+ Access-Control-Max-Age:
30
+ - '1000'
31
+ Content-Length:
32
+ - '44'
33
+ Content-Type:
34
+ - application/json
35
+ body:
36
+ encoding: UTF-8
37
+ string: '{"jsonrpc":"2.0","result":"2.4.6","id":5157}'
38
+ http_version:
39
+ recorded_at: Fri, 09 Oct 2015 11:05:53 GMT
40
+ recorded_with: VCR 2.9.3
@@ -0,0 +1,40 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://localhost/zabbix/api_jsonrpc.php
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"jsonrpc":"2.0","method":"apiinfo.version","params":{},"auth":null,"id":559}'
9
+ headers:
10
+ Content-Type:
11
+ - application/json-rpc
12
+ response:
13
+ status:
14
+ code: 200
15
+ message: OK
16
+ headers:
17
+ Date:
18
+ - Fri, 09 Oct 2015 11:20:48 GMT
19
+ Server:
20
+ - Apache/2.4.7 (Ubuntu)
21
+ X-Powered-By:
22
+ - PHP/5.5.9-1ubuntu4.12
23
+ Access-Control-Allow-Origin:
24
+ - '*'
25
+ Access-Control-Allow-Headers:
26
+ - Content-Type
27
+ Access-Control-Allow-Methods:
28
+ - POST
29
+ Access-Control-Max-Age:
30
+ - '1000'
31
+ Content-Length:
32
+ - '43'
33
+ Content-Type:
34
+ - application/json
35
+ body:
36
+ encoding: UTF-8
37
+ string: '{"jsonrpc":"2.0","result":"2.4.6","id":559}'
38
+ http_version:
39
+ recorded_at: Fri, 09 Oct 2015 11:20:48 GMT
40
+ recorded_with: VCR 2.9.3
@@ -0,0 +1,40 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://localhost/zabbix/api_jsonrpc.php
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"jsonrpc":"2.0","method":"user.login","params":{"user":"Admin","password":"zabbix"},"auth":null,"id":8485}'
9
+ headers:
10
+ Content-Type:
11
+ - application/json-rpc
12
+ response:
13
+ status:
14
+ code: 200
15
+ message: OK
16
+ headers:
17
+ Date:
18
+ - Fri, 09 Oct 2015 13:09:05 GMT
19
+ Server:
20
+ - Apache/2.4.7 (Ubuntu)
21
+ X-Powered-By:
22
+ - PHP/5.5.9-1ubuntu4.12
23
+ Access-Control-Allow-Origin:
24
+ - '*'
25
+ Access-Control-Allow-Headers:
26
+ - Content-Type
27
+ Access-Control-Allow-Methods:
28
+ - POST
29
+ Access-Control-Max-Age:
30
+ - '1000'
31
+ Content-Length:
32
+ - '71'
33
+ Content-Type:
34
+ - application/json
35
+ body:
36
+ encoding: UTF-8
37
+ string: '{"jsonrpc":"2.0","result":"ef466e128438daf7fb3ab4602b37a58c","id":8485}'
38
+ http_version:
39
+ recorded_at: Fri, 09 Oct 2015 13:09:05 GMT
40
+ recorded_with: VCR 2.9.3
@@ -0,0 +1,41 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://localhost/zabbix/api_jsonrpc.php
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"jsonrpc":"2.0","method":"user.login","params":{"user":"Admin","password":"shit"},"auth":null,"id":1405}'
9
+ headers:
10
+ Content-Type:
11
+ - application/json-rpc
12
+ response:
13
+ status:
14
+ code: 200
15
+ message: OK
16
+ headers:
17
+ Date:
18
+ - Fri, 09 Oct 2015 13:10:50 GMT
19
+ Server:
20
+ - Apache/2.4.7 (Ubuntu)
21
+ X-Powered-By:
22
+ - PHP/5.5.9-1ubuntu4.12
23
+ Access-Control-Allow-Origin:
24
+ - '*'
25
+ Access-Control-Allow-Headers:
26
+ - Content-Type
27
+ Access-Control-Allow-Methods:
28
+ - POST
29
+ Access-Control-Max-Age:
30
+ - '1000'
31
+ Content-Length:
32
+ - '125'
33
+ Content-Type:
34
+ - application/json
35
+ body:
36
+ encoding: UTF-8
37
+ string: '{"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid params.","data":"Login
38
+ name or password is incorrect."},"id":1405}'
39
+ http_version:
40
+ recorded_at: Fri, 09 Oct 2015 13:10:50 GMT
41
+ recorded_with: VCR 2.9.3
@@ -0,0 +1,40 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://localhost/zabbix/api_jsonrpc.php
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"jsonrpc":"2.0","method":"apiinfo.version","params":{},"auth":null,"id":5157}'
9
+ headers:
10
+ Content-Type:
11
+ - application/json-rpc
12
+ response:
13
+ status:
14
+ code: 200
15
+ message: OK
16
+ headers:
17
+ Date:
18
+ - Fri, 09 Oct 2015 11:05:53 GMT
19
+ Server:
20
+ - Apache/2.4.7 (Ubuntu)
21
+ X-Powered-By:
22
+ - PHP/5.5.9-1ubuntu4.12
23
+ Access-Control-Allow-Origin:
24
+ - '*'
25
+ Access-Control-Allow-Headers:
26
+ - Content-Type
27
+ Access-Control-Allow-Methods:
28
+ - POST
29
+ Access-Control-Max-Age:
30
+ - '1000'
31
+ Content-Length:
32
+ - '44'
33
+ Content-Type:
34
+ - application/json
35
+ body:
36
+ encoding: UTF-8
37
+ string: '{"jsonrpc":"2.0","result":"2.5.6","id":5157}'
38
+ http_version:
39
+ recorded_at: Fri, 09 Oct 2015 11:05:53 GMT
40
+ recorded_with: VCR 2.9.3
@@ -0,0 +1,40 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://localhost/zabbix/api_jsonrpc.php
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"jsonrpc":"2.0","method":"apiinfo.version","params":{},"auth":null,"id":5157}'
9
+ headers:
10
+ Content-Type:
11
+ - application/json-rpc
12
+ response:
13
+ status:
14
+ code: 200
15
+ message: OK
16
+ headers:
17
+ Date:
18
+ - Fri, 09 Oct 2015 11:05:53 GMT
19
+ Server:
20
+ - Apache/2.4.7 (Ubuntu)
21
+ X-Powered-By:
22
+ - PHP/5.5.9-1ubuntu4.12
23
+ Access-Control-Allow-Origin:
24
+ - '*'
25
+ Access-Control-Allow-Headers:
26
+ - Content-Type
27
+ Access-Control-Allow-Methods:
28
+ - POST
29
+ Access-Control-Max-Age:
30
+ - '1000'
31
+ Content-Length:
32
+ - '44'
33
+ Content-Type:
34
+ - application/json
35
+ body:
36
+ encoding: UTF-8
37
+ string: '{"jsonrpc":"2.0","result":"2.1.6","id":5157}'
38
+ http_version:
39
+ recorded_at: Fri, 09 Oct 2015 11:05:53 GMT
40
+ recorded_with: VCR 2.9.3
@@ -0,0 +1,40 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://localhost/zabbix/api_jsonrpc.php
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"jsonrpc":"2.0","method":"apiinfo.version","params":{},"auth":null,"id":5157}'
9
+ headers:
10
+ Content-Type:
11
+ - application/json-rpc
12
+ response:
13
+ status:
14
+ code: 200
15
+ message: OK
16
+ headers:
17
+ Date:
18
+ - Fri, 09 Oct 2015 11:05:53 GMT
19
+ Server:
20
+ - Apache/2.4.7 (Ubuntu)
21
+ X-Powered-By:
22
+ - PHP/5.5.9-1ubuntu4.12
23
+ Access-Control-Allow-Origin:
24
+ - '*'
25
+ Access-Control-Allow-Headers:
26
+ - Content-Type
27
+ Access-Control-Allow-Methods:
28
+ - POST
29
+ Access-Control-Max-Age:
30
+ - '1000'
31
+ Content-Length:
32
+ - '44'
33
+ Content-Type:
34
+ - application/json
35
+ body:
36
+ encoding: UTF-8
37
+ string: '{"jsonrpc":"2.0","result":"2.4.6","id":5157}'
38
+ http_version:
39
+ recorded_at: Fri, 09 Oct 2015 11:05:53 GMT
40
+ recorded_with: VCR 2.9.3
@@ -0,0 +1,40 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://localhost/zabbix/api_jsonrpc.php
6
+ body:
7
+ encoding: UTF-8
8
+ string: '{"jsonrpc":"2.0","method":"user.login","params":{"user":"Admin","password":"zabbix"},"auth":null,"id":9935}'
9
+ headers:
10
+ Content-Type:
11
+ - application/json-rpc
12
+ response:
13
+ status:
14
+ code: 200
15
+ message: OK
16
+ headers:
17
+ Date:
18
+ - Fri, 09 Oct 2015 11:27:16 GMT
19
+ Server:
20
+ - Apache/2.4.7 (Ubuntu)
21
+ X-Powered-By:
22
+ - PHP/5.5.9-1ubuntu4.12
23
+ Access-Control-Allow-Origin:
24
+ - '*'
25
+ Access-Control-Allow-Headers:
26
+ - Content-Type
27
+ Access-Control-Allow-Methods:
28
+ - POST
29
+ Access-Control-Max-Age:
30
+ - '1000'
31
+ Content-Length:
32
+ - '71'
33
+ Content-Type:
34
+ - application/json
35
+ body:
36
+ encoding: UTF-8
37
+ string: '{"jsonrpc":"2.0","result":"d4caf3d6e0b0ba799091efe9a21b2cc1","id":9935}'
38
+ http_version:
39
+ recorded_at: Fri, 09 Oct 2015 11:27:16 GMT
40
+ recorded_with: VCR 2.9.3
@@ -0,0 +1,19 @@
1
+ require 'minitest/autorun'
2
+ require 'ffaker'
3
+ require 'vcr'
4
+
5
+ ZABBIX_TEST_URI = ENV.fetch('ZABBIX_TEST_URI', 'http://localhost/zabbix')
6
+ ZABBIX_TEST_USER = ENV.fetch('ZABBIX_TEST_USER', 'Admin')
7
+ ZABBIX_TEST_PASSWORD = ENV.fetch('ZABBIX_TEST_PASSWORD', 'zabbix')
8
+
9
+ module Minitest::Assertions
10
+ def assert_success(object)
11
+ assert object.success?,
12
+ "Expected #{object.inspect}.success? to be true, but have false"
13
+ end
14
+ end
15
+
16
+ VCR.configure do |config|
17
+ config.cassette_library_dir = 'test/fixtures/vcr_casettes'
18
+ config.hook_into :webmock
19
+ end
@@ -0,0 +1,14 @@
1
+ require 'test_helper'
2
+ require 'zobbix/user/login_request'
3
+
4
+ class Zobbix
5
+ module User
6
+ class LoginRequestTest < Minitest::Test
7
+ def test_it_logins_successfully
8
+ VCR.use_cassette('user.login') do
9
+ refute_nil LoginRequest.perform(ZABBIX_TEST_URI, 'Admin', 'zabbix').result
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,44 @@
1
+ require 'zobbix'
2
+
3
+ class ZobbixTest < Minitest::Test
4
+ def zbx
5
+ Zobbix.new(uri: ZABBIX_TEST_URI,
6
+ user: ZABBIX_TEST_USER,
7
+ password: ZABBIX_TEST_PASSWORD)
8
+ end
9
+
10
+ def test_low_version
11
+ VCR.use_cassette('check_version/low_version') do
12
+ assert_raises(Zobbix::UnsupportedVersionError) { zbx.check_version! }
13
+ end
14
+ end
15
+
16
+ def test_high_version
17
+ VCR.use_cassette('check_version/high_version') do
18
+ assert_raises(Zobbix::UnsupportedVersionError) { zbx.check_version! }
19
+ end
20
+ end
21
+
22
+ def test_ok_version
23
+ VCR.use_cassette('check_version/ok_version') do
24
+ zbx.check_version!
25
+ pass
26
+ end
27
+ end
28
+
29
+ def test_ok_authenticate
30
+ VCR.use_cassette('authentication') do
31
+ zbx.authenticate!
32
+ pass
33
+ end
34
+ end
35
+
36
+ def test_bad_authenticate
37
+ VCR.use_cassette('bad_authentication') do
38
+ client = Zobbix.new(uri: ZABBIX_TEST_URI, user: ZABBIX_TEST_USER, password: 'shit')
39
+ assert_raises(Zobbix::AuthenticationError) do
40
+ client.authenticate!
41
+ end
42
+ end
43
+ end
44
+ end
data/zobbix.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ $:.unshift(File.expand_path('../lib', __FILE__))
2
+ require 'zobbix/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "zobbix"
6
+ spec.version = Zobbix::VERSION
7
+ spec.authors = ["Michael Lutsiuk"]
8
+ spec.email = ["michael.lutsiuk@gmail.com"]
9
+ spec.summary = "Zabbix API Wrapper"
10
+ spec.description = "Minimal wrapper for Zabbix Server API"
11
+ spec.homepage = "https://github.com/mediaslave24/zobbix"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency 'rake', '~> 10.0'
20
+ spec.add_development_dependency 'minitest', '~> 5.8'
21
+ spec.add_development_dependency 'ffaker', '~> 2.1'
22
+ spec.add_development_dependency 'vcr', '~> 2.9'
23
+ spec.add_development_dependency 'pry', '~> 0.10'
24
+
25
+ spec.add_runtime_dependency 'httparty', '~> 0.13'
26
+ end
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zobbix
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Lutsiuk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '10.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '10.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.8'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ffaker
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: vcr
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.9'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.10'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.10'
83
+ - !ruby/object:Gem::Dependency
84
+ name: httparty
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.13'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.13'
97
+ description: Minimal wrapper for Zabbix Server API
98
+ email:
99
+ - michael.lutsiuk@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - Gemfile.lock
107
+ - README.md
108
+ - Rakefile
109
+ - lib/zobbix.rb
110
+ - lib/zobbix/api_request.rb
111
+ - lib/zobbix/api_response.rb
112
+ - lib/zobbix/apiinfo/version_request.rb
113
+ - lib/zobbix/core_ext/hash.rb
114
+ - lib/zobbix/credentials.rb
115
+ - lib/zobbix/user/login_request.rb
116
+ - lib/zobbix/version.rb
117
+ - test/api_request_test.rb
118
+ - test/api_response_test.rb
119
+ - test/apiinfo/version_request_test.rb
120
+ - test/credentials_test.rb
121
+ - test/fixtures/vcr_casettes/api_request.yml
122
+ - test/fixtures/vcr_casettes/apiinfo_version.yml
123
+ - test/fixtures/vcr_casettes/authentication.yml
124
+ - test/fixtures/vcr_casettes/bad_authentication.yml
125
+ - test/fixtures/vcr_casettes/check_version/high_version.yml
126
+ - test/fixtures/vcr_casettes/check_version/low_version.yml
127
+ - test/fixtures/vcr_casettes/check_version/ok_version.yml
128
+ - test/fixtures/vcr_casettes/user_login.yml
129
+ - test/test_helper.rb
130
+ - test/user/login_request_test.rb
131
+ - test/zobbix_test.rb
132
+ - zobbix.gemspec
133
+ homepage: https://github.com/mediaslave24/zobbix
134
+ licenses:
135
+ - MIT
136
+ metadata: {}
137
+ post_install_message:
138
+ rdoc_options: []
139
+ require_paths:
140
+ - lib
141
+ required_ruby_version: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ requirements: []
152
+ rubyforge_project:
153
+ rubygems_version: 2.2.2
154
+ signing_key:
155
+ specification_version: 4
156
+ summary: Zabbix API Wrapper
157
+ test_files:
158
+ - test/api_request_test.rb
159
+ - test/api_response_test.rb
160
+ - test/apiinfo/version_request_test.rb
161
+ - test/credentials_test.rb
162
+ - test/fixtures/vcr_casettes/api_request.yml
163
+ - test/fixtures/vcr_casettes/apiinfo_version.yml
164
+ - test/fixtures/vcr_casettes/authentication.yml
165
+ - test/fixtures/vcr_casettes/bad_authentication.yml
166
+ - test/fixtures/vcr_casettes/check_version/high_version.yml
167
+ - test/fixtures/vcr_casettes/check_version/low_version.yml
168
+ - test/fixtures/vcr_casettes/check_version/ok_version.yml
169
+ - test/fixtures/vcr_casettes/user_login.yml
170
+ - test/test_helper.rb
171
+ - test/user/login_request_test.rb
172
+ - test/zobbix_test.rb
173
+ has_rdoc: