1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
#!/usr/bin/perl -w
#
# Use SNMP to get free disk space or inode status from a Network Appliance
#
# exit values:
# 1 - free space or inodes on any host dropped below the supplied parameter
# 2 - network or SNMP error (SNMP library error, no response from server)
# 3 - config error - (filesystem in config file does not exist on filer)
# USAGE
# [--community=<SNMP COMMUNITY>] [--timeout=<seconds>]
# [--config=/path/to/configfile] [--list] host1 host2 ...
# EXAMPLES
# --list option will dump current status from requested hosts:
# netappfree.monitor --list filer1 filer2 filer3
# sample output:
# filer ONTAP filesystem KB total KB avail Inode%
# ----------------------------------------------------------------------------
# filer1 6.1.2R3 /vol/vol0/ 61092616 6773416 86
# filer1 6.1.2R3 /vol/vol0/.snaps 2545524 1260240 0
# sample invocation in mon.cf, with local MIB directory for the Netapp MIB
# NETWORK-APPLIANCE-MIB.txt (copy from /etc/mib/netapp.mib on filer):
# service freespace
# description test freespace and inodes on Netapp filers
# depend SELF:ping
# MIBDIRS=/usr/local/share/snmp/mibs
# interval 7m
# monitor netappfree.monitor
# CONFIG FILE FORMAT
#
# Run "netappfree --list host1 host2 ..." first to get list of filesystems
# and whether inodes are properly reported. If you don't want to monitor
# inodes for a particular FS, leave tha column blank.
#
#
# host filesystem freespace [InodeThreshold]
# (in kb, gb, or mb) (in % or k)
#
# filer1 /vol/main/ 5gb 90%
# filer2 /vol/vol0/ 5gb 500k
#
# This requires the UCD SNMP library and G.S. Marzot's Perl SNMP
# module.
#
# Originally by Jim Trocki. Modified by Theo Van Dinter
# (tvd@colltech.com, felicity@kluge.net) to add verbose error output,
# more error checking, etc. Can be used in conjunction with
# snapdelete.alert to auto-remove snapshots if needed.
# Modified December 2003 by Ed Ravin (eravin@panix.com) to add inode
# checking, detect nonexistent filesystem in config file, pass perl -w
# checks, added more info to error messages for clarity, updated doc comments
# above.
# $Id: netappfree.monitor,v 1.1.1.1 2004/06/09 05:18:04 trockij Exp $
#
# Copyright (C) 1998, Jim Trocki
# Copyright (C) 1999-2001, Theo Van Dinter
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
use Mon::SNMP;
use Getopt::Long;
sub list;
sub readcf;
sub toKB;
$ENV{"MIBS"} = 'RFC1213-MIB:NETWORK-APPLIANCE-MIB';
GetOptions (\%opt, "community=s", "timeout=i", "retries=i", "config=s", "list");
die "no host arguments\n" if (@ARGV == 0);
$RET = 0;
@ERRS = ();
%HOSTS = ();
$COMM = $opt{"community"} || "public";
$TIMEOUT = $opt{"timeout"} || 2; $TIMEOUT *= 1000 * 1000;
$RETRIES = $opt{"retries"} || 5;
$CONFIG = $opt{"config"} || (-d "/etc/mon" ? "/etc/mon" : "/usr/lib/mon/etc")
. "/netappfree.cf";
($dfIndex, $dfFileSys, $dfKBytesTotal, $dfKBytesAvail,
$dfInodesFree, $dfPerCentInodeCapacity) = (0..5);
list (@ARGV) if ($opt{"list"});
readcf ($CONFIG) || die "could not read config: $!\n";
foreach $host (@ARGV) {
next if (!defined $FREE{$host});
if (!defined($s = new SNMP::Session (DestHost => $host,
Timeout => $TIMEOUT, Community => $COMM,
Retries => $RETRIES))) {
$RET = ($RET == 1) ? 1 : 2;
$HOSTS{$host} ++;
push (@ERRS, "could not create session to $host: " . $SNMP::Session::ErrorStr);
next;
}
$v = new SNMP::VarList (
['dfIndex'],
['dfFileSys'],
['dfKBytesTotal'],
['dfKBytesAvail'],
['dfInodesFree'],
['dfPerCentInodeCapacity'],
);
if ( $v->[$dfIndex]->tag !~ /^df/ ) {
push(@ERRS,"OIDs not mapping correctly! Check that NetApp MIB is available!");
$RET = 1;
last;
}
while (defined $s->getnext($v)) {
last if ($v->[$dfIndex]->tag !~ /dfIndex/);
my $filesys= $v->[$dfFileSys]->val;
next unless exists($FREE{$host}{$filesys});
if ($v->[$dfKBytesAvail]->val < $FREE{$host}{$filesys}{'bytes'}) {
$HOSTS{$host} ++;
push (@ERRS, sprintf ("%1.1fGB free on %s:%s (threshold %1.1fGB, fs size %1.1fGB)",
$v->[$dfKBytesAvail]->val / 1024 / 1024,
$host, $filesys,
$FREE{$host}{$filesys}{'bytes'} / 1024 / 1024,
$v->[$dfKBytesTotal]->val / 1024 / 1024)
);
$RET = 1;
}
# mark filesys entry as seen in filer's MIB
$FREE{$host}{$v->[$dfFileSys]->val}{'existsOnFiler'}= 1;
if (defined($FREE{$host}{$v->[$dfFileSys]->val}{'inode'})) {
my $inodefreewanted= $FREE{$host}{$v->[$dfFileSys]->val}{'inode'};
if (0 < $inodefreewanted and $inodefreewanted < 1) { # percentage?
if ($v->[$dfPerCentInodeCapacity]->val > $inodefreewanted * 100) { # percentage exceeded?
$HOSTS{$host} ++;
push (@ERRS, sprintf("%d%% inodes used on %s:%s, over threshold of %d%%",
$v->[$dfPerCentInodeCapacity]->val,
$host, $v->[$dfFileSys]->val,
$inodefreewanted * 100
));
$RET = 1;
}
}
elsif ($v->[$dfInodesFree]->val < $inodefreewanted) {
$HOSTS{$host} ++;
push (@ERRS, sprintf("%1.1f inodes free on %s:%s, below threshold of %1.1f",
$v->[$dfInodesFree]->val,
$host, $v->[$dfFileSys]->val,
$inodefreewanted
));
$RET = 1;
}
}
}
if ($s->{ErrorNum}) {
$HOSTS{$host} ++;
push (@ERRS, "could not get dfIndex for $host: " . $s->{ErrorStr});
$RET = ($RET == 1) ? 1 : 2;
}
}
foreach $host (@ARGV)
{
foreach $filesys (keys %{$FREE{$host}})
{
if (! $FREE{$host}{$filesys}{'existsOnFiler'} ) {
$HOSTS{$host} ++;
push (@ERRS, "filesystem $filesys does not exist on $host");
$RET = ($RET == 1) ? 1 : 3;
}
}
}
if ($RET) {
print join(" ", sort keys %HOSTS), "\n\n", join("\n", @ERRS), "\n";
}
exit $RET;
#
# read configuration file
#
sub readcf {
my ($f) = @_;
my ($l, $host, $filesys, $free, $inodefree);
open (CF, $f) || return undef;
while (<CF>) {
next if (/^\s*#/ || /^\s*$/);
chomp;
($host, $filesys, $free, $inodefree) = split;
if (!defined ($FREE{$host}{$filesys}{'bytes'} = toKB ($free))) {
die "error free specification, config $f, line $.\n";
}
if (!defined ($FREE{$host}{$filesys}{'inode'} = toIN ($inodefree))) {
# allow this to be optional for compatibility
# die "error inodefree specification, config $f, line $.\n";
}
$FREE{$host}{$filesys}{'existsOnFiler'}= 0;
}
close (CF);
}
sub toKB {
my ($free) = @_;
my ($n, $u);
if ($free =~ /^(\d+\.\d+)(kb|mb|gb)$/i) {
($n, $u) = ($1, "\L$2");
} elsif ($free =~ /^(\d+)(kb|mb|gb)$/i) {
($n, $u) = ($1, "\L$2");
} else {
return undef;
}
return (int ($n * 1024)) if ($u eq "mb");
return (int ($n * 1024 * 1024)) if ($u eq "gb");
int ($n);
}
sub toIN {
my ($infree) =@_;
return undef unless defined($infree);
if ($infree =~ /^(\d+\.?\d+)%$/) { # percentage
return $1 / 100;
}
if ($infree =~ /^(\d+\.?\d+)(k|kb)$/) { # kilos?
return $1 * 1024;
}
if ($infree =~ /^(\d+\.?\d+)$/) { # bare??
return $1;
}
return undef;
}
sub list {
my (@hosts) = @_;
foreach $host (@hosts) {
if (!defined($s = new SNMP::Session (DestHost => $host,
Timeout => $TIMEOUT,
Community => $COMM,
Retries => $RETRIES))) {
print STDERR "could not create session to $host: " . $SNMP::Session::ErrorStr, "\n";
next;
}
$ver = $s->get(['sysDescr', 0]);
$ver =~ s/^netapp.*release\s*([^:]+):.*$/$1/i;
$v = new SNMP::VarList (
['dfIndex'],
['dfFileSys'],
['dfKBytesTotal'],
['dfKBytesAvail'],
['dfInodesFree'],
['dfPerCentInodeCapacity'],
);
while (defined $s->getnext($v)) {
last if ($v->[$dfIndex]->tag !~ /dfIndex/);
write;
}
}
exit 0;
}
format STDOUT_TOP =
filer ONTAP filesystem KB total KB avail Inode%
------------------------------------------------------------------------------
.
format STDOUT =
@<<<<<<<<<<<<<< @<<<<<<<<<< @<<<<<<<<<<<<<<< @>>>>>>>>>> @>>>>>>>>>> @>>
$host, $ver, $v->[1]->[2], $v->[2]->[2], $v->[3]->[2], $v->[5]->[2]
.
|