funktional prototype get_net
This commit is contained in:
parent
b961b84f81
commit
4eb3478db5
@ -1,4 +1,4 @@
|
||||
#!/usr/bin/perl -tw
|
||||
#!/usr/bin/perl -w
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
@ -6,26 +6,14 @@ use lib './libs';
|
||||
use Getopt::Long qw(:config no_ignore_case);
|
||||
use Data::Dumper;
|
||||
use Net::SNMP;
|
||||
use Cache::File;
|
||||
|
||||
my $verbose = '';
|
||||
my $type = '';
|
||||
my $mode = '';
|
||||
my $host = '';
|
||||
my $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 $PARAMS = {
|
||||
type => '',
|
||||
mode => '',
|
||||
host => '',
|
||||
community => '',
|
||||
};
|
||||
|
||||
my $typeMap = {
|
||||
'snmpd' => 'TYPE::SNMPD',
|
||||
@ -39,34 +27,83 @@ my $modeMap = {
|
||||
'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 {
|
||||
my ($handler,$data) = @_;
|
||||
|
||||
}
|
||||
|
||||
sub get_temp {
|
||||
my ($handler,$data) = @_;
|
||||
|
||||
}
|
||||
|
||||
|
||||
sub get_net {
|
||||
print "Get Net Data...\n";
|
||||
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);
|
||||
my ($handler,$data) = @_;
|
||||
my $params = {};
|
||||
|
||||
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 {
|
||||
@ -81,4 +118,5 @@ Usage: $0 [...]
|
||||
return(0);
|
||||
}
|
||||
|
||||
&main();
|
||||
exit(0);
|
||||
|
@ -1,6 +1,8 @@
|
||||
package TYPE::SNMPD;
|
||||
|
||||
require Exporter;
|
||||
use Net::SNMP;
|
||||
use Data::Dumper;
|
||||
|
||||
our @ISA = qw(Exporter);
|
||||
our @EXPORT = qw();
|
||||
@ -10,18 +12,27 @@ my $oidMap = {
|
||||
CPU => { CPU0 => '' },
|
||||
TEMP => { TEMP0 => '' },
|
||||
NET => {
|
||||
IfTable => '.1.3.6.1.2.1.2',
|
||||
IfInOct => '',
|
||||
IfOutOct => '',
|
||||
IfInErr => '',
|
||||
IfOutErr => '',
|
||||
IfDescr => '.1.3.6.1.2.1.2.2.1.2',
|
||||
IfInOct => '.1.3.6.1.2.1.2.2.1.10',
|
||||
IfOutOct => '.1.3.6.1.2.1.2.2.1.16',
|
||||
IfInErr => '.1.3.6.1.2.1.2.2.1.14',
|
||||
IfOutErr => '.1.3.6.1.2.1.2.2.1.20',
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
sub new {
|
||||
my ($class) = @_;
|
||||
my $self = bless($oidMap,$class);
|
||||
my ($class,$p) = @_;
|
||||
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);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user