Code

Fix Debian bug #425129: SMB guest mode won't work
[nagiosplug.git] / plugins-scripts / check_disk_smb.pl
index 02b7150028c2ad44ce612b38d6e4ffbf19139d05..022fa505ceabe4913e102e6cd1b6696dbdd322a5 100755 (executable)
 #  drives is a pain in the butt
 # 2-May-2002 - SGhosh fix for embedded perl
 #
-# $Id$
 #
 
 require 5.004;
 use POSIX;
 use strict;
 use Getopt::Long;
-use vars qw($opt_P $opt_V $opt_h $opt_H $opt_s $opt_W $opt_u $opt_p $opt_w $opt_c $verbose);
+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);
 use vars qw($PROGNAME);
 use lib utils.pm ;
 use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
@@ -49,10 +48,11 @@ GetOptions
         "u=s" => \$opt_u, "username=s" => \$opt_u,
         "s=s" => \$opt_s, "share=s"    => \$opt_s,
         "W=s" => \$opt_W, "workgroup=s" => \$opt_W,
-        "H=s" => \$opt_H, "hostname=s" => \$opt_H);
+        "H=s" => \$opt_H, "hostname=s" => \$opt_H,
+        "a=s" => \$opt_a, "address=s" => \$opt_a);
 
 if ($opt_V) {
-       print_revision($PROGNAME,'$Revision$'); #'
+       print_revision($PROGNAME,'@NP_VERSION@'); #'
        exit $ERRORS{'OK'};
 }
 
@@ -65,7 +65,7 @@ my $smbclientoptions= $opt_P ? "-p $opt_P " : "";
 # Options checking
 
 ($opt_H) || ($opt_H = shift) || usage("Host name not specified\n");
-my $host = $1 if ($opt_H =~ /^([-_.A-Za-z0-9]+\$?)$/);
+my $host = $1 if ($opt_H =~ /^([-_.A-Za-z0-9 ]+\$?)$/);
 ($host) || usage("Invalid host: $opt_H\n");
 
 ($opt_s) || ($opt_s = shift) || usage("Share volume not specified\n");
@@ -78,6 +78,7 @@ my $user = $1 if ($opt_u =~ /^([-_.A-Za-z0-9\\]+)$/);
 
 ($opt_p) || ($opt_p = shift) || ($opt_p = "");
 my $pass = $1 if ($opt_p =~ /(.*)/);
+$pass = "-N" if ($opt_p eq "");
 
 ($opt_w) || ($opt_w = shift) || ($opt_w = 85);
 my $warn = $1 if ($opt_w =~ /^([0-9]{1,2}\%?|100\%?|[0-9]+[kMG])$/);
@@ -87,24 +88,63 @@ my $warn = $1 if ($opt_w =~ /^([0-9]{1,2}\%?|100\%?|[0-9]+[kMG])$/);
 my $crit = $1 if ($opt_c =~ /^([0-9]{1,2}\%?|100\%?|[0-9]+[kMG])$/);
 ($crit) || usage("Invalid critical threshold: $opt_c\n");
 
+# 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( ( ($opt_w =~ /([0-9]){1,2}$/ ) && ($opt_c =~ /([0-9]){1,2}$/ )  )|| (( $opt_w =~ /[kMG]/ ) && ($opt_c =~ /[kMG]/) )  ){
+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 ( $opt_w =~ /[kMG]/) {
+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 =~ /(.*)/);
 
+my $address = $1 if (defined($opt_a) && $opt_a =~ /(.*)/);
+
 # end of options checking
 
 
@@ -123,10 +163,21 @@ alarm($TIMEOUT);
 # Execute an "ls" on the share using smbclient program
 # get the results into $res
 if (defined($workgroup)) {
-       $res = qx/$smbclient \/\/$host\/$share -W $workgroup -U $user%$pass $smbclientoptions -c ls/;
+       if (defined($address)) {
+               print "$smbclient " . "\/\/$host\/$share" ." $pass -W $workgroup -U $user $smbclientoptions -I $address -c ls\n" if ($verbose);
+               $res = qx/$smbclient "\/\/$host\/$share" $pass -W $workgroup -U $user $smbclientoptions -I $address -c ls/;
+       } else {
+               print "$smbclient " . "\/\/$host\/$share" ." $pass -W $workgroup -U $user $smbclientoptions -c ls\n" if ($verbose);
+               $res = qx/$smbclient "\/\/$host\/$share" $pass -W $workgroup -U $user $smbclientoptions -c ls/;
+       }
 } else {
-       print "$smbclient " . "\/\/$host\/$share" ." $pass -U $user $smbclientoptions -c ls\n" if ($verbose);
-       $res = qx/$smbclient \/\/$host\/$share -U $user%$pass $smbclientoptions -c ls/;
+       if (defined($address)) {
+               print "$smbclient " . "\/\/$host\/$share" ." $pass -U $user $smbclientoptions -I $address -c ls\n" if ($verbose);
+               $res = qx/$smbclient "\/\/$host\/$share" $pass -U $user $smbclientoptions -I $address -c ls/;
+       } else {
+               print "$smbclient " . "\/\/$host\/$share" ." $pass -U $user $smbclientoptions -c ls\n" if ($verbose);
+               $res = qx/$smbclient "\/\/$host\/$share" $pass -U $user $smbclientoptions -c ls/;
+       }
 }
 #Turn off alarm
 alarm(0);
@@ -147,35 +198,6 @@ if (/\s*(\d*) blocks of size (\d*)\. (\d*) blocks available/) {
        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 ($opt_w =~ /^([0-9]+$)/) {
-               $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_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;
-       }
 
        if (int($avail / 1024) > 0) {
                $avail = int($avail / 1024);
@@ -231,11 +253,11 @@ exit $ERRORS{$state};
 
 sub print_usage () {
        print "Usage: $PROGNAME -H <host> -s <share> -u <user> -p <password> 
-      -w <warn> -c <crit> [-W <workgroup>] [-P <port>]\n";
+      -w <warn> -c <crit> [-W <workgroup>] [-P <port>] [-a <IP>]\n";
 }
 
 sub print_help () {
-       print_revision($PROGNAME,'$Revision$');
+       print_revision($PROGNAME,'@NP_VERSION@');
        print "Copyright (c) 2000 Michael Anthon/Karl DeBisschop
 
 Perl Check SMB Disk plugin for Nagios
@@ -249,10 +271,12 @@ Perl Check SMB Disk plugin for Nagios
    Share name to be tested
 -W, --workgroup=STRING
    Workgroup or Domain used (Defaults to \"WORKGROUP\")
+-a, --address=IP
+   IP-address of HOST (only necessary if HOST is in another network)
 -u, --user=STRING
    Username to log in to server. (Defaults to \"guest\")
 -p, --password=STRING
-   Password to log in to server. (Defaults to \"guest\")
+   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%)