2014-07-11 19:01:00 +00:00
|
|
|
#!@PERL@ -w
|
2013-11-26 22:53:19 +00:00
|
|
|
#
|
|
|
|
#
|
|
|
|
# check_disk.pl <host> <share> <user> <pass> [warn] [critical] [port]
|
|
|
|
#
|
2014-07-11 19:01:00 +00:00
|
|
|
# Monitoring host script to get the disk usage from a SMB share
|
2013-11-26 22:53:19 +00:00
|
|
|
#
|
|
|
|
# Changes and Modifications
|
|
|
|
# =========================
|
|
|
|
# 7-Aug-1999 - Michael Anthon
|
|
|
|
# Created from check_disk.pl script provided with netsaint_statd (basically
|
|
|
|
# cause I was too lazy (or is that smart?) to write it from scratch)
|
|
|
|
# 8-Aug-1999 - Michael Anthon
|
|
|
|
# Modified [warn] and [critical] parameters to accept format of nnn[M|G] to
|
|
|
|
# allow setting of limits in MBytes or GBytes. Percentage settings for large
|
|
|
|
# drives is a pain in the butt
|
|
|
|
# 2-May-2002 - SGhosh fix for embedded perl
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
require 5.004;
|
|
|
|
use POSIX;
|
|
|
|
use strict;
|
|
|
|
use Getopt::Long;
|
2013-11-26 22:57:29 +00:00
|
|
|
use vars qw($opt_P $opt_V $opt_h $opt_H $opt_s $opt_W $opt_u $opt_p $opt_w $opt_c $opt_a $verbose);
|
2013-11-26 22:53:19 +00:00
|
|
|
use vars qw($PROGNAME);
|
2014-07-11 19:01:00 +00:00
|
|
|
use FindBin;
|
|
|
|
use lib "$FindBin::Bin";
|
|
|
|
use lib '@libexecdir@';
|
2013-11-26 22:53:19 +00:00
|
|
|
use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
|
|
|
|
|
|
|
|
sub print_help ();
|
|
|
|
sub print_usage ();
|
|
|
|
|
|
|
|
$PROGNAME = "check_disk_smb";
|
|
|
|
|
2014-07-11 19:01:00 +00:00
|
|
|
$ENV{'PATH'}='@TRUSTED_PATH@';
|
2013-11-26 22:53:19 +00:00
|
|
|
$ENV{'BASH_ENV'}='';
|
|
|
|
$ENV{'ENV'}='';
|
|
|
|
|
|
|
|
Getopt::Long::Configure('bundling');
|
|
|
|
GetOptions
|
|
|
|
("v" => \$verbose, "verbose" => \$verbose,
|
|
|
|
"P=s" => \$opt_P, "port=s" => \$opt_P,
|
|
|
|
"V" => \$opt_V, "version" => \$opt_V,
|
|
|
|
"h" => \$opt_h, "help" => \$opt_h,
|
|
|
|
"w=s" => \$opt_w, "warning=s" => \$opt_w,
|
|
|
|
"c=s" => \$opt_c, "critical=s" => \$opt_c,
|
|
|
|
"p=s" => \$opt_p, "password=s" => \$opt_p,
|
|
|
|
"u=s" => \$opt_u, "username=s" => \$opt_u,
|
|
|
|
"s=s" => \$opt_s, "share=s" => \$opt_s,
|
|
|
|
"W=s" => \$opt_W, "workgroup=s" => \$opt_W,
|
2013-11-26 22:57:29 +00:00
|
|
|
"H=s" => \$opt_H, "hostname=s" => \$opt_H,
|
|
|
|
"a=s" => \$opt_a, "address=s" => \$opt_a);
|
2013-11-26 22:53:19 +00:00
|
|
|
|
|
|
|
if ($opt_V) {
|
2013-11-26 22:56:50 +00:00
|
|
|
print_revision($PROGNAME,'@NP_VERSION@'); #'
|
2013-11-26 22:53:19 +00:00
|
|
|
exit $ERRORS{'OK'};
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($opt_h) {print_help(); exit $ERRORS{'OK'};}
|
|
|
|
|
2013-11-26 22:57:29 +00:00
|
|
|
my $smbclient = $utils::PATH_TO_SMBCLIENT;
|
2013-11-26 23:00:57 +00:00
|
|
|
$smbclient || usage("check requires smbclient, smbclient not set\n");
|
|
|
|
-x $smbclient || usage("check requires smbclient, $smbclient: $!\n");
|
2013-11-26 22:53:19 +00:00
|
|
|
|
|
|
|
# Options checking
|
|
|
|
|
2013-11-26 22:57:29 +00:00
|
|
|
($opt_H) || ($opt_H = shift @ARGV) || usage("Host name not specified\n");
|
|
|
|
my $host = $1 if ($opt_H =~ /^([-_.A-Za-z0-9 ]+\$?)$/);
|
2013-11-26 22:53:19 +00:00
|
|
|
($host) || usage("Invalid host: $opt_H\n");
|
|
|
|
|
2013-11-26 22:57:29 +00:00
|
|
|
($opt_s) || ($opt_s = shift @ARGV) || usage("Share volume not specified\n");
|
2013-11-26 22:58:53 +00:00
|
|
|
my $share = $1 if ($opt_s =~ /^([-_.A-Za-z0-9 ]+\$?)$/);
|
2013-11-26 22:53:19 +00:00
|
|
|
($share) || usage("Invalid share: $opt_s\n");
|
|
|
|
|
2013-11-26 22:57:29 +00:00
|
|
|
defined($opt_u) || ($opt_u = shift @ARGV) || ($opt_u = "guest");
|
|
|
|
my $user = $1 if ($opt_u =~ /^([-_.A-Za-z0-9\\]*)$/);
|
|
|
|
defined($user) || usage("Invalid user: $opt_u\n");
|
2013-11-26 22:53:19 +00:00
|
|
|
|
2013-11-26 22:57:29 +00:00
|
|
|
defined($opt_p) || ($opt_p = shift @ARGV) || ($opt_p = "");
|
2013-11-26 22:53:19 +00:00
|
|
|
my $pass = $1 if ($opt_p =~ /(.*)/);
|
|
|
|
|
2013-11-26 22:57:29 +00:00
|
|
|
($opt_w) || ($opt_w = shift @ARGV) || ($opt_w = 85);
|
2013-11-26 22:53:19 +00:00
|
|
|
my $warn = $1 if ($opt_w =~ /^([0-9]{1,2}\%?|100\%?|[0-9]+[kMG])$/);
|
|
|
|
($warn) || usage("Invalid warning threshold: $opt_w\n");
|
|
|
|
|
2013-11-26 22:57:29 +00:00
|
|
|
($opt_c) || ($opt_c = shift @ARGV) || ($opt_c = 95);
|
2013-11-26 22:53:19 +00:00
|
|
|
my $crit = $1 if ($opt_c =~ /^([0-9]{1,2}\%?|100\%?|[0-9]+[kMG])$/);
|
|
|
|
($crit) || usage("Invalid critical threshold: $opt_c\n");
|
|
|
|
|
2013-11-26 22:57:29 +00:00
|
|
|
# Execute the given command line and return anything it writes to STDOUT and/or
|
|
|
|
# STDERR. (This might be useful for other plugins, too, so it should possibly
|
|
|
|
# be moved to utils.pm.)
|
|
|
|
sub output_and_error_of {
|
|
|
|
local *CMD;
|
|
|
|
local $/ = undef;
|
|
|
|
my $pid = open CMD, "-|";
|
|
|
|
if (defined($pid)) {
|
|
|
|
if ($pid) {
|
|
|
|
return <CMD>;
|
|
|
|
} else {
|
|
|
|
open STDERR, ">&STDOUT" and exec @_;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
2013-11-26 22:53:19 +00:00
|
|
|
# split the type from the unit value
|
|
|
|
#Check $warn and $crit for type (%/M/G) and set up for tests
|
|
|
|
#P = Percent, K = KBytes
|
|
|
|
my $warn_type;
|
|
|
|
my $crit_type;
|
|
|
|
|
|
|
|
if ($opt_w =~ /^([0-9]+)\%?$/) {
|
|
|
|
$warn = "$1";
|
|
|
|
$warn_type = "P";
|
|
|
|
} elsif ($opt_w =~ /^([0-9]+)k$/) {
|
|
|
|
$warn_type = "K";
|
|
|
|
$warn = $1;
|
|
|
|
} elsif ($opt_w =~ /^([0-9]+)M$/) {
|
|
|
|
$warn_type = "K";
|
|
|
|
$warn = $1 * 1024;
|
|
|
|
} elsif ($opt_w =~ /^([0-9]+)G$/) {
|
|
|
|
$warn_type = "K";
|
|
|
|
$warn = $1 * 1048576;
|
|
|
|
}
|
|
|
|
if ($opt_c =~ /^([0-9]+)\%?$/) {
|
|
|
|
$crit = "$1";
|
|
|
|
$crit_type = "P";
|
|
|
|
} elsif ($opt_c =~ /^([0-9]+)k$/) {
|
|
|
|
$crit_type = "K";
|
|
|
|
$crit = $1;
|
|
|
|
} elsif ($opt_c =~ /^([0-9]+)M$/) {
|
|
|
|
$crit_type = "K";
|
|
|
|
$crit = $1 * 1024;
|
|
|
|
} elsif ($opt_c =~ /^([0-9]+)G$/) {
|
|
|
|
$crit_type = "K";
|
|
|
|
$crit = $1 * 1048576;
|
|
|
|
}
|
|
|
|
|
|
|
|
# check if both warning and critical are percentage or size
|
|
|
|
unless( ( $warn_type eq "P" && $crit_type eq "P" ) || ( $warn_type ne "P" && $crit_type ne "P" ) ){
|
|
|
|
$opt_w =~ s/\%/\%\%/g;
|
|
|
|
$opt_c =~ s/\%/\%\%/g;
|
|
|
|
usage("Both warning and critical should be same type- warning: $opt_w critical: $opt_c \n");
|
|
|
|
}
|
|
|
|
|
|
|
|
# verify warning is less than critical
|
|
|
|
if ( $warn_type eq "K") {
|
|
|
|
unless ( $warn > $crit) {
|
|
|
|
usage("Disk size: warning ($opt_w) should be greater than critical ($opt_c) \n");
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
unless ( $warn < $crit) {
|
|
|
|
$opt_w =~ s/\%/\%\%/g;
|
|
|
|
$opt_c =~ s/\%/\%\%/g;
|
|
|
|
usage("Percentage: warning ($opt_w) should be less than critical ($opt_c) \n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my $workgroup = $1 if (defined($opt_W) && $opt_W =~ /(.*)/);
|
|
|
|
|
2013-11-26 22:57:29 +00:00
|
|
|
my $address = $1 if (defined($opt_a) && $opt_a =~ /(.*)/);
|
|
|
|
|
2013-11-26 22:53:19 +00:00
|
|
|
# end of options checking
|
|
|
|
|
|
|
|
|
|
|
|
my $state = "OK";
|
|
|
|
my $answer = undef;
|
|
|
|
my $res = undef;
|
2013-11-26 22:59:06 +00:00
|
|
|
my $perfdata = "";
|
2013-11-26 22:53:19 +00:00
|
|
|
my @lines = undef;
|
|
|
|
|
2014-07-11 19:01:00 +00:00
|
|
|
# Just in case of problems, let's not hang the monitoring system
|
2013-11-26 22:53:19 +00:00
|
|
|
$SIG{'ALRM'} = sub {
|
|
|
|
print "No Answer from Client\n";
|
|
|
|
exit $ERRORS{"UNKNOWN"};
|
|
|
|
};
|
|
|
|
alarm($TIMEOUT);
|
|
|
|
|
2013-11-26 22:59:47 +00:00
|
|
|
# Execute a "du" on the share using smbclient program
|
2013-11-26 22:53:19 +00:00
|
|
|
# get the results into $res
|
2013-11-26 22:57:29 +00:00
|
|
|
my @cmd = (
|
|
|
|
$smbclient,
|
|
|
|
"//$host/$share",
|
|
|
|
"-U", "$user%$pass",
|
|
|
|
defined($workgroup) ? ("-W", $workgroup) : (),
|
|
|
|
defined($address) ? ("-I", $address) : (),
|
|
|
|
defined($opt_P) ? ("-p", $opt_P) : (),
|
2013-11-26 22:59:47 +00:00
|
|
|
"-c", "du"
|
2013-11-26 22:57:29 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
print join(" ", @cmd) . "\n" if ($verbose);
|
|
|
|
$res = output_and_error_of(@cmd) or exit $ERRORS{"UNKNOWN"};
|
|
|
|
|
2013-11-26 22:53:19 +00:00
|
|
|
#Turn off alarm
|
|
|
|
alarm(0);
|
|
|
|
|
|
|
|
#Split $res into an array of lines
|
|
|
|
@lines = split /\n/, $res;
|
|
|
|
|
|
|
|
#Get the last line into $_
|
2013-11-26 22:59:47 +00:00
|
|
|
$_ = $lines[$#lines-1];
|
2013-11-26 22:53:19 +00:00
|
|
|
#print "$_\n";
|
|
|
|
|
|
|
|
#Process the last line to get free space.
|
|
|
|
#If line does not match required regexp, return an UNKNOWN error
|
|
|
|
if (/\s*(\d*) blocks of size (\d*)\. (\d*) blocks available/) {
|
|
|
|
|
2013-11-26 22:59:06 +00:00
|
|
|
my ($avail_bytes) = $3 * $2;
|
|
|
|
my ($total_bytes) = $1 * $2;
|
|
|
|
my ($occupied_bytes) = $1 * $2 - $avail_bytes;
|
|
|
|
my ($avail) = $avail_bytes/1024;
|
2013-11-26 22:53:19 +00:00
|
|
|
my ($capper) = int(($3/$1)*100);
|
|
|
|
my ($mountpt) = "\\\\$host\\$share";
|
|
|
|
|
2013-11-26 22:59:06 +00:00
|
|
|
# TODO : why is the kB the standard unit for args ?
|
|
|
|
my ($warn_bytes) = $total_bytes - $warn * 1024;
|
|
|
|
if ($warn_type eq "P") {
|
|
|
|
$warn_bytes = $warn * $1 * $2 / 100;
|
|
|
|
}
|
|
|
|
my ($crit_bytes) = $total_bytes - $crit * 1024;
|
|
|
|
if ($crit_type eq "P") {
|
|
|
|
$crit_bytes = $crit * $1 * $2 / 100;
|
|
|
|
}
|
|
|
|
|
2013-11-26 22:53:19 +00:00
|
|
|
|
|
|
|
if (int($avail / 1024) > 0) {
|
|
|
|
$avail = int($avail / 1024);
|
|
|
|
if (int($avail /1024) > 0) {
|
|
|
|
$avail = (int(($avail / 1024)*100))/100;
|
|
|
|
$avail = $avail ."G";
|
|
|
|
} else {
|
|
|
|
$avail = $avail ."M";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$avail = $avail ."K";
|
|
|
|
}
|
|
|
|
|
|
|
|
#print ":$warn:$warn_type:\n";
|
|
|
|
#print ":$crit:$crit_type:\n";
|
|
|
|
#print ":$avail:$avail_bytes:$capper:$mountpt:\n";
|
2013-11-26 22:59:06 +00:00
|
|
|
$perfdata = "'" . $share . "'=" . $occupied_bytes . 'B;'
|
|
|
|
. $warn_bytes . ';'
|
|
|
|
. $crit_bytes . ';'
|
|
|
|
. '0;'
|
|
|
|
. $total_bytes;
|
2013-11-26 22:53:19 +00:00
|
|
|
|
2013-11-26 22:59:06 +00:00
|
|
|
if ($occupied_bytes > $crit_bytes) {
|
|
|
|
$state = "CRITICAL";
|
|
|
|
$answer = "CRITICAL: Only $avail ($capper%) free on $mountpt";
|
|
|
|
} elsif ( $occupied_bytes > $warn_bytes ) {
|
2013-11-26 22:53:19 +00:00
|
|
|
$state = "WARNING";
|
2013-11-26 22:59:06 +00:00
|
|
|
$answer = "WARNING: Only $avail ($capper%) free on $mountpt";
|
2013-11-26 22:53:19 +00:00
|
|
|
} else {
|
2013-11-26 22:59:06 +00:00
|
|
|
$answer = "Disk ok - $avail ($capper%) free on $mountpt";
|
2013-11-26 22:53:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
2013-11-26 22:59:06 +00:00
|
|
|
$answer = "Result from smbclient not suitable";
|
2013-11-26 22:53:19 +00:00
|
|
|
$state = "UNKNOWN";
|
|
|
|
foreach (@lines) {
|
2013-11-26 22:58:53 +00:00
|
|
|
if (/(Access denied|NT_STATUS_LOGON_FAILURE|NT_STATUS_ACCESS_DENIED)/) {
|
2013-11-26 22:59:06 +00:00
|
|
|
$answer = "Access Denied";
|
2013-11-26 22:53:19 +00:00
|
|
|
$state = "CRITICAL";
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
if (/(Unknown host \w*|Connection.*failed)/) {
|
2013-11-26 22:59:06 +00:00
|
|
|
$answer = "$1";
|
2013-11-26 22:53:19 +00:00
|
|
|
$state = "CRITICAL";
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
if (/(You specified an invalid share name|NT_STATUS_BAD_NETWORK_NAME)/) {
|
2013-11-26 22:59:06 +00:00
|
|
|
$answer = "Invalid share name \\\\$host\\$share";
|
2013-11-26 22:53:19 +00:00
|
|
|
$state = "CRITICAL";
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
print $answer;
|
2013-11-26 22:59:06 +00:00
|
|
|
print " | " . $perfdata if ($perfdata);
|
|
|
|
print "\n";
|
2013-11-26 22:53:19 +00:00
|
|
|
print "$state\n" if ($verbose);
|
|
|
|
exit $ERRORS{$state};
|
|
|
|
|
|
|
|
sub print_usage () {
|
|
|
|
print "Usage: $PROGNAME -H <host> -s <share> -u <user> -p <password>
|
2013-11-26 22:57:29 +00:00
|
|
|
-w <warn> -c <crit> [-W <workgroup>] [-P <port>] [-a <IP>]\n";
|
2013-11-26 22:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub print_help () {
|
2013-11-26 22:56:50 +00:00
|
|
|
print_revision($PROGNAME,'@NP_VERSION@');
|
2013-11-26 22:53:19 +00:00
|
|
|
print "Copyright (c) 2000 Michael Anthon/Karl DeBisschop
|
|
|
|
|
2014-07-11 19:01:00 +00:00
|
|
|
Perl Check SMB Disk plugin for monitoring
|
2013-11-26 22:53:19 +00:00
|
|
|
|
|
|
|
";
|
|
|
|
print_usage();
|
|
|
|
print "
|
|
|
|
-H, --hostname=HOST
|
|
|
|
NetBIOS name of the server
|
|
|
|
-s, --share=STRING
|
|
|
|
Share name to be tested
|
|
|
|
-W, --workgroup=STRING
|
|
|
|
Workgroup or Domain used (Defaults to \"WORKGROUP\")
|
2013-11-26 22:57:29 +00:00
|
|
|
-a, --address=IP
|
|
|
|
IP-address of HOST (only necessary if HOST is in another network)
|
2013-11-26 22:53:19 +00:00
|
|
|
-u, --user=STRING
|
|
|
|
Username to log in to server. (Defaults to \"guest\")
|
|
|
|
-p, --password=STRING
|
|
|
|
Password to log in to server. (Defaults to an empty password)
|
|
|
|
-w, --warning=INTEGER or INTEGER[kMG]
|
|
|
|
Percent of used space at which a warning will be generated (Default: 85%)
|
|
|
|
|
|
|
|
-c, --critical=INTEGER or INTEGER[kMG]
|
|
|
|
Percent of used space at which a critical will be generated (Defaults: 95%)
|
|
|
|
-P, --port=INTEGER
|
|
|
|
Port to be used to connect to. Some Windows boxes use 139, others 445 (Defaults to smbclient default)
|
|
|
|
|
|
|
|
If thresholds are followed by either a k, M, or G then check to see if that
|
|
|
|
much disk space is available (kilobytes, Megabytes, Gigabytes)
|
|
|
|
|
|
|
|
Warning percentage should be less than critical
|
|
|
|
Warning (remaining) disk space should be greater than critical.
|
|
|
|
|
|
|
|
";
|
|
|
|
support();
|
|
|
|
}
|