zbx 1.0 → 1.1.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/bin/zbx +4 -0
- data/lib/zbx.rb +3 -0
- data/lib/zbx/api.rb +14 -19
- data/lib/zbx/cli.rb +84 -0
- data/lib/zbx/http_client.rb +12 -7
- data/lib/zbx/version.rb +1 -1
- data/spec/auth_spec.rb +5 -3
- data/spec/functional_spec.rb +3 -2
- data/spec/spec_helper.rb +1 -0
- data/zbx.gemspec +2 -0
- metadata +22 -3
data/bin/zbx
ADDED
data/lib/zbx.rb
CHANGED
data/lib/zbx/api.rb
CHANGED
@@ -4,24 +4,24 @@ module ZBX
|
|
4
4
|
# if block is given, it will be evaluated in the object
|
5
5
|
# that is to say, you can write something like this.
|
6
6
|
#
|
7
|
-
# ZBX::API.new(user,
|
7
|
+
# ZBX::API.new(user, password, api_url) do
|
8
8
|
# host.get(hostids: 1)
|
9
9
|
# end
|
10
10
|
#
|
11
11
|
# -- this is equal to the following --
|
12
12
|
#
|
13
|
-
# ZBX::API.new(user,
|
13
|
+
# ZBX::API.new(user, password, api_url).host.get(hostids: 1)
|
14
14
|
|
15
|
-
def initialize user=nil,
|
15
|
+
def initialize user=nil, password=nil, api_url=nil, &b
|
16
16
|
@auth = nil
|
17
|
-
@user, @
|
17
|
+
@user, @password, @http = user, password, HttpClient.new(api_url)
|
18
18
|
|
19
19
|
instance_eval(&b) if block_given?
|
20
20
|
end
|
21
21
|
|
22
22
|
def set option={}
|
23
|
-
@user, @auth = option[:
|
24
|
-
@
|
23
|
+
@user, @auth = option[:user], nil if option[:user]
|
24
|
+
@password, @auth = option[:password], nil if option[:password]
|
25
25
|
@http.api_url = option[:api_url] if option[:api_url]
|
26
26
|
end
|
27
27
|
|
@@ -34,22 +34,17 @@ module ZBX
|
|
34
34
|
# - jsonrpc = '2.0'
|
35
35
|
# - auth = an authentication token
|
36
36
|
|
37
|
-
params[:output]
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
37
|
+
params[:output] = "extend" unless params[:output] or params["output"]
|
38
|
+
opts = {method: method,
|
39
|
+
params: params,
|
40
|
+
id: _id,
|
41
|
+
jsonrpc: '2.0'}
|
42
|
+
opts[:auth] = auth! unless method == 'user.login'
|
43
|
+
@http.request opts
|
43
44
|
end
|
44
45
|
|
45
46
|
def auth!
|
46
|
-
@auth ||=
|
47
|
-
params: {user: @user, password: @pass},
|
48
|
-
id: _id,
|
49
|
-
jsonrpc: '2.0')
|
50
|
-
|
51
|
-
# if login failed, we raise an auth error !
|
52
|
-
@auth or raise "Authentication failed"
|
47
|
+
@auth ||= user.login user: @user, password: @password
|
53
48
|
end
|
54
49
|
|
55
50
|
def method_missing m, &b
|
data/lib/zbx/cli.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'thor'
|
3
|
+
|
4
|
+
module ZBX
|
5
|
+
module CLI
|
6
|
+
module Helper
|
7
|
+
extend self
|
8
|
+
def browse link
|
9
|
+
case RbConfig::CONFIG['host_os']
|
10
|
+
when /mswin|mingw|cygwin/
|
11
|
+
system "start #{link}"
|
12
|
+
when /darwin/
|
13
|
+
system "open #{link}"
|
14
|
+
when /linux|bsd/
|
15
|
+
system "xdg-open #{link}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class CLI < Thor
|
21
|
+
map "-s" => :send
|
22
|
+
map "-d" => :doc
|
23
|
+
|
24
|
+
# ---------------------------- send ------------------------------
|
25
|
+
|
26
|
+
desc "send [method] [data]", "Send api request to zabbix server, -s is its short term"
|
27
|
+
long_desc <<-LLL
|
28
|
+
Example:
|
29
|
+
\x5> $ zbx send host.get '{"hostids": 321}'
|
30
|
+
|
31
|
+
> $ zbx -s host.get '{"hostids": 321}'
|
32
|
+
|
33
|
+
# specify user name and password
|
34
|
+
\x5> $ zbx -s host.get '{"hostids": 321}' -u me -p mypassword
|
35
|
+
LLL
|
36
|
+
|
37
|
+
method_option :user, type: :string, aliases: '-u', desc: "zabbix username, default defined in ~/.zbxconfig"
|
38
|
+
method_option :password, type: :string, aliases: '-p', desc: "zabbix password, default defined in ~/.zbxconfig"
|
39
|
+
method_option :api_url, type: :string, aliases: '-l', desc: "zabbix api url, default defined in ~/.zbxconfig"
|
40
|
+
|
41
|
+
def send(method, data='{}')
|
42
|
+
config_file = File.expand_path('~/.zbxconfig')
|
43
|
+
default = File.exists?(config_file) ? YAML.load_file(config_file) : {}
|
44
|
+
client = ZBX::API.new(options[:user] || default["user"],
|
45
|
+
options[:password] || default["password"],
|
46
|
+
options[:api_url] || default["api_url"])
|
47
|
+
# x = method.split('.')
|
48
|
+
say client.request method, JSON.parse(data)
|
49
|
+
# say client.send(x.first).send(x.last, JSON.parse(data))
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
# ---------------------------- doc ------------------------------
|
54
|
+
|
55
|
+
desc "doc [method-name or entity-name]", "Open the api doc in browser, -d its short term"
|
56
|
+
long_desc <<-LLL
|
57
|
+
Example:
|
58
|
+
\x5> $ zbx doc host.get
|
59
|
+
|
60
|
+
> $ zbx -d host
|
61
|
+
|
62
|
+
> $ zbx doc hostgroup -v 1.8
|
63
|
+
LLL
|
64
|
+
method_option :version, default: 2.0, aliases: "-v", type: :numeric, desc: "api version"
|
65
|
+
|
66
|
+
def doc method=''
|
67
|
+
version = options[:version]
|
68
|
+
base = "https://www.zabbix.com/documentation/#{version}/"
|
69
|
+
suffix = case version
|
70
|
+
when 1.8
|
71
|
+
'api/'
|
72
|
+
when 2.0
|
73
|
+
'manual/appendix/api/'
|
74
|
+
when 2.2
|
75
|
+
'manual/api/reference/'
|
76
|
+
end
|
77
|
+
url = base << suffix << method.gsub('.', '/')
|
78
|
+
say "browse #{url}"
|
79
|
+
Helper.browse url
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
data/lib/zbx/http_client.rb
CHANGED
@@ -12,9 +12,18 @@ module ZBX
|
|
12
12
|
req = Net::HTTP::Post.new uri.request_uri
|
13
13
|
req.add_field('Content-Type', 'application/json-rpc')
|
14
14
|
req.body = options.to_json
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
res = http.request(req)
|
16
|
+
|
17
|
+
begin
|
18
|
+
parsed = JSON.parse(res.body)
|
19
|
+
rescue
|
20
|
+
raise "Zabbix API Request Error: \n HTTP Response : #{res.inspect} \n #{res.body}"
|
21
|
+
end
|
22
|
+
|
23
|
+
if parsed["error"] or parsed["result"].nil?
|
24
|
+
raise "Zabbix API Request Message(Bad request) : #{parsed}"
|
25
|
+
end
|
26
|
+
parsed['result']
|
18
27
|
end
|
19
28
|
|
20
29
|
def http
|
@@ -33,10 +42,6 @@ module ZBX
|
|
33
42
|
|
34
43
|
def api_url= url
|
35
44
|
@api_url, @uri, @http = url, nil, nil
|
36
|
-
self
|
37
45
|
end
|
38
46
|
end
|
39
47
|
end
|
40
|
-
|
41
|
-
|
42
|
-
|
data/lib/zbx/version.rb
CHANGED
data/spec/auth_spec.rb
CHANGED
@@ -1,15 +1,17 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe ZBX::API do
|
4
|
-
|
4
|
+
info = YAML.load_file(File.expand_path('~/.zbxconfig'))
|
5
|
+
user, password, api_url = info["user"], info["password"], info["api_url"]
|
6
|
+
|
5
7
|
before(:each) do
|
6
8
|
@invalid_client = ZBX::API.new "fakeusername", "fakepassword", api_url
|
7
|
-
@valid_client = ZBX::API.new
|
9
|
+
@valid_client = ZBX::API.new user, password, api_url
|
8
10
|
end
|
9
11
|
|
10
12
|
describe "invalid client authentication" do
|
11
13
|
it "should raise a runtime error!" do
|
12
|
-
expect { @invalid_client.auth! }.to raise_error(RuntimeError, /
|
14
|
+
expect { @invalid_client.auth! }.to raise_error(RuntimeError, /Login name or password/)
|
13
15
|
end
|
14
16
|
end
|
15
17
|
|
data/spec/functional_spec.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe ZBX::API do
|
4
|
-
|
4
|
+
info = YAML.load_file(File.expand_path('~/.zbxconfig'))
|
5
|
+
user, password, api_url = info["user"], info["password"], info["api_url"]
|
5
6
|
|
6
7
|
before(:each) do
|
7
|
-
@client = ZBX::API.new
|
8
|
+
@client = ZBX::API.new user, password, api_url
|
8
9
|
end
|
9
10
|
|
10
11
|
describe "Using host.get to check if api request works " do
|
data/spec/spec_helper.rb
CHANGED
data/zbx.gemspec
CHANGED
@@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
+
spec.add_dependency 'thor'
|
22
|
+
|
21
23
|
spec.add_development_dependency "bundler", "~> 1.3"
|
22
24
|
spec.add_development_dependency "rake"
|
23
25
|
spec.add_development_dependency "rspec"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zbx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,24 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-08-
|
12
|
+
date: 2013-08-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: thor
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
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: '0'
|
14
30
|
- !ruby/object:Gem::Dependency
|
15
31
|
name: bundler
|
16
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -62,7 +78,8 @@ dependencies:
|
|
62
78
|
description: yet another zabbix api wrapper and a dsl
|
63
79
|
email:
|
64
80
|
- mylastnameisyan@gmail.com
|
65
|
-
executables:
|
81
|
+
executables:
|
82
|
+
- zbx
|
66
83
|
extensions: []
|
67
84
|
extra_rdoc_files: []
|
68
85
|
files:
|
@@ -71,8 +88,10 @@ files:
|
|
71
88
|
- LICENSE.txt
|
72
89
|
- README.md
|
73
90
|
- Rakefile
|
91
|
+
- bin/zbx
|
74
92
|
- lib/zbx.rb
|
75
93
|
- lib/zbx/api.rb
|
94
|
+
- lib/zbx/cli.rb
|
76
95
|
- lib/zbx/entity.rb
|
77
96
|
- lib/zbx/http_client.rb
|
78
97
|
- lib/zbx/version.rb
|