zbx 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +22 -3
- data/lib/zbx/api.rb +34 -12
- data/lib/zbx/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
|
-
#
|
1
|
+
# ZBX
|
2
2
|
|
3
|
-
|
3
|
+
This project is inspired by the following repos:
|
4
|
+
|
5
|
+
- [zabbixapi](https://github.com/vadv/zabbixapi)
|
6
|
+
- [pyzabbix](https://github.com/lukecyca/pyzabbix).
|
4
7
|
|
5
8
|
## Installation
|
6
9
|
|
@@ -18,7 +21,23 @@ Or install it yourself as:
|
|
18
21
|
|
19
22
|
## Usage
|
20
23
|
|
21
|
-
|
24
|
+
```ruby
|
25
|
+
require 'zbx'
|
26
|
+
|
27
|
+
# the following code are doing the same thing ,that is get a host whose id is 10160
|
28
|
+
|
29
|
+
ZBX::API.new username, password, api_url do
|
30
|
+
host.get hostids: 10160
|
31
|
+
end
|
32
|
+
|
33
|
+
ZBX::API.new username, password, api_url do
|
34
|
+
host do
|
35
|
+
get hostids: 10160
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
ZBX::API.new(username, password, api_url).host.get hostids: 10160
|
40
|
+
```
|
22
41
|
|
23
42
|
## Contributing
|
24
43
|
|
data/lib/zbx/api.rb
CHANGED
@@ -12,21 +12,28 @@ module ZBX
|
|
12
12
|
#
|
13
13
|
# ZBX::API.new(user, pass, api_url).host.get(hostids: 1)
|
14
14
|
|
15
|
-
def initialize user, pass, url, &b
|
15
|
+
def initialize user=nil, pass=nil, url=nil, &b
|
16
16
|
@auth = nil
|
17
17
|
@user = user
|
18
18
|
@pass = pass
|
19
|
-
@
|
19
|
+
@api_url = url
|
20
|
+
instance_eval(&b) if block_given?
|
21
|
+
end
|
20
22
|
|
21
|
-
|
22
|
-
@
|
23
|
-
|
24
|
-
|
25
|
-
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
26
|
-
end
|
23
|
+
def username! username
|
24
|
+
@user, @auth = username, nil
|
25
|
+
end
|
26
|
+
alias_method :username=, :username!
|
27
27
|
|
28
|
-
|
28
|
+
def password! password
|
29
|
+
@pass, @auth = password, nil
|
30
|
+
end
|
31
|
+
alias_method :password=, :password!
|
32
|
+
|
33
|
+
def api_url! api_url
|
34
|
+
@api_url, @auth = api_url, nil
|
29
35
|
end
|
36
|
+
alias_method :api_url=, :api_url!
|
30
37
|
|
31
38
|
def request method, params={}
|
32
39
|
# in any api request except `user.login`
|
@@ -41,7 +48,8 @@ module ZBX
|
|
41
48
|
_request(method: method,
|
42
49
|
params: params,
|
43
50
|
auth: auth!,
|
44
|
-
id: _id,
|
51
|
+
id: _id,
|
52
|
+
jsonrpc: '2.0')
|
45
53
|
end
|
46
54
|
|
47
55
|
def auth!
|
@@ -60,12 +68,26 @@ module ZBX
|
|
60
68
|
|
61
69
|
private
|
62
70
|
|
71
|
+
def _http
|
72
|
+
# Our http client. Borrowed from zabbixapi(https://github.com/vadv/zabbixapi)
|
73
|
+
@http ||= Net::HTTP.new _uri.host, _uri.port
|
74
|
+
if _uri.port == 443
|
75
|
+
@http.use_ssl = true
|
76
|
+
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
77
|
+
end
|
78
|
+
@http
|
79
|
+
end
|
80
|
+
|
81
|
+
def _uri
|
82
|
+
@uri ||= URI.parse(@api_url)
|
83
|
+
end
|
84
|
+
|
63
85
|
def _request options={}
|
64
86
|
# send post request
|
65
|
-
req = Net::HTTP::Post.new
|
87
|
+
req = Net::HTTP::Post.new _uri.request_uri
|
66
88
|
req.add_field('Content-Type', 'application/json-rpc')
|
67
89
|
req.body = options.to_json
|
68
|
-
JSON.parse(
|
90
|
+
JSON.parse(_http.request(req).body)['result']
|
69
91
|
rescue
|
70
92
|
nil
|
71
93
|
end
|
data/lib/zbx/version.rb
CHANGED