Debian Codegraph @

check_hpasm/check_hpasm-4.9/plugins-scripts/HP/SNMP/Utils.pm

raw
1 package SNMP::Utils;
2
3 use strict;
4
5 {
6 sub get_indices {
7 my $oids = shift;
8 my $entry = shift;
9 my $numindices = shift;
10 # find all oids beginning with $entry
11 # then skip one field for the sequence
12 # then read the next numindices fields
13 my $entrypat = $entry;
14 $entrypat =~ s/\./\\\./g;
15 my @indices = map {
16 /^$entrypat\.\d+\.(.*)/ && $1;
17 } grep {
18 /^$entrypat/
19 } keys %{$oids};
20 my %seen = ();
21 my @o = map {[split /\./]} sort grep !$seen{$_}++, @indices;
22 return @o;
23 }
24
25 sub get_size {
26 my $oids = shift;
27 my $entry = shift;
28 my $entrypat = $entry;
29 $entrypat =~ s/\./\\\./g;
30 my @entries = grep {
31 /^$entrypat/
32 } keys %{$oids};
33 return scalar(@entries);
34 }
35
36 sub get_object {
37 my $oids = shift;
38 my $object = shift;
39 my @indices = @_;
40 #my $oid = $object.'.'.join('.', @indices);
41 my $oid = $object;
42 $oid .= '.'.join('.', @indices) if (@indices);
43 return $oids->{$oid};
44 }
45
46 sub get_object_value {
47 my $oids = shift;
48 my $object = shift;
49 my $values = shift;
50 my @indices = @_;
51 my $key = get_object($oids, $object, @indices);
52 if (defined $key) {
53 return $values->{$key};
54 } else {
55 return undef;
56 }
57 }
58
59 #SNMP::Utils::counter([$idxs1, $idxs2], $idx1, $idx2),
60 # this flattens a n-dimensional array and returns the absolute position
61 # of the element at position idx1,idx2,...,idxn
62 # element 1,2 in table 0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2 is at pos 6
63 sub get_number {
64 my $indexlists = shift; #, zeiger auf array aus [1, 2]
65 my @element = @_;
66 my $dimensions = scalar(@{$indexlists->[0]});
67 my @sorted = ();
68 my $number = 0;
69 if ($dimensions == 1) {
70 @sorted =
71 sort { $a->[0] <=> $b->[0] } @{$indexlists};
72 } elsif ($dimensions == 2) {
73 @sorted =
74 sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @{$indexlists};
75 } elsif ($dimensions == 3) {
76 @sorted =
77 sort { $a->[0] <=> $b->[0] ||
78 $a->[1] <=> $b->[1] ||
79 $a->[2] <=> $b->[2] } @{$indexlists};
80 }
81 foreach (@sorted) {
82 if ($dimensions == 1) {
83 if ($_->[0] == $element[0]) {
84 last;
85 }
86 } elsif ($dimensions == 2) {
87 if ($_->[0] == $element[0] && $_->[1] == $element[1]) {
88 last;
89 }
90 } elsif ($dimensions == 3) {
91 if ($_->[0] == $element[0] &&
92 $_->[1] == $element[1] &&
93 $_->[2] == $element[2]) {
94 last;
95 }
96 }
97 $number++;
98 }
99 return ++$number;
100 }
101
102 }
103