Class: SNMP::SNMPv1_Trap

Inherits:
Object
  • Object
show all
Defined in:
lib/snmp/pdu.rb

Overview

The PDU class for traps in SNMPv1.

Constant Summary collapse

GENERIC_TRAP_NAME =

Name map for all of the generic traps defined in RFC 1157.

{
  0 => :coldStart,
  1 => :warmStart,
  2 => :linkDown,
  3 => :linkUp,
  4 => :authenticationFailure,
  5 => :egpNeighborLoss,
  6 => :enterpriseSpecific
}
GENERIC_TRAP_CODE =

Code map for all of the generic traps defined in RFC 1157.

GENERIC_TRAP_NAME.invert

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enterprise, agent_addr, generic_trap, specific_trap, timestamp, varbind_list) ⇒ SNMPv1_Trap

Returns a new instance of SNMPv1_Trap



341
342
343
344
345
346
347
348
# File 'lib/snmp/pdu.rb', line 341

def initialize(enterprise, agent_addr, generic_trap, specific_trap, timestamp, varbind_list)
  @enterprise = enterprise
  @agent_addr = agent_addr
  self.generic_trap = generic_trap
  @specific_trap = specific_trap
  @timestamp = timestamp
  @varbind_list = varbind_list
end

Instance Attribute Details

#agent_addrObject

Returns the value of attribute agent_addr



319
320
321
# File 'lib/snmp/pdu.rb', line 319

def agent_addr
  @agent_addr
end

#enterpriseObject

Returns the value of attribute enterprise



318
319
320
# File 'lib/snmp/pdu.rb', line 318

def enterprise
  @enterprise
end

#source_ipObject

Returns the source IP address for the trap, usually derived from the source IP address of the packet that delivered the trap.



316
317
318
# File 'lib/snmp/pdu.rb', line 316

def source_ip
  @source_ip
end

#specific_trapObject

Returns the value of attribute specific_trap



320
321
322
# File 'lib/snmp/pdu.rb', line 320

def specific_trap
  @specific_trap
end

#timestampObject

Returns the value of attribute timestamp



321
322
323
# File 'lib/snmp/pdu.rb', line 321

def timestamp
  @timestamp
end

#varbind_listObject Also known as: vb_list

Returns the value of attribute varbind_list



322
323
324
# File 'lib/snmp/pdu.rb', line 322

def varbind_list
  @varbind_list
end

Class Method Details

.decode(pdu_data, mib = nil) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/snmp/pdu.rb', line 326

def self.decode(pdu_data, mib=nil)
  oid_data, remainder = decode_object_id(pdu_data)
  enterprise = ObjectId.new(oid_data)
  ip_data, remainder = decode_ip_address(remainder)
  agent_addr = IpAddress.new(ip_data)
  generic_trap, remainder = decode_integer(remainder)
  specific_trap, remainder = decode_integer(remainder)
  time_data, remainder = decode_timeticks(remainder)
  timestamp = TimeTicks.new(time_data)
  varbind_list, remainder = VarBindList.decode(remainder, mib)
  assert_no_remainder(remainder)
  SNMPv1_Trap.new(enterprise, agent_addr, generic_trap, specific_trap,
                  timestamp, varbind_list)
end

Instance Method Details

#each_varbind(&block) ⇒ Object



389
390
391
# File 'lib/snmp/pdu.rb', line 389

def each_varbind(&block)
  @varbind_list.each(&block)
end

#encodeObject



379
380
381
382
383
384
385
386
387
# File 'lib/snmp/pdu.rb', line 379

def encode
  pdu_data = @enterprise.encode <<
    @agent_addr.encode <<
    encode_integer(@generic_trap) <<
    encode_integer(@specific_trap) <<
    @timestamp.encode <<
    @varbind_list.encode
  encode_tlv(SNMPv1_Trap_PDU_TAG, pdu_data)
end

#generic_trapObject



375
376
377
# File 'lib/snmp/pdu.rb', line 375

def generic_trap
  GENERIC_TRAP_NAME[@generic_trap]
end

#generic_trap=(trap) ⇒ Object



364
365
366
367
368
369
370
371
372
373
# File 'lib/snmp/pdu.rb', line 364

def generic_trap=(trap)
  @generic_trap = GENERIC_TRAP_CODE[trap]
  unless @generic_trap
    if trap.respond_to?(:to_i) && GENERIC_TRAP_NAME[trap.to_i]
      @generic_trap = trap
    else
      raise InvalidGenericTrap, trap.to_s
    end
  end
end