SnmpKit.Trap (snmpkit v2.0.1)

Receives SNMP notifications (SNMPv1 traps, SNMPv2c traps and informs) and hands each one to a handler.

{:ok, receiver} = SnmpKit.Trap.start_link(port: 162, handler: &MyApp.Alerts.handle/1)

# or in a supervision tree
children = [{SnmpKit.Trap, port: 162, handler: {MyApp.Alerts, :handle, []}}]

Informs are acknowledged automatically. SNMPv3 notifications are counted under :unsupported and dropped.

Options

  • :port - UDP port to bind (default 162; use 0 for an ephemeral port and read it back with port/1)
  • :bind_address - interface to bind (default "0.0.0.0")
  • :handler - fun/1, {module, function, extra_args} (the notification is prepended to extra_args), or a pid that receives {:snmp_trap, notification}
  • :communities - list of accepted community strings; nil (default) accepts any
  • :acknowledge_informs - default true
  • :include_names, :include_formatted - varbind enrichment, as for SnmpKit.SNMP calls
  • :name - GenServer registration

Notifications

Each notification is a map:

%{
  kind: :trap | :inform,
  version: :v1 | :v2c,
  community: "public",
  source: {{192, 168, 1, 10}, 49152},   # sender address and port
  agent_address: {192, 168, 1, 10},     # v1: the agent-addr field
  trap_oid: [1, 3, 6, 1, 6, 3, 1, 1, 5, 3],
  trap_name: "linkDown",                # nil when unknown
  uptime: 123456,                       # sysUpTime, centiseconds
  request_id: 42,                       # v2c only
  enterprise: nil,                      # v1 only
  generic_trap: nil, specific_trap: nil,# v1 only
  varbinds: [%{oid: ..., type: ..., value: ..., name: ..., formatted: ...}],
  received_at: ~U[...]
}

For SNMPv2c the sysUpTime.0 and snmpTrapOID.0 varbinds are kept in :varbinds as well as lifted into :uptime and :trap_oid. For SNMPv1 the trap OID is derived per RFC 3584.

Summary

Functions

The UDP port the receiver is bound to.

Starts a receiver. See the module documentation for options.

Counters: :received, :traps, :informs, :acknowledged, :decode_errors, :rejected_community, :unsupported, :handler_errors.

Stops the receiver and closes its socket.

Types

notification()

@type notification() :: map()

Functions

port(server)

@spec port(GenServer.server()) :: :inet.port_number()

The UDP port the receiver is bound to.

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

Starts a receiver. See the module documentation for options.

stats(server)

@spec stats(GenServer.server()) :: map()

Counters: :received, :traps, :informs, :acknowledged, :decode_errors, :rejected_community, :unsupported, :handler_errors.

stop(server)

@spec stop(GenServer.server()) :: :ok

Stops the receiver and closes its socket.