class ZabbixRPCClient

Attributes

debug[R]
uri[R]

Public Class Methods

new(options) click to toggle source
# File lib/zapix/zabbix_rpc_client.rb, line 8
def initialize(options)
  @uri = URI.parse(options[:service_url])
  @username = options[:username]
  @password = options[:password]
  @debug = options[:debug]
  @auth_token = authenticate
end

Public Instance Methods

authenticate() click to toggle source
# File lib/zapix/zabbix_rpc_client.rb, line 49
def authenticate
  user_login('user' => @username, 'password' => @password)
  user_login('user' => @username, 'password' => @password)
end
http_post_request(post_body) click to toggle source
# File lib/zapix/zabbix_rpc_client.rb, line 33
def http_post_request(post_body)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  request = Net::HTTP::Post.new(uri.request_uri)
  request.content_type = 'application/json'
  request.body = post_body
  puts "[DEBUG] Send request: #{request.body}" if debug

  begin
    return http.request(request).body
  rescue
    puts "[DEBUG] Retrying sending request: #{request.body}" if debug
    retry
  end
end
id() click to toggle source
# File lib/zapix/zabbix_rpc_client.rb, line 54
def id
  rand(100_000)
end
map_name(name) click to toggle source
# File lib/zapix/zabbix_rpc_client.rb, line 58
def map_name(name)
  name.to_s.sub('_', '.')
end
method_missing(name, *args) click to toggle source
# File lib/zapix/zabbix_rpc_client.rb, line 16
def method_missing(name, *args)
  method_name = map_name(name)

  post_body = {
    'method' => method_name,
    'params' => args.first,
    'id' => id,
    'jsonrpc' => '2.0',
    'auth' => @auth_token
  }.to_json

  resp = JSON.parse(http_post_request(post_body))
  raise JSONRPCError, resp['error'] if resp['error']
  puts "[DEBUG] Get answer: #{resp}" if debug
  resp['result']
end