Class: SNMP::TrapListener
- Inherits:
-
Object
- Object
- SNMP::TrapListener
- Defined in:
- lib/snmp/manager.rb
Overview
Defined Under Namespace
Classes: Config
Constant Summary collapse
- NULL_HANDLER =
Proc.new {}
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Retrieves the current configuration of this TrapListener.
Instance Method Summary collapse
-
#exit ⇒ Object
(also: #kill, #terminate)
Stops the trap handler thread and releases the socket.
-
#initialize(options = {}, &block) ⇒ TrapListener
constructor
Start a trap handler thread.
-
#join ⇒ Object
Joins the current thread to the trap handler thread.
- #load_module(name, mib_dir = @mib_dir) ⇒ Object
- #load_modules(module_list, mib_dir = @mib_dir) ⇒ Object
-
#on_trap(object_id, &block) ⇒ Object
Define a trap handler block for a specific trap ObjectId.
-
#on_trap_default(&block) ⇒ Object
Define the default trap handler.
-
#on_trap_v1(&block) ⇒ Object
Define a trap handler block for all SNMPv1 traps.
-
#on_trap_v2c(&block) ⇒ Object
Define a trap handler block for all SNMPv2c traps.
Constructor Details
#initialize(options = {}, &block) ⇒ TrapListener
Start a trap handler thread. If a block is provided then the block is executed before trap handling begins. This block is typically used to define the trap handler blocks.
The trap handler blocks execute in the context of the trap handler thread.
The most specific trap handler is executed when a trap arrives. Only one handler is executed. The handlers are checked in the following order:
-
handler for a specific OID
-
handler for a specific SNMP version
-
default handler
The default for the :community option is ‘nil’ allows traps with any community to be accepted. To only accept traps from a specific community, the community may also be set to a string (e.g. ‘public’) or a list of strings (e.g. [‘public’, ‘my_private_community’] ).
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 |
# File 'lib/snmp/manager.rb', line 621 def initialize(={}, &block) config = Config.new() @transport = config.create_transport @community = config.community @max_bytes = config.max_recv_bytes @mib = MIB.new @mib_dir = config.mib_dir load_modules(config.mib_modules, @mib_dir) @config = config.applied_config @handler_init = block @oid_handler = {} @v1_handler = nil @v2c_handler = nil @default_handler = nil @lock = Mutex.new @handler_thread = Thread.new(self) { |m| process_traps(m) } end |
Instance Attribute Details
#config ⇒ Object (readonly)
Retrieves the current configuration of this TrapListener.
600 601 602 |
# File 'lib/snmp/manager.rb', line 600 def config @config end |
Instance Method Details
#exit ⇒ Object Also known as: kill, terminate
Stops the trap handler thread and releases the socket.
See also Thread#exit.
694 695 696 697 |
# File 'lib/snmp/manager.rb', line 694 def exit @handler_thread.exit @transport.close end |
#join ⇒ Object
Joins the current thread to the trap handler thread.
See also Thread#join.
685 686 687 |
# File 'lib/snmp/manager.rb', line 685 def join @handler_thread.join end |
#load_module(name, mib_dir = @mib_dir) ⇒ Object
702 703 704 |
# File 'lib/snmp/manager.rb', line 702 def load_module(name, mib_dir = @mib_dir) @mib.load_module(name, mib_dir) end |
#load_modules(module_list, mib_dir = @mib_dir) ⇒ Object
706 707 708 |
# File 'lib/snmp/manager.rb', line 706 def load_modules(module_list, mib_dir = @mib_dir) module_list.each { |m| load_module(m, mib_dir) } end |
#on_trap(object_id, &block) ⇒ Object
Define a trap handler block for a specific trap ObjectId. This handler only applies to SNMPv2 traps. Note that symbolic OIDs are not supported by this method (like in the SNMP.Manager class).
655 656 657 658 |
# File 'lib/snmp/manager.rb', line 655 def on_trap(object_id, &block) raise ArgumentError, "a block must be provided" unless block @lock.synchronize { @oid_handler[ObjectId.new(object_id)] = block } end |
#on_trap_default(&block) ⇒ Object
Define the default trap handler. The default trap handler block is executed only if no other block is applicable. This handler should expect to receive both SNMPv1_Trap and SNMPv2_Trap objects.
645 646 647 648 |
# File 'lib/snmp/manager.rb', line 645 def on_trap_default(&block) raise ArgumentError, "a block must be provided" unless block @lock.synchronize { @default_handler = block } end |
#on_trap_v1(&block) ⇒ Object
Define a trap handler block for all SNMPv1 traps. The trap yielded to the block will always be an SNMPv1_Trap.
664 665 666 667 |
# File 'lib/snmp/manager.rb', line 664 def on_trap_v1(&block) raise ArgumentError, "a block must be provided" unless block @lock.synchronize { @v1_handler = block } end |
#on_trap_v2c(&block) ⇒ Object
Define a trap handler block for all SNMPv2c traps. The trap yielded to the block will always be an SNMPv2_Trap. Note that InformRequest is a subclass of SNMPv2_Trap, so inform PDUs are also received by this handler.
675 676 677 678 |
# File 'lib/snmp/manager.rb', line 675 def on_trap_v2c(&block) raise ArgumentError, "a block must be provided" unless block @lock.synchronize { @v2c_handler = block } end |