Code:
# Execute an "ls" on the share using smbclient program
# get the results into $res
if (defined($workgroup)) {
$res = qx/$smbclient \/\/$host\/$share $pass -W $workgroup -U $user $smbclientoptions -c ls/;
} else {
$res = qx/$smbclient \/\/$host\/$share $pass -U $user $smbclientoptions -c ls/;
}
#Turn off alarm
alarm(0);
#Split $res into an array of lines
@lines = split /\n/, $res;
#Get the last line into $_
$_ = $lines[$#lines];
#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/) {
my ($avail) = ($3*$2)/1024;
my ($avail_bytes) = $avail;
my ($capper) = int(($3/$1)*100);
my ($mountpt) = "\\\\$host\\$share";
#Check $warn and $crit for type (%/M/G) and set up for tests
#P = Percent, K = KBytes
my $warn_type;
my $crit_type;
if ($warn =~ /^([0-9]+$)/) {
$warn_type = "P";
} elsif ($warn =~ /^([0-9]+)k$/) {
my ($warn_type) = "K";
$warn = $1;
} elsif ($warn =~ /^([0-9]+)M$/) {
$warn_type = "K";
$warn = $1 * 1024;
} elsif ($warn =~ /^([0-9]+)G$/) {
$warn_type = "K";
$warn = $1 * 1048576;
}
if ($crit =~ /^([0-9]+$)/) {
$crit_type = "P";
} elsif ($crit =~ /^([0-9]+)k$/) {
$crit_type = "K";
$crit = $1;
} elsif ($crit =~ /^([0-9]+)M$/) {
$crit_type = "K";
$crit = $1 * 1024;
} elsif ($crit =~ /^([0-9]+)G$/) {
$crit_type = "K";
$crit = $1 * 1048576;
}
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";
if ((($warn_type eq "P") && (100 - $capper) < $warn) || (($warn_type eq "K") && ($avail_bytes > $warn))) {
$answer = "Disk ok - $avail ($capper%) free on $mountpt\n";
} elsif ((($crit_type eq "P") && (100 - $capper) < $crit) || (($crit_type eq "K") && ($avail_bytes > $crit))) {
$state = "WARNING";
$answer = "Only $avail ($capper%) free on $mountpt\n";
} else {
$state = "CRITICAL";
$answer = "Only $avail ($capper%) free on $mountpt\n";
}
} else {
$answer = "Result from smbclient not suitable\n";
$state = "UNKNOWN";
foreach (@lines) {
if (/Access denied/) {
$answer = "Access Denied\n";
$state = "CRITICAL";
last;
}
if (/(Unknown host \w*)/) {
$answer = "$1\n";
$state = "CRITICAL";
last;
}
if (/(You specified an invalid share name)/) {
$answer = "Invalid share name \\\\$host\\$share\n";
$state = "CRITICAL";
last;
}
}
}
print $answer;
print "$state\n" if ($verbose);
exit $ERRORS{$state};
sub print_usage () {
print "Usage: $PROGNAME -H <host> -s <share> -u <user> -p <password>
-w <warn> -c <crit> [-W <workgroup>]\n";
}
sub print_help () {
print_revision($PROGNAME,'$Revision: 1.5.2.1 $ ');
print "Copyright (c) 2000 Michael Anthon/Karl DeBisschop
Perl Check SMB Disk plugin for NetSaint
";
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\")
-u, --user=STRING
Username to log in to server. (Defaults to \"guest\")
-p, --password=STRING
Password to log in to server. (Defaults to \"guest\")
-w, --warning=INTEGER
Percent of used space at which a warning will be generated (Default: 85%)
-c, --critical=INTEGER
Percent of used space at which a critical will be generated (Defaults: 95%)
";
support();
}
sub version () {
print_revision($PROGNAME,'$Revision: 1.5.2.1 $ ');
exit $ERRORS{'OK'};
}
sub help () {
print_help();
exit $ERRORS{'OK'};
}
[/code]