Monitoring-GLPlugin
view release on metacpan or search on metacpan
lib/Monitoring/GLPlugin/SNMP.pm view on Meta::CPAN
$key_attrs = $sortednames[0];
}
$key_attrs = [$key_attrs];
}
my $key_attr_id = join('#', @{$key_attrs});
my $cache = sprintf "%s_%s_%s_cache", $mib, $table, $key_attr_id;
my $statefile = $self->create_entry_cache_file($mib, $table, $key_attr_id);
if ($force == -1 && -f $statefile) {
$must_update = 1;
# brauchts unter keinen umstaenden.
# z.b. wenn ein vorhergehender update_interfaces_cache keine aenderungen
# angezeigt hat, dann spart man sich hier den etherlike-update o.ae.
$self->debug(sprintf 'skip update of %s %s %s %s cache',
$self->opts->hostname, $self->opts->mode, $mib, $table);
} elsif ($force != 0 || ! -f $statefile || ((stat $statefile)[9]) < ($update_deadline)) {
$must_update = 1;
$self->debug(sprintf 'force update of %s %s %s %s cache',
$self->opts->hostname, $self->opts->mode, $mib, $table);
$self->{$cache} = {};
foreach my $entry ($self->get_snmp_table_objects($mib, $table, undef, $key_attrs)) {
my $key = join('#', map { $entry->{$_} } @{$key_attrs});
my $hash = $key . '-//-' . join('.', @{$entry->{indices}});
$self->{$cache}->{$hash} = $entry->{indices};
}
$self->save_cache($mib, $table, $key_attrs);
}
$self->load_cache($mib, $table, $key_attrs);
return $must_update;
}
sub save_cache {
my ($self, $mib, $table, $key_attrs) = @_;
my $cache = sprintf "%s_%s_%s_cache", $mib, $table, join('#', @{$key_attrs});
$self->create_statefilesdir();
my $statefile = $self->create_entry_cache_file($mib, $table, join('#', @{$key_attrs}));
open(STATE, ">".$statefile.".".$$);
printf STATE Data::Dumper::Dumper($self->{$cache});
close STATE;
rename $statefile.".".$$, $statefile;
$self->debug(sprintf "saved %s to %s",
Data::Dumper::Dumper($self->{$cache}), $statefile);
}
sub load_cache {
my ($self, $mib, $table, $key_attrs) = @_;
my $cache = sprintf "%s_%s_%s_cache", $mib, $table, join('#', @{$key_attrs});
my $statefile = $self->create_entry_cache_file($mib, $table, join('#', @{$key_attrs}));
$self->{$cache} = {};
if ( -f $statefile) {
our $VAR1;
our $VAR2;
eval {
require $statefile;
};
if($@) {
printf "FATAL: Could not load cache!\n";
}
# keinesfalls mehr require verwenden!!!!!!
# beim require enthaelt VAR1 andere werte als beim slurp
# und zwar diejenigen, die beim letzten save_cache geschrieben wurden.
my $content = do { local (@ARGV, $/) = $statefile; my $x = <>; close ARGV; $x };
$VAR1 = eval "$content";
$self->debug(sprintf "load %s", Data::Dumper::Dumper($VAR1));
$self->{$cache} = $VAR1;
}
}
################################################################
# top-level convenience functions
#
sub get_snmp_objects {
my ($self, $mib, @mos) = @_;
foreach (@mos) {
my $value = $self->get_snmp_object($mib, $_);
if (defined $value) {
$self->{$_} = $value;
}
}
}
sub get_snmp_tables {
my ($self, $mib, $infos) = @_;
foreach my $info (@{$infos}) {
my $arrayname = $info->[0];
my $table = $info->[1];
my $class = $info->[2];
my $filter = $info->[3];
my $rows = $info->[4];
$self->{$arrayname} = [] if ! exists $self->{$arrayname};
if (! exists $Monitoring::GLPlugin::SNMP::tablecache->{$mib} || ! exists $Monitoring::GLPlugin::SNMP::tablecache->{$mib}->{$table}) {
$Monitoring::GLPlugin::SNMP::tablecache->{$mib}->{$table} = [];
foreach ($self->get_snmp_table_objects($mib, $table, undef, $rows)) {
push(@{$Monitoring::GLPlugin::SNMP::tablecache->{$mib}->{$table}}, $_);
my $new_object = $class->new(%{$_});
next if (defined $filter && ! &$filter($new_object));
push(@{$self->{$arrayname}}, $new_object);
}
} else {
$self->debug(sprintf "get_snmp_tables %s %s cache hit", $mib, $table);
foreach (@{$Monitoring::GLPlugin::SNMP::tablecache->{$mib}->{$table}}) {
my $new_object = $class->new(%{$_});
next if (defined $filter && ! &$filter($new_object));
push(@{$self->{$arrayname}}, $new_object);
}
}
}
}
sub merge_tables {
my ($self, $into, @from) = @_;
my $into_indices = {};
map { $into_indices->{$_->{flat_indices}} = $_ } @{$self->{$into}};
foreach (@from) {
foreach my $element (@{$self->{$_}}) {
if (exists $into_indices->{$element->{flat_indices}}) {
foreach my $key (keys %{$element}) {
$into_indices->{$element->{flat_indices}}->{$key} = $element->{$key};
}
}
}
( run in 1.041 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )