Class: Bfire::Aggregator::Zabbix

Inherits:
Object
  • Object
show all
Defined in:
lib/bfire/aggregator/zabbix.rb

Instance Method Summary collapse

Constructor Details

#initialize(session, experiment, opts = {}) ⇒ Zabbix

Returns a new instance of Zabbix.



6
7
8
9
10
11
12
13
14
# File 'lib/bfire/aggregator/zabbix.rb', line 6

def initialize(session, experiment, opts = {})
  @session = session
  @username = opts[:username] || "Admin"
  @password = opts[:password] || "zabbix"
  @experiment = experiment
  @token, @request_id = nil, 0
  @uri = @experiment.uri.to_s+"/zabbix"
  @max_attempts = 5
end

Instance Method Details

#authenticateObject



50
51
52
# File 'lib/bfire/aggregator/zabbix.rb', line 50

def authenticate
  @token = request("user.authenticate", {"user" => @username, "password" => @password})
end

#request(method, params = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bfire/aggregator/zabbix.rb', line 16

def request(method, params = {})
  begin
    authenticate if @token.nil? && method != "user.authenticate"
    @request_id += 1
    q = { "jsonrpc" => "2.0", "auth" => @token, "id" => @request_id,
          "method" => method, "params" => params }
    resource = @session.post(@uri,
      JSON.dump(q),
      :head => {
        'Content-Type' => 'application/json',
        'Accept' => 'application/json'
      }
    )

    # That fucking zabbix API returns "text/plain" as content-type...
    h = JSON.parse(resource.response.body)

    if h['error']
      if h['error']['data'] == "Not authorized"
        @token = nil
        request(method, params)
      else
        raise StandardError, "Received error: #{h.inspect}" if h['error']
      end
    else
      h['result']
    end
  rescue Restfully::HTTP::Error => e
    # retry ad vitam eternam
    sleep 5
    retry
  end
end