Class: Rubix::Connection
- Inherits:
-
Object
- Object
- Rubix::Connection
- Includes:
- Logs
- Defined in:
- lib/rubix/connection.rb
Instance Attribute Summary collapse
-
#auth ⇒ Object
readonly
Returns the value of attribute auth.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#request_id ⇒ Object
readonly
Returns the value of attribute request_id.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#uri ⇒ Object
Returns the value of attribute uri.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #authorization_params ⇒ Object
- #authorize! ⇒ Object
- #authorized? ⇒ Boolean
- #host_with_port ⇒ Object
-
#initialize(uri_or_string, username = nil, password = nil) ⇒ Connection
constructor
A new instance of Connection.
- #raw_post_request(raw_params) ⇒ Object
- #request(method, params) ⇒ Object
- #send_raw_request(raw_params) ⇒ Object
- #till_response(attempt = 1, max_attempts = 5, &block) ⇒ Object
Methods included from Logs
#debug, #error, #fatal, #info, #log_name, #warn
Constructor Details
#initialize(uri_or_string, username = nil, password = nil) ⇒ Connection
Returns a new instance of Connection.
15 16 17 18 19 20 |
# File 'lib/rubix/connection.rb', line 15 def initialize uri_or_string, username=nil, password=nil self.uri = uri_or_string @username = username || uri.user @password = password || uri.password @request_id = 0 end |
Instance Attribute Details
#auth ⇒ Object (readonly)
Returns the value of attribute auth.
13 14 15 |
# File 'lib/rubix/connection.rb', line 13 def auth @auth end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
13 14 15 |
# File 'lib/rubix/connection.rb', line 13 def password @password end |
#request_id ⇒ Object (readonly)
Returns the value of attribute request_id.
13 14 15 |
# File 'lib/rubix/connection.rb', line 13 def request_id @request_id end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
13 14 15 |
# File 'lib/rubix/connection.rb', line 13 def server @server end |
#uri ⇒ Object
Returns the value of attribute uri.
13 14 15 |
# File 'lib/rubix/connection.rb', line 13 def uri @uri end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
13 14 15 |
# File 'lib/rubix/connection.rb', line 13 def username @username end |
Instance Method Details
#authorization_params ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rubix/connection.rb', line 36 def { :jsonrpc => "2.0", :id => request_id, :method => "user.login", :params => { :user => username, :password => password } } end |
#authorize! ⇒ Object
48 49 50 51 52 53 |
# File 'lib/rubix/connection.rb', line 48 def response = till_response { send_raw_request() } raise AuthenticationError.new("Could not authenticate with Zabbix API at #{uri}: #{response.}") if response.error? raise AuthenticationError.new("Malformed response from Zabbix API: #{response.body}") unless response.string? @auth = response.result end |
#authorized? ⇒ Boolean
32 33 34 |
# File 'lib/rubix/connection.rb', line 32 def !auth.nil? end |
#host_with_port ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/rubix/connection.rb', line 92 def host_with_port if uri.port.nil? || uri.port.to_i == 80 uri.host else "#{uri.host}:#{uri.port}" end end |
#raw_post_request(raw_params) ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/rubix/connection.rb', line 83 def raw_post_request raw_params json_body = raw_params.to_json Rubix.logger.log(Logger::DEBUG, json_body, 'SEND') if Rubix.logger Net::HTTP::Post.new(uri.path).tap do |req| req['Content-Type'] = 'application/json-rpc' req.body = json_body end end |
#request(method, params) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/rubix/connection.rb', line 69 def request method, params unless till_response do raw_params = { :jsonrpc => "2.0", :id => request_id, :method => method, :params => params, :auth => auth } send_raw_request(raw_params) end end |
#send_raw_request(raw_params) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/rubix/connection.rb', line 100 def send_raw_request raw_params @request_id += 1 begin raw_response = server.request(raw_post_request(raw_params)) rescue NoMethodError, SocketError => e raise RequestError.new("Could not connect to Zabbix server at #{host_with_port}") end raw_response end |
#till_response(attempt = 1, max_attempts = 5, &block) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rubix/connection.rb', line 55 def till_response attempt=1, max_attempts=5, &block response = block.call Rubix.logger.log(Logger::DEBUG, response.body, 'RECV') if Rubix.logger case when response.code.to_i >= 500 && attempt <= max_attempts sleep 1 # FIXME make the sleep time configurable... till_response(attempt + 1, max_attempts, &block) when response.code.to_i >= 500 raise ConnectionError.new("Too many consecutive failed requests (#{max_attempts}) to the Zabbix API at (#{uri}).") else Response.new(response) end end |