zabbix-client 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cf8aed8ec273e471d347a2c8c97d8720c7cb6933
4
- data.tar.gz: 8fe616fdbadd37b4e5ecffad87625b385bb6d5de
3
+ metadata.gz: 9d5585021d980030d1b89a8c14c701cd1dd1f84c
4
+ data.tar.gz: a23f3b6127ee92b0aae5380e247d2b82651618b8
5
5
  SHA512:
6
- metadata.gz: 7ae12cd805d4f265553b241b27277f7f61eb30b71a5e47980e1512a29d46a0b44c1704cefa782c24e4b8829ada0b6012d09a5f3cbd2f069e369dd4cc87fedc50
7
- data.tar.gz: fe4681e21634288d551308dd8b3f3c41ca7f64a4c21fde26d9f3e7c7a98135c7c43d5f582cf46794243141c474d3fd88b1885598be200d5e071e6720987ea637
6
+ metadata.gz: be8bce17e565303c4aa7139c35f8c8ca013dc4747b14edf5aedd6a2834b651cd6bacb2f2079dc1147660660349fe810385d36211f2f037eb712e21b4c6c7220f
7
+ data.tar.gz: 036c4bec2b878d72e7823955637d4025a6c4eb83e46f3f4052b97d07d868c5189381314b82c98775770f6443d48b61d1fa4d7219b1ef3b0f1d5219349bb98079
data/README.md CHANGED
@@ -24,13 +24,46 @@ Or install it yourself as:
24
24
  require 'zabbix/client'
25
25
 
26
26
  client = Zabbix::Client.new('http://localhost/zabbix/api_jsonrpc.php')
27
+
28
+ # https://www.zabbix.com/documentation/2.0/manual/appendix/api/user/login
27
29
  client.user.login(user: 'Admin', password: 'zabbix')
30
+
31
+ # https://www.zabbix.com/documentation/2.0/manual/appendix/api/apiinfo/version
28
32
  p client.apiinfo.version #=> "2.0.12"
29
- p client.template.getobjects(:host => ['Template OS Linux'])
33
+
34
+ # https://www.zabbix.com/documentation/2.0/manual/appendix/api/template/getobjects
35
+ p client.template.getobjects(host: ['Template OS Linux'])
30
36
  #=> [{"hostid"=>"10001",
31
- "proxy_hostid"=>"0",
32
- "host"=>"Template OS Linux",
33
- ...
37
+ # "proxy_hostid"=>"0",
38
+ # "host"=>"Template OS Linux",
39
+ # ...
40
+ ```
41
+
42
+ ### Use proxy
43
+
44
+ ```ruby
45
+ client = Zabbix::Client.new(
46
+ 'http://localhost/zabbix/api_jsonrpc.php',
47
+ proxy_user: 'username', proxy_password: 'password'
48
+ )
49
+ ```
50
+
51
+ ### Basic auth
52
+
53
+ ```ruby
54
+ client = Zabbix::Client.new(
55
+ 'http://localhost/zabbix/api_jsonrpc.php',
56
+ basic_auth_user: 'username', basic_auth_password: 'password'
57
+ )
58
+ ```
59
+
60
+ ### Debug mode
61
+
62
+ ```ruby
63
+ client = Zabbix::Client.new(
64
+ 'http://localhost/zabbix/api_jsonrpc.php',
65
+ debug: true
66
+ )
34
67
  ```
35
68
 
36
69
  ## Related links
@@ -1,12 +1,14 @@
1
1
  class Zabbix::Client
2
- DEFAULT_HEADERS = {
3
- 'Content-Type' => 'application/json-rpc'
4
- }
5
-
6
2
  class Method
7
3
  JSON_RPC_VERSION = '2.0'
8
4
  JSON_RPC_REQUEST_ID = 1
5
+
9
6
  LOGIN_METHOD = 'user.login'
7
+ LOGOUT_METHOD = 'user.logout'
8
+
9
+ DEFAULT_HEADERS = {
10
+ 'Content-Type' => 'application/json-rpc'
11
+ }
10
12
 
11
13
  def initialize(prefix, client)
12
14
  @prefix = prefix
@@ -22,14 +24,17 @@ class Zabbix::Client
22
24
  response = query(method, params, options)
23
25
 
24
26
  if (error = response['error'])
25
- error = error.merge('method' => method, 'params' => params, 'options' => options)
27
+ error = error.merge('method' => method, 'params' => params)
26
28
  raise Zabbix::Client::Error.new(error)
27
29
  end
28
30
 
29
31
  result = response['result']
30
32
 
31
- if method == LOGIN_METHOD
33
+ case method
34
+ when LOGIN_METHOD
32
35
  @client.auth = result
36
+ when LOGOUT_METHOD
37
+ @client.auth = nil
33
38
  end
34
39
 
35
40
  if block
@@ -51,24 +56,38 @@ class Zabbix::Client
51
56
 
52
57
  body[:auth] = @client.auth if @client.auth
53
58
  body = JSON.dump(body)
54
- http = Net::HTTP.new(@client.url.host, @client.url.port)
59
+
60
+ proxy_user = @client.options[:proxy_user]
61
+ proxy_password = @client.options[:proxy_password]
62
+ http = Net::HTTP.Proxy(nil, nil).new(@client.url.host, @client.url.port)
55
63
 
56
64
  if @client.url.scheme == 'https'
57
- https.use_ssl = true
58
- https.verify_mode = OpenSSL::SSL::VERIFY_NONE
65
+ http.use_ssl = true
66
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
59
67
  end
60
68
 
61
- @client.http_hook.call(http) if @client.http_hook
69
+ http.set_debug_output($stderr) if @client.options[:debug]
62
70
 
63
71
  http.start do |h|
64
- response = h.post(@client.url.path, body, @client.headers)
72
+ headers = DEFAULT_HEADERS.merge(@client.options[:headers] || {})
73
+ request = Net::HTTP::Post.new(@client.url.path, headers)
74
+ request.body = body
75
+
76
+ basic_auth_user = @client.options[:basic_auth_user]
77
+ basic_auth_password = @client.options[:basic_auth_password]
78
+
79
+ if basic_auth_user and basic_auth_password
80
+ request.basic_auth(basic_auth_user, basic_auth_password)
81
+ end
82
+
83
+ response = h.request(request)
65
84
  JSON.parse(response.body)
66
85
  end
67
86
  end
68
87
 
69
88
  def validate_args(args)
70
89
  unless (0..2).include?(args.length)
71
- raise ArgumentError, "wrong number of arguments: #{args.inspect} (#{args.length} for 1..2)"
90
+ raise ArgumentError, "wrong number of arguments: #{args.inspect} (#{args.length} for 0..2)"
72
91
  end
73
92
 
74
93
  args.each do |arg|
@@ -80,14 +99,12 @@ class Zabbix::Client
80
99
  end # Method
81
100
 
82
101
  attr_reader :url
83
- attr_reader :headers
84
- attr_reader :http_hook
102
+ attr_reader :options
85
103
  attr_accessor :auth
86
104
 
87
- def initialize(url, headers = {}, &http_hook)
105
+ def initialize(url, options = {})
88
106
  @url = URI.parse(url)
89
- @headers = DEFAULT_HEADERS.merge(headers)
90
- @http_hook = http_hook
107
+ @options = options
91
108
  end
92
109
 
93
110
  def method_missing(method_name)
@@ -1,5 +1,5 @@
1
1
  module Zabbix
2
2
  class Client
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
5
5
  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.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Sugawara