--- check_disk_smb.pl.ORIG Sun Apr 17 22:22:41 2005 +++ check_disk_smb.pl Mon Nov 28 20:11:19 2005 @@ -1,7 +1,7 @@ #!/usr/bin/perl -w # # -# check_disk.pl [warn] [critical] [port] +# check_disk.pl [warn] [critical] [port] # # Nagios host script to get the disk usage from a SMB share # @@ -15,6 +15,10 @@ # 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 +# 11-Nov-2005 - John Relph +# allow specifying IP address for NetBIOS names that don't resolve +# correctly using DNS; specifically for *nix samba servers using +# virtual names # # $Id: check_disk_smb.pl,v 1.12 2005/04/17 22:22:41 seanius Exp $ # @@ -23,7 +27,7 @@ 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_I $opt_s $opt_W $opt_u $opt_p $opt_w $opt_c $verbose); use vars qw($PROGNAME); use lib utils.pm ; use utils qw($TIMEOUT %ERRORS &print_revision &support &usage); @@ -49,7 +53,9 @@ "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, + "I=s" => \$opt_I, "ipaddr=s" => \$opt_I + ); if ($opt_V) { print_revision($PROGNAME,'$Revision: 1.12 $'); #' @@ -68,8 +74,14 @@ my $host = $1 if ($opt_H =~ /^([-_.A-Za-z0-9]+\$?)$/); ($host) || usage("Invalid host: $opt_H\n"); +($opt_I) || ($opt_I = shift) || ($opt_I = ''); +my $ipaddr = ($opt_I =~ /^([-_.A-Za-z0-9]+\$?)$/ ? $1 : ''); +if ($opt_I) { ($ipaddr) || usage("Invalid IP address: $opt_I\n"); } + +$smbclientoptions .= "-I $ipaddr " if $ipaddr; + ($opt_s) || ($opt_s = shift) || usage("Share volume not specified\n"); -my $share = $1 if ($opt_s =~ /^([-_.A-Za-z0-9]+\$?)$/); +my $share = $1 if ($opt_s =~ /^([-_.A-Za-z0-9 ]+\$?)$/); ($share) || usage("Invalid share: $opt_s\n"); ($opt_u) || ($opt_u = shift) || ($opt_u = "guest"); @@ -120,27 +132,23 @@ }; alarm($TIMEOUT); -# Execute an "ls" on the share using smbclient program +# Execute a "du" 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/; + print "$smbclient " . "\/\/$host\/$share" ." $pass -W $workgroup -U $user $smbclientoptions -c du\n" if ($verbose); + $res = qx/$smbclient "\/\/$host\/$share" -W $workgroup -U $user%$pass $smbclientoptions -c du/; } 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/; + print "$smbclient " . "\/\/$host\/$share" ." $pass -U $user $smbclientoptions -c du\n" if ($verbose); + $res = qx/$smbclient "\/\/$host\/$share" -U $user%$pass $smbclientoptions -c du/; } #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"; +print "result: $res" if ($verbose); -#Process the last line to get free space. +#Process the output 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/) { +if ($res =~ m/\s*(\d*) blocks of size (\d*)\. (\d*) blocks available/s) { my ($avail) = ($3*$2)/1024; my ($avail_bytes) = $avail; @@ -189,9 +197,9 @@ $avail = $avail ."K"; } -#print ":$warn:$warn_type:\n"; -#print ":$crit:$crit_type:\n"; -#print ":$avail:$avail_bytes:$capper:$mountpt:\n"; +print ":$warn:$warn_type:\n" if ($verbose); +print ":$crit:$crit_type:\n" if ($verbose); +print ":$avail:$avail_bytes:$capper:$mountpt:\n" if ($verbose); if ((($warn_type eq "P") && (100 - $capper) < $warn) || (($warn_type eq "K") && ($avail_bytes > $warn))) { $answer = "Disk ok - $avail ($capper%) free on $mountpt\n"; @@ -205,18 +213,18 @@ } else { $answer = "Result from smbclient not suitable\n"; $state = "UNKNOWN"; - foreach (@lines) { - if (/(Access denied|NT_STATUS_LOGON_FAILURE)/) { + foreach ($res) { + if (/(Access denied|NT_STATUS_LOGON_FAILURE)/s) { $answer = "Access Denied\n"; $state = "CRITICAL"; last; } - if (/(Unknown host \w*|Connection.*failed)/) { + if (/(Unknown host \w*|Connection.*failed)/s) { $answer = "$1\n"; $state = "CRITICAL"; last; } - if (/(You specified an invalid share name|NT_STATUS_BAD_NETWORK_NAME)/) { + if (/(You specified an invalid share name|NT_STATUS_BAD_NETWORK_NAME)/s) { $answer = "Invalid share name \\\\$host\\$share\n"; $state = "CRITICAL"; last; @@ -230,7 +238,7 @@ exit $ERRORS{$state}; sub print_usage () { - print "Usage: $PROGNAME -H -s -u -p + print "Usage: $PROGNAME -H -I -s -u -p -w -c [-W ] [-P ]\n"; } @@ -245,6 +253,8 @@ print " -H, --hostname=HOST NetBIOS name of the server +-I, --ipaddr=IPADDR + IP address of the server -s, --share=STRING Share name to be tested -W, --workgroup=STRING