Sha256: bd37dae577fd2f8620e0a1969b97c33e96270a2692583debe42e6dc691e55b78

Contents?: true

Size: 1.97 KB

Versions: 3

Compression:

Stored size: 1.97 KB

Contents

require 'json'
require 'net/http'

class ZabbixApi
  class Client

    def id
      Random.rand(100000)
    end

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

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

    def initialize(options = {})
      @options = options
      @auth_hash = auth
      unless ENV['http_proxy'].nil?
        @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
    end

    def message_json(body)
      message = {
        :method  => body[:method],
        :params  => body[:params],
        :auth    => @auth_hash,
        :id      => id,
        :jsonrpc => '2.0'
      }
      JSON.generate(message)
    end

    def http_request(body)
      uri = URI.parse(@options[:url])
      unless @proxy_uri.nil? 
        http = Net::HTTP.new(uri.host, uri.port)
      else
        http = Net::HTTP.Proxy(@proxy_host, @proxy_port, @proxy_user, @proxy_pass).new(uri.host, uri.port)
      end
      request = Net::HTTP::Post.new(uri.request_uri)
      request.add_field('Content-Type', 'application/json-rpc')
      request.body = body
      response = http.request(request)
      raise "HTTP Error: #{response.code} on #{@options[:url]}" unless response.code == "200"
      puts "[DEBUG] Get answer: #{response.body}" if @options[:debug]
      response.body
    end

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

    def api_request(body)
      _request message_json(body)
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
zabbixapi-0.4.5 lib/zabbixapi/client.rb
zabbixapi-0.4.4 lib/zabbixapi/client.rb
zabbixapi-0.4.3 lib/zabbixapi/client.rb