jonnystorm / snmp-elixir

An SNMP client library for Elixir
Mozilla Public License 2.0
33 stars 12 forks source link

snmp walk #30

Closed mehdipourfar closed 6 years ago

mehdipourfar commented 6 years ago

Does this package support snmp walk? If yes, how?

jonnystorm commented 6 years ago

It does now. :)

31 adds SNMP.walk/3, which lazily iterates SNMP.get_next/3. Just like NetSNMP's walk, this function will halt the stream when it encounters an OID outside the specified subtree.

Thank you for contributing!

mehdipourfar commented 6 years ago

Thanks a lot. I have decided to use this library in my project, but I don't know where to start since there is no documentation. Suppose I want to execute this simple snmpget command. snmpget -v 2c -c public 192.168.1.30 ifOutOctets.201443072

After calling SNMP.start I get the following error:

13:03:47.558 [error] GenServer SNMP.DiscoveryAgent terminating (MatchError) no match of right hand side value: {:error, :enoent} (snmp_ex) lib/snmp/discovery_agent.ex:57: SNMP.DiscoveryAgent.do_seed_config/4 (snmp_ex) lib/snmp/discovery_agent.ex:82: SNMP.DiscoveryAgent.seed_agent_config/1 (snmp_ex) lib/snmp/discovery_agent.ex:45: SNMP.DiscoveryAgent.seed_config/1 (snmp_ex) lib/snmp/discovery_agent.ex:36: SNMP.DiscoveryAgent.handle_cast/2 (stdlib) gen_server.erl:637: :gen_server.try_dispatch/4 (stdlib) gen_server.erl:711: :gen_server.handle_msg/6 (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3 Last message: {:"$gen_cast", {:seed_and_start_agent, []}} State: [] (EXIT from #PID<0.193.0>) shell process exited with reason: an exception was raised: ** (MatchError) no match of right hand side value: {:error, :enoent} (snmp_ex) lib/snmp/discovery_agent.ex:57: SNMP.DiscoveryAgent.do_seed_config/4 (snmp_ex) lib/snmp/discovery_agent.ex:82: SNMP.DiscoveryAgent.seed_agent_config/1 (snmp_ex) lib/snmp/discovery_agent.ex:45: SNMP.DiscoveryAgent.seed_config/1 (snmp_ex) lib/snmp/discovery_agent.ex:36: SNMP.DiscoveryAgent.handle_cast/2 (stdlib) gen_server.erl:637: :gen_server.try_dispatch/4 (stdlib) gen_server.erl:711: :gen_server.handle_msg/6 (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

jonnystorm commented 6 years ago

Yes, I'm afraid this project is still very much in its infancy. Thank you for contributing!

I'm sorry about the trouble. The crash you saw is because we had an important path hardcoded in the discovery agent. Obviously it was meant to be temporary. :)

Commit b2db81a should solve the problem. Please try again at your earliest convenience, at let me know how it goes.

jonnystorm commented 6 years ago

@mehdipourfar bcb95a1 adds usage examples to README.md. Please let me know if you encounter any more trouble.

Thanks!

mehdipourfar commented 6 years ago

Now it starts but with some errors about some MIB compilations and then it returns :ok at last. Thanks a lot. When I try to do a get request, the following errors appear:

(exit) exited in: GenServer.call(SNMP.DiscoveryAgent, {:discover_engine_id, %URI{authority: "172.31.116.134", fragment: nil, host: "172.31.116.134", path: nil, port: nil, query: nil, scheme: "snmp", userinfo: nil}, []}, 5000) (EXIT) time out (elixir) lib/gen_server.ex:836: GenServer.call/3 (snmp_ex) lib/snmp.ex:361: SNMP.discover_engine_id/2 (snmp_ex) lib/snmp.ex:496: SNMP.perform_snmp_op/5 iex(6)> 13:12:43.385 [error] GenServer SNMP.DiscoveryAgent terminating ** (MatchError) no match of right hand side value: {:error, :timeout} (snmp_ex) lib/snmp/discovery_agent.ex:231: SNMP.DiscoveryAgent.handle_call/3 (stdlib) gen_server.erl:661: :gen_server.try_handle_call/4 (stdlib) gen_server.erl:690: :gen_server.handle_msg/6 (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3 Last message (from #PID<0.171.0>): {:discover_engine_id, %URI{authority: "172.31.116.134", fragment: nil, host: "172.31.116.134", path: nil, port: nil, query: nil, scheme: "snmp", userinfo: nil}, []} State: [] Client #PID<0.171.0> is alive

(iex) lib/iex/evaluator.ex:92: IEx.Evaluator.loop/1
(iex) lib/iex/evaluator.ex:24: IEx.Evaluator.init/4
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

(EXIT from #PID<0.171.0>) shell process exited with reason: an exception was raised: (MatchError) no match of right hand side value: {:error, :timeout} (snmp_ex) lib/snmp/discovery_agent.ex:231: SNMP.DiscoveryAgent.handle_call/3 (stdlib) gen_server.erl:661: :gen_server.try_handle_call/4 (stdlib) gen_server.erl:690: :gen_server.handle_msg/6 (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

Interactive Elixir (1.6.6) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> 13:12:43.388 [error] GenServer :snmpa_supervisor terminating ** (MatchError) no match of right hand side value: {:error, :timeout} (snmp_ex) lib/snmp/discovery_agent.ex:231: SNMP.DiscoveryAgent.handle_call/3 (stdlib) gen_server.erl:661: :gen_server.try_handle_call/4 (stdlib) gen_server.erl:690: :gen_server.handle_msg/6 (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3 Last message: {:EXIT, #PID<0.192.0>, {{:badmatch, {:error, :timeout}}, [{SNMP.DiscoveryAgent, :handle_call, 3, [file: 'lib/snmp/discovery_agent.ex', line: 231]}, {:gen_server, :try_handle_call, 4, [file: 'gen_server.erl', line: 661]}, {:gen_server, :handle_msg, 6, [file: 'gen_server.erl', line: 690]}, {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 249]}]}} State: {:state, {:local, :snmpa_supervisor}, :one_for_all, {[:snmpa_agent_sup, :config, :snmpa_target_cache, :snmpa_local_db, :snmpa_symbolic_store, :snmpa_misc_sup], %{config: {:child, :undefined, :config, {:snmpa_supervisor, :config, [[:v1, :v2, :v3], [dir: '/tmp/snmp_ex/conf/agent']]}, :transient, 2000, :worker, [:config, :snmpa_supervisor]}, snmpa_agent_sup: {:child, #PID<0.213.0>, :snmpa_agent_sup, {:snmpa_agent_sup, :start_link, [{:snmpa_agent, {:snmpa_agent, :start_link, [:normal, :snmp_master_agent, :none, #Reference<0.2162689419.2360868867.140963>, [verbosity: :silence, mibs: ['/usr/lib/erlang/lib/snmp-5.2.11/priv/mibs/SNMPv2-MIB', '/usr/lib/erlang/lib/snmp-5.2.11/priv/mibs/SNMP-FRAMEWORK-MIB', '/usr/lib/erlang/lib/snmp-5.2.11/priv/mibs/SNMP-MPD-MIB'], mib_storage: [module: :snmpa_mib_storage_ets], set_mechanism: :snmpa_set, authentication_service: :snmpa_acm, multi_threaded: false, versions: [:v1, :v2, :v3], net_if: [], mib_server: [], note_store: [], gb_max_vbs: 1000]]}, :permanent, 15000, :worker, [:snmpa_agent]}]}, :permanent, :infinity, :supervisor, [:snmpa_agent_sup, :supervisor]}, snmpa_local_db: {:child, #PID<0.206.0>, :snmpa_local_db, {:snmpa_local_db, :start_link, [:normal, '/tmp/snmp_ex/conf/agent/db', :create_db_and_dir, []]}, :permanent, 5000, :worker, [:snmpa_local_db]}, snmpa_misc_sup: {:child, #PID<0.204.0>, :snmpa_misc_sup, {:snmpa_misc_sup, :start_link, []}, :permanent, :infinity, :supervisor, [:snmpa_misc_sup, :supervisor]}, snmpa_symbolic_store: {:child, #PID<0.205.0>, :snmpa_symbolic_store, {:snmpa_symbolic_store, :start_link, [:normal, [mib_storage: [module: :snmpa_mib_storage_ets]]]}, :permanent, 2000, :worker, [:snmpa_symbolic_store]}, snmpa_target_cache: {:child, #PID<0.209.0>, :snmpa_target_cache, {:snmpa_target_cache, :start_link, [:normal, []]}, :transient, 2000, :worker, [:snmpa_target_cache]}}}, :undefined, 0, 3600, [], 0, :snmpa_supervisor, [:master, [agent_type: :master, discovery: [originating: [enable: true], terminating: [enable: true]], db_dir: '/tmp/snmp_ex/conf/agent/db', db_init_error: :create_db_and_dir, config: [dir: '/tmp/snmp_ex/conf/agent']]]}

I think the problem is with agent_engine_id which I don't know what is it. In perform_snmp_op you call a function named generate_target_name and there you get sha_sum of uri and crentials. But as far as I know, only version 3 of snmp is encrypted. After that, you pass the uri and target to discover_engine_id function. There you pass target to :snmpm_config.get_agent_engine_id and if the result contains error (In my case, it has), you try to get engine_id by passing only url to discovery_engine (target name which contains credentials is not passed).

jonnystorm commented 6 years ago

I created a branch called 'graceful-discovery-timeout'. Would you mind giving that a try?

Also, can you tell me what kind of SNMP agent you're querying? Better still, the values of the sysObjectId.0 and sysDescr.0 OIDs will help me decide how to fix this permanently.

Thanks!

mehdipourfar commented 6 years ago

I have tried to get sysObjectId.0 and this is the error that I get.

(exit) exited in: GenServer.call(SNMP.DiscoveryAgent, {:discover_engine_id, %URI{authority: "172.31.116.134", fragment: nil, host: "172.31.116.134", path: nil, port: nil, query: nil, scheme: "snmp", userinfo: nil}, []}, 5000) (EXIT) time out (elixir) lib/gen_server.ex:836: GenServer.call/3 (snmp_ex) lib/snmp.ex:469: anonymous fn/2 in SNMP.perform_snmp_op/5 (snmp_ex) lib/snmp.ex:478: SNMP.perform_snmp_op/5

jonnystorm commented 6 years ago

I've pushed some additional changes to that branch. Please give it a try, and let me know if it helps.

mehdipourfar commented 6 years ago

I think you have forgotten to push:)

jonnystorm commented 6 years ago

No, it's there. :) b95afc7

Be sure to mix deps.clean snmp_ex, first!

mehdipourfar commented 6 years ago

Now it works. Thank you very much:)

jonnystorm commented 6 years ago

You're very welcome!