zabbix-client 0.0.9 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +2 -0
- data/.travis.yml +6 -0
- data/README.md +1 -0
- data/Rakefile +3 -0
- data/lib/zabbix/client/client.rb +4 -2
- data/lib/zabbix/client/version.rb +1 -1
- data/spec/spec_helper.rb +87 -0
- data/spec/zabbix/client_spec.rb +116 -0
- data/zabbix-client.gemspec +2 -1
- metadata +27 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5ad7955298428f796e35412241a4d1338ae9fa16
|
4
|
+
data.tar.gz: 180b9d92f9d42baa3f864a5a8113838bc90ec9d3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0810ae905c0b2864fde7081749ad5d54d29f6f6ff9f1ec46c9f863a94fd3b5ae6606f29c19c77620dbc6aec4a4fb7dfb31439c820f99c58899495fb7125cc4af
|
7
|
+
data.tar.gz: ee5eb325cc555b14566d61ae8d170be17751121bba79a4a5816da939e0bbaef31813e482c8be7576d822775e16a7f3606d62a13b1059186ffa60bd22d19b87a8
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
This is a simple client of Zabbix API.
|
4
4
|
|
5
5
|
[![Gem Version](https://badge.fury.io/rb/zabbix-client.svg)](http://badge.fury.io/rb/zabbix-client)
|
6
|
+
[![Build Status](https://travis-ci.org/winebarrel/zabbix-client.svg?branch=master)](https://travis-ci.org/winebarrel/zabbix-client)
|
6
7
|
|
7
8
|
## Installation
|
8
9
|
|
data/Rakefile
CHANGED
data/lib/zabbix/client/client.rb
CHANGED
@@ -81,7 +81,9 @@ class Zabbix::Client
|
|
81
81
|
|
82
82
|
http.start do |h|
|
83
83
|
headers = DEFAULT_HEADERS.merge(options[:headers] || {})
|
84
|
-
|
84
|
+
path = @client.url.path
|
85
|
+
path = '/' if path.empty?
|
86
|
+
request = Net::HTTP::Post.new(path, headers)
|
85
87
|
request.body = body
|
86
88
|
|
87
89
|
basic_auth_user = options[:basic_auth_user]
|
@@ -98,7 +100,7 @@ class Zabbix::Client
|
|
98
100
|
rescue JSON::ParserError
|
99
101
|
response.value
|
100
102
|
# Throw the following exception if no exception is thrown
|
101
|
-
raise response.code
|
103
|
+
raise [response.code, response.message.dump, response.body].join(' ')
|
102
104
|
end
|
103
105
|
end
|
104
106
|
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
$: << File.expand_path('..', __FILE__)
|
2
|
+
require 'zabbix/client'
|
3
|
+
require 'json'
|
4
|
+
require 'webrick'
|
5
|
+
require 'webrick/httpproxy'
|
6
|
+
require 'webrick/https'
|
7
|
+
|
8
|
+
trap(:INT) do
|
9
|
+
$zabbix_server.shutdown if $zabbix_server
|
10
|
+
end
|
11
|
+
|
12
|
+
RSpec.configure do |config|
|
13
|
+
config.after(:each) do
|
14
|
+
if $zabbix_server
|
15
|
+
shutdown_server($zabbix_server)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def start_server(server)
|
21
|
+
Thread.start do
|
22
|
+
server.start
|
23
|
+
end
|
24
|
+
|
25
|
+
sleep 0.1 until server.status == :Running
|
26
|
+
end
|
27
|
+
|
28
|
+
def shutdown_server(server)
|
29
|
+
server.shutdown
|
30
|
+
sleep 0.1 until server.status == :Stop
|
31
|
+
end
|
32
|
+
|
33
|
+
def default_handler
|
34
|
+
proc do |req, res|
|
35
|
+
res.body = JSON.dump(:result => {
|
36
|
+
:body => req.body,
|
37
|
+
:header => req.header,
|
38
|
+
})
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def run_client(options = {})
|
43
|
+
options = {:handler => default_handler}.merge(options)
|
44
|
+
|
45
|
+
server_opts = {
|
46
|
+
:Port => 20080,
|
47
|
+
:Logger => WEBrick::Log.new('/dev/null'),
|
48
|
+
:AccessLog => []
|
49
|
+
}
|
50
|
+
|
51
|
+
if user_pass = options.delete(:basic_auth)
|
52
|
+
server_opts[:RequestCallback] = proc do |req, res|
|
53
|
+
WEBrick::HTTPAuth.basic_auth(req, res, "my realm") do |username, password|
|
54
|
+
[username, password] == user_pass
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
if ssl = options.delete(:ssl)
|
60
|
+
server_opts[:SSLEnable] = true
|
61
|
+
server_opts[:SSLCertName] = [['CN', WEBrick::Utils::getservername]]
|
62
|
+
end
|
63
|
+
|
64
|
+
$zabbix_server = WEBrick::HTTPServer.new(server_opts)
|
65
|
+
|
66
|
+
$zabbix_server.mount_proc('/', &options.delete(:handler))
|
67
|
+
start_server($zabbix_server)
|
68
|
+
|
69
|
+
url = (ssl ? 'https' : 'http') + '://localhost:20080'
|
70
|
+
client = Zabbix::Client.new(url, options)
|
71
|
+
yield(client)
|
72
|
+
end
|
73
|
+
|
74
|
+
def proxy
|
75
|
+
begin
|
76
|
+
server = WEBrick::HTTPProxyServer.new(
|
77
|
+
:Port => 30080,
|
78
|
+
:Logger => WEBrick::Log.new('/dev/null'),
|
79
|
+
:AccessLog => [])
|
80
|
+
|
81
|
+
start_server(server)
|
82
|
+
|
83
|
+
yield('localhost', 30080)
|
84
|
+
ensure
|
85
|
+
shutdown_server(server) if server
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
describe Zabbix::Client do
|
2
|
+
context "when call procedure" do
|
3
|
+
it do
|
4
|
+
run_client do |client|
|
5
|
+
result = client.apiinfo.version
|
6
|
+
expect(result["body"]).to eq "{\"jsonrpc\":\"2.0\",\"method\":\"apiinfo.version\",\"params\":[],\"id\":1}"
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
it do
|
11
|
+
run_client do |client|
|
12
|
+
result = client.item.get(:itemids => [1, 2, 3])
|
13
|
+
expect(result["body"]).to eq "{\"jsonrpc\":\"2.0\",\"method\":\"item.get\",\"params\":{\"itemids\":[1,2,3]},\"id\":1}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
it do
|
18
|
+
run_client do |client|
|
19
|
+
result = client.item.delete([1, 2, 3])
|
20
|
+
expect(result["body"]).to eq "{\"jsonrpc\":\"2.0\",\"method\":\"item.delete\",\"params\":[1,2,3],\"id\":1}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "login/logout" do
|
26
|
+
it do
|
27
|
+
run_client do |client|
|
28
|
+
client.user.login(:user => "scott", :password => "tiger")
|
29
|
+
|
30
|
+
result = client.apiinfo.version
|
31
|
+
expect(result["body"]).to eq "{\"jsonrpc\":\"2.0\",\"method\":\"apiinfo.version\",\"params\":[],\"id\":1}"
|
32
|
+
|
33
|
+
result = client.item.get(:itemids => [1, 2, 3])
|
34
|
+
expect(result["body"]).to eq "{\"jsonrpc\":\"2.0\",\"method\":\"item.get\",\"params\":{\"itemids\":[1,2,3]},\"id\":1,\"auth\":{\"body\":\"{\\\"jsonrpc\\\":\\\"2.0\\\",\\\"method\\\":\\\"user.login\\\",\\\"params\\\":{\\\"user\\\":\\\"scott\\\",\\\"password\\\":\\\"tiger\\\"},\\\"id\\\":1}\",\"header\":{\"content-type\":[\"application/json-rpc\"],\"accept-encoding\":[\"gzip;q=1.0,deflate;q=0.6,identity;q=0.3\"],\"accept\":[\"*/*\"],\"user-agent\":[\"Ruby\"],\"host\":[\"localhost:20080\"],\"content-length\":[\"91\"]}}}"
|
35
|
+
|
36
|
+
client.user.logout
|
37
|
+
|
38
|
+
result = client.item.get(:itemids => [1, 2, 3])
|
39
|
+
expect(result["body"]).to eq "{\"jsonrpc\":\"2.0\",\"method\":\"item.get\",\"params\":{\"itemids\":[1,2,3]},\"id\":1}"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "when error happen" do
|
45
|
+
it do
|
46
|
+
handler = proc do |req, res|
|
47
|
+
res.status = 404
|
48
|
+
res.body = 'Not Found'
|
49
|
+
end
|
50
|
+
|
51
|
+
run_client(:handler => handler) do |client|
|
52
|
+
expect {
|
53
|
+
client.item.get(:itemids => [1, 2, 3])
|
54
|
+
}.to raise_error(Net::HTTPServerException, '404 "Not Found "')
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it do
|
59
|
+
handler = proc do |req, res|
|
60
|
+
res.status = 200
|
61
|
+
res.body = 'non-JSON data'
|
62
|
+
end
|
63
|
+
|
64
|
+
run_client(:handler => handler) do |client|
|
65
|
+
expect {
|
66
|
+
client.item.get(:itemids => [1, 2, 3])
|
67
|
+
}.to raise_error(RuntimeError, '200 "OK " non-JSON data')
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context "when add header" do
|
73
|
+
it do
|
74
|
+
run_client do |client|
|
75
|
+
result = client.item.get({:itemids => [1, 2, 3]}, :headers => {"User-Agent" => "Any User Agent"})
|
76
|
+
expect(result["header"]["user-agent"]).to eq ["Any User Agent"]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
it do
|
81
|
+
run_client(:headers => {"User-Agent" => "Any User Agent"}) do |client|
|
82
|
+
result = client.apiinfo.version
|
83
|
+
expect(result["header"]["user-agent"]).to eq ["Any User Agent"]
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context "when use proxy" do
|
89
|
+
it do
|
90
|
+
proxy do |proxy_host, proxy_port|
|
91
|
+
run_client(:proxy_host => proxy_host, :proxy_port => proxy_port) do |client|
|
92
|
+
result = client.item.get(:itemids => [1, 2, 3])
|
93
|
+
expect(result["body"]).to eq "{\"jsonrpc\":\"2.0\",\"method\":\"item.get\",\"params\":{\"itemids\":[1,2,3]},\"id\":1}"
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
context "when basic auth" do
|
100
|
+
it do
|
101
|
+
run_client(:basic_auth => ["scott", "tiger"], :basic_auth_user => "scott", :basic_auth_password => "tiger") do |client|
|
102
|
+
result = client.item.get(:itemids => [1, 2, 3])
|
103
|
+
expect(result["body"]).to eq "{\"jsonrpc\":\"2.0\",\"method\":\"item.get\",\"params\":{\"itemids\":[1,2,3]},\"id\":1}"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context "when https" do
|
109
|
+
it do
|
110
|
+
run_client(:ssl => true) do |client|
|
111
|
+
result = client.item.get(:itemids => [1, 2, 3])
|
112
|
+
expect(result["body"]).to eq "{\"jsonrpc\":\"2.0\",\"method\":\"item.get\",\"params\":{\"itemids\":[1,2,3]},\"id\":1}"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
data/zabbix-client.gemspec
CHANGED
@@ -18,6 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ['lib']
|
20
20
|
|
21
|
-
spec.add_development_dependency 'bundler'
|
21
|
+
spec.add_development_dependency 'bundler'
|
22
22
|
spec.add_development_dependency 'rake', '~> 10.0'
|
23
|
+
spec.add_development_dependency 'rspec', '>= 3.0.0'
|
23
24
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zabbix-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Genki Sugawara
|
@@ -14,16 +14,16 @@ dependencies:
|
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.0.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.0.0
|
41
55
|
description: This is a simple client of Zabbix API.
|
42
56
|
email:
|
43
57
|
- sgwr_dts@yahoo.co.jp
|
@@ -46,6 +60,8 @@ extensions: []
|
|
46
60
|
extra_rdoc_files: []
|
47
61
|
files:
|
48
62
|
- .gitignore
|
63
|
+
- .rspec
|
64
|
+
- .travis.yml
|
49
65
|
- Gemfile
|
50
66
|
- LICENSE.txt
|
51
67
|
- README.md
|
@@ -54,6 +70,8 @@ files:
|
|
54
70
|
- lib/zabbix/client/client.rb
|
55
71
|
- lib/zabbix/client/error.rb
|
56
72
|
- lib/zabbix/client/version.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
- spec/zabbix/client_spec.rb
|
57
75
|
- zabbix-client.gemspec
|
58
76
|
homepage: https://github.com/winebarrel/zabbix-client
|
59
77
|
licenses:
|
@@ -75,8 +93,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
93
|
version: '0'
|
76
94
|
requirements: []
|
77
95
|
rubyforge_project:
|
78
|
-
rubygems_version: 2.
|
96
|
+
rubygems_version: 2.4.1
|
79
97
|
signing_key:
|
80
98
|
specification_version: 4
|
81
99
|
summary: This is a simple client of Zabbix API.
|
82
|
-
test_files:
|
100
|
+
test_files:
|
101
|
+
- spec/spec_helper.rb
|
102
|
+
- spec/zabbix/client_spec.rb
|