Class: ZabbixApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbixapi/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/zabbixapi/client.rb', line 28

def initialize(options = {})
  @options = options
  if ENV['http_proxy'] != nil && options[:no_proxy] != true
    @proxy_uri = URI.parse(ENV['http_proxy'])
    @proxy_host = @proxy_uri.host
    @proxy_port = @proxy_uri.port
    @proxy_user, @proxy_pass = @proxy_uri.userinfo.split(/:/) if @proxy_uri.userinfo
  end
  unless api_version =~ /(2\.4|3\.[02])\.\d+/
    raise ApiError.new("Zabbix API version: #{api_version} is not support by this version of zabbixapi")
  end
  @auth_hash = auth
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/zabbixapi/client.rb', line 8

def options
  @options
end

Instance Method Details

#_request(body) ⇒ Object

Raises:



87
88
89
90
91
92
# File 'lib/zabbixapi/client.rb', line 87

def _request(body)
  puts "[DEBUG] Send request: #{body}" if @options[:debug]
  result = JSON.parse(http_request(body))
  raise ApiError.new("Server answer API error\n #{JSON.pretty_unparse(result['error'])}\n on request:\n #{JSON.pretty_unparse(JSON.parse(body))}", result) if result['error']
  result['result']
end

#api_request(body) ⇒ Object



94
95
96
# File 'lib/zabbixapi/client.rb', line 94

def api_request(body)
  _request message_json(body)
end

#api_versionObject



14
15
16
# File 'lib/zabbixapi/client.rb', line 14

def api_version
  @version ||= api_request(:method => "apiinfo.version", :params => {})
end

#authObject



18
19
20
21
22
23
24
25
26
# File 'lib/zabbixapi/client.rb', line 18

def auth
  api_request(
    :method => 'user.login',
    :params => {
      :user     => @options[:user],
      :password => @options[:password],
    }
  )
end

#http_request(body) ⇒ Object

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/zabbixapi/client.rb', line 55

def http_request(body)
  uri = URI.parse(@options[:url])
  # set the time out the default (60) or to what the user passed
  @options[:timeout] == nil ? timeout = 60 : timeout = @options[:timeout]
  puts "[DEBUG] Timeout for request set to #{timeout} seconds" if @options[:debug]

  unless @proxy_uri.nil?
    http = Net::HTTP.Proxy(@proxy_host, @proxy_port, @proxy_user, @proxy_pass).new(uri.host, uri.port)

    if uri.scheme == 'https'
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
  else
    http = Net::HTTP.new(uri.host, uri.port)
    if uri.scheme == 'https'
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
  end
  http.read_timeout = timeout

  request = Net::HTTP::Post.new(uri.request_uri)
  request.basic_auth @options[:http_user], @options[:http_password] if @options[:http_user]
  request.add_field('Content-Type', 'application/json-rpc')
  request.body = body
  response = http.request(request)
  raise HttpError.new("HTTP Error: #{response.code} on #{@options[:url]}", response) unless response.code == '200'
  puts "[DEBUG] Get answer: #{response.body}" if @options[:debug]
  response.body
end

#idObject



10
11
12
# File 'lib/zabbixapi/client.rb', line 10

def id
  rand(100000)
end

#message_json(body) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/zabbixapi/client.rb', line 42

def message_json(body)
  message = {
    :method  => body[:method],
    :params  => body[:params],
    :id      => id,
    :jsonrpc => '2.0'
  }

  message[:auth] = @auth_hash unless (body[:method] == 'apiinfo.version' or body[:method] == 'user.login')

  JSON.generate(message)
end