| 1 | ############################################################################# | ||
| 2 | # | ||
| 3 | # NOTE: This file under revision control using RCS | ||
| 4 | # Any changes made without RCS will be lost | ||
| 5 | # | ||
| 6 | # $Source: /usr/local/cvsroot/vbtk/VBTK/Snmp.pm,v $ | ||
| 7 | # $Revision: 1.11 $ | ||
| 8 | # $Date: 2002/03/04 20:53:07 $ | ||
| 9 | # $Author: bhenry $ | ||
| 10 | # $Locker: $ | ||
| 11 | # $State: Exp $ | ||
| 12 | # | ||
| 13 | # Purpose: A common perl library used to monitor Snmp agents, | ||
| 14 | # writing to Log files, and setting VBServer objects as | ||
| 15 | # specified. | ||
| 16 | # | ||
| 17 | # Invoked by: vbsnmp | ||
| 18 | # | ||
| 19 | # Depends on: VBTK::Common.pm, VBTK::Parser | ||
| 20 | # | ||
| 21 | # Copyright (C) 1996 - 2002 Brent Henry | ||
| 22 | # | ||
| 23 | # This program is free software; you can redistribute it and/or | ||
| 24 | # modify it under the terms of version 2 of the GNU General Public | ||
| 25 | # License as published by the Free Software Foundation available at: | ||
| 26 | # http://www.gnu.org/copyleft/gpl.html | ||
| 27 | # | ||
| 28 | # This program is distributed in the hope that it will be useful, | ||
| 29 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 30 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 31 | # GNU General Public License for more details. | ||
| 32 | # | ||
| 33 | ############################################################################# | ||
| 34 | # | ||
| 35 | # | ||
| 36 | # REVISION HISTORY: | ||
| 37 | # | ||
| 38 | # $Log: Snmp.pm,v $ | ||
| 39 | # Revision 1.11 2002/03/04 20:53:07 bhenry | ||
| 40 | # *** empty log message *** | ||
| 41 | # | ||
| 42 | # Revision 1.10 2002/03/04 16:49:09 bhenry | ||
| 43 | # Changed requirement back to perl 5.6.0 | ||
| 44 | # | ||
| 45 | # Revision 1.9 2002/03/02 00:53:55 bhenry | ||
| 46 | # Documentation updates | ||
| 47 | # | ||
| 48 | # Revision 1.8 2002/02/19 19:10:42 bhenry | ||
| 49 | # *** empty log message *** | ||
| 50 | # | ||
| 51 | # Revision 1.7 2002/02/13 07:38:51 bhenry | ||
| 52 | # Disabled RrdLogRecovery and removed use of @log | ||
| 53 | # | ||
| 54 | # Revision 1.6 2002/01/28 22:51:27 bhenry | ||
| 55 | # *** empty log message *** | ||
| 56 | # | ||
| 57 | # Revision 1.5 2002/01/28 22:48:23 bhenry | ||
| 58 | # *** empty log message *** | ||
| 59 | # | ||
| 60 | # Revision 1.4 2002/01/28 19:35:14 bhenry | ||
| 61 | # Bug Fixes | ||
| 62 | # | ||
| 63 | # Revision 1.3 2002/01/25 07:15:10 bhenry | ||
| 64 | # Changed to inherit from Parser | ||
| 65 | # | ||
| 66 | # | ||
| 67 | |||
| 68 | package VBTK::Snmp; | ||
| 69 | |||
| 70 | use 5.6.0; | ||
| 71 | use strict; | ||
| 72 | use warnings; | ||
| 73 | # I like using undef as a value so I'm turning off the uninitialized warnings | ||
| 74 | no warnings qw(uninitialized); | ||
| 75 | |||
| 76 | use VBTK; | ||
| 77 | use VBTK::Common; | ||
| 78 | use VBTK::Parser; | ||
| 79 | use SNMP; | ||
| 80 | use Storable qw(dclone); | ||
| 81 | |||
| 82 | #$SNMP::debugging = 3; | ||
| 83 | #$SNMP::auto_init_mib = 0; | ||
| 84 | $SNMP::use_long_names = 1; | ||
| 85 | |||
| 86 | SNMP::initMib(); # parses default list of Mib modules from default dirs | ||
| 87 | SNMP::addMibDirs("/usr/local/share/snmp/mibs"); | ||
| 88 | |||
| 89 | # Inherit methods from Parser and SNMP classes | ||
| 90 | our @ISA = qw(VBTK::Parser); | ||
| 91 | |||
| 92 | our $VERBOSE = $ENV{VERBOSE}; | ||
| 93 | |||
| 94 | our $DEFAULT_TIMEOUT=3; | ||
| 95 | our $DEFAULT_RETRIES=1; | ||
| 96 | |||
| 97 | #------------------------------------------------------------------------------- | ||
| 98 | # Function: new | ||
| 99 | # Description: Object constructor. Allocates memory for all class members, | ||
| 100 | # create an Snmp session, check passed Snmp labels. | ||
| 101 | # | ||
| 102 | # Input Parms: VBTK::Snmp name/value hash | ||
| 103 | # Output Parms: Pointer to self | ||
| 104 | #------------------------------------------------------------------------------- | ||
| 105 | sub new | ||
| 106 | { | ||
| 107 | my ($type,$self); | ||
| 108 | |||
| 109 | # If we're passed a hash as the first element, then it's probably from an | ||
| 110 | # inheriting class | ||
| 111 | if((defined $_[0])&&(UNIVERSAL::isa($_[0], 'HASH'))) | ||
| 112 | { | ||
| 113 | $self = shift; | ||
| 114 | } | ||
| 115 | # Otherwise, allocate a new hash, bless it and handle any passed parms | ||
| 116 | else | ||
| 117 | { | ||
| 118 | $type = shift; | ||
| 119 | $self = {}; | ||
| 120 | bless $self, $type; | ||
| 121 | |||
| 122 | # Store all passed input name pairs in the object | ||
| 123 | $self->set(@_); | ||
| 124 | } | ||
| 125 | |||
| 126 | # Setup a hash of default parameters | ||
| 127 | my $defaultParms = { | ||
| 128 | Interval => 60, | ||
| 129 | Labels => $::REQUIRED, | ||
| 130 | Host => $::REQUIRED, | ||
| 131 | Port => $::REQUIRED, | ||
| 132 | Community => undef, | ||
| 133 | VBServerURI => $::VBURI, | ||
| 134 | VBHeader => undef, | ||
| 135 | VBDetail => [ '$data' ], | ||
| 136 | LogFile => undef, | ||
| 137 | LogHeader => undef, | ||
| 138 | LogDetail => undef, | ||
| 139 | RotateLogAt => undef, | ||
| 140 | PreProcessor => undef, | ||
| 141 | Timeout => $DEFAULT_TIMEOUT, | ||
| 142 | Retries => $DEFAULT_RETRIES, | ||
| 143 | ErrorStatus => $::FAILED, | ||
| 144 | GetMultRows => undef | ||
| 145 | }; | ||
| 146 | |||
| 147 | # Run the validation, setting defaults if values are not already set | ||
| 148 | $self->validateParms($defaultParms); | ||
| 149 | |||
| 150 | # Create a parser object, passing along all the input name/value pairs. | ||
| 151 | # Unused name/value pairs will be ignored | ||
| 152 | $self->SUPER::new() || return undef; | ||
| 153 | |||
| 154 | &log("Setting up SNMP Session to $self->{Host}:$self->{Port}") | ||
| 155 | if ($VERBOSE); | ||
| 156 | |||
| 157 | my($label,$oid,$msg); | ||
| 158 | |||
| 159 | # Create the SNMP object | ||
| 160 | $self->{snmpSession} = new SNMP::Session( | ||
| 161 | DestHost => $self->{Host}, | ||
| 162 | Community => $self->{Community}, | ||
| 163 | RemotePort => $self->{Port}, | ||
| 164 | Version => '1', | ||
| 165 | Timeout => $self->{Timeout} * 1000000, | ||
| 166 | Retries => $self->{Retries}); | ||
| 167 | |||
| 168 | fatal("Can't connect to '$self->{Host}:$self->{Port}'") | ||
| 169 | if ($self->{snmpSession} eq ''); | ||
| 170 | |||
| 171 | # Validate passed Snmp labels | ||
| 172 | foreach $label (@{$self->{Labels}}) | ||
| 173 | { | ||
| 174 | $oid = SNMP::translateObj($label); | ||
| 175 | |||
| 176 | &fatal("Cannot determine oid for '$label'") if ($oid eq ''); | ||
| 177 | &log("Translated '$label' to '$oid'") if ($VERBOSE > 1); | ||
| 178 | } | ||
| 179 | |||
| 180 | # Add self to the list of all defined VBTK::Snmp objects | ||
| 181 | &VBTK::register($self); | ||
| 182 | return $self; | ||
| 183 | } | ||
| 184 | |||
| 185 | #------------------------------------------------------------------------------- | ||
| 186 | # Function: addMibFiles | ||
| 187 | # Description: Call the SNMP::addMibFiles method | ||
| 188 | # Input Parms: List of Mib files to include | ||
| 189 | # Output Parms: None | ||
| 190 | #------------------------------------------------------------------------------- | ||
| 191 | sub addMibFiles | ||
| 192 | { | ||
| 193 | SNMP::addMibFiles(@_); | ||
| 194 | (0); | ||
| 195 | } | ||
| 196 | |||
| 197 | #------------------------------------------------------------------------------- | ||
| 198 | # Function: addMibDirs | ||
| 199 | # Description: Call the SNMP::addMibDirs method | ||
| 200 | # Input Parms: List of Mib dirs to search | ||
| 201 | # Output Parms: None | ||
| 202 | #------------------------------------------------------------------------------- | ||
| 203 | sub addMibDirs | ||
| 204 | { | ||
| 205 | SNMP::addMibDirs(@_); | ||
| 206 | return 1; | ||
| 207 | } | ||
| 208 | |||
| 209 | #------------------------------------------------------------------------------- | ||
| 210 | # Function: run | ||
| 211 | # Description: Retrieve and process the specified Snmp values | ||
| 212 | # Input Parms: None | ||
| 213 | # Output Parms: Time till next retrieval | ||
| 214 | #------------------------------------------------------------------------------- | ||
| 215 | sub run | ||
| 216 | { | ||
| 217 | # Unload variables from the object | ||
| 218 | my $self = shift; | ||
| 219 | my $snmpSession = $self->{snmpSession}; | ||
| 220 | my $Labels = $self->{Labels}; | ||
| 221 | my $Interval = $self->{Interval}; | ||
| 222 | my $GetMultRows = $self->{GetMultRows}; | ||
| 223 | my $ErrorStatus = $self->{ErrorStatus}; | ||
| 224 | my $Host = $self->{Host}; | ||
| 225 | my $Port = $self->{Port}; | ||
| 226 | |||
| 227 | # Define local variables | ||
| 228 | my (@data,$value,$lastvalue,$delta,$flag,@table,$vars,@label_array,$keylabel); | ||
| 229 | my ($label,$ptr,$sleepTime,$msg); | ||
| 230 | my $now = time; | ||
| 231 | |||
| 232 | # If it's not time to run yet, then return | ||
| 233 | if(($sleepTime = $self->calcSleepTime()) > 0) | ||
| 234 | { | ||
| 235 | &log("Not time to check $Host:$Port, wait $sleepTime seconds") | ||
| 236 | if ($VERBOSE > 1); | ||
| 237 | return ($sleepTime,$::NOT_FINISHED); | ||
| 238 | } | ||
| 239 | |||
| 240 | &log("Constructing the varlist") if ($VERBOSE); | ||
| 241 | # Construct a 2-dimensional array of label names. This is the format needed | ||
| 242 | # to make the call to SNMP::Varlist. | ||
| 243 | foreach $label (@{$Labels}) | ||
| 244 | { | ||
| 245 | push(@label_array, [ $label, 0 ]); | ||
| 246 | } | ||
| 247 | |||
| 248 | # Pull out the first label name | ||
| 249 | $keylabel = $label_array[0][0]; | ||
| 250 | |||
| 251 | # Setup the vars object to be used in data retrieval | ||
| 252 | &log("Retrieving SNMP values from $Host:$Port") if ($VERBOSE > 1); | ||
| 253 | $vars = new SNMP::VarList ( @label_array ); | ||
| 254 | |||
| 255 | for(;;) | ||
| 256 | { | ||
| 257 | # Retrieve one row of data. If the 'GetMultRows' option was specified | ||
| 258 | # then keep calling getnext until you've retrieve all matching rows. | ||
| 259 | if($GetMultRows) { @data = $snmpSession->getnext($vars); } | ||
| 260 | else { @data = $snmpSession->get($vars); } | ||
| 261 | |||
| 262 | # If there are errors, then pass the error along to the parser object | ||
| 263 | # along with the error message written in red. | ||
| 264 | if ($snmpSession->{ErrorStr} ne '') | ||
| 265 | { | ||
| 266 | $msg = "Error: Can't retrieve snmp values: " . $snmpSession->{ErrorStr}; | ||
| 267 | &log($msg); | ||
| 268 | $self->parseData(undef,$ErrorStatus,red($msg) . "\n"); | ||
| 269 | last; | ||
| 270 | } | ||
| 271 | |||
| 272 | if($GetMultRows) | ||
| 273 | { | ||
| 274 | if($$vars[0]->tag =~ /$keylabel$/) | ||
| 275 | { | ||
| 276 | &log("Retrieved " . join(":",@data)) if ($VERBOSE > 2); | ||
| 277 | push(@table, [ @data ]); | ||
| 278 | next; | ||
| 279 | } | ||
| 280 | } | ||
| 281 | else | ||
| 282 | { | ||
| 283 | &log("Retrieved " . join(":",@data)) if ($VERBOSE > 2); | ||
| 284 | push(@table, [ @data ]); | ||
| 285 | } | ||
| 286 | |||
| 287 | # Check to see if any data was retrieved, parse it appropriately | ||
| 288 | # and then break out of the loop. | ||
| 289 | if($table[0] eq '') | ||
| 290 | { | ||
| 291 | $msg = "Error: No values retrieved"; | ||
| 292 | &log($msg); | ||
| 293 | $self->parseData(undef,$ErrorStatus,red($msg) . "\n"); | ||
| 294 | } | ||
| 295 | else | ||
| 296 | { | ||
| 297 | $self->parseData(\@table); | ||
| 298 | } | ||
| 299 | last; | ||
| 300 | } | ||
| 301 | |||
| 302 | $sleepTime = $self->calcSleepTime(1); | ||
| 303 | |||
| 304 | ($sleepTime,$::NOT_FINISHED); | ||
| 305 | } | ||
| 306 | |||
| 307 | 1; | ||
| 308 | __END__ |