Changeset 2463

Show
Ignore:
Timestamp:
09/22/09 23:06:06 (3 years ago)
Author:
ligne
Message:

add tests for Munin::Plugin::SNMP module, and fix the bugs they brought to the surface. also includes a bit of POD reformatting and typo-fixing.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/plugins/lib/Munin/Plugin/SNMP.pm

    r1966 r2463  
    5959 
    6060use strict; 
     61 
    6162use Net::SNMP; 
    6263 
    6364use vars qw(@ISA); 
    64  
    6565@ISA = qw(Net::SNMP); 
    6666 
     
    8383=head2 config_session() - Decode environment to get the needed plugin configuration parameters 
    8484 
    85   ($host,$port,$version,$tail) = Munin::Plugin::SNMP->config_session(); 
     85  ($host, $port, $version, $tail) = Munin::Plugin::SNMP->config_session(); 
    8686 
    8787This is a convenience function for the "config" part of the plugin - 
    8888it decodes the environment/plugin name to retrieve the information 
    89 needed in the configuration phase.  It returns a 4 tuple consisting of 
    90 1) the host name 2) the udp port to use 3) the SNMP version to use, 
    91 and 4) the tail of the plugin name: whatever is left of the plugin 
     89needed in the configuration phase.  It returns a 4 tuple consisting of: 
     90 
     91=over 
     92 
     93=item 1) the host name 
     94 
     95=item 2) the udp port to use 
     96 
     97=item 3) the SNMP version to use (3 for version 3, 2 for version 1 or 2c) 
     98 
     99=item 4) the tail of the plugin name: whatever is left of the plugin 
    92100name after "snmp_<host>_". 
     101 
     102=back 
    93103 
    94104The tail can be interesting for the "fetch" part of the plugin as 
     
    97107=cut 
    98108 
    99     my $host    = undef; 
    100     my $version = $ENV{version} || '2'; 
    101     my $port    = $ENV{port}    || 161; 
    102     my $tail    = undef; 
     109    my ($host, $port, $version, $tail); 
    103110 
    104111    # Decode plugin/symlink name and extract meaning from it - if possible. 
    105112    if ($0 =~ /^(?:.*\/)?snmp(v3)?_([^_]+)_(.*)/) { 
    106         my $v3 = $1 || '0'; 
    107         $version = 3 if $v3; 
     113        $version = '3' if $1; 
    108114        $host = $2; 
     115        $tail = $3; 
    109116        if ($host =~ /^([^:]+):(\d+)$/) { 
    110             $host = $2; 
    111             $port = $3; 
    112         } 
    113         $tail = $4; 
     117            $host = $1; 
     118            $port = $2; 
     119        } 
    114120    } 
    115121 
    116122    # The environment overrides the symlink.  The other way around is 
    117123    # not useful. 
    118     $host = $ENV{host} || $host; 
    119  
    120     if (!defined($host)) { 
    121         die 'Could not find hostname.'; 
    122     } 
    123  
    124     return ($host,$port,$version,$tail); 
     124    $host    = $ENV{host}    || $host    || die "Could not find hostname"; 
     125    $version = $ENV{version} || $version || '2'; 
     126    $port    = $ENV{port}    || $port    || 161; 
     127 
     128    return ($host, $port, $version, $tail); 
    125129} 
    126130 
     
    156160     env.v3authpassword s3cr1tpa55w0rd 
    157161 
    158 See below for how to configure for each diffetent case.  The first 
    159 case above shows Munins default configuration. 
    160  
    161 NOTE: munin_node_configure does not yet utilize the "v3" thing. 
     162See below for how to configure for each different case.  The first 
     163case above shows Munin's default configuration. 
     164 
     165NOTE: munin-node-configure does not yet utilize the "v3" thing. 
    162166 
    163167The following environment variables are consulted: 
     
    193197snmpv2c or snmpv3.  SNMP v2 is better as it supports bulk operations. 
    194198Therefore 2 is the default in Munin::Plugin::SNMP.  If your device 
    195 supports v3 that may be even better as it supports propper security - 
     199supports v3 that may be even better as it supports proper security - 
    196200but the encryption may slow things down. 
    197201 
     
    203207 
    204208    if (!defined($host)) { 
    205         die 'Could not find hostname.'
     209        die "Could not find hostname.\n"
    206210    } 
    207211 
  • trunk/plugins/t/munin_plugin_snmp.t

    r1966 r2463  
    22use strict; 
    33 
    4 use Test::More tests => 1
     4use Test::More tests => 12
    55 
    66use_ok('Munin::Plugin::SNMP'); 
     7 
     8 
     9### config_session 
     10{ 
     11        my @tests = ( 
     12                [ 
     13                        '/usr/share/munin/plugins/snmp_prentice_mchoan', 
     14                        [ 'prentice', 161, 2, 'mchoan' ], 
     15                        'Full path' 
     16                ], 
     17                [ 
     18                        'snmp_kenneth_mchoan', 
     19                        [ 'kenneth', 161, 2, 'mchoan' ], 
     20                ], 
     21                [ 
     22                        'snmp_fiona_urvill_3', 
     23                        [ 'fiona', 161, 2, 'urvill_3' ], 
     24                        'Different tail' 
     25                ], 
     26                [ 
     27                        'snmpv3_ash_watt', 
     28                        [ 'ash', 161, 3, 'watt' ], 
     29                        'SNMPv3' 
     30                ], 
     31                [ 
     32                        'snmp_hamish:162_mchoan', 
     33                        [ 'hamish', 162, 2, 'mchoan' ], 
     34                        'Port specified' 
     35                ], 
     36                [ 
     37                        'snmp_verity.walker_mchoan', 
     38                        [ 'verity.walker', 161, 2, 'mchoan' ], 
     39                        'FQDN host' 
     40                ], 
     41        ); 
     42        while (my $test = shift @tests) { 
     43                my ($zero, $expected, $message) = @$test; 
     44                local $0 = $zero; 
     45                my @got = Munin::Plugin::SNMP->config_session(); 
     46                is_deeply(\@got, $expected);     
     47        } 
     48 
     49        # unable to get hostname 
     50        { 
     51                local $0 = 'fergus_urvill'; 
     52                undef $@; 
     53                eval { Munin::Plugin::SNMP->config_session() }; 
     54                ok(defined($@),"threw an error when hostname couldn't be found") 
     55                        or diag($@); 
     56        } 
     57 
     58        # overriding from the environment 
     59        { 
     60                local $ENV{host} = 'araucaria'; 
     61                local $0 = 'snmp_john_graham', 
     62                is_deeply( 
     63                        [ Munin::Plugin::SNMP->config_session() ], 
     64                        [ 'araucaria', 161, 2, 'graham' ], 
     65                        'port set in environment' 
     66                );       
     67        }        
     68        { 
     69                local $ENV{port} = '162'; 
     70                local $0 = 'snmp_john_graham', 
     71                is_deeply( 
     72                        [ Munin::Plugin::SNMP->config_session() ], 
     73                        [ 'john', 162, 2, 'graham' ], 
     74                        'port set in environment' 
     75                );       
     76        }        
     77        { 
     78                local $ENV{version} = '3'; 
     79                local $0 = 'snmp_john_graham', 
     80                is_deeply( 
     81                        [ Munin::Plugin::SNMP->config_session() ], 
     82                        [ 'john', 161, 3, 'graham' ], 
     83                        'version set in v2 plugin environment' 
     84                );       
     85        }        
     86        { 
     87                local $ENV{version} = '2'; 
     88                local $0 = 'snmpv3_john_graham', 
     89                is_deeply( 
     90                        [ Munin::Plugin::SNMP->config_session() ], 
     91                        [ 'john', 161, 2, 'graham' ], 
     92                        'version 2 set in v3 plugin environment' 
     93                );       
     94        }        
     95 
     96} 
     97