Changeset 2463
- Timestamp:
- 09/22/09 23:06:06 (3 years ago)
- Files:
-
- trunk/plugins/lib/Munin/Plugin/SNMP.pm (modified) (6 diffs)
- trunk/plugins/t/munin_plugin_snmp.t (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/plugins/lib/Munin/Plugin/SNMP.pm
r1966 r2463 59 59 60 60 use strict; 61 61 62 use Net::SNMP; 62 63 63 64 use vars qw(@ISA); 64 65 65 @ISA = qw(Net::SNMP); 66 66 … … 83 83 =head2 config_session() - Decode environment to get the needed plugin configuration parameters 84 84 85 ($host, $port,$version,$tail) = Munin::Plugin::SNMP->config_session();85 ($host, $port, $version, $tail) = Munin::Plugin::SNMP->config_session(); 86 86 87 87 This is a convenience function for the "config" part of the plugin - 88 88 it 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 89 needed 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 92 100 name after "snmp_<host>_". 101 102 =back 93 103 94 104 The tail can be interesting for the "fetch" part of the plugin as … … 97 107 =cut 98 108 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); 103 110 104 111 # Decode plugin/symlink name and extract meaning from it - if possible. 105 112 if ($0 =~ /^(?:.*\/)?snmp(v3)?_([^_]+)_(.*)/) { 106 my $v3 = $1 || '0'; 107 $version = 3 if $v3; 113 $version = '3' if $1; 108 114 $host = $2; 115 $tail = $3; 109 116 if ($host =~ /^([^:]+):(\d+)$/) { 110 $host = $2; 111 $port = $3; 112 } 113 $tail = $4; 117 $host = $1; 118 $port = $2; 119 } 114 120 } 115 121 116 122 # The environment overrides the symlink. The other way around is 117 123 # 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); 125 129 } 126 130 … … 156 160 env.v3authpassword s3cr1tpa55w0rd 157 161 158 See below for how to configure for each diffe tent case. The first159 case above shows Munin s default configuration.160 161 NOTE: munin _node_configure does not yet utilize the "v3" thing.162 See below for how to configure for each different case. The first 163 case above shows Munin's default configuration. 164 165 NOTE: munin-node-configure does not yet utilize the "v3" thing. 162 166 163 167 The following environment variables are consulted: … … 193 197 snmpv2c or snmpv3. SNMP v2 is better as it supports bulk operations. 194 198 Therefore 2 is the default in Munin::Plugin::SNMP. If your device 195 supports v3 that may be even better as it supports prop per security -199 supports v3 that may be even better as it supports proper security - 196 200 but the encryption may slow things down. 197 201 … … 203 207 204 208 if (!defined($host)) { 205 die 'Could not find hostname.';209 die "Could not find hostname.\n"; 206 210 } 207 211 trunk/plugins/t/munin_plugin_snmp.t
r1966 r2463 2 2 use strict; 3 3 4 use Test::More tests => 1 ;4 use Test::More tests => 12; 5 5 6 6 use_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
