Class: ZabbixManager::Client
- Inherits:
-
Object
- Object
- ZabbixManager::Client
- Defined in:
- lib/zabbix_manager/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, Hash
Returns the API version from the Zabbix Server.
-
#auth ⇒ Hash, String
Log in to the Zabbix Server and generate an auth token using the API.
-
#debug? ⇒ boolean
ZabbixManager::Basic.log does not like @client.options.
- #http_request(body) ⇒ String
- #id ⇒ Integer
-
#initialize(options = {}) ⇒ ZabbixManager::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 = {}) ⇒ ZabbixManager::Client
Initializes a new Client object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/zabbix_manager/client.rb', line 56 def initialize( = {}) @options = 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 if api_version.match?(/^7\.\d+\.\d+$/) = "Zabbix API version: #{api_version} is not supported by this version of zabbix_manager" if @options[:ignore_version] puts "[WARNING] #{}" if @options[:debug] else raise ZabbixManager::ManagerError, end end @auth_hash = auth puts "[DEBUG] Auth token: #{@auth_hash}" if @options[:debug] end |
Instance Attribute Details
#options ⇒ Hash (readonly)
11 12 13 |
# File 'lib/zabbix_manager/client.rb', line 11 def @options end |
Instance Method Details
#_request(body) ⇒ Hash, String
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/zabbix_manager/client.rb', line 135 def _request(body) result = JSON.parse(http_request(body)) # 异常信息抛出 if result["error"] raise ManagerError.new( "Server answer API error\n #{JSON.pretty_unparse(result["error"])}\n on request:\n #{pretty_body(body)}", result ) end result["result"] end |
#api_request(body) ⇒ Hash, String
Execute Zabbix API requests and return response
163 164 165 166 |
# File 'lib/zabbix_manager/client.rb', line 163 def api_request(body) # ap body _request (body) end |
#api_version ⇒ String, Hash
Returns the API version from the Zabbix Server
21 22 23 |
# File 'lib/zabbix_manager/client.rb', line 21 def api_version api_request(method: "apiinfo.version", params: {}) end |
#auth ⇒ Hash, String
Log in to the Zabbix Server and generate an auth token using the API
28 29 30 31 32 33 34 35 36 |
# File 'lib/zabbix_manager/client.rb', line 28 def auth api_request( method: "user.login", params: { user: @options[:user], password: @options[:password] } ) end |
#debug? ⇒ boolean
ZabbixManager::Basic.log does not like @client.options
41 42 43 |
# File 'lib/zabbix_manager/client.rb', line 41 def debug? !@options || @options[:debug] end |
#http_request(body) ⇒ String
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/zabbix_manager/client.rb', line 98 def http_request(body) uri = URI.parse(@options[:url]) # set the time out the default (60) or to what the user passed timeout = @options[:timeout].nil? ? 60 : @options[:timeout] puts "[DEBUG] Timeout for request set to #{timeout} seconds" if @options[: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 @options[:http_user], @options[:http_password] if @options[:http_user] request.add_field("Content-Type", "application/json-rpc") request.body = body puts "[DEBUG] HTTP request params: #{request.body}" if @options[:debug] response = http.request(request) raise HttpError.new("HTTP Error: #{response.code} on #{@options[:url]}", response) unless response.code == "200" puts "[DEBUG] HTTP response answer: #{response.body}" if @options[:debug] response.body end |
#id ⇒ Integer
14 15 16 |
# File 'lib/zabbix_manager/client.rb', line 14 def id rand 10_000 end |
#message_json(body) ⇒ String
Convert message body to JSON string for the Zabbix API
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/zabbix_manager/client.rb', line 82 def (body) = { method: body[:method], params: body[:params], id: id, jsonrpc: "2.0" } # 除登录认证和获取版本后之外,都需要携带TOKEN查询 [:auth] = @auth_hash unless body[:method] == "apiinfo.version" || body[:method] == "user.login" JSON() end |
#pretty_body(body) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/zabbix_manager/client.rb', line 147 def pretty_body(body) parsed_body = JSON.parse(body) # If password is in body hide it if parsed_body["params"].is_a?(Hash) && parsed_body["params"].key?("password") parsed_body["params"]["password"] = "***" end JSON.pretty_unparse(parsed_body) end |