rzabbix 0.1 → 0.2
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/README.md +2 -2
- data/lib/rzabbix.rb +39 -1
- data/lib/rzabbix/base.rb +29 -0
- data/lib/rzabbix/connection.rb +108 -0
- data/lib/rzabbix/host.rb +63 -0
- data/lib/rzabbix/host_group.rb +56 -0
- data/lib/rzabbix/template.rb +55 -0
- data/lib/rzabbix/version.rb +1 -1
- metadata +8 -4
data/README.md
CHANGED
@@ -11,8 +11,8 @@ To install RZabbix, run the following command:
|
|
11
11
|
To use RZabbix:
|
12
12
|
|
13
13
|
gem 'rzabbix'
|
14
|
-
|
15
|
-
host =
|
14
|
+
RZabbix::Base.set_credentials("zabbix.example.com", "user", "password")
|
15
|
+
host = RZabbix::Host.find("my-dns-example.com")
|
16
16
|
|
17
17
|
## External Links
|
18
18
|
|
data/lib/rzabbix.rb
CHANGED
@@ -2,4 +2,42 @@ module RZabbix
|
|
2
2
|
|
3
3
|
end
|
4
4
|
|
5
|
-
require '
|
5
|
+
require 'rzabbix/connection'
|
6
|
+
require 'rzabbix/base'
|
7
|
+
require 'rzabbix/host'
|
8
|
+
require 'rzabbix/host_group'
|
9
|
+
require 'rzabbix/template'
|
10
|
+
require 'rzabbix/version'
|
11
|
+
|
12
|
+
class Hash
|
13
|
+
|
14
|
+
def rzbx_symbolize_keys
|
15
|
+
inject({}) do |options, (key, value)|
|
16
|
+
options[(key.to_sym rescue key) || key] = value
|
17
|
+
options
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
def rzbx_recursively_symbolize_keys
|
23
|
+
result = self.rzbx_symbolize_keys
|
24
|
+
result.keys.each do |key|
|
25
|
+
if result[key].is_a?(Hash) || result[key].is_a?(Array)
|
26
|
+
result[key] = result[key].rzbx_recursively_symbolize_keys
|
27
|
+
end
|
28
|
+
end
|
29
|
+
result
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class Array
|
34
|
+
def rzbx_recursively_symbolize_keys
|
35
|
+
self.map do |item|
|
36
|
+
if item.is_a?(Hash) || item.is_a?(Array)
|
37
|
+
item.rzbx_recursively_symbolize_keys
|
38
|
+
else
|
39
|
+
item
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/rzabbix/base.rb
CHANGED
@@ -1,9 +1,38 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'uri'
|
3
|
+
require 'net/http'
|
4
|
+
require 'net/https'
|
5
|
+
|
1
6
|
module RZabbix
|
2
7
|
|
3
8
|
class Base
|
4
9
|
|
10
|
+
def self.perform_request(*args)
|
11
|
+
Connection.perform_request(*args)
|
12
|
+
end
|
5
13
|
|
14
|
+
###########################################################################################################
|
15
|
+
# Instance methods
|
16
|
+
###########################################################################################################
|
6
17
|
|
18
|
+
attr_accessor :attributes
|
19
|
+
|
20
|
+
def initialize(attributes)
|
21
|
+
super()
|
22
|
+
self.attributes = self.class.default_attributes.merge(attributes)
|
23
|
+
end
|
24
|
+
|
25
|
+
def [](key)
|
26
|
+
self.attributes[key.to_sym]
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_json(*args)
|
30
|
+
self.attributes.to_json(*args)
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_hash
|
34
|
+
self.attributes
|
35
|
+
end
|
7
36
|
end
|
8
37
|
|
9
38
|
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
module RZabbix
|
2
|
+
|
3
|
+
class Connection
|
4
|
+
|
5
|
+
API_OUTPUT_SHORTEN = "shorten"
|
6
|
+
API_OUTPUT_REFER = "refer"
|
7
|
+
API_OUTPUT_EXTEND = "extend"
|
8
|
+
|
9
|
+
class ResponseCodeError < StandardError; end
|
10
|
+
|
11
|
+
class << self
|
12
|
+
attr_accessor :credentials
|
13
|
+
attr_accessor :use_ssl
|
14
|
+
attr_accessor :auth
|
15
|
+
end
|
16
|
+
|
17
|
+
self.use_ssl = false
|
18
|
+
|
19
|
+
def self.set_credentials(api_url, api_user, api_password)
|
20
|
+
self.credentials = {:api_url=>api_url, :api_user=>api_user, :api_password=>api_password}
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.next_request_id
|
24
|
+
@request_id = @request_id ? @request_id+1 : 1
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.auth
|
28
|
+
@auth ||= begin
|
29
|
+
auth_message = {
|
30
|
+
'auth' => nil,
|
31
|
+
'method' => 'user.authenticate',
|
32
|
+
'params' => {
|
33
|
+
'user' => Connection.credentials[:api_user],
|
34
|
+
'password' => Connection.credentials[:api_password],
|
35
|
+
'0' => '0'
|
36
|
+
}
|
37
|
+
}
|
38
|
+
do_request(auth_message)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
def self.perform_request(controller, action, params)
|
44
|
+
message = message_for(controller, action, params)
|
45
|
+
do_request(message)
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.message_for(controller, action, params = {})
|
49
|
+
{
|
50
|
+
'method' => "#{controller}.#{action}",
|
51
|
+
'params' => params.to_hash.merge(:output=>API_OUTPUT_EXTEND),
|
52
|
+
'auth' => Connection.auth
|
53
|
+
}
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.do_request(message)
|
57
|
+
id = next_request_id
|
58
|
+
|
59
|
+
message['id'] = id
|
60
|
+
message['jsonrpc'] = '2.0'
|
61
|
+
|
62
|
+
message_json = JSON.generate(message)
|
63
|
+
|
64
|
+
#puts "#{JSON.parse(message_json).inspect}\n\n\n"
|
65
|
+
|
66
|
+
uri = URI.parse(Connection.credentials[:api_url])
|
67
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
68
|
+
if (use_ssl)
|
69
|
+
http.use_ssl = true
|
70
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
71
|
+
end
|
72
|
+
|
73
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
74
|
+
request.add_field('Content-Type', 'application/json-rpc')
|
75
|
+
request.body=(message_json)
|
76
|
+
responce = http.request(request)
|
77
|
+
|
78
|
+
if ( responce.code != "200" ) then
|
79
|
+
raise ResponseCodeError.new("Responce code from [" + credentials[:api_url] + "] is " + responce.code)
|
80
|
+
end
|
81
|
+
|
82
|
+
responce_body_hash = JSON.parse(responce.body)
|
83
|
+
|
84
|
+
#if not ( responce_body_hash['id'] == id ) then
|
85
|
+
# raise Zabbix::InvalidAnswerId.new("Wrong ID in zabbix answer")
|
86
|
+
#end
|
87
|
+
|
88
|
+
|
89
|
+
# Check errors in zabbix answer. If error exist - raise exception Zabbix::Error
|
90
|
+
if ( error = responce_body_hash['error'] ) then
|
91
|
+
error_message = error['message']
|
92
|
+
error_data = error['data']
|
93
|
+
error_code = error['code']
|
94
|
+
|
95
|
+
e_message = "Code: [" + error_code.to_s + "]. Message: [" + error_message +\
|
96
|
+
"]. Data: [" + error_data + "]."
|
97
|
+
|
98
|
+
raise StandardError.new(e_message)
|
99
|
+
end
|
100
|
+
|
101
|
+
result = responce_body_hash['result']
|
102
|
+
result.respond_to?(:rzbx_recursively_symbolize_keys) ? result.rzbx_recursively_symbolize_keys : result
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
data/lib/rzabbix/host.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
module RZabbix
|
2
|
+
|
3
|
+
class Host < Base
|
4
|
+
|
5
|
+
def self.find(host_id)
|
6
|
+
hosts = perform_request(:host, :get, :hostids=>[host_id], :output=>"extend")
|
7
|
+
self.new(hosts.first.rzbx_recursively_symbolize_keys) unless hosts.empty?
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.find_host_id_by_hostname(hostname)
|
11
|
+
hosts = perform_request(:host, :get, :filter => {:host=>hostname})
|
12
|
+
hosts.first[:hostid]
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.find_by_hostname(hostname)
|
16
|
+
hosts = perform_request(:host, :get, :filter => {:host=>hostname}, :output=>"extend")
|
17
|
+
self.new(hosts.first) unless hosts.empty?
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.create(attributes)
|
21
|
+
host = self.new(attributes)
|
22
|
+
result = perform_request(:host, :create, host)
|
23
|
+
result && result[:hostids] ? self.find(result[:hostids].first) : nil
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.default_attributes
|
27
|
+
{
|
28
|
+
#:hostid=>nil, #int Host ID
|
29
|
+
:host=>nil, #string Host name.
|
30
|
+
:port=>10050, #int Port number.
|
31
|
+
:status=>0, #int Host Status.
|
32
|
+
:useip=>0, #int Use IP.
|
33
|
+
:dns=>'', #string DNS.
|
34
|
+
:ip=>'0.0.0.0', #string IP.
|
35
|
+
:proxy_hostid=> 0, #int Proxy Host ID.
|
36
|
+
:useipmi=> 0 , #int Use IPMI.
|
37
|
+
:ipmi_ip=>'', #string IPMAI IP.
|
38
|
+
:ipmi_port=>623, #int IPMI port.
|
39
|
+
:ipmi_authtype=>0, #int IPMI authentication type.
|
40
|
+
:ipmi_privilege=>0, #int IPMI privilege.
|
41
|
+
:ipmi_username=>'', #string IPMI username.
|
42
|
+
:ipmi_password=>'' #string IPMI password.
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_hash(*args)
|
47
|
+
host_hash = self.attributes.inject({}) do |hash, (attr, v)|
|
48
|
+
case attr.to_sym
|
49
|
+
when :groups
|
50
|
+
hash[:groups] = v.map {|g| { :groupid=>g.attributes[:groupid]}}
|
51
|
+
when :templates
|
52
|
+
hash[:templates] = v.map {|t| {:templateid=>t.attributes[:templateid]}}
|
53
|
+
else
|
54
|
+
hash[attr] = v
|
55
|
+
end
|
56
|
+
hash
|
57
|
+
end
|
58
|
+
host_hash
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module RZabbix
|
2
|
+
|
3
|
+
class HostGroup < Base
|
4
|
+
|
5
|
+
def self.find_by_name(name)
|
6
|
+
groups = perform_request(:hostgroup, :get, :filter => {:name=>name}, :output=>"extend")
|
7
|
+
self.new(groups.first.rzbx_recursively_symbolize_keys) unless groups.empty?
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.default_attributes
|
11
|
+
{
|
12
|
+
:nodeids => nil,
|
13
|
+
:groupids => nil,
|
14
|
+
:hostids => nil,
|
15
|
+
:templateids => nil,
|
16
|
+
:graphids => nil,
|
17
|
+
:triggerids => nil,
|
18
|
+
:maintenanceids => nil,
|
19
|
+
:monitored_hosts => nil,
|
20
|
+
:templated_hosts => nil,
|
21
|
+
:real_hosts => nil,
|
22
|
+
:not_proxy_hosts => nil,
|
23
|
+
:with_items => nil,
|
24
|
+
:with_monitored_items => nil,
|
25
|
+
:with_historical_items => nil,
|
26
|
+
:with_triggers => nil,
|
27
|
+
:with_monitored_triggers => nil,
|
28
|
+
:with_httptests => nil,
|
29
|
+
:with_monitored_httptests => nil,
|
30
|
+
:with_graphs => nil,
|
31
|
+
:editable => nil,
|
32
|
+
:nopermissions => nil
|
33
|
+
|
34
|
+
# :filter => nil,
|
35
|
+
# :pattern => '',
|
36
|
+
#
|
37
|
+
# :output => Connection::API_OUTPUT_REFER,
|
38
|
+
# :extendoutput => nil,
|
39
|
+
# :select_hosts => nil,
|
40
|
+
# :select_templates => nil,
|
41
|
+
# :count => nil,
|
42
|
+
# :preservekeys => nil,
|
43
|
+
# :sortfield => '',
|
44
|
+
# :sortorder => '',
|
45
|
+
# :limit => nil,
|
46
|
+
# :limitSelects => nil
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
def to_json(*args)
|
51
|
+
self.attributes.to_json(*args)
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module RZabbix
|
2
|
+
|
3
|
+
class Template < Base
|
4
|
+
|
5
|
+
def self.find_by_name(name)
|
6
|
+
templates_hash = perform_request(:template, :get, :filter => {:host=>name}, :output=>"extend")
|
7
|
+
if templates_hash.kind_of?(Hash) && !templates_hash.keys.empty?
|
8
|
+
template_hash = templates_hash[templates_hash.keys.first]
|
9
|
+
self.new(template_hash.rzbx_recursively_symbolize_keys)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.default_attributes
|
14
|
+
{
|
15
|
+
:nodeids => nil,
|
16
|
+
:groupids => nil,
|
17
|
+
:templateids => nil,
|
18
|
+
:parentTemplateids => nil,
|
19
|
+
:hostids => nil,
|
20
|
+
:graphids => nil,
|
21
|
+
:itemids => nil,
|
22
|
+
:triggerids => nil,
|
23
|
+
:with_items => nil,
|
24
|
+
:with_triggers => nil,
|
25
|
+
:with_graphs => nil,
|
26
|
+
:editable => nil,
|
27
|
+
:nopermissions => nil
|
28
|
+
|
29
|
+
# :filter => nil,
|
30
|
+
# :pattern => '',
|
31
|
+
#
|
32
|
+
# :output => API_OUTPUT_REFER,
|
33
|
+
# :extendoutput => nil,
|
34
|
+
# :select_groups => nil,
|
35
|
+
# :select_hosts => nil,
|
36
|
+
# :select_templates => nil,
|
37
|
+
# :selectParentTemplates => nil,
|
38
|
+
# :select_items => nil,
|
39
|
+
# :select_triggers => nil,
|
40
|
+
# :select_graphs => nil,
|
41
|
+
# :select_applications => nil,
|
42
|
+
# :select_macros => nil,
|
43
|
+
# :countOutput => nil,
|
44
|
+
# :groupCount => nil,
|
45
|
+
# :preservekeys => nil,
|
46
|
+
# :sortfield => '',
|
47
|
+
# :sortorder => '',
|
48
|
+
# :limit => nil,
|
49
|
+
# :limitSelects => nil
|
50
|
+
}
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
data/lib/rzabbix/version.rb
CHANGED
metadata
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rzabbix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: "0.
|
8
|
+
- 2
|
9
|
+
version: "0.2"
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Neer Friedman
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-12-
|
17
|
+
date: 2010-12-09 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -41,8 +41,12 @@ extensions: []
|
|
41
41
|
extra_rdoc_files: []
|
42
42
|
|
43
43
|
files:
|
44
|
+
- lib/rzabbix/host_group.rb
|
44
45
|
- lib/rzabbix/base.rb
|
45
46
|
- lib/rzabbix/version.rb
|
47
|
+
- lib/rzabbix/host.rb
|
48
|
+
- lib/rzabbix/template.rb
|
49
|
+
- lib/rzabbix/connection.rb
|
46
50
|
- lib/rzabbix.rb
|
47
51
|
- README.md
|
48
52
|
- ROADMAP.md
|