Class: ZabbixApi::Client
- Inherits:
-
Object
- Object
- ZabbixApi::Client
- Defined in:
- lib/zabbixapi/client.rb
Instance Attribute Summary collapse
- #options ⇒ Hash readonly
Instance Method Summary collapse
- #_request(body) ⇒ Hash, String
-
#api_request(body) ⇒ Hash, String
Execute Zabbix API requests and return response.
-
#api_version ⇒ String
Returns the API version from the Zabbix Server.
-
#auth ⇒ Hash
Log in to the Zabbix Server and generate an auth token using the API.
- #http_request(body) ⇒ String
- #id ⇒ Integer
-
#initialize(options = {}) ⇒ ZabbixApi::Client
constructor
Initializes a new Client object.
-
#message_json(body) ⇒ String
Convert message body to JSON string for the Zabbix API.
- #pretty_body(body) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ ZabbixApi::Client
Initializes a new Client object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/zabbixapi/client.rb', line 47 def initialize( = {}) = if !ENV['http_proxy'].nil? && [: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 =~ %r{^4.[0|2]\.\d+$} puts "[WARN] Zabbix API version: #{api_version} is not supported by this version of zabbixapi" end @auth_hash = auth end |
Instance Attribute Details
#options ⇒ Hash (readonly)
9 10 11 |
# File 'lib/zabbixapi/client.rb', line 9 def end |
Instance Method Details
#_request(body) ⇒ Hash, String
118 119 120 121 122 123 124 |
# File 'lib/zabbixapi/client.rb', line 118 def _request(body) puts "[DEBUG] Send request: #{body}" if [:debug] result = JSON.parse(http_request(body)) raise ApiError.new("Server answer API error\n #{JSON.pretty_unparse(result['error'])}\n on request:\n #{pretty_body(body)}", result) if result['error'] result['result'] end |
#api_request(body) ⇒ Hash, String
Execute Zabbix API requests and return response
139 140 141 |
# File 'lib/zabbixapi/client.rb', line 139 def api_request(body) _request (body) end |
#api_version ⇒ String
Returns the API version from the Zabbix Server
19 20 21 |
# File 'lib/zabbixapi/client.rb', line 19 def api_version @api_version ||= api_request(method: 'apiinfo.version', params: {}) end |
#auth ⇒ Hash
Log in to the Zabbix Server and generate an auth token using the API
26 27 28 29 30 31 32 33 34 |
# File 'lib/zabbixapi/client.rb', line 26 def auth api_request( method: 'user.login', params: { user: [:user], password: [:password] } ) end |
#http_request(body) ⇒ String
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/zabbixapi/client.rb', line 81 def http_request(body) uri = URI.parse([:url]) # set the time out the default (60) or to what the user passed timeout = [:timeout].nil? ? 60 : [:timeout] puts "[DEBUG] Timeout for request set to #{timeout} seconds" if [:debug] http = if @proxy_uri Net::HTTP.Proxy(@proxy_host, @proxy_port, @proxy_user, @proxy_pass).new(uri.host, uri.port) else Net::HTTP.new(uri.host, uri.port) end if uri.scheme == 'https' http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end http.open_timeout = timeout http.read_timeout = timeout request = Net::HTTP::Post.new(uri.request_uri) request.basic_auth [:http_user], [:http_password] if [: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 [:debug] response.body end |
#id ⇒ Integer
12 13 14 |
# File 'lib/zabbixapi/client.rb', line 12 def id rand(100_000) end |
#message_json(body) ⇒ String
Convert message body to JSON string for the Zabbix API
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/zabbixapi/client.rb', line 66 def (body) = { method: body[:method], params: body[:params], id: id, jsonrpc: '2.0' } [:auth] = @auth_hash unless body[:method] == 'apiinfo.version' || body[:method] == 'user.login' JSON.generate() end |
#pretty_body(body) ⇒ Object
126 127 128 129 130 131 132 133 |
# File 'lib/zabbixapi/client.rb', line 126 def pretty_body(body) parsed_body = JSON.parse(body) # If password is in body hide it parsed_body['params']['password'] = '***' if parsed_body['params'].is_a?(Hash) && parsed_body['params'].key?('password') JSON.pretty_unparse(parsed_body) end |