Class: NETSNMP::Client
- Inherits:
-
Object
- Object
- NETSNMP::Client
- Defined in:
- lib/netsnmp/client.rb
Overview
Main Entity, provides the user-facing API to communicate with SNMP Agents
Under the hood it creates a “session” (analogous to the net-snmp C session), which will be used to proxy all the communication to the agent. the Client ensures that you only write pure ruby and read pure ruby, not concerning with snmp-speak like PDUs, varbinds and the like.
Constant Summary collapse
- RETRIES =
5
Instance Method Summary collapse
-
#close ⇒ Object
Closes the inner section.
-
#get(*oid_opts) {|response| ... } ⇒ Object
Performs an SNMP GET Request.
-
#get_next(*oid_opts) {|response| ... } ⇒ Object
Performs an SNMP GETNEXT Request.
-
#inform(*oid_opts) {|response| ... } ⇒ Object
Perform a SNMP INFORM Request.
-
#initialize(version: nil, **options) {|client| ... } ⇒ Client
constructor
A new instance of Client.
-
#set(*oid_opts) {|response| ... } ⇒ Object
Perform a SNMP SET Request.
-
#walk(oid:) ⇒ Enumerator
Perform a SNMP Walk (issues multiple subsequent GENEXT requests within the subtree rooted on an OID).
Constructor Details
#initialize(version: nil, **options) {|client| ... } ⇒ Client
Returns a new instance of Client.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/netsnmp/client.rb', line 26 def initialize(version: nil, **) version = case version when Integer then version # assume the use know what he's doing when /v?1/ then 0 when /v?2c?/ then 1 when /v?3/ then 3 else 3 # rubocop:disable Lint/DuplicateBranch end @retries = .fetch(:retries, RETRIES).to_i @session ||= version == 3 ? V3Session.new(**) : Session.new(version: version, **) return unless block_given? begin yield self ensure close end end |
Instance Method Details
#close ⇒ Object
Closes the inner section
47 48 49 |
# File 'lib/netsnmp/client.rb', line 47 def close @session.close end |
#get(*oid_opts) {|response| ... } ⇒ Object
Performs an SNMP GET Request
55 56 57 58 59 60 61 |
# File 'lib/netsnmp/client.rb', line 55 def get(*oid_opts) request = @session.build_pdu(:get, *oid_opts) response = handle_retries { @session.send(request) } yield response if block_given? values = response.varbinds.map(&:value) values.size > 1 ? values : values.first end |
#get_next(*oid_opts) {|response| ... } ⇒ Object
Performs an SNMP GETNEXT Request
67 68 69 70 71 72 73 |
# File 'lib/netsnmp/client.rb', line 67 def get_next(*oid_opts) request = @session.build_pdu(:getnext, *oid_opts) response = handle_retries { @session.send(request) } yield response if block_given? values = response.varbinds.map { |v| [v.oid, v.value] } values.size > 1 ? values : values.first end |
#inform(*oid_opts) {|response| ... } ⇒ Object
Perform a SNMP INFORM Request
143 144 145 146 147 148 149 |
# File 'lib/netsnmp/client.rb', line 143 def inform(*oid_opts) request = @session.build_pdu(:inform, *oid_opts) response = handle_retries { @session.send(request) } yield response if block_given? values = response.varbinds.map(&:value) values.size > 1 ? values : values.first end |
#set(*oid_opts) {|response| ... } ⇒ Object
Perform a SNMP SET Request
131 132 133 134 135 136 137 |
# File 'lib/netsnmp/client.rb', line 131 def set(*oid_opts) request = @session.build_pdu(:set, *oid_opts) response = handle_retries { @session.send(request) } yield response if block_given? values = response.varbinds.map(&:value) values.size > 1 ? values : values.first end |
#walk(oid:) ⇒ Enumerator
Perform a SNMP Walk (issues multiple subsequent GENEXT requests within the subtree rooted on an OID)
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/netsnmp/client.rb', line 81 def walk(oid:) walkoid = OID.build(oid) Enumerator.new do |y| code = walkoid first_response_code = nil catch(:walk) do loop do get_next(oid: code) do |response| response.varbinds.each do |varbind| code = varbind.oid if !OID.parent?(walkoid, code) || varbind.value.eql?(:endofmibview) || (code == first_response_code) throw(:walk) else y << [code, varbind.value] end first_response_code ||= code end end end end end end |