Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi there,

I am receiving SNMP traps through syslog that are as follows:

Trap 1
------

Nov 30 16:54:10 localhost snmptrapd(2271): 192.168.0.5:
Link Up Trap (0) Uptime: 3:14:25.28,
interfaces.ifTable.ifEntry.ifIndex.2 = 2,
interfaces.ifTable.ifEntry.ifDescr.2 = Ethernet1,
interfaces.ifTable.ifEntry.ifType.2 = ethernetCsmacd(6),
enterprises.9.2.2.1.1.20.2 = "up"

Trap 2
------

Nov 30 16:54:20 localhost snmptrapd(2271): 192.168.0.5:
Link Down Trap (0) Uptime: 3:14:30.90,
interfaces.ifTable.ifEntry.ifIndex.2 = 2,
interfaces.ifTable.ifEntry.ifDescr.2 = Ethernet1,
interfaces.ifTable.ifEntry.ifType.2 = ethernetCsmacd(6),
enterprises.9.2.2.1.1.20.2 = "administratively
down"

I have placed the information that I need into an array.
What I need to do is store subsequent traps into arrays and
compare them and act on certain conditions. For example,
if interface Ethernet1 is up and then receives a Link Down Trap,
the output should reflect the change. This is a
cumulative state that should be reflected in the output
message if the Ethernet1 shows Link Up Trap. All traps are
received in the syslog and the messages should continue to
output as long as syslog updates continue.
There are other IP Addresses and interfaces that need to
be tracked as well.
Here is an example of the three variables that I need, placed into an array:

IP Address: 192.168.0.5
Trap: Link Up Trap
Interface: Ethernet1

I would be grateful for any suggestions.

Replies are listed 'Best First'.
Re: Comparing arrays
by atcroft (Abbot) on Dec 01, 2002 at 19:22 UTC

    I would suspect that it might be an easier option to use a hash of arrays, keyed on the IP address, so that you access something like $traparray{$device}[$eventnumber][$element]. This way would appear to allow easy access events for a specific $device.

    I think that would allow you to push the items (traptype, interface, otherinfo) into @{$traparray{$device}}, thus not having to maintain track of $eventnumber at that time.

    I look forward to the comments of other, wiser monks who may have more/better suggestions.

Re: Comparing arrays
by spurperl (Priest) on Dec 02, 2002 at 07:33 UTC
    If you want to keep subsequent traps and compare them, you may want to use an array of hashes. The array lists all traps, in the order of their occurence and the hash maps IP, Trap type and Interface.

    If you want to get seriously into this, you can also consider objects. Create an object to represent a Trap. Then you can compare them easily, define your own methods for them, etc.

    BTW, it's hard to see a direct correlation between the subject and the body of your post.