From 4eb3478db5bfcbc70bcb26220c990c4b1a7e4986 Mon Sep 17 00:00:00 2001 From: Arne Baeumler Date: Sun, 5 Nov 2017 18:03:48 +0100 Subject: [PATCH] funktional prototype get_net --- plugins/snmp/check_snmp.pl | 108 +++++++++++++++++++++----------- plugins/snmp/libs/TYPE/SNMPD.pm | 25 +++++--- 2 files changed, 91 insertions(+), 42 deletions(-) diff --git a/plugins/snmp/check_snmp.pl b/plugins/snmp/check_snmp.pl index d4506ca..c01c708 100755 --- a/plugins/snmp/check_snmp.pl +++ b/plugins/snmp/check_snmp.pl @@ -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); diff --git a/plugins/snmp/libs/TYPE/SNMPD.pm b/plugins/snmp/libs/TYPE/SNMPD.pm index 5cfd925..d79f9e7 100644 --- a/plugins/snmp/libs/TYPE/SNMPD.pm +++ b/plugins/snmp/libs/TYPE/SNMPD.pm @@ -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); }