SnmpKit Examples

Runnable scripts that demonstrate SnmpKit against simulated devices. Run them from a checkout with the library compiled:

mix run examples/getting_started.exs
mix run examples/unified_api_demo.exs
mix run examples/cable_modem_simulation.exs

Each script also runs standalone (elixir examples/getting_started.exs), fetching snmpkit from Hex with Mix.install when the library is not already loaded.

To use SnmpKit in your own project add it to mix.exs:

def deps do
  [{:snmpkit, "~> 2.0"}]
end

The scripts

ScriptShows
getting_started.exsa simulated device, GET, WALK, tables, bulk, async, multi-target, MIB resolution, error handling
unified_api_demo.exsthe SnmpKit.SNMP, SnmpKit.MIB and SnmpKit.Sim modules and the SnmpKit shortcuts
oid_formats_demo.exsdotted OID strings with and without a leading dot (SnmpKit.SnmpLib.OID)
multi_return_format_demo.exs:list, :with_targets and :map results from multi-target calls (unreachable targets, so every result is an error)
single_socket_demo.exsthe shared-socket engine behind multi-target calls (unreachable targets)
scalable_high_concurrency_polling.exspolling a population of 100 simulated cable modems with SnmpKit.SnmpMgr.Multi
cable_modem_simulation.exs, quick_cable_modem.exsDOCSIS cable modem simulation from OID maps and a JSON profile; the devices keep running until Ctrl-C
cable_modem_profile.jsona JSON device profile for {:json_profile, path}
docsis_mib_example.exscompiling DOCS-IF-MIB from the test fixtures and resolving its names

Patterns worth copying

Operations return enriched maps

{:ok, %{value: descr, formatted: formatted, type: :octet_string}} =
  SnmpKit.SNMP.get("192.168.1.1", "sysDescr.0")

{:ok, rows} = SnmpKit.SNMP.walk("192.168.1.1", "ifTable")
{:ok, rows} = SnmpKit.SNMP.bulk_walk("192.168.1.1", "system")

MIB lookups

{:ok, oid} = SnmpKit.MIB.resolve("sysDescr.0")
{:ok, name} = SnmpKit.MIB.reverse_lookup([1, 3, 6, 1, 2, 1, 1, 1, 0])
{:ok, children} = SnmpKit.MIB.children([1, 3, 6, 1, 2, 1, 1])

A simulated device in a test

setup do
  {:ok, profile} = SnmpKit.SnmpSim.ProfileLoader.load_profile(:router)
  {:ok, _device} = SnmpKit.Sim.start_device(profile, port: 30161)
  %{target: "127.0.0.1:30161"}
end

test "can query the device", %{target: target} do
  assert {:ok, %{value: "Cisco Router"}} = SnmpKit.SNMP.get(target, "sysDescr.0")
end

Many targets at once

requests = for target <- targets, do: {target, "sysUpTime.0"}
results = SnmpKit.SNMP.get_multi(requests, max_concurrent: 50)
# [{:ok, [%{value: ...}]}, {:error, :timeout}, ...] in request order

Streaming a large table

"192.168.1.1"
|> SnmpKit.SNMP.walk_stream("ipRouteTable")
|> Stream.each(&process_entry/1)
|> Stream.run()

Error handling

case SnmpKit.SNMP.get(target, oid) do
  {:ok, %{value: value}} -> {:ok, value}
  {:error, :timeout} -> {:error, :device_unreachable}
  {:error, :no_such_object} -> {:error, :oid_not_found}
  {:error, reason} -> {:error, reason}
end

More

New examples are welcome; keep them runnable with mix run, self-contained (start their own simulated devices), and add them to the table above.