2010-03-11 17:35:48 +01:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
|
2010-11-21 19:22:35 +01:00
|
|
|
# Copyright 2010, Tomas Edwardsson <tommi@opensource.is>
|
|
|
|
|
|
|
|
# check_cifs 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 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# check_cifs 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2010-03-11 17:35:48 +01:00
|
|
|
use strict;
|
2010-03-31 13:14:01 +02:00
|
|
|
use Nagios::Plugin;
|
2010-03-11 17:35:48 +01:00
|
|
|
use Data::Dumper;
|
|
|
|
|
2010-03-31 13:14:01 +02:00
|
|
|
|
|
|
|
# Create the Nagios plugin object
|
|
|
|
my $np = Nagios::Plugin->new(
|
2010-03-31 16:25:39 +02:00
|
|
|
usage => "Usage: %s -H <hostname> -u <username> -p <password> -s <share>",
|
2010-03-31 13:14:01 +02:00
|
|
|
version => "0.01",
|
|
|
|
);
|
|
|
|
|
|
|
|
# Add valid arguments
|
|
|
|
$np->add_arg(
|
|
|
|
spec => 'hostname|H=s',
|
|
|
|
help => '-H, --hostname=<hostname>',
|
|
|
|
required => 1,
|
|
|
|
);
|
|
|
|
|
|
|
|
$np->add_arg(
|
|
|
|
spec => 'username|u=s',
|
|
|
|
help => '-u, --username=<username>',
|
|
|
|
required => 1,
|
|
|
|
);
|
|
|
|
|
|
|
|
$np->add_arg(
|
|
|
|
spec => 'password|p=s',
|
|
|
|
help => '-p, --password=<password>',
|
|
|
|
required => 1,
|
|
|
|
);
|
|
|
|
|
|
|
|
$np->add_arg(
|
|
|
|
spec => 'share|s=s',
|
|
|
|
help => '-s, --share=<password>',
|
|
|
|
required => 1,
|
|
|
|
);
|
|
|
|
|
|
|
|
$np->add_arg(
|
|
|
|
spec => 'kerberos|k',
|
|
|
|
help => '-k, --kerberos',
|
|
|
|
required => 0,
|
|
|
|
);
|
|
|
|
|
|
|
|
$np->add_arg(
|
|
|
|
spec => 'writefile|w=s',
|
|
|
|
help => '-w, --writefile=<filepath>',
|
|
|
|
required => 0,
|
|
|
|
);
|
|
|
|
|
2010-03-31 16:25:39 +02:00
|
|
|
$np->add_arg(
|
|
|
|
spec => 'writesize|W=i',
|
|
|
|
help => '-W, --writesize=<megabytes>',
|
|
|
|
required => 0,
|
|
|
|
default => 10
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
my @tmpfiles = ();
|
|
|
|
|
|
|
|
sub createtmpfile($) {
|
|
|
|
my $size = shift;
|
|
|
|
|
|
|
|
$size = 10 if (!defined $size);
|
|
|
|
|
|
|
|
$np->nagios_exit(UNKNOWN, "Size for param writesize not numeric") if ($size !~ /^\d+$/);
|
|
|
|
|
|
|
|
my $tmpfile = `mktemp check-cifs-XXXXXX -p /tmp`;
|
|
|
|
my $rc = $? >> 8;
|
|
|
|
chomp($tmpfile);
|
|
|
|
|
|
|
|
push @tmpfiles, $tmpfile;
|
|
|
|
if ($rc) {
|
|
|
|
$np->nagios_exit(UNKNOWN, "Unable to run mktemp for temporary file");
|
|
|
|
}
|
|
|
|
|
|
|
|
open TMP, ">$tmpfile" or
|
|
|
|
$np->nagios_exit(UNKNOWN, "Cannot open tempfile for writing: $!");
|
|
|
|
|
|
|
|
for (1..$size) {
|
|
|
|
print TMP "0"x(1024*1024);
|
|
|
|
}
|
|
|
|
close TMP;
|
2010-03-11 17:35:48 +01:00
|
|
|
|
2010-03-31 16:25:39 +02:00
|
|
|
return $tmpfile;
|
|
|
|
}
|
2010-03-11 17:35:48 +01:00
|
|
|
|
|
|
|
sub testbin($) {
|
|
|
|
my $program = shift;
|
|
|
|
eval {
|
|
|
|
system("$program &> /dev/null");
|
|
|
|
};
|
|
|
|
my $exit_value = $? >> 8;
|
|
|
|
if ($exit_value != 127) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
2010-03-31 16:25:39 +02:00
|
|
|
$np->nagios_exit(UNKNOWN, "Could not execute $program");
|
2010-03-11 17:35:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-03-31 16:25:39 +02:00
|
|
|
# Check needed binaries
|
2010-03-11 17:35:48 +01:00
|
|
|
testbin("smbclient");
|
|
|
|
testbin("kinit");
|
|
|
|
testbin("kdestroy");
|
|
|
|
|
|
|
|
|
2010-03-31 16:25:39 +02:00
|
|
|
# Parse command line operations
|
2010-03-31 13:14:01 +02:00
|
|
|
$np->getopts;
|
|
|
|
|
2010-03-31 16:25:39 +02:00
|
|
|
|
|
|
|
# Initialize global for kerberos credentials cache
|
|
|
|
my $krb5cc = "";
|
|
|
|
|
2010-03-11 17:35:48 +01:00
|
|
|
# Login with kerberos
|
2010-03-31 13:14:01 +02:00
|
|
|
if ($np->opts->kerberos) {
|
2010-03-31 16:25:39 +02:00
|
|
|
# Cache saved to temporary file too avoid clashed with
|
|
|
|
# other plugins
|
|
|
|
$krb5cc = createtmpfile(0);
|
2010-03-31 13:14:01 +02:00
|
|
|
|
2010-03-31 16:25:39 +02:00
|
|
|
# Execute kinit kerberised login
|
|
|
|
my $kcmd = sprintf("echo '%s' | kinit -c %s '%s' 2>&1", $np->opts->password, $krb5cc, $np->opts->username);
|
2010-03-31 13:14:01 +02:00
|
|
|
my $kinit = `$kcmd`;
|
2010-03-11 17:35:48 +01:00
|
|
|
my $exit_value = $? >> 8;
|
|
|
|
|
2010-03-31 16:25:39 +02:00
|
|
|
# Check return code for failures
|
2010-03-11 17:35:48 +01:00
|
|
|
if ($exit_value != 0) {
|
|
|
|
chomp $kinit;
|
2010-03-31 16:25:39 +02:00
|
|
|
$np->nagios_exit(CRITICAL, "Unable to log in with kerberos - $kinit");
|
2010-03-11 17:35:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-31 16:25:39 +02:00
|
|
|
# Array of arguments for smbclient based on various circumstances
|
2010-03-11 17:35:48 +01:00
|
|
|
my @smbclient_opts;
|
|
|
|
my @smbclient_commands;
|
|
|
|
|
2010-03-31 16:25:39 +02:00
|
|
|
# Set servicename
|
|
|
|
push @smbclient_opts, sprintf("//%s/%s", $np->opts->hostname, $np->opts->share);
|
2010-03-31 13:14:01 +02:00
|
|
|
|
2010-03-31 16:25:39 +02:00
|
|
|
# Kerberised or NTLM ?
|
2010-03-31 13:14:01 +02:00
|
|
|
if ($np->opts->kerberos) {
|
2010-03-11 17:35:48 +01:00
|
|
|
push @smbclient_opts, '-k'
|
|
|
|
} else {
|
2010-03-31 16:25:39 +02:00
|
|
|
push @smbclient_opts, "'" . $np->opts->password . "'", '-U', "'" . $np->opts->username . "'",
|
2010-03-11 17:35:48 +01:00
|
|
|
}
|
|
|
|
|
2010-03-31 16:25:39 +02:00
|
|
|
|
|
|
|
# We want to test writing a file to the remote share
|
|
|
|
if ($np->opts->writefile) {
|
|
|
|
my $tmpfile = createtmpfile($np->opts->writesize);
|
|
|
|
my $cmd = ($krb5cc ? "KRB5CCNAME=$krb5cc " : "") . "smbclient " . join(" ", @smbclient_opts) . " -c 'put $tmpfile " . $np->opts->writefile . "' 2>&1 |";
|
|
|
|
open SMBCLIENT, $cmd or
|
|
|
|
$np->nagios_exit(CRITICAL, "Unable to run CIFS connection: $!");
|
|
|
|
|
|
|
|
# Toss header
|
|
|
|
<SMBCLIENT>;
|
|
|
|
|
|
|
|
my $return = <SMBCLIENT>;
|
|
|
|
chomp($return);
|
|
|
|
|
|
|
|
if ($return =~ /^putting file \S+? as \S+? \((\d+\.\d*) /) {
|
|
|
|
close SMBCLIENT;
|
|
|
|
$np->add_perfdata(
|
|
|
|
label => "file_transfer",
|
|
|
|
value => $1,
|
|
|
|
uom => "KBps" );
|
|
|
|
|
|
|
|
$np->nagios_exit(OK, "Connected and uploaded file at rate of $1" . ($np->opts->kerberos ? " (kerberised)" : ""));
|
|
|
|
} else {
|
|
|
|
close SMBCLIENT;
|
|
|
|
$np->nagios_exit(CRITICAL, "Unable to upload file" . ($np->opts->kerberos ? " (kerberised)" : "") . ": $return");
|
|
|
|
}
|
|
|
|
|
|
|
|
# Just a regular connection test
|
|
|
|
} else {
|
|
|
|
my $cmd = ($krb5cc ? "KRB5CCNAME=$krb5cc " : "") . "smbclient " . join(" ", @smbclient_opts) . " -c '' 2>&1 |";
|
|
|
|
open SMBCLIENT, $cmd or
|
|
|
|
$np->nagios_exit(CRITICAL, "Unable to run CIFS connection: $!");
|
|
|
|
|
|
|
|
# Toss header
|
|
|
|
<SMBCLIENT>;
|
|
|
|
my $nterror = <SMBCLIENT>;
|
|
|
|
close SMBCLIENT;
|
|
|
|
|
|
|
|
chomp($nterror) if ($nterror);
|
|
|
|
if ($nterror) {
|
|
|
|
$np->nagios_exit(CRITICAL, "Unable to connect to share" . ($np->opts->kerberos ? " (kerberised)" : "") . ": $nterror");
|
|
|
|
}
|
|
|
|
$np->nagios_exit(OK, "Connected successfully" . ($np->opts->kerberos ? " (kerberised)" : ""));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Hack to remove the krb5 credentials file if we have exited
|
|
|
|
END {
|
|
|
|
|
|
|
|
if (@tmpfiles) {
|
|
|
|
foreach my $f (@tmpfiles) {
|
|
|
|
if (-f $f) {
|
|
|
|
unlink $f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2010-03-11 17:35:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
|