#!/usr/bin/perl #check (all) remote filesystems on a host over ssh (passwordless/with #a ssh key). all filesystems are checked against the same values #for critical and warning and based off %full. so a -c 95 #means send a critical alert if any of the the remote filesystems are at or #above 95% use. #copyright John Greenfelder (zgreenfelder(at)gmail.com), October 2012 #released under GPL, licesne available at http://www.gnu.org/licenses/gpl.html # #when using this, it's important to remember that -i ignores everything that #matches. e.g. -i var would ignore /var, /var/tmp, /var/mysql, or even #things like /home/invariablyimportant/mustbe/watched/filesystem # use warnings; use vars qw($PROGNAME $VERSION $output $values $result $defperfargs); use Nagios::Plugin::Functions qw(%STATUS_TEXT); use Nagios::Plugin; use File::Basename; use Net::SSH::Perl; $PROGNAME = basename($0); $VERSION = '0.0.5'; my $np = Nagios::Plugin->new( usage => "Usage: %s -H [ -u ] \n" . " [ -t ] [ -w ] [ -c ]\n" . " [ -i fs[:fs] ] [ -f fs[:fs] ] [-f zfs] \n\n", version => $VERSION, plugin => $PROGNAME, shortname => uc($PROGNAME), blurb => 'check file systems over ssh. userID this is run as needs to have' . " ssh keys setup to login to remote machines. \n" . " -i to ignore fs (regex), each seperated by a : \n" . " or default to check all filesystems on remote host \n", timeout => 30, ); $np->add_arg( spec => 'host|H=s', help => "-H, --host=\n" . ' remote hostname.', required => 0, ); $np->add_arg( spec => 'username|u=s', help => "-u, --username=\n" . ' Username to connect as.', required => 0, ); $np->add_arg( spec => 'filesystem|f=s', help => "-f, --filesystem=zfs\n" . " set to zfs if you monitor remote zfs filesystems, otherwise\n" . " anyything mounted that does not start with a / in the device name\n" . " is ignored. e.g. linux tmpfs", required => 0, ); $np->add_arg( spec => 'warning|w=s', help => "-w, --warning=THRESHOLD\n" . " Warning threshold. See\n" . " http://nagiosplug.sourceforge.net/developer-guidelines.html\n" . ' for the threshold format.', required => 1, ); $np->add_arg( spec => 'critical|c=s', help => "-c, --critical=THRESHOLD\n" . " Critical threshold. See\n" . " http://nagiosplug.sourceforge.net/developer-guidelines.html\n " . " for the threshold format.", required => 1, ); $np->add_arg( spec => "ignore|i=s", help => "-i, --ignore=fs[:fs]\n" . " ignore fs list\n" . " format regex:[regex], either -i, -f or blank, not both. " . "blank means all fs ", required => 0, ); $np->getopts; my $host = $np->opts->host; my $username = $np->opts->username; my $warning = $np->opts->warning; my $critical = $np->opts->critical; my $ignore = $np->opts->ignore; my $dev_check = $np->opts->filesystem; my $nagios_out=""; my $result=0; #print "connecting to $host\n"; #my $ssh = Net::SSH::Perl->new($host, debug => 1, idenity_files => [ $identity ]); my $keys = [ '~nagios/.ssh/id_dsa', '~nagios/.ssh/id_rsa' ]; #my $ssh = Net::SSH::Perl->new($host, protocol=>2, RSAAuthentication=>yes identity_files => $keys ); my $ssh = Net::SSH::Perl->new($host, protocol=>2, 'RSAAuthentication yes', identity_files => $keys ); $ssh->login(); my($stdout,$stderr, $exit) = $ssh->cmd("PATH=/sw/usr/bin:/sw/bin:/usr/bin:/bin ; df -P "); $output=$stdout; if($ignore){ @ignore_fs=split(/:/,$ignore); foreach $ignore_fs_entry (@ignore_fs){ #print "removing $ignore_fs_entry from output\n"; $output=~s/.*$ignore_fs_entry.*//g; $output=~s/^\s*\n//mg; } } @fs_lines=split(/\n/,$output); foreach $line (@fs_lines){ #print "$line\n"; @items=split(/\s+/,$line); if($items[4]=~/\d\%/){ $compare=$items[4]; $mpoint=$items[5]; $mdev=$items[0]; $compare=~s/\%//; $found=0; if((($mpoint=~/^\//) && ($mdev=~/^\//))||($dev_check="zfs")){ if(($compare >= $critical) && ($found==0)){ $found=1; $nagios_out= $nagios_out . "$items[5]=$items[4];$warning;$critical "; if($result= $warning) && ($found==0)){ #print "set warning based on $line\n"; $found=1; $nagios_out= $nagios_out . "$items[5]=$items[4];$warning;$critical "; if($resultnagios_exit($result,$nagios_out);