summaryrefslogtreecommitdiffstats
path: root/web/attachments/158101-check_disk_smb.pl.PATCH
blob: 193932eafd72a854e439fadc42a1b82cbc2020de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
--- 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 <host> <share> <user> <pass> [warn] [critical] [port]
+# check_disk.pl <ip> <host> <share> <user> <pass> [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 <host> -s <share> -u <user> -p <password> 
+	print "Usage: $PROGNAME -H <host> -I <ipaddr> -s <share> -u <user> -p <password>
       -w <warn> -c <crit> [-W <workgroup>] [-P <port>]\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