zabx 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.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Thomas Whitton
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ Zabx
2
+ ====
3
+
4
+ Zabbix api client written in ruby.
5
+
6
+ Example
7
+ =======
8
+
9
+ ```ruby
10
+ require 'zabx'
11
+
12
+ api_address = 'http://localhost/zabbix/api_jsonrpc.php'
13
+
14
+ api = Zabx::Api.new(api_address, :username => 'Admin', :password => 'zabbix')
15
+
16
+ api.apiinfo.version
17
+ api.event.get
18
+ api.user.create({
19
+ :alias => 'alias',
20
+ :name => 'name',
21
+ :surname => 'surname',
22
+ :passwd => 'password',
23
+ :usrgrps => [{
24
+ :usrgrpid => "7"
25
+ }]
26
+ })
27
+ ```
28
+
29
+ License
30
+ =======
31
+
32
+ MIT
data/lib/zabx/error.rb ADDED
@@ -0,0 +1,21 @@
1
+ module Zabx
2
+ class Error < StandardError; end
3
+
4
+ class JsonRpcError < Error
5
+ attr_reader :message, :code, :data
6
+
7
+ def initialize(message, code, data = nil)
8
+ @message = message
9
+ @code = code
10
+ @data = data
11
+ end
12
+
13
+ def to_s
14
+ output = "#{code}: #{message}"
15
+ output += " #{data}" unless data.nil?
16
+ end
17
+ end
18
+
19
+ class HttpError < Error; end
20
+ class ConnectionError < Error; end
21
+ end
@@ -0,0 +1,73 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require 'uuidtools'
4
+ require 'zabx/error'
5
+
6
+ module Zabx
7
+ class JsonRpc
8
+ JSON_RPC_VERSION = "2.0"
9
+
10
+ def initialize(url, options = {})
11
+ @url = url
12
+ @connection = Faraday.new do |connection|
13
+ connection.request :json
14
+ connection.response :json, :content_type => /\bjson$/
15
+ connection.response :follow_redirects
16
+ connection.adapter Faraday.default_adapter
17
+ end
18
+
19
+ @namespace = options.delete :namespace
20
+ @options = options
21
+ end
22
+
23
+ def single_request(method, args)
24
+ namespaced_method = @namespace.nil? ? method : "#{@namespace}.#{method}"
25
+ body = @options.merge({
26
+ :jsonrpc => JSON_RPC_VERSION,
27
+ :method => namespaced_method,
28
+ :params => args,
29
+ :id => UUIDTools::UUID.random_create.to_i
30
+ })
31
+ begin
32
+ @connection.post @url, body
33
+ rescue Faraday::ConnectionFailed => exception
34
+ raise ConnectionError, exception.message
35
+ end
36
+ end
37
+
38
+ def single_response(response)
39
+ error_check response
40
+ response["result"]
41
+ end
42
+
43
+ def error_check response
44
+ if response.has_key? "error"
45
+ error = response["error"]
46
+ raise JsonRpcError.new(error["message"], error["code"], error["data"])
47
+ end
48
+ end
49
+
50
+ def status_code_check response
51
+ case response.status
52
+ when 400...500
53
+ raise HttpError, "HTTP client error with status code #{response.status}"
54
+ when 500...600
55
+ raise HttpError, "HTTP server error with status code #{response.status}"
56
+ end
57
+ end
58
+
59
+ def invoke(method, args)
60
+ response = single_request(method, args)
61
+ status_code_check(response)
62
+ single_response(response.body)
63
+ end
64
+
65
+ def missing_method(method, *args)
66
+ if args.length == 1 && args[0].is_a?(Hash)
67
+ invoke(method, args[0])
68
+ else
69
+ invoke(method, args)
70
+ end
71
+ end
72
+ end
73
+ end
data/lib/zabx/zabx.rb ADDED
@@ -0,0 +1,50 @@
1
+ require 'zabx/json_rpc'
2
+
3
+ module Zabx
4
+ class Api
5
+ class Query
6
+ def initialize(url, options = {})
7
+ @rpc = JsonRpc.new(url, options)
8
+ end
9
+
10
+ def invoke(method, args)
11
+ @rpc.invoke(method, args)
12
+ end
13
+
14
+ def method_missing(method, *args)
15
+ if args.length == 1 && args[0].is_a?(Hash)
16
+ invoke(method, args[0])
17
+ else
18
+ invoke(method, args)
19
+ end
20
+ end
21
+ end
22
+
23
+ def initialize(url, options)
24
+ @url = url
25
+ @username = options.delete(:username)
26
+ @password = options.delete(:password)
27
+ end
28
+
29
+ def auth
30
+ @auth ||= query(:namespace => :user).login(:user => @username, :password => @password)
31
+ end
32
+
33
+ def invoke(method, args = {})
34
+ query(:auth => auth).invoke(method, args)
35
+ end
36
+
37
+ def apiinfo
38
+ query :namespace => "apiinfo"
39
+ end
40
+
41
+ def method_missing(method, *args)
42
+ query(:auth => auth, :namespace => method)
43
+ end
44
+
45
+ private
46
+ def query(options = {})
47
+ Query.new(@url, options)
48
+ end
49
+ end
50
+ end
data/lib/zabx.rb ADDED
@@ -0,0 +1 @@
1
+ require 'zabx/zabx'
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zabx
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Thomas Whitton
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-08-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.8.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.8.1
30
+ - !ruby/object:Gem::Dependency
31
+ name: faraday
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.9.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.9.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: faraday_middleware
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 0.9.1
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.1
62
+ - !ruby/object:Gem::Dependency
63
+ name: uuidtools
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 2.1.5
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 2.1.5
78
+ description: Zabbix api client written in ruby
79
+ email: mail@thomaswhitton.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files: []
83
+ files:
84
+ - README.md
85
+ - LICENSE.txt
86
+ - lib/zabx.rb
87
+ - lib/zabx/json_rpc.rb
88
+ - lib/zabx/zabx.rb
89
+ - lib/zabx/error.rb
90
+ homepage: https://github.com/oracal/zabx
91
+ licenses:
92
+ - MIT
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 1.8.24
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: Zabbix api client written in ruby
115
+ test_files: []