Class: Rubix::Connection

Inherits:
Object
  • Object
show all
Includes:
Logs
Defined in:
lib/rubix/connection.rb

Overview

Wraps and abstracts the process of connecting to a Zabbix API.

Constant Summary collapse

'zbx_sessionid'
CONTENT_TYPE =

The content type header to send when emulating a browser.

'multipart/form-data'

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • uri_or_string (URI, String)

    the address of the Zabbix API server to connect to

  • username (String) (defaults to: nil)

    the username of an existing Zabbix API User account with API access

  • password (String) (defaults to: nil)

    the password for this account



55
56
57
58
59
60
# File 'lib/rubix/connection.rb', line 55

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

#authString (readonly)

API for this session.

Returns:

  • (String)

    the authentication token provided by the Zabbix



30
31
32
# File 'lib/rubix/connection.rb', line 30

def auth
  @auth
end

#last_responseRubix::Response (readonly)

Returns the last response from the Zabbix API – useful for logging purposes.

Returns:

  • (Rubix::Response)

    the last response from the Zabbix API – useful for logging purposes



42
43
44
# File 'lib/rubix/connection.rb', line 42

def last_response
  @last_response
end

#passwordString (readonly)

Returns the password of the Zabbix account used to authenticate.

Returns:

  • (String)

    the password of the Zabbix account used to authenticate



39
40
41
# File 'lib/rubix/connection.rb', line 39

def password
  @password
end

#request_idFixnum (readonly)

Returns the ID of the next request that will be sent.

Returns:

  • (Fixnum)

    the ID of the next request that will be sent.



33
34
35
# File 'lib/rubix/connection.rb', line 33

def request_id
  @request_id
end

#serverNet::HTTP (readonly)

Returns the HTTP server backing the Zabbix API.

Returns:

  • (Net::HTTP)

    the HTTP server backing the Zabbix API.



26
27
28
# File 'lib/rubix/connection.rb', line 26

def server
  @server
end

#uriURI

Returns The URI for the Zabbix API.

Returns:

  • (URI)

    The URI for the Zabbix API.



23
24
25
# File 'lib/rubix/connection.rb', line 23

def uri
  @uri
end

#usernameString (readonly)

Returns the username of the Zabbix account used to authenticate.

Returns:

  • (String)

    the username of the Zabbix account used to authenticate



36
37
38
# File 'lib/rubix/connection.rb', line 36

def username
  @username
end

Instance Method Details

#authorize!Object

Force the connection to execute an authorization request and renew (or set) the authorization token.



111
112
113
114
115
116
# File 'lib/rubix/connection.rb', line 111

def authorize!
  response = Response.new(till_response { send_api_request(authorization_params) })
  raise AuthenticationError.new("Could not authenticate with Zabbix API at #{uri}: #{response.error_message}") 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?

Returns:

  • (Boolean)


105
106
107
# File 'lib/rubix/connection.rb', line 105

def authorized?
  !auth.nil?
end

#request(method, params) ⇒ Rubix::Response

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' }

Parameters:

  • method (String)

    the name of the Zabbix API method

  • params (Hash, Array)

    parameters for the method call

Returns:



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rubix/connection.rb', line 74

def request method, params
  authorize! unless authorized?
  response = till_response do
    send_api_request :jsonrpc => "2.0",
    :id      => request_id,
    :method  => method,
    :params  => params,
    :auth    => auth
  end
  Response.new(response)
end

#web_request(verb, path, data = {}) ⇒ Net::HTTP::Response

Send a request to the Zabbix web application. The request is designed to emulate a web browser.

Any values in data which are file handles will trigger a multipart POST request, uploading those files.

Parameters:

  • verb (String)

    one of “GET” or “POST”

  • path (String)

    the path to send the request to

  • data (Hash) (defaults to: {})

    the data to include in the request

Returns:

  • (Net::HTTP::Response)


96
97
98
99
100
101
# File 'lib/rubix/connection.rb', line 96

def web_request verb, path, data={}
  authorize! unless authorized?
  till_response do
    send_web_request(verb, path, data)
  end
end