class Zabby::Connection

Constants

JSONRPC_SCRIPT

Name of the Zabbix RPC script

Attributes

auth[R]
password[R]
proxy_host[R]
proxy_password[R]
proxy_user[R]
request_id[R]
request_path[R]
uri[R]
user[R]

Public Class Methods

new() click to toggle source
# File lib/rbZabbix/connection.rb, line 14
def initialize
  reset
end

Public Instance Methods

authenticate() click to toggle source

@return [Authentication key]

# File lib/rbZabbix/connection.rb, line 52
def authenticate
  auth_message = format_message('user', 'login',
                                'user' => @user,
                                'password' => @password)
  @auth = query_zabbix_rpc(auth_message)
rescue Zabby::APIError
  # Older Zabbix 1.8.x used a different authentication method. You have to guess...
  auth_message = format_message('user', 'authenticate',
                                'user' => @user,
                                'password' => @password)
  @auth = query_zabbix_rpc(auth_message)
rescue Exception => e
  @auth = nil
  raise e
end
format_exception(zabbix_response) click to toggle source

Raise a Zabby exception @param [Hash] zabbix_response JSON formatted Zabbix response. @raise [Zabby::AuthenticationError] Authentication error. @raise [zabby::APIError] Generic Zabbix Web Services error.

# File lib/rbZabbix/connection.rb, line 115
def format_exception(zabbix_response)
  error = zabbix_response['error']
  error_message = error['message']
  error_data = error['data']
  error_code = error['code']

  if error_data == "Login name or password is incorrect"
    raise AuthenticationError.new(error_message, error_code, error_data)
  else
    raise APIError.new(error_message, error_code, error_data)
  end
end
format_message(element, action, params = {}) click to toggle source
# File lib/rbZabbix/connection.rb, line 68
def format_message(element, action, params = {})
  {
      'jsonrpc' => '2.0',
      'id' => next_request_id,
      'method' => "#{element}.#{action}",
      'params' => params,
      'auth' => @auth
  }
end
http() click to toggle source

Prepare http object

# File lib/rbZabbix/connection.rb, line 98
def http
  if @proxy_host
    http = Net::HTTP::Proxy(@proxy_host.host, @proxy_host.port, @proxy_user, @proxy_password).new(@uri.host, @uri.port)
  else
    http = Net::HTTP.new(@uri.host, @uri.port)
  end
  if @uri.scheme == "https"
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  http
end
logged_in?() click to toggle source
# File lib/rbZabbix/connection.rb, line 43
def logged_in?
  !@auth.nil?
end
login(config) click to toggle source
# File lib/rbZabbix/connection.rb, line 24
def login(config)
  return @auth if @auth
  
  @uri = URI.parse(config.server)
  @user = config.user
  @password = config.password
  if config.proxy_host
    @proxy_host = URI.parse(config.proxy_host)
    @proxy_user = config.proxy_user
    @proxy_password = config.proxy_password
  end
  @request_path = @uri.path[-4,4] == '.php' ? @uri.path : @uri.path + JSONRPC_SCRIPT
  authenticate
end
logout() click to toggle source
# File lib/rbZabbix/connection.rb, line 39
def logout
  reset
end
next_request_id() click to toggle source
# File lib/rbZabbix/connection.rb, line 47
def next_request_id
  @request_id += 1
end
perform_request(element, action, params) click to toggle source

Perform an authenticated request @return [Object] The Zabbix response (Hash, Boolean, etc.) in JSON format.

# File lib/rbZabbix/connection.rb, line 80
def perform_request(element, action, params)
  raise AuthenticationError.new("Not logged in") if !logged_in?

  message = format_message(element, action, params)
  query_zabbix_rpc(message)
end
query_zabbix_rpc(message) click to toggle source

Query the Zabbix Web Services and extract the JSON response. @param [Hash] message request in JSON format. @return [Object] The Zabbix response (Hash, Boolean, etc.) in JSON format. @raise [Zabby::ResponseCodeError] HTTP error.

# File lib/rbZabbix/connection.rb, line 132
def query_zabbix_rpc(message)
  # Send the request!
  http_response = http.request(request(message))

  # Check for HTTP errors.
  if http_response.code != "200"
    raise ResponseCodeError.new("Error from #{@uri}", http_response.code, http_response.body)
  end

  zabbix_response = JSON.parse(http_response.body)

  # Check for Zabbix errors.
  if zabbix_response['error']
    format_exception(zabbix_response)
  end

  zabbix_response['result']
end
request(message) click to toggle source

Prepare a JSON request HTTP Post format @param [Hash] message A hash with all parameters for the Zabbix web service. @return [Net::HTTP::Post] Message ready to be POSTed.

# File lib/rbZabbix/connection.rb, line 90
def request(message)
  req = Net::HTTP::Post.new(@request_path)
  req.add_field('Content-Type', 'application/json-rpc')
  req.body = JSON.generate(message)
  req
end
reset() click to toggle source
# File lib/rbZabbix/connection.rb, line 18
def reset
  @uri = @user = @password = @proxy_host = @proxy_user = @proxy_password = nil
  @request_id = 0
  @auth = nil
end