Class: ZabbixApi::Client
- Inherits:
-
Object
- Object
- ZabbixApi::Client
- Defined in:
- lib/zabbixapi/client.rb
Instance Method Summary collapse
- #_request(body) ⇒ Object
- #api_request(body) ⇒ Object
- #api_version ⇒ Object
- #auth ⇒ Object
- #http_request(body) ⇒ Object
- #id ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #message_json(body) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/zabbixapi/client.rb', line 25 def initialize( = {}) @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 |
Instance Method Details
#_request(body) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/zabbixapi/client.rb', line 63 def _request(body) puts "[DEBUG] Send request: #{body}" if @options[:debug] result = JSON.parse(http_request(body)) raise "Server answer API error:\n #{JSON.pretty_unparse(result['error'])}\n on request:\n #{JSON.pretty_unparse(JSON.parse(body))}" if result['error'] result['result'] end |
#api_request(body) ⇒ Object
70 71 72 |
# File 'lib/zabbixapi/client.rb', line 70 def api_request(body) _request (body) end |
#api_version ⇒ Object
11 12 13 |
# File 'lib/zabbixapi/client.rb', line 11 def api_version @version ||= api_request(:method => "apiinfo.version", :params => {}) end |
#auth ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/zabbixapi/client.rb', line 15 def auth api_request( :method => 'user.authenticate', :params => { :user => @options[:user], :password => @options[:password], } ) end |
#http_request(body) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/zabbixapi/client.rb', line 47 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 |
#id ⇒ Object
7 8 9 |
# File 'lib/zabbixapi/client.rb', line 7 def id Random.rand(100000) end |
#message_json(body) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/zabbixapi/client.rb', line 36 def (body) = { :method => body[:method], :params => body[:params], :auth => @auth_hash, :id => id, :jsonrpc => '2.0' } JSON.generate() end |