UnifiApi.Network.Alarms (UnifiApi v0.4.0)

Copy Markdown View Source

UniFi Network API (v1) — alarms.

Returns active and archived alarms — the entries that show in the controller dashboard's "Alerts" pane: gateway down, AP disconnected, IDS detection, threshold crossings, etc.

Requires cookie + CSRF authentication. See UnifiApi.Auth.Cookie.

Alarm fields

  • _id, key, time, datetime, site_id
  • archived — boolean
  • msg — human-readable description
  • subsystem"wlan", "lan", "wan", "vpn", "system", "ips", "alarm"
  • severity"info", "warn", "critical"
  • For IPS alarms: app_proto, catname, dest_ip, src_ip, proto, signature, usgip (gateway IP), inner_alert_action

Note: Like Events, the field set comes from community/unpoller documentation rather than first-party schema. File an issue if your controller returns extra or differently-shaped fields.

Summary

Functions

Marks an alarm as archived.

Lists alarms for a site.

Returns a lazy stream that auto-paginates alarms via _start / _limit.

Functions

archive(client, site_id, alarm_id)

@spec archive(Req.Request.t(), String.t(), String.t()) ::
  {:ok, term()} | {:error, UnifiApi.Error.t()}

Marks an alarm as archived.

Examples

{:ok, _} = UnifiApi.Network.Alarms.archive(client, "default", alarm_id)

list(client, site_id, opts \\ [])

@spec list(Req.Request.t(), String.t(), keyword()) ::
  {:ok, term()} | {:error, UnifiApi.Error.t()}

Lists alarms for a site.

Options

  • :archivedtrue returns only archived alarms; false returns only active ones; nil (default) returns both.
  • :limit_limit=N server-side cap.
  • :start_start=N pagination offset.

Examples

# All active alarms
{:ok, alarms} = UnifiApi.Network.Alarms.list(client, "default", archived: false)

# Just IPS detections
{:ok, alarms} = UnifiApi.Network.Alarms.list(client, "default")
Enum.filter(alarms, &(&1["subsystem"] == "ips"))

stream(client, site_id, opts \\ [])

@spec stream(Req.Request.t(), String.t(), keyword()) :: Enumerable.t()

Returns a lazy stream that auto-paginates alarms via _start / _limit.

Error contract

A mid-stream error does not raise by default: the stream halts and yields {:error, %UnifiApi.StreamError{}, last_start} as its final element, so the enumerable is heterogeneous and Enum.map(stream, & &1["key"]) crashes on a transient 500. Match the tail:

items = Enum.to_list(stream)

case List.last(items) do
  {:error, error, cursor} -> {:error, error, cursor}
  _ -> {:ok, items}
end

Pass raise_errors: true to raise UnifiApi.StreamError instead.

Options

  • :archivedtrue for archived only, false for active only.
  • :limit — page size (default 500).
  • :max_pages — halt after this many successful pages (default: unbounded).
  • :max_items — halt once this many alarms have been yielded; the final page is truncated to fit (default: unbounded).
  • :raise_errors — when true, raise UnifiApi.StreamError on a mid-stream error instead of yielding {:error, reason, last_start} as the final element (default: false).

:max_pages, :max_items and :raise_errors are forwarded verbatim to UnifiApi.Client.stream_v1/3 and behave exactly as they do in UnifiApi.Client.stream/3. Any other key raises ArgumentError.

Examples

# At most 50 active alarms, however many pages that takes
UnifiApi.Network.Alarms.stream(authed, "default", archived: false, max_items: 50)
|> Enum.to_list()