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

Methods included from Debug

#print_packet, #time

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)



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

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 = {}) ⇒ Object

Send an SNMPv2 inform

Options

  • oid: The OID of the inform

  • uptime: Integer indicating the uptime of this agent

  • varbinds: An array of hashes, like those used for PDU#add_varbind



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

def inform(options = {})
  pdu = build_v2_trap_pdu(Constants::SNMP_MSG_INFORM, options)
  response = send_pdu(pdu)
  if response.kind_of?(PDU)
    response.free # Always free the response PDU
    :success # If Session#send_pdu didn't raise, we succeeded
  else
    # If the result was other than a PDU, that's a problem
    raise "Unexpected response type for inform: #{response.class}"
  end
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



28
29
30
31
# File 'lib/net/snmp/trap_session.rb', line 28

def trap(options = {})
  pdu = build_v1_trap_pdu(options)
  send_pdu(pdu)
end

#trap_v2(options = {}) ⇒ Object

Send an SNMPv2 trap

Options

  • oid: The OID of the inform

  • uptime: Integer indicating the uptime of this agent

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.



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

def trap_v2(options = {})
  pdu = build_v2_trap_pdu(Constants::SNMP_MSG_TRAP2, options)
  send_pdu(pdu)
end