Class: Net::SNMP::TrapSession

Inherits:
Session
  • Object
show all
Defined in:
lib/net/snmp/trap_session.rb

Instance Attribute Summary

Attributes inherited from Session

#callback, #community, #peername, #requests, #struct, #version

Instance Method Summary collapse

Methods inherited from Session

#close, #columns, #default_max_repeaters, #errno, #error, #error_message, #get, #get_bulk, #get_next, #method_missing, open, #print_errors, #select, #send_pdu, #set, #snmp_err, #table, #walk

Methods included from Debug

#debug, debug=

Constructor Details

#initialize(options = {}) ⇒ TrapSession

Represents a session for sending SNMP traps



5
6
7
8
# File 'lib/net/snmp/trap_session.rb', line 5

def initialize(options = {})
  options[:peername] = "#{options[:peername]}:162"
  super(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Net::SNMP::Session

Instance Method Details

#inform(options = {}, &callback) ⇒ Object

Send an SNMPv2 inform. Can accept a callback to execute on confirmation of the inform +options

  • :oid The OID of the inform

  • :varbinds A list of Varbind objects to send with the inform



43
44
45
46
47
48
49
50
# File 'lib/net/snmp/trap_session.rb', line 43

def inform(options = {}, &callback)
  if options[:oid].kind_of?(String)
    options[:oid] = Net::SNMP::OID.new(options[:oid])
  end
  pdu = PDU.new(Constants::SNMP_MSG_INFORM)
  build_trap_pdu(pdu, options)
  send_pdu(pdu, &callback)
end

#trap(options = {}) ⇒ Object

Send an SNMPv1 trap options

  • :enterprise The Oid of the enterprise

  • :trap_type The generic trap type.

  • :specific_type The specific trap type



15
16
17
18
19
20
21
22
23
24
# File 'lib/net/snmp/trap_session.rb', line 15

def trap(options = {})
  pdu = PDU.new(Constants::SNMP_MSG_TRAP)
  options[:enterprise] ||= '1.3.6.1.4.1.3.1.1'  # uh, just send netsnmp enterprise i guess
  pdu.enterprise = OID.new(options[:enterprise])
  pdu.trap_type = options[:trap_type] || 1  # need to check all these defaults
  pdu.specific_type = options[:specific_type] || 0
  pdu.time = 1    # put what here?
  send_pdu(pdu)
  true
end

#trap_v2(options = {}) ⇒ Object

Send an SNMPv2 trap +options

  • :oid The Oid of the trap

  • :varbinds A list of Varbind objects to send with the trap



30
31
32
33
34
35
36
37
# File 'lib/net/snmp/trap_session.rb', line 30

def trap_v2(options = {})
  if options[:oid].kind_of?(String)
    options[:oid] = Net::SNMP::OID.new(options[:oid])
  end
  pdu = PDU.new(Constants::SNMP_MSG_TRAP2)
  build_trap_pdu(pdu, options)
  send_pdu(pdu)
end