Class: SNMP::Manager
- Inherits:
-
Object
- Object
- SNMP::Manager
- Defined in:
- lib/snmp/manager.rb
Overview
SNMP Manager
This class provides a manager for interacting with a single SNMP agent.
Example
require 'snmp'
manager = SNMP::Manager.new(:Host => 'localhost', :Port => 1061)
response = manager.get(["1.3.6.1.2.1.1.1.0", "1.3.6.1.2.1.1.2.0"])
response.each_varbind {|vb| puts vb.inspect}
manager.close
Symbolic Object Names
Symbolic names for SNMP object IDs can be used as parameters to the APIs in this class if the MIB modules are imported and the names of the MIBs are included in the MibModules configuration parameter.
See MIB.varbind_list for a description of valid parameter formats.
The following modules are loaded by default: “SNMPv2-MIB”, “IF-MIB”, “IP-MIB”, “TCP-MIB”, “UDP-MIB”. All of the current IETF MIBs have been imported and are available for loading.
Additional modules may be imported using the MIB class. The current implementation of the importing code requires that the external ‘smidump’ tool is available in your PATH. This tool can be obtained from the libsmi website at www.ibr.cs.tu-bs.de/projects/libsmi/ .
Example
Do this once:
SNMP::MIB.import_module(MY_MODULE_FILENAME, MIB_OUTPUT_DIR)
Include your module in MibModules each time you create a Manager:
SNMP::Manager.new(:Host => 'localhost', :MibDir => MIB_OUTPUT_DIR,
:MibModules => ["MY-MODULE-MIB", "SNMPv2-MIB", ...])
Constant Summary collapse
- DefaultConfig =
Default configuration. Individual options may be overridden when the Manager is created.
{ :Host => 'localhost', :Port => 161, :Community => 'public', :WriteCommunity => nil, :Version => :SNMPv2c, :Timeout => 1, :Retries => 5, :Transport => UDPTransport, :MaxReceiveBytes => 8000, :MibDir => MIB::DEFAULT_MIB_PATH, :MibModules => ["SNMPv2-MIB", "IF-MIB", "IP-MIB", "TCP-MIB", "UDP-MIB"]}
- @@request_id =
RequestId.new
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Retrieves the current configuration of this Manager.
-
#mib ⇒ Object
readonly
Retrieves the MIB for this Manager.
Class Method Summary collapse
-
.open(config = {}) ⇒ Object
Creates a Manager but also takes an optional block and automatically closes the transport connection used by this manager after the block completes.
Instance Method Summary collapse
-
#close ⇒ Object
Close the transport connection for this manager.
-
#get(object_list) ⇒ Object
Sends a get request for the supplied list of ObjectId or VarBind objects.
-
#get_bulk(non_repeaters, max_repetitions, object_list) ⇒ Object
Sends a get-bulk request.
-
#get_next(object_list) ⇒ Object
Sends a get-next request for the supplied list of ObjectId or VarBind objects.
-
#initialize(config = {}) ⇒ Manager
constructor
A new instance of Manager.
- #load_module(name) ⇒ Object
-
#next_request_id=(request_id) ⇒ Object
Set the next request-id instead of letting it be generated automatically.
-
#set(object_list) ⇒ Object
Sends a set request using the supplied list of VarBind objects.
-
#walk(object_list) ⇒ Object
Walks a list of ObjectId or VarBind objects using get_next until the response to the first OID in the list reaches the end of its MIB subtree.
Constructor Details
#initialize(config = {}) ⇒ Manager
Returns a new instance of Manager.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/snmp/manager.rb', line 149 def initialize(config = {}) if block_given? warn "SNMP::Manager::new() does not take block; use SNMP::Manager::open() instead" end @config = DefaultConfig.merge(config) @config[:WriteCommunity] = @config[:WriteCommunity] || @config[:Community] @community = @config[:Community] @write_community = @config[:WriteCommunity] @snmp_version = @config[:Version] @max_bytes = @config[:MaxReceiveBytes] @timeout = @config[:Timeout] @retries = @config[:Retries] @transport = @config[:Transport].new(@config[:Host], @config[:Port]) @mib = MIB.new load_modules(@config[:MibModules], @config[:MibDir]) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Retrieves the current configuration of this Manager.
142 143 144 |
# File 'lib/snmp/manager.rb', line 142 def config @config end |
#mib ⇒ Object (readonly)
Retrieves the MIB for this Manager.
147 148 149 |
# File 'lib/snmp/manager.rb', line 147 def mib @mib end |
Class Method Details
.open(config = {}) ⇒ Object
Creates a Manager but also takes an optional block and automatically closes the transport connection used by this manager after the block completes.
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/snmp/manager.rb', line 171 def self.open(config = {}) manager = Manager.new(config) if block_given? begin yield manager ensure manager.close end end end |
Instance Method Details
#close ⇒ Object
Close the transport connection for this manager.
185 186 187 |
# File 'lib/snmp/manager.rb', line 185 def close @transport.close end |
#get(object_list) ⇒ Object
Sends a get request for the supplied list of ObjectId or VarBind objects.
Returns a Response PDU with the results of the request.
199 200 201 202 203 |
# File 'lib/snmp/manager.rb', line 199 def get(object_list) varbind_list = @mib.varbind_list(object_list, :NullValue) request = GetRequest.new(@@request_id.next, varbind_list) try_request(request) end |
#get_bulk(non_repeaters, max_repetitions, object_list) ⇒ Object
Sends a get-bulk request. The non_repeaters parameter specifies the number of objects in the object_list to be retrieved once. The remaining objects in the list will be retrieved up to the number of times specified by max_repetitions.
223 224 225 226 227 228 229 230 231 |
# File 'lib/snmp/manager.rb', line 223 def get_bulk(non_repeaters, max_repetitions, object_list) varbind_list = @mib.varbind_list(object_list, :NullValue) request = GetBulkRequest.new( @@request_id.next, varbind_list, non_repeaters, max_repetitions) try_request(request) end |
#get_next(object_list) ⇒ Object
Sends a get-next request for the supplied list of ObjectId or VarBind objects.
Returns a Response PDU with the results of the request.
211 212 213 214 215 |
# File 'lib/snmp/manager.rb', line 211 def get_next(object_list) varbind_list = @mib.varbind_list(object_list, :NullValue) request = GetNextRequest.new(@@request_id.next, varbind_list) try_request(request) end |
#load_module(name) ⇒ Object
189 190 191 |
# File 'lib/snmp/manager.rb', line 189 def load_module(name) @mib.load_module(name) end |
#next_request_id=(request_id) ⇒ Object
Set the next request-id instead of letting it be generated automatically. This method is useful for testing and debugging.
296 297 298 |
# File 'lib/snmp/manager.rb', line 296 def next_request_id=(request_id) @@request_id.force_next(request_id) end |
#set(object_list) ⇒ Object
Sends a set request using the supplied list of VarBind objects.
Returns a Response PDU with the results of the request.
238 239 240 241 242 |
# File 'lib/snmp/manager.rb', line 238 def set(object_list) varbind_list = @mib.varbind_list(object_list, :KeepValue) request = SetRequest.new(@@request_id.next, varbind_list) try_request(request, @write_community) end |
#walk(object_list) ⇒ Object
Walks a list of ObjectId or VarBind objects using get_next until the response to the first OID in the list reaches the end of its MIB subtree.
The varbinds from each get_next are yielded to the given block as they are retrieved. The result is yielded as a VarBind when walking a single object or as a VarBindList when walking a list of objects.
Normally this method is used for walking tables by providing an ObjectId for each column of the table.
For example:
SNMP::Manager.open(:Host => "localhost") do |manager|
manager.walk("ifTable") { |vb| puts vb }
end
SNMP::Manager.open(:Host => "localhost") do |manager|
manager.walk(["ifIndex", "ifDescr"]) do |index, descr|
puts "#{index.value} #{descr.value}"
end
end
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/snmp/manager.rb', line 268 def walk(object_list) raise ArgumentError, "expected a block to be given" unless block_given? varbind_list = @mib.varbind_list(object_list, :NullValue) start_oid = varbind_list.first.name last_oid = start_oid loop do varbind_list = get_next(varbind_list).varbind_list first_vb = varbind_list.first stop_oid = first_vb.name if stop_oid <= last_oid warn "OIDs are not increasing, #{last_oid} followed by #{stop_oid}" break end break unless stop_oid.subtree_of?(start_oid) break if stop_oid == EndOfMibView last_oid = stop_oid if varbind_list.length == 1 yield first_vb else yield varbind_list end end end |