Class: RZabbix::Connection
- Inherits:
-
Object
- Object
- RZabbix::Connection
show all
- Defined in:
- lib/rzabbix/connection.rb
Defined Under Namespace
Classes: ResponseCodeError
Constant Summary
collapse
- API_OUTPUT_SHORTEN =
"shorten"
- API_OUTPUT_REFER =
"refer"
- API_OUTPUT_EXTEND =
"extend"
Class Attribute Summary collapse
Class Method Summary
collapse
Class Attribute Details
.auth ⇒ Object
Returns the value of attribute auth.
14
15
16
|
# File 'lib/rzabbix/connection.rb', line 14
def auth
@auth
end
|
.credentials ⇒ Object
Returns the value of attribute credentials.
12
13
14
|
# File 'lib/rzabbix/connection.rb', line 12
def credentials
@credentials
end
|
.use_ssl ⇒ Object
Returns the value of attribute use_ssl.
13
14
15
|
# File 'lib/rzabbix/connection.rb', line 13
def use_ssl
@use_ssl
end
|
Class Method Details
.do_request(message) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/rzabbix/connection.rb', line 56
def self.do_request(message)
id = next_request_id
message['id'] = id
message['jsonrpc'] = '2.0'
message_json = JSON.generate(message)
uri = URI.parse(Connection.credentials[:api_url])
http = Net::HTTP.new(uri.host, uri.port)
if (use_ssl)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
request = Net::HTTP::Post.new(uri.request_uri)
request.add_field('Content-Type', 'application/json-rpc')
request.body=(message_json)
responce = http.request(request)
if ( responce.code != "200" ) then
raise ResponseCodeError.new("Responce code from [" + credentials[:api_url] + "] is " + responce.code)
end
responce_body_hash = JSON.parse(responce.body)
if ( error = responce_body_hash['error'] ) then
error_message = error['message']
error_data = error['data']
error_code = error['code']
e_message = "Code: [" + error_code.to_s + "]. Message: [" + error_message +\
"]. Data: [" + error_data + "]."
raise StandardError.new(e_message)
end
result = responce_body_hash['result']
result.respond_to?(:rzbx_recursively_symbolize_keys) ? result.rzbx_recursively_symbolize_keys : result
end
|
.message_for(controller, action, params = {}) ⇒ Object
48
49
50
51
52
53
54
|
# File 'lib/rzabbix/connection.rb', line 48
def self.message_for(controller, action, params = {})
{
'method' => "#{controller}.#{action}",
'params' => params.to_hash.merge(:output=>API_OUTPUT_EXTEND),
'auth' => Connection.auth
}
end
|
.next_request_id ⇒ Object
23
24
25
|
# File 'lib/rzabbix/connection.rb', line 23
def self.next_request_id
@request_id = @request_id ? @request_id+1 : 1
end
|
43
44
45
46
|
# File 'lib/rzabbix/connection.rb', line 43
def self.perform_request(controller, action, params)
message = message_for(controller, action, params)
do_request(message)
end
|
.set_credentials(api_url, api_user, api_password) ⇒ Object
19
20
21
|
# File 'lib/rzabbix/connection.rb', line 19
def self.set_credentials(api_url, api_user, api_password)
self.credentials = {:api_url=>api_url, :api_user=>api_user, :api_password=>api_password}
end
|