Changeset 8236 for lang/perl/App-MadEye

Show
Ignore:
Timestamp:
03/21/08 11:40:09 (9 months ago)
Author:
tokuhirom
Message:

snmp_session() 関数をつかってすっきりさす

Files:
1 modified

Legend:

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

    r8142 r8236  
    33use warnings; 
    44use App::MadEye::Plugin::Agent::Base; 
    5 use Net::SNMP; 
     5use App::MadEye::Util; 
    66 
    77my $oid_map = { 
     
    1717 
    1818    my $threshold = $self->config->{config}->{threshold} or die "missing threshold"; 
    19     my $community = $self->config->{config}->{community} or die "missing community"; 
    20     my $port      = $self->config->{config}->{port}    || 161; 
    21     my $timeout   = $self->config->{config}->{timeout} || 10; 
    2219 
    23     my $session = open_session($host, $community, $port, $timeout); 
    24     my $response = fetch_data($session); 
    25     $session->close(); 
     20    my $response = snmp_session( 
     21        $self, 
     22        $host => sub { 
     23            my $session = shift; 
     24 
     25            return +{ 
     26                map { 
     27                    $_ => ( 
     28                        $session->get_table( 
     29                            -baseoid => $oid_map->{$_}, 
     30                        ) or die "cannot get a $_ : " . $session->error 
     31                    ) 
     32                } 
     33                qw/hrStorageDescr hrStorageUsed hrStorageType hrStorageSize/ 
     34            }; 
     35        }, 
     36    ); 
    2637 
    2738    my $result = ''; 
     
    3748    }); 
    3849    return $result; 
    39 } 
    40  
    41 sub open_session { 
    42     my ($host, $community, $port, $timeout) = @_; 
    43     my ($session, $error) = Net::SNMP->session( 
    44         -hostname  => $host, 
    45         -community => $community, 
    46         -port      => $port, 
    47         -timeout   => $timeout, 
    48     ); 
    49     if (!defined($session)) { 
    50         warn "ERROR: $error.\n"; 
    51     } 
    52     $session; 
    53 } 
    54  
    55 sub fetch_data { 
    56     my $session = shift; 
    57  
    58     return +{ 
    59         map { 
    60             $_ => ( 
    61                 $session->get_table( 
    62                     -baseoid => $oid_map->{$_}, 
    63                 ) or die "cannot get a $_ : " . $session->error 
    64             ) 
    65         } 
    66         qw/hrStorageDescr hrStorageUsed hrStorageType hrStorageSize/ 
    67     }; 
    6850} 
    6951