SNMP (snmp_ex v0.7.0)

An SNMP client library for Elixir.

Summary

Functions

Performs a SNMP BULKWALK operation, efficiently retrieving a subtree of MIB objects.

Returns a keyword list containing the given SNMP credentials.

Converts oid to dot-delimited string.

Perform an SNMP GET/SET request.

Callback implementation for Application.start/2.

Converts dot-delimited oid string to list.

Perform an SNMP walk using GETNEXT operations.

Types

@type asn1_type() :: atom() | binary()
Link to this type

asn1_value()

@type asn1_value() :: any()
Link to this type

credential()

@type credential() :: map()
@type mib_name() :: String.t()
@type object_id() :: object_name() | [non_neg_integer(), ...]
Link to this type

object_name()

@type object_name() :: binary() | atom()
Link to this type

req_options()

@type req_options() :: Keyword.t()
Link to this type

req_params()

@type req_params() :: %{
  uri: URI.t(),
  credential: credential(),
  varbinds: [req_varbind(), ...]
}
Link to this type

req_varbind()

@type req_varbind() ::
  %{oid: object_id()}
  | %{oid: object_id(), type: asn1_type()}
  | %{oid: object_id(), type: asn1_type(), value: asn1_value()}
Link to this type

request_result()

@type request_result() :: {:ok, [varbind(), ...]} | {:error, any()}
Link to this type

snmp_credential()

@type snmp_credential() :: SNMP.CommunityCredential.t() | SNMP.USMCredential.t()
@type varbind() :: %{oid: object_id(), type: asn1_type(), value: asn1_value()}

Functions

Link to this function

bulkwalk(request, options \\ [])

@spec bulkwalk(req_params(), req_options()) :: Enumerable.t()

Performs a SNMP BULKWALK operation, efficiently retrieving a subtree of MIB objects.

Parameters

  • request: Map containing uri, credential, and varbinds
  • options: Keyword list of options including max_repetitions and timeout

Returns

  • List of varbinds in the requested subtree
Link to this function

credential(map)

Returns a keyword list containing the given SNMP credentials.

Example

iex> SNMP.credential(%{community: "public"})
%SNMP.CommunityCredential{version: :v1, sec_model: :v1, community: ~c"public"}

iex> SNMP.credential(
...>   %{version: :v2, community: "public"}
...> )
%SNMP.CommunityCredential{version: :v2, sec_model: :v2c, community: ~c"public"}

iex> SNMP.credential(%{sec_name: "user"})
%SNMP.USMCredential{
  version: :v3,
  sec_model: :usm,
  sec_level: :noAuthNoPriv,
  sec_name: ~c"user",
  auth: :usmNoAuthProtocol,
  auth_pass: nil,
  priv: :usmNoPrivProtocol,
  priv_pass: nil
}


iex> SNMP.credential(
...>   %{sec_name: "user",
...>     auth: :sha,
...>     auth_pass: "authpass",
...>   }
...> )
%SNMP.USMCredential{
  version: :v3,
  sec_model: :usm,
  sec_level: :authNoPriv,
  sec_name: ~c"user",
  auth: :usmHMACSHAAuthProtocol,
  auth_pass: ~c"authpass",
  priv: :usmNoPrivProtocol,
  priv_pass: nil
}


iex> SNMP.credential(
...>   %{sec_name: "user",
...>     auth: :sha,
...>     auth_pass: "authpass",
...>     priv: :aes,
...>     priv_pass: "privpass",
...>   }
...> )
%SNMP.USMCredential{
  version: :v3,
  sec_model: :usm,
  sec_level: :authPriv,
  sec_name: ~c"user",
  auth: :usmHMACSHAAuthProtocol,
  auth_pass: ~c"authpass",
  priv: :usmAesCfb128Protocol,
  priv_pass: ~c"privpass"
}
Link to this function

list_oid_to_string(oid)

@spec list_oid_to_string([non_neg_integer()]) :: String.t() | no_return()

Converts oid to dot-delimited string.

Example

iex> SNMP.list_oid_to_string([1,3,6,1,2,1,1,5,0])
"1.3.6.1.2.1.1.5.0"
Link to this function

load_mib(mib_name)

@spec load_mib(mib_name()) :: :ok | {:error, term()}
Link to this function

request(map, options \\ [])

@spec request(req_params(), req_options()) :: request_result()

Perform an SNMP GET/SET request.

Example

iex> %{uri: URI.parse("snmp://an-snmp-host.local"),
...>   credential: v2_cred,
...>   varbinds: [%{oid: [1,3,6,1,2,1,1,5,0]}],
...> } |> SNMP.request
{ :ok,
  [ %{oid: [1, 3, 6, 1, 2, 1, 1, 5, 0],
      type: :"OCTET STRING",
      value: "an-snmp-host"
    }
  ]
}

iex> %{uri: URI.parse("snmp://an-snmp-host.local"),
...>   credential: v2_cred,
...>   varbinds: [
...>     %{oid: [1,3,6,1,2,1,1,5,0],
...>       type: :s,
...>       value: "new-hostname",
...>     },
...>   ],
...> } |> SNMP.request
{ :ok,
  [ %{oid: [1, 3, 6, 1, 2, 1, 1, 5, 0],
      type: :"OCTET STRING",
      value: "new-hostname"
    }
  ]
}
Link to this function

resolve_object_name_to_oid(oid)

@spec resolve_object_name_to_oid(object_id()) ::
  {:ok, object_id()} | {:error, term()} | no_return()
Link to this function

start(type, args)

Callback implementation for Application.start/2.

Link to this function

string_oid_to_list(oid)

@spec string_oid_to_list(String.t()) :: [non_neg_integer()] | no_return()

Converts dot-delimited oid string to list.

Example

iex> SNMP.string_oid_to_list("1.3.6.1.2.1.1.5.0")
[1,3,6,1,2,1,1,5,0]
Link to this function

sync_get(target, oids, timeout)

Link to this function

walk(map, options \\ [])

@spec walk(req_params(), req_options()) :: Enumerable.t()

Perform an SNMP walk using GETNEXT operations.

This function returns a stream, which ensures that the resulting walk is bounded.

Example

iex> %{uri: URI.parse("snmp://an-snmp-host.local"),
...>   credential: v3_cred,
...>   varbinds: [%{oid: "ipAddrTable"}],
...> } |> SNMP.walk
...> |> Enum.take(1)
[ %{oid: [1, 3, 6, 1, 2, 1, 4, 20, 1, 1, 192, 0, 2, 1],
    type: :IpAddress,
    value: [192, 0, 2, 1],
  }
]