Class: ZabbixApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbixapi/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ZabbixApi::Client

Initializes a new Client object

Parameters:

  • options (Hash) (defaults to: {})
  • opts (Hash)

    a customizable set of options



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/zabbixapi/client.rb', line 66

def initialize(options = {})
  @options = options
  if !ENV['http_proxy'].nil? && options[: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{^7.[0|2]\.\d+$}
    message = "Zabbix API version: #{api_version} is not supported by this version of zabbixapi"
    if @options[:ignore_version]
      puts "[WARNING] #{message}" if @options[:debug]
    else
      raise ZabbixApi::ApiError.new(message)
    end
  end

  @auth_hash = auth
end

Instance Attribute Details

#optionsHash (readonly)

Parameters:

  • options (Hash)
  • opts (Hash)

    a customizable set of options

Returns:

  • (Hash)


9
10
11
# File 'lib/zabbixapi/client.rb', line 9

def options
  @options
end

Instance Method Details

#_request(body) ⇒ Hash, String

Parameters:

  • body (String)

Returns:

  • (Hash, String)

Raises:



140
141
142
143
144
145
146
# File 'lib/zabbixapi/client.rb', line 140

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

  result['result']
end

#api_request(body) ⇒ Hash, String

Execute Zabbix API requests and return response

Parameters:

  • body (Hash)

Returns:

  • (Hash, String)


161
162
163
# File 'lib/zabbixapi/client.rb', line 161

def api_request(body)
  _request body
end

#api_versionString

Returns the API version from the Zabbix Server

Returns:

  • (String)


19
20
21
22
# File 'lib/zabbixapi/client.rb', line 19

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

#authHash

Log in to the Zabbix Server and generate an auth token using the API

Returns:

  • (Hash)


27
28
29
30
31
32
33
34
35
# File 'lib/zabbixapi/client.rb', line 27

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

#debug?boolean

ZabbixApi::Basic.log does not like @client.options

Returns:

  • (boolean)


40
41
42
# File 'lib/zabbixapi/client.rb', line 40

def debug?
  return ! @options || @options[:debug]
end

#http_request(body) ⇒ String

Parameters:

  • body (String)

Returns:

  • (String)

Raises:



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
132
133
134
135
136
# File 'lib/zabbixapi/client.rb', line 103

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.add_field('Content-Type', 'application/json-rpc')
  request.add_field('Authorization', "Bearer #{@auth_hash}") unless body[:method] == 'apiinfo.version' || body[:method] == 'user.login'
  request.body = message_json(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 @options[:debug]
  response.body
end

#idInteger

Returns:

  • (Integer)


12
13
14
# File 'lib/zabbixapi/client.rb', line 12

def id
  rand(100_000)
end

#logoutBoolean

Log out from the Zabbix Server

Returns:

  • (Boolean)


48
49
50
51
52
53
# File 'lib/zabbixapi/client.rb', line 48

def logout
  api_request(
	 :method => 'user.logout',
	 :params => []
  )
end

#message_json(body) ⇒ String

Convert message body to JSON string for the Zabbix API

Parameters:

  • body (Hash)

Returns:

  • (String)


90
91
92
93
94
95
96
97
98
99
# File 'lib/zabbixapi/client.rb', line 90

def message_json(body)
  message = {
    method: body[:method],
    params: body[:params],
    id: id,
    jsonrpc: '2.0'
  }

  JSON.generate(message)
end

#pretty_body(body) ⇒ Object



148
149
150
151
152
153
154
155
# File 'lib/zabbixapi/client.rb', line 148

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