Elixir and SNMP?

603 views
Skip to first unread message

Augie De Blieck Jr.

unread,
Dec 19, 2013, 9:45:21 AM12/19/13
to elixir-l...@googlegroups.com
Has anyone done any work with Elixir to access devices via SNMP?

I'm looking at this angle as a possible way to bring Elixir to my day
job in the future, where I help manage and monitor a large number of
routers and switches across our network. We use SNMP commands to pull
information off those boxes and track things.

So far, it looks like straight-up Erlang would be the better way to
go, but even the Quick Start guide for SNMP in Erlang is a scary bit
of business:

https://erlangcentral.org/wiki/index.php/SNMP_Quick_Start

Or maybe I need to work my way deeper into the language and have a
better understanding of OTP before reading that...

Any ideas? Is there any sample code out there I should look at?

Thanks in advance,
-Augie

José Valim

unread,
Dec 19, 2013, 9:54:15 AM12/19/13
to elixir-l...@googlegroups.com
Here is another reference, this time from Erlang docs:


It is very domain specific so I am afraid I can't help much here.

That said, since all the docs are in Erlang, it may be better to start using Erlang and them port the working code to Elixir once you get more acquainted with the library (and we can definitely help with that).



José Valim
Skype: jv.ptec
Founder and Lead Developer



--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Augie De Blieck Jr.

unread,
Dec 19, 2013, 11:35:42 AM12/19/13
to elixir-l...@googlegroups.com
Thanks, José. That sounds like the way to go.

It's funny - we did such a good job abstracting away all the ugly SNMP
stuff years ago, I had forgotten all the work that went into that.
Starting from scratch again is a bit daunting. C'est la vie!

-Augie

Augie De Blieck Jr.

unread,
Jul 14, 2014, 10:16:54 AM7/14/14
to elixir-l...@googlegroups.com
Answering my own question from last December here. Ernie Miller blogged today about his findings with Elixir and SNMP.


It's pretty nutty what you have to do to get it all working, but he's had success...  Now we know it can be done. ;-)

-Augie

Dave Martin

unread,
Jul 14, 2014, 1:11:54 PM7/14/14
to elixir-l...@googlegroups.com
I didn't find it necessary to do the manager part if you only want to query other agents. (I'm also not currently using the config files, so I'm not sure how that would play if you leave out the manager in that case).

Augie De Blieck Jr.

unread,
Jul 14, 2014, 2:02:45 PM7/14/14
to elixir-l...@googlegroups.com
You haven't posted any of it on Github, have you? As simple as it may
or may not be, I love working off examples.

This is all a good reminder that the Elixir community has grown a lot
since last December. It's probably worth re-asking old questions
again, in case others have done things that were once not done yet.
;-)

-Augie
> --
> You received this message because you are subscribed to the Google Groups
> "elixir-lang-talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to elixir-lang-ta...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Dave Martin

unread,
Jul 15, 2014, 12:01:31 PM7/15/14
to elixir-l...@googlegroups.com
With the caveat that this isn't necessarily pretty and I was just trying to get it working and even newer to elixir than I still am, and the doc comments were not written with others in mind, and I should probably clean it up, and un-hardcode things, this is the gist of what I came up with:

  def sysOid, do: [1,3,6,1,2,1,1,2,0]
  def ospfLsdbEntry, do: [1,3,6,1,2,1,14,4,1]
  def ospfLsdbAdvertisement, do: [1,3,6,1,2,1,14,4,1,8]

  def start do
    :snmpm.start
    :snmpm.register_user :me, :snmpm_user_default, self
    :snmpm.register_agent :me, 'targetname',
      [engine_id: 'myengine', address: [192,168,1,1], community: 'public']
  end
  def stop, do: :snmpm.stop

 @doc """
  do the leading elements in the first list match all of the elements in the
  second list?
  """
  def starts_with([_|_], []), do: true
  def starts_with([head|tail1], [head|tail2]), do: starts_with(tail1, tail2)
  def starts_with([], []), do: true
  def starts_with(_, _), do: false

  @doc """
  walk an smnp oid, call func on each element with arguments (oid, value, state)
  get the next (oid, value) after oid, if oid starts with prefix call
  func with (oid, value, state), and repeat with next oid as the new oid.
  state will be replaced with the return value of func.
  returns state.
  """
  defp walk(oid, prefix, func, state) do
    {:ok, snmpReply, _remaining} = :snmpm.sync_get_next2 :me, 'targetname', [oid]
    {:noError, _errorIndex, varBinds} = snmpReply
    [varBind|_T] = varBinds
    {:varbind, newoid, _type, value, index} = varBind
    if starts_with newoid, prefix do
      state = func.(oid, value, state)
      if is_list index do
        state = walk newoid ++ index, prefix, func, state
      else
        state = walk newoid ++ [index], prefix, func, state
      end
    end
    state
  end

 @doc """
  walk an smnp oid, call func on each element with arguments (oid, value, state).
  state will be replaced with the return value of func each time it is called.
  returns state.
  """
  def walk(oid, func, state), do: walk(oid, oid, func, state)

an example walk function:

  # debugging
  # pass to walk to print the walked values, which are assumed to be OSPF LSAs
  def walkfunc_print(_oid, value, state) do
    lsa = value |> :erlang.list_to_binary |> ospf_lsa
    IO.puts inspect lsa
    :io.nl
    state
  end

walk ospfLsdbAdvertisement, &walkfunc_print/3, nil

(and I just realized, my walk function may not work if you happen to walk the last table in an agent, as it needs to check for a "there is no next" return).
Reply all
Reply to author
Forward
0 new messages