Class: Rubix::Connection
- Inherits:
-
Object
- Object
- Rubix::Connection
- Includes:
- Logs
- Defined in:
- lib/rubix/connection.rb
Overview
Wraps and abstracts the process of connecting to a Zabbix API.
Instance Attribute Summary collapse
-
#auth ⇒ String
readonly
API for this session.
-
#last_response ⇒ Rubix::Response
readonly
The last response from the Zabbix API – useful for logging purposes.
-
#password ⇒ String
readonly
The password of the Zabbix account used to authenticate.
-
#request_id ⇒ Fixnum
readonly
The ID of the next request that will be sent.
-
#server ⇒ Net::HTTP
readonly
The HTTP server backing the Zabbix API.
-
#uri ⇒ URI
The URI for the Zabbix API.
-
#username ⇒ String
readonly
The username of the Zabbix account used to authenticate.
Instance Method Summary collapse
-
#authorize! ⇒ Object
Force the connection to execute an authorization request and renew (or set) the authorization token.
-
#authorized? ⇒ Boolean
Has this connection already been authorized and provided with a authorization token from the Zabbix API?.
-
#initialize(uri_or_string, username = nil, password = nil) ⇒ Connection
constructor
Set up a connection to a Zabbix API.
-
#request(method, params) ⇒ Object
Send a request to the Zabbix API.
Methods included from Logs
#debug, #error, #fatal, #info, #warn
Constructor Details
#initialize(uri_or_string, username = nil, password = nil) ⇒ Connection
Set up a connection to a Zabbix API.
The uri_or_string
can be either a string or a URI
object.
The username
and password
provided must correspond to an existing Zabbix account with API access enabled.
47 48 49 50 51 52 |
# File 'lib/rubix/connection.rb', line 47 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 ⇒ String (readonly)
API for this session.
22 23 24 |
# File 'lib/rubix/connection.rb', line 22 def auth @auth end |
#last_response ⇒ Rubix::Response (readonly)
Returns the last response from the Zabbix API – useful for logging purposes.
34 35 36 |
# File 'lib/rubix/connection.rb', line 34 def last_response @last_response end |
#password ⇒ String (readonly)
Returns the password of the Zabbix account used to authenticate.
31 32 33 |
# File 'lib/rubix/connection.rb', line 31 def password @password end |
#request_id ⇒ Fixnum (readonly)
Returns the ID of the next request that will be sent.
25 26 27 |
# File 'lib/rubix/connection.rb', line 25 def request_id @request_id end |
#server ⇒ Net::HTTP (readonly)
Returns the HTTP server backing the Zabbix API.
18 19 20 |
# File 'lib/rubix/connection.rb', line 18 def server @server end |
#uri ⇒ URI
Returns The URI for the Zabbix API.
15 16 17 |
# File 'lib/rubix/connection.rb', line 15 def uri @uri end |
#username ⇒ String (readonly)
Returns the username of the Zabbix account used to authenticate.
28 29 30 |
# File 'lib/rubix/connection.rb', line 28 def username @username end |
Instance Method Details
#authorize! ⇒ Object
Force the connection to execute an authorization request and renew (or set) the authorization token.
88 89 90 91 92 93 |
# File 'lib/rubix/connection.rb', line 88 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
Has this connection already been authorized and provided with a authorization token from the Zabbix API?
82 83 84 |
# File 'lib/rubix/connection.rb', line 82 def !auth.nil? end |
#request(method, params) ⇒ Object
Send a request to the Zabbix API. Will return a Rubix::Response object.
Documentation on what methods and parameters are available can be found in the Zabbix API documentation
Rubix.connection.request 'host.get', 'filter' => { 'host' => 'foobar' }
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rubix/connection.rb', line 66 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 |