Zabbix v0.2.0 Zabbix.API View Source

Provides simple wrapper to Zabbix API.

Example usage

Create client

iex(1)> Zabbix.API.create_client("https://zabbix.example.com")
:ok

Log in

iex(2)> Zabbix.API.login("elixir", "elixir")
{:ok, "7959e30884cf778cbf693c66c46a382c"}

Do what you want

iex(3)> Zabbix.API.call("apiinfo.version", %{})
{:ok, %{"id" => 2, "jsonrpc" => "2.0", "result" => "4.2.6"}}

iex(4)> Zabbix.API.call("host.get", %{hostids: [10001, 10042, 10069], output: ["name"]})
{:ok,
 %{
   "id" => 3,
   "jsonrpc" => "2.0",
   "result" => [
     %{"hostid" => "10001", "name" => "ZBX-APP001"},
     %{"hostid" => "10042", "name" => "ZBX-APP042"},
     %{"hostid" => "10069", "name" => "ZBX-APP069"}
   ]
 }}

Log out

iex(5)> Zabbix.API.logout()
{:ok, :deauthorized}

Link to this section Summary

Functions

Performs request to Zabbix API.

Updates session state with new client instance.

Authorizes in Zabbix API using `token` and updates session state with granted token.

Authorizes in Zabbix API using `user` and `password` and updates session state with granted token.

Deauthorizes in Zabbix API and updates session state with `nil` token.

Link to this section Functions

Link to this function

call(method, params, opts \\ [])

View Source

Performs request to Zabbix API.

The function will perform request to specified `method` of Zabbix API with specified `params`.

Session state must be initialized with `create_client/2` before performing requests.

Most of methods requires authorization. You can auth with `login/2` function using login and password or with `login/1` using token.

Examples

iex> Zabbix.API.call("apiinfo.version", %{})
{:ok, %{"id" => 2, "jsonrpc" => "2.0", "result" => "4.2.6"}}

iex> Zabbix.API.call("host.get", %{hostids: [10001, 10042, 10069], output: ["name"]})
{:ok,
 %{
   "id" => 3,
   "jsonrpc" => "2.0",
   "result" => [
     %{"hostid" => "10001", "name" => "ZBX-APP001"},
     %{"hostid" => "10042", "name" => "ZBX-APP042"},
     %{"hostid" => "10069", "name" => "ZBX-APP069"}
   ]
 }}

Additional option `:timeout` can be passed to override defalut client timeout.

Example with timeout override

iex> Zabbix.API.call("apiinfo.version", %{}, timeout: 15)
{:error, :timeout}
Link to this function

create_client(url, timeout \\ 5000)

View Source

Updates session state with new client instance.

The function will set `url` as base URL for all requests with `timeout` milliseconds timeout.

Calling `create_client/2` will always session state.

Examples

iex> Zabbix.API.create_client("http://zabbix.example.com")
:ok

iex> Zabbix.API.create_client("http://example.com/zabbix", 1_000)
:ok

Authorizes in Zabbix API using `token` and updates session state with granted token.

The function will call `call/3` with `token` as parameter and store granted token in session state in case of successful authorization.

Examples

iex> Zabbix.API.login("ea22fa26bf0a446301055920bf2f25a2")
{:ok, "ea22fa26bf0a446301055920bf2f25a2"}

iex> Zabbix.API.login("incorrect_token")
{:error, :unauthorized}

Authorizes in Zabbix API using `user` and `password` and updates session state with granted token.

The function will call `call/3` with `user` and `password` as parameters and store granted token in session state in case of successful authorization.

Examples

iex> Zabbix.API.login("elixir", "correct_password")
{:ok, "ea22fa26bf0a446301055920bf2f25a2"}

iex> Zabbix.API.login("elixir", "incorrect_password")
{:error, :unauthorized}

Deauthorizes in Zabbix API and updates session state with `nil` token.

The function will call `call/3` and store `nil` token in session state in case of successful logout.

Examples

iex> Zabbix.API.logout()
{:ok, :deauthorized}