Class: Rubix::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#authObject (readonly)

Returns the value of attribute auth.



13
14
15
# File 'lib/rubix/connection.rb', line 13

def auth
  @auth
end

#passwordObject (readonly)

Returns the value of attribute password.



13
14
15
# File 'lib/rubix/connection.rb', line 13

def password
  @password
end

#request_idObject (readonly)

Returns the value of attribute request_id.



13
14
15
# File 'lib/rubix/connection.rb', line 13

def request_id
  @request_id
end

#serverObject (readonly)

Returns the value of attribute server.



13
14
15
# File 'lib/rubix/connection.rb', line 13

def server
  @server
end

#uriObject

Returns the value of attribute uri.



13
14
15
# File 'lib/rubix/connection.rb', line 13

def uri
  @uri
end

#usernameObject (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_paramsObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rubix/connection.rb', line 36

def authorization_params
  {
    :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 authorize!
  response = till_response { send_raw_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

Returns:

  • (Boolean)


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

def authorized?
  !auth.nil?
end

#host_with_portObject



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
  authorize! unless authorized?
  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