funktional prototype get_net

This commit is contained in:
Arne Baeumler 2017-11-05 18:03:48 +01:00
parent b961b84f81
commit 4eb3478db5
2 changed files with 91 additions and 42 deletions

View File

@ -1,4 +1,4 @@
#!/usr/bin/perl -tw #!/usr/bin/perl -w
use strict; use strict;
use warnings; use warnings;
@ -6,26 +6,14 @@ use lib './libs';
use Getopt::Long qw(:config no_ignore_case); use Getopt::Long qw(:config no_ignore_case);
use Data::Dumper; use Data::Dumper;
use Net::SNMP; use Net::SNMP;
use Cache::File;
my $verbose = ''; my $PARAMS = {
my $type = ''; type => '',
my $mode = ''; mode => '',
my $host = ''; host => '',
my $community = ''; community => '',
};
GetOptions (
'c=s' => \$community,
'h' => \&help,
'H=s' => \$host,
'm=s' => \$mode,
't=s' => \$type,
'community=s' => \$community,
'help' => \&help,
'host=s' => \$host,
'mode=s' => \$mode,
'type=s' => \$type,
);
my $typeMap = { my $typeMap = {
'snmpd' => 'TYPE::SNMPD', 'snmpd' => 'TYPE::SNMPD',
@ -39,34 +27,83 @@ my $modeMap = {
'net' => \&get_net 'net' => \&get_net
}; };
my $t = $typeMap->{$type};
eval "use $t";
my $object = eval { $t->new() };
sub main {
GetOptions (
'c|community=s' => \$PARAMS->{community},
'h|help' => \&help,
'H|host=s' => \$PARAMS->{host},
'm|mode=s' => \$PARAMS->{mode},
't|type=s' => \$PARAMS->{type},
);
my $handler = &initHandler($typeMap->{$PARAMS->{type}}) or die($@);
my $result = eval { $modeMap->{$mode}->() }; my $cache = Cache::File->new(
cache_root => '/tmp/check_snmp/cache',
default_expires => '3600 sec'
) or die($@);
my $data = $cache->get($PARAMS->{host}) || {};
my $result = $modeMap->{$PARAMS->{mode}}->($handler,$data);
print "result: " . Dumper($result);
return(1);
}
sub initHandler {
my ($t) = @_;
die("no type") unless($t);
eval "use $t;";
die($@) if($@);
my $r = ${t}->new($PARAMS);
die($@) if($@);
return($r);
}
sub _net_generate_ifIndex {
my ($handler,$data) = @_;
my $r = {};
my $t = $handler->{SNMP}->get_table(-baseoid => $handler->{OID}->{NET}->{IfDescr});
foreach my $k (sort keys %$t) {
if($k =~ /^$handler->{OID}->{NET}->{IfDescr}\.(\d+)/) {
$r->{$1} = $t->{$k};
}
}
return($r);
}
sub get_cpu { sub get_cpu {
my ($handler,$data) = @_;
} }
sub get_temp { sub get_temp {
my ($handler,$data) = @_;
} }
sub get_net { sub get_net {
print "Get Net Data...\n"; my ($handler,$data) = @_;
my %params; my $params = {};
my ($snmp,$error) = Net::SNMP->session(
-hostname => $host,
-community => $community,
-nonblocking => 0
);
my $result = $snmp->get_table(-baseoid => $object->{NET}->{IfTable});
print Dumper($result);
return (%params); print "Cache: ";
unless(exists($data->{net_ifIndex})) {
$data->{net_ifIndex} = &_net_generate_ifIndex($handler) ;
}
foreach my $i (sort keys %{$data->{net_ifIndex}}) {
foreach my $p (sort keys %{$handler->{OID}->{NET}}) {
my $r = $handler->{SNMP}->get_request(-varbindlist => [ $handler->{OID}->{NET}->{$p} . ".$i" ]);
($params->{$data->{net_ifIndex}->{$i}}->{$p}) = values %$r;
}
}
return ($params);
} }
sub help { sub help {
@ -81,4 +118,5 @@ Usage: $0 [...]
return(0); return(0);
} }
&main();
exit(0); exit(0);

View File

@ -1,6 +1,8 @@
package TYPE::SNMPD; package TYPE::SNMPD;
require Exporter; require Exporter;
use Net::SNMP;
use Data::Dumper;
our @ISA = qw(Exporter); our @ISA = qw(Exporter);
our @EXPORT = qw(); our @EXPORT = qw();
@ -10,18 +12,27 @@ my $oidMap = {
CPU => { CPU0 => '' }, CPU => { CPU0 => '' },
TEMP => { TEMP0 => '' }, TEMP => { TEMP0 => '' },
NET => { NET => {
IfTable => '.1.3.6.1.2.1.2', IfDescr => '.1.3.6.1.2.1.2.2.1.2',
IfInOct => '', IfInOct => '.1.3.6.1.2.1.2.2.1.10',
IfOutOct => '', IfOutOct => '.1.3.6.1.2.1.2.2.1.16',
IfInErr => '', IfInErr => '.1.3.6.1.2.1.2.2.1.14',
IfOutErr => '', IfOutErr => '.1.3.6.1.2.1.2.2.1.20',
}, },
}; };
sub new { sub new {
my ($class) = @_; my ($class,$p) = @_;
my $self = bless($oidMap,$class); my $self = bless({},$class);
$self->{OID} = $oidMap;
my ($s,$e) = Net::SNMP->session(
-hostname => $p->{host},
-community => $p->{community}
);
die($e) if($e);
$self->{SNMP} = $s;
return($self); return($self);
} }