Changeset 8234 for lang/perl/App-MadEye

Show
Ignore:
Timestamp:
03/21/08 11:03:26 (8 months ago)
Author:
tokuhirom
Message:

refactoring.

Location:
lang/perl/App-MadEye/trunk/lib/App/MadEye
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/App-MadEye/trunk/lib/App/MadEye/Plugin/Agent/SNMP/Process.pm

    r8155 r8234  
    33use warnings; 
    44use App::MadEye::Plugin::Agent::Base; 
    5 use Net::SNMP; 
     5use App::MadEye::Util; 
     6use List::Util qw/first/; 
    67 
    78my $hrSWRunPath = '.1.3.6.1.2.1.25.4.2.1.4'; 
     
    1213    my $process   = $self->config->{config}->{process}   or die "missing process"; 
    1314 
    14     my $response = $self->snmp_session( 
     15    my $response = snmp_session( 
     16        $self, 
    1517        $host => sub { 
    1618            my $session = shift; 
     
    2224    ); 
    2325 
    24     my $process_cnt = scalar grep { $_ eq $process } values %$response; 
    25  
    26     App::MadEye->context->log(debug => "$host has $process_cnt $process"); 
    27  
    28     if ($process_cnt > 0) { 
     26    if (first { $_ eq $process } values %$response) { 
    2927        return; # alive 
    3028    } else { 
    31         return $process_cnt; 
    32     } 
    33 } 
    34  
    35 sub snmp_session { 
    36     my ($self, $host, $callback, ) = @_; 
    37  
    38     my $community = $self->config->{config}->{community} or die "missing community"; 
    39     my $port      = $self->config->{config}->{port}    || 161; 
    40     my $timeout   = $self->config->{config}->{timeout} || 10; 
    41  
    42     my ($session, $error) = Net::SNMP->session( 
    43         -hostname  => $host, 
    44         -community => $community, 
    45         -port      => $port, 
    46         -timeout   => $timeout, 
    47     ); 
    48  
    49     if (not defined($session)) { 
    50         die "ERROR: $error.\n"; 
    51     } else { 
    52         my $response = $callback->($session); 
    53         $session->close(); 
    54         return $response; 
     29        return "404 $process not found"; 
    5530    } 
    5631} 
  • lang/perl/App-MadEye/trunk/lib/App/MadEye/Util.pm

    r8154 r8234  
    44use base qw/Exporter/; 
    55 
    6 our @EXPORT = qw/timeout log_stopwatch get_schema_from_pod/; 
     6our @EXPORT = qw/timeout get_schema_from_pod snmp_session/; 
    77 
    88use Sys::Syslog qw/:DEFAULT/; 
     
    1111use YAML (); 
    1212use Time::HiRes qw/gettimeofday/; 
     13use Net::SNMP; 
    1314 
    1415sub timeout($$&) {    ## no critic. 
     
    4849} 
    4950 
     51sub snmp_session { 
     52    my ($agent, $host, $callback, ) = @_; 
     53 
     54    my $community = $agent->config->{config}->{community} or die "missing community"; 
     55    my $port      = $agent->config->{config}->{port}    || 161; 
     56    my $timeout   = $agent->config->{config}->{timeout} || 10; 
     57 
     58    my ($session, $error) = Net::SNMP->session( 
     59        -hostname  => $host, 
     60        -community => $community, 
     61        -port      => $port, 
     62        -timeout   => $timeout, 
     63    ); 
     64 
     65    if (not defined($session)) { 
     66        die "ERROR: $error.\n"; 
     67    } else { 
     68        my $response = $callback->($session); 
     69        $session->close(); 
     70        return $response; 
     71    } 
     72} 
     73 
     74 
    50751;