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 +4 -4
- data/README.md +37 -4
- data/lib/zabbix/client/client.rb +34 -17
- data/lib/zabbix/client/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9d5585021d980030d1b89a8c14c701cd1dd1f84c
|
4
|
+
data.tar.gz: a23f3b6127ee92b0aae5380e247d2b82651618b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
|
32
|
-
|
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
|
data/lib/zabbix/client/client.rb
CHANGED
@@ -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
|
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
|
-
|
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
|
-
|
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
|
-
|
58
|
-
|
65
|
+
http.use_ssl = true
|
66
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
59
67
|
end
|
60
68
|
|
61
|
-
|
69
|
+
http.set_debug_output($stderr) if @client.options[:debug]
|
62
70
|
|
63
71
|
http.start do |h|
|
64
|
-
|
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
|
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 :
|
84
|
-
attr_reader :http_hook
|
102
|
+
attr_reader :options
|
85
103
|
attr_accessor :auth
|
86
104
|
|
87
|
-
def initialize(url,
|
105
|
+
def initialize(url, options = {})
|
88
106
|
@url = URI.parse(url)
|
89
|
-
@
|
90
|
-
@http_hook = http_hook
|
107
|
+
@options = options
|
91
108
|
end
|
92
109
|
|
93
110
|
def method_missing(method_name)
|