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, #port, #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, #send_pdu_blocking, #set, #snmp_err, #table, #walk

Methods included from Debug

#debug, #error, #fatal, #info, #print_packet, #time, #warn

Constructor Details

#initialize(options = {}) ⇒ TrapSession

options

  • :peername The address where the trap will be sent

  • :port The port where the trap will be sent (default = 162)



9
10
11
12
13
14
15
16
17
# File 'lib/net/snmp/trap_session.rb', line 9

def initialize(options = {})
  # Unless the port was supplied in the peername...
  unless options[:peername][":"]
    # ...default to standard trap port
    options[:port] ||= 162
  end

  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



68
69
70
71
72
73
74
75
76
77
# File 'lib/net/snmp/trap_session.rb', line 68

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)
  result = send_pdu(pdu, &callback)
  pdu.free
  result
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

  • uptime: The uptime for this agent



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/net/snmp/trap_session.rb', line 27

def trap(options = {})
  pdu = PDU.new(Constants::SNMP_MSG_TRAP)
  options[:enterprise] ||= '1.3.6.1.4.1.3.1.1'
  pdu.enterprise = OID.new(options[:enterprise].to_s)
  pdu.trap_type = options[:trap_type].to_i || 1  # need to check all these defaults
  pdu.specific_type = options[:specific_type].to_i || 0
  pdu.time = options[:uptime].to_i || 1
  pdu.agent_addr = options[:agent_addr] || '127.0.0.1'
  if options[:varbinds]
    options[:varbinds].each do |vb|
      pdu.add_varbind(vb)
    end
  end
  result = send_pdu(pdu)
  pdu.free
  result
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

TODO: You can only send v1 traps on a v1 session, and same for v2. So, we could always have the client call ‘trap` and just do the right thing based on the session.



53
54
55
56
57
58
59
60
61
62
# File 'lib/net/snmp/trap_session.rb', line 53

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)
  result = send_pdu(pdu)
  pdu.free
  result
end