Class: SNMP::TrapListener
- Inherits:
-
Object
- Object
- SNMP::TrapListener
- Defined in:
- lib/snmp/manager.rb
Overview
Constant Summary collapse
- DefaultConfig =
{ :Host => 'localhost', :Port => 162, :Community => 'public', :ServerTransport => UDPServerTransport, :MaxReceiveBytes => 8000}
- NULL_HANDLER =
Proc.new {}
Instance Method Summary collapse
-
#exit ⇒ Object
(also: #kill, #terminate)
Stops the trap handler thread and releases the socket.
-
#initialize(config = {}, &block) ⇒ TrapListener
constructor
Start a trap handler thread.
-
#join ⇒ Object
Joins the current thread to the trap handler thread.
-
#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(config = {}, &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
557 558 559 560 561 562 563 564 565 566 567 568 |
# File 'lib/snmp/manager.rb', line 557 def initialize(config={}, &block) @config = DefaultConfig.dup.update(config) @transport = @config[:ServerTransport].new(@config[:Host], @config[:Port]) @max_bytes = @config[:MaxReceiveBytes] @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 Method Details
#exit ⇒ Object Also known as: kill, terminate
Stops the trap handler thread and releases the socket.
See also Thread#exit.
624 625 626 627 |
# File 'lib/snmp/manager.rb', line 624 def exit @handler_thread.exit @transport.close end |
#join ⇒ Object
Joins the current thread to the trap handler thread.
See also Thread#join.
615 616 617 |
# File 'lib/snmp/manager.rb', line 615 def join @handler_thread.join 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).
585 586 587 588 |
# File 'lib/snmp/manager.rb', line 585 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.
575 576 577 578 |
# File 'lib/snmp/manager.rb', line 575 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.
594 595 596 597 |
# File 'lib/snmp/manager.rb', line 594 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.
605 606 607 608 |
# File 'lib/snmp/manager.rb', line 605 def on_trap_v2c(&block) raise ArgumentError, "a block must be provided" unless block @lock.synchronize { @v2c_handler = block } end |