| 1 | ### SNMP_Table.pm | ||
| 2 | ### | ||
| 3 | ### Convenience routines for dealing with SNMP tables | ||
| 4 | ### | ||
| 5 | ### Author: Simon Leinen <simon@switch.ch> | ||
| 6 | ### Date created: 24-Apr-2005 | ||
| 7 | ### | ||
| 8 | ### Tables are a central concept to SNMP (or, more precisely, of the | ||
| 9 | ### SMI or Structure of Management Instrumentation). SNMP tables are | ||
| 10 | ### conceptually similar to tables in relational database management | ||
| 11 | ### systems (RDBMS). Each SNMP table has at least one conceptual | ||
| 12 | ### columns, of which at least one serve as an index columns. | ||
| 13 | ### | ||
| 14 | ### This little library implements a form of "object-relational | ||
| 15 | ### mapping", or rather, a "relational-object mapping" from SNMP | ||
| 16 | ### tables to Perl objects. | ||
| 17 | ### | ||
| 18 | ### In particular, they convert SNMP table rows into Perl objects, and | ||
| 19 | ### allow a user to store these objects into some composite structure | ||
| 20 | ### based on the index(es). | ||
| 21 | |||
| 22 | package SNMP_Table; | ||
| 23 | |||
| 24 | require 5.004; | ||
| 25 | |||
| 26 | use strict; | ||
| 27 | use vars qw(@ISA @EXPORT $VERSION); | ||
| 28 | use warnings; | ||
| 29 | use Exporter; | ||
| 30 | |||
| 31 | use SNMP_util; | ||
| 32 | |||
| 33 | $VERSION = '1.16'; | ||
| 34 | |||
| 35 | @ISA = qw(Exporter); | ||
| 36 | |||
| 37 | @EXPORT = qw(snmp_rows_to_objects snmp_row_to_object snmp_map_row_objects); | ||
| 38 | |||
| 39 | ### snmp_rows_to_objects TARGET, CLASS, PREFIX, COLUMNS... | ||
| 40 | ### | ||
| 41 | ### Returns a reference to a hash that maps a table's index to objects | ||
| 42 | ### created from the set of COLUMNS. The COLUMNS are partial OID | ||
| 43 | ### names, to each of which the PREFIX is prepended. An object is | ||
| 44 | ### created for each row in the table, by creating a hash reference | ||
| 45 | ### with a slot for each column, named by the (partial) column name. | ||
| 46 | ### It is blessed to the CLASS. | ||
| 47 | ### | ||
| 48 | ### For example, if we have the following table at $TARGET: | ||
| 49 | ### | ||
| 50 | ### index fooBar fooBaz fooBlech | ||
| 51 | ### | ||
| 52 | ### 1000 asd 23498 vohdajae | ||
| 53 | ### 1001 fgh 45824 yaohetoo | ||
| 54 | ### 1002 jkl 89732 engahghi | ||
| 55 | ### | ||
| 56 | ### Then the call: | ||
| 57 | ### | ||
| 58 | ### snmp_rows_to_objects ($TARGET, 'MyFoo', 'foo', 'bar', 'baz', 'blech') | ||
| 59 | ### | ||
| 60 | ### will create a hash reference similar to this: | ||
| 61 | ### | ||
| 62 | ### $result = {}; | ||
| 63 | ### $result{1000} = bless { 'bar' => 'asd', | ||
| 64 | ### 'baz' => 23498, | ||
| 65 | ### 'blech' => 'vohdajae' }, 'MyFoo'; | ||
| 66 | ### $result{1001} = bless { 'bar' => 'fgh', | ||
| 67 | ### 'baz' => 45824, | ||
| 68 | ### 'blech' => 'yaohetoo' }, 'MyFoo'; | ||
| 69 | ### $result{1002} = bless { 'bar' => 'jkl', | ||
| 70 | ### 'baz' => 89732, | ||
| 71 | ### 'blech' => 'engahghi' }, 'MyFoo'; | ||
| 72 | ### | ||
| 73 | sub snmp_rows_to_objects ($$$@) { | ||
| 74 | my ($target, $class, $prefix, @cols) = @_; | ||
| 75 | my $result = {}; | ||
| 76 | snmp_map_row_objects | ||
| 77 | ($target, $class, | ||
| 78 | sub () { | ||
| 79 | my ($index, $object) = @_; | ||
| 80 | $result->{$index} = $object; | ||
| 81 | }, | ||
| 82 | $prefix, @cols); | ||
| 83 | return $result; | ||
| 84 | } | ||
| 85 | |||
| 86 | ### snmp_map_row_objects TARGET, CLASS, MAPFN, PREFIX, COLUMNS... | ||
| 87 | ### | ||
| 88 | ### This function traverses a table, creating an object for each row, | ||
| 89 | ### and applying the user-supplied MAPFN to each of these objects. | ||
| 90 | ### | ||
| 91 | ### The table is defined by PREFIX and COLUMNS, as described for | ||
| 92 | ### snmp_rows_to_objects above. An object is created according to | ||
| 93 | ### CLASS and COLUMNS, as described above. The difference is that, | ||
| 94 | ### rather than putting all objects in a hash, we simply apply the | ||
| 95 | ### user-supplied MAPFN to each row object. | ||
| 96 | ### | ||
| 97 | sub snmp_map_row_objects ($$$$@) { | ||
| 98 | my ($target, $class, $mapfn, $prefix, @cols) = @_; | ||
| 99 | my @coloids = map ($prefix.ucfirst $_,@cols); | ||
| 100 | my @slotnames = @cols; | ||
| 101 | snmpmaptable ($target, | ||
| 102 | sub () { | ||
| 103 | my ($index, @colvals) = @_; | ||
| 104 | my $object = bless { 'index' => $index }, $class; | ||
| 105 | foreach my $slotname (@slotnames) { | ||
| 106 | $object->{$slotname} = shift @colvals; | ||
| 107 | } | ||
| 108 | &$mapfn ($index, $object); | ||
| 109 | }, | ||
| 110 | @coloids); | ||
| 111 | } | ||
| 112 | |||
| 113 | ### snmp_row_to_object TARGET, CLASS, INDEX, PREFIX, COLUMNS... | ||
| 114 | ### | ||
| 115 | ### This can be used if one is only interested in a single row, | ||
| 116 | ### defined by INDEX. This function uses a large SNMP get request to | ||
| 117 | ### retrieve all columns of interest, and assembles the result into a | ||
| 118 | ### hash blessed to CLASS. This hash directly represents the row | ||
| 119 | ### object. Note that this function returns just a single hash, as | ||
| 120 | ### opposed to snmp_rows_to_objects, which returns a hash that maps | ||
| 121 | ### index values to such hashes. | ||
| 122 | ### | ||
| 123 | sub snmp_row_to_object ($$$$@ ) { | ||
| 124 | my ($target, $class, $index, $prefix, @cols) = @_; | ||
| 125 | my @coloids = map ($prefix.ucfirst $_.".".$index,@cols); | ||
| 126 | my @result = snmpget ($target, @coloids); | ||
| 127 | my %result = (); | ||
| 128 | foreach my $col (@cols) { | ||
| 129 | $result{$col} = shift @result; | ||
| 130 | } | ||
| 131 | bless \%result, $class; | ||
| 132 | return \%result; | ||
| 133 | } | ||
| 134 | |||
| 135 | 1; |