zabbix-client 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cf8aed8ec273e471d347a2c8c97d8720c7cb6933
4
+ data.tar.gz: 8fe616fdbadd37b4e5ecffad87625b385bb6d5de
5
+ SHA512:
6
+ metadata.gz: 7ae12cd805d4f265553b241b27277f7f61eb30b71a5e47980e1512a29d46a0b44c1704cefa782c24e4b8829ada0b6012d09a5f3cbd2f069e369dd4cc87fedc50
7
+ data.tar.gz: fe4681e21634288d551308dd8b3f3c41ca7f64a4c21fde26d9f3e7c7a98135c7c43d5f582cf46794243141c474d3fd88b1885598be200d5e071e6720987ea637
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ test.rb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in zabbix-client.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Genki Sugawara
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Zabbix::Client
2
+
3
+ This is a simple client of Zabbix API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'zabbix-client'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install zabbix-client
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'zabbix/client'
25
+
26
+ client = Zabbix::Client.new('http://localhost/zabbix/api_jsonrpc.php')
27
+ client.user.login(user: 'Admin', password: 'zabbix')
28
+ p client.apiinfo.version #=> "2.0.12"
29
+ p client.template.getobjects(:host => ['Template OS Linux'])
30
+ #=> [{"hostid"=>"10001",
31
+ "proxy_hostid"=>"0",
32
+ "host"=>"Template OS Linux",
33
+ ...
34
+ ```
35
+
36
+ ## Related links
37
+
38
+ * [Zabbix 1.8 API reference](https://www.zabbix.com/documentation/1.8/api)
39
+ * [Zabbix 2.0 API reference](https://www.zabbix.com/documentation/2.0/manual/appendix/api/api)
40
+ * [Zabbix 2.2 API reference](https://www.zabbix.com/documentation/2.2/manual/api)
41
+ * [Zabbix 2.4 API reference](https://www.zabbix.com/documentation/2.4/manual/api)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler/gem_tasks'
2
+
@@ -0,0 +1,9 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'net/https'
4
+ require 'uri'
5
+
6
+ require 'zabbix/client/version'
7
+
8
+ require 'zabbix/client/client'
9
+ require 'zabbix/client/error'
@@ -0,0 +1,96 @@
1
+ class Zabbix::Client
2
+ DEFAULT_HEADERS = {
3
+ 'Content-Type' => 'application/json-rpc'
4
+ }
5
+
6
+ class Method
7
+ JSON_RPC_VERSION = '2.0'
8
+ JSON_RPC_REQUEST_ID = 1
9
+ LOGIN_METHOD = 'user.login'
10
+
11
+ def initialize(prefix, client)
12
+ @prefix = prefix
13
+ @client = client
14
+ end
15
+
16
+ def method_missing(method_name, *args, &block)
17
+ validate_args(args)
18
+
19
+ method = "#{@prefix}.#{method_name}"
20
+ params = args[0] || []
21
+ options = args[1] || {}
22
+ response = query(method, params, options)
23
+
24
+ if (error = response['error'])
25
+ error = error.merge('method' => method, 'params' => params, 'options' => options)
26
+ raise Zabbix::Client::Error.new(error)
27
+ end
28
+
29
+ result = response['result']
30
+
31
+ if method == LOGIN_METHOD
32
+ @client.auth = result
33
+ end
34
+
35
+ if block
36
+ block.call(result)
37
+ else
38
+ result
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def query(method, params, options)
45
+ body = {
46
+ :jsonrpc => JSON_RPC_VERSION,
47
+ :method => method,
48
+ :params => params,
49
+ :id => JSON_RPC_REQUEST_ID,
50
+ }.merge(options)
51
+
52
+ body[:auth] = @client.auth if @client.auth
53
+ body = JSON.dump(body)
54
+ http = Net::HTTP.new(@client.url.host, @client.url.port)
55
+
56
+ if @client.url.scheme == 'https'
57
+ https.use_ssl = true
58
+ https.verify_mode = OpenSSL::SSL::VERIFY_NONE
59
+ end
60
+
61
+ @client.http_hook.call(http) if @client.http_hook
62
+
63
+ http.start do |h|
64
+ response = h.post(@client.url.path, body, @client.headers)
65
+ JSON.parse(response.body)
66
+ end
67
+ end
68
+
69
+ def validate_args(args)
70
+ unless (0..2).include?(args.length)
71
+ raise ArgumentError, "wrong number of arguments: #{args.inspect} (#{args.length} for 1..2)"
72
+ end
73
+
74
+ args.each do |arg|
75
+ unless arg.kind_of?(Hash)
76
+ raise TypeError, "wrong argument: #{arg.inspect} (expected Hash)"
77
+ end
78
+ end
79
+ end
80
+ end # Method
81
+
82
+ attr_reader :url
83
+ attr_reader :headers
84
+ attr_reader :http_hook
85
+ attr_accessor :auth
86
+
87
+ def initialize(url, headers = {}, &http_hook)
88
+ @url = URI.parse(url)
89
+ @headers = DEFAULT_HEADERS.merge(headers)
90
+ @http_hook = http_hook
91
+ end
92
+
93
+ def method_missing(method_name)
94
+ Zabbix::Client::Method.new(method_name, self)
95
+ end
96
+ end
@@ -0,0 +1,10 @@
1
+ class Zabbix::Client::Error < StandardError
2
+ def initialize(error)
3
+ super(error.inspect)
4
+ @error = error
5
+ end
6
+
7
+ def error
8
+ @error
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ module Zabbix
2
+ class Client
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'zabbix/client/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'zabbix-client'
8
+ spec.version = Zabbix::Client::VERSION
9
+ spec.authors = ['Genki Sugawara']
10
+ spec.email = ['sgwr_dts@yahoo.co.jp']
11
+ spec.summary = %q{This is a simple client of Zabbix API.}
12
+ spec.description = %q{This is a simple client of Zabbix API.}
13
+ spec.homepage = 'https://github.com/winebarrel/zabbix-client'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zabbix-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Genki Sugawara
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: This is a simple client of Zabbix API.
42
+ email:
43
+ - sgwr_dts@yahoo.co.jp
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/zabbix/client.rb
54
+ - lib/zabbix/client/client.rb
55
+ - lib/zabbix/client/error.rb
56
+ - lib/zabbix/client/version.rb
57
+ - zabbix-client.gemspec
58
+ homepage: https://github.com/winebarrel/zabbix-client
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.4.1
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: This is a simple client of Zabbix API.
82
+ test_files: []