Class: Zabbix::API

Inherits:
Object
  • Object
show all
Includes:
WrAPI::Authentication, WrAPI::Connection, WrAPI::Request, Authentication, Request::JSONRPC2
Defined in:
lib/zabbix/api.rb

Overview

Note:

This class includes connection, request handling, JSON-RPC 2.0 support, and authentication from WrAPI.

This class is the core API client that interfaces with Zabbix. It manages configuration options, API connections, and helper utilities.

Examples:

Create a new Zabbix API client:

api = Zabbix::API.new(endpoint: 'https://zabbix.example.com/api_jsonrpc.php', api_key: 'your_key')

Direct Known Subclasses

Client

Constant Summary

Constants included from Request::JSONRPC2

Request::JSONRPC2::ZABBIX_ENDPOINT

Instance Method Summary collapse

Methods included from Authentication

#login

Methods included from Request::JSONRPC2

#rpc_call

Constructor Details

#initialize(options = {}) ⇒ API

Initialize a new Zabbix::API instance and copy settings from the singleton configuration.

Parameters:

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

    Configuration options for the Zabbix API client. Options override any default values set in the singleton Zabbix configuration.

Options Hash (options):

  • :endpoint (String)

    The base URL of the Zabbix API.

  • :user_agent (String)

    The User-Agent header sent with requests.

  • :timeout (Integer)

    Request timeout in seconds.



33
34
35
36
37
38
# File 'lib/zabbix/api.rb', line 33

def initialize(options = {})
  options = Zabbix.options.merge(options)  # Merge provided options with default options
  WrAPI::Configuration::VALID_OPTIONS_KEYS.each do |key|
    send("#{key}=", options[key])  # Assign each configuration option dynamically
  end
end

Instance Method Details

#configHash

Retrieve the current configuration for the API client.

Examples:

Get the client configuration:

api = Zabbix::API.new
puts api.config  # => { endpoint: "https://...", user_agent: "...", timeout: 60 }

Returns:

  • (Hash)

    A hash containing the current configuration options.



46
47
48
49
50
51
52
# File 'lib/zabbix/api.rb', line 46

def config
  conf = {}
  WrAPI::Configuration::VALID_OPTIONS_KEYS.each do |key|
    conf[key] = send(key)  # Build a hash of current configuration values
  end
  conf
end

#zabbix_clock(secs) ⇒ DateTime

Convert a Zabbix clock timestamp to a Ruby DateTime object.

Examples:

Convert Zabbix clock to DateTime:

api.zabbix_clock(1684500000)  # => #<DateTime: 2023-05-19T09:20:00+00:00>

Parameters:

  • secs (String, Integer)

    The Zabbix clock time in seconds since the Unix epoch.

Returns:

  • (DateTime)

    A DateTime object representing the input time.



61
62
63
# File 'lib/zabbix/api.rb', line 61

def zabbix_clock(secs)
  Time.at(secs.to_i).to_datetime  # Convert seconds since Unix epoch to DateTime
end