summaryrefslogtreecommitdiffstats
path: root/web/attachments/284178-34_fix_smbclient_check_disk_smb.dpatch
blob: 0e53eb1633f422e60a9a33e579b6febb89d0c27a (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
#! /bin/sh /usr/share/dpatch/dpatch-run
## 34_fix_smbclient_check_disk_smb.dpatch by Jan Wagner <waja@cyconet.org>
## patch provided by Stephane Chazelas <stephane@artesyncp.com>
##
## DP: Fixes use of smbclient

@DPATCH@
diff -urNad nagios-plugins-1.4.12~/plugins-scripts/check_disk_smb.pl nagios-plugins-1.4.12/plugins-scripts/check_disk_smb.pl
--- nagios-plugins-1.4.12~/plugins-scripts/check_disk_smb.pl	2008-07-02 23:08:03.000000000 +0200
+++ nagios-plugins-1.4.12/plugins-scripts/check_disk_smb.pl	2008-07-02 23:10:42.000000000 +0200
@@ -26,17 +26,13 @@
 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);
+use utils qw($TIMEOUT %ERRORS &print_revision &support &usage &output_and_error_of);
 
 sub print_help ();
 sub print_usage ();
 
 $PROGNAME = "check_disk_smb";
 
-$ENV{'PATH'}='';
-$ENV{'BASH_ENV'}=''; 
-$ENV{'ENV'}='';
-
 Getopt::Long::Configure('bundling');
 GetOptions
 	("v"   => \$verbose, "verbose"    => \$verbose,
@@ -59,9 +55,7 @@
 
 if ($opt_h) {print_help(); exit $ERRORS{'OK'};}
 
-my $smbclient= "$utils::PATH_TO_SMBCLIENT " ;
-my $smbclientoptions= $opt_P ? "-p $opt_P " : "";
-
+my $smbclient = $utils::PATH_TO_SMBCLIENT;
 
 # Options checking
 
@@ -73,13 +67,12 @@
 my $share = $1 if ($opt_s =~ /^([-_.A-Za-z0-9]+\$?)$/);
 ($share) || usage("Invalid share: $opt_s\n");
 
-($opt_u) || ($opt_u = shift @ARGV) || ($opt_u = "guest");
-my $user = $1 if ($opt_u =~ /^([-_.A-Za-z0-9\\]+)$/);
-($user) || usage("Invalid user: $opt_u\n");
+defined($opt_u) || ($opt_u = shift @ARGV) || ($opt_u = "guest");
+my $user = $1 if ($opt_u =~ /^([-_.A-Za-z0-9\\]*)$/);
+defined($user) || usage("Invalid user: $opt_u\n");
 
-($opt_p) || ($opt_p = shift @ARGV) || ($opt_p = "");
+defined($opt_p) || ($opt_p = shift @ARGV) || ($opt_p = "");
 my $pass = $1 if ($opt_p =~ /(.*)/);
-$pass = "-N" if ($opt_p eq "");
 
 ($opt_w) || ($opt_w = shift @ARGV) || ($opt_w = 85);
 my $warn = $1 if ($opt_w =~ /^([0-9]{1,2}\%?|100\%?|[0-9]+[kMG])$/);
@@ -163,23 +156,19 @@
 
 # Execute an "ls" on the share using smbclient program
 # get the results into $res
-if (defined($workgroup)) {
-	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 {
-	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/;
-	}
-}
+my @cmd = (
+  $smbclient,
+  "//$host/$share",
+  "-U", "$user%$pass",
+  defined($workgroup) ? ("-W", $workgroup) : (),
+  defined($address) ? ("-I", $address) : (),
+  defined($opt_P) ? ("-p", $opt_P) : (),
+  "-c", "ls"
+);
+
+print join(" ", @cmd) . "\n" if ($verbose);
+$res = output_and_error_of(@cmd) or exit $ERRORS{"UNKNOWN"};
+
 #Turn off alarm
 alarm(0);
 
diff -urNad nagios-plugins-1.4.12~/plugins-scripts/utils.pm.in nagios-plugins-1.4.12/plugins-scripts/utils.pm.in
--- nagios-plugins-1.4.12~/plugins-scripts/utils.pm.in	2007-07-07 13:55:48.000000000 +0200
+++ nagios-plugins-1.4.12/plugins-scripts/utils.pm.in	2008-07-02 23:08:04.000000000 +0200
@@ -8,7 +8,8 @@
 
 require Exporter;
 @ISA = qw(Exporter);
-@EXPORT_OK = qw($TIMEOUT %ERRORS &print_revision &support &usage);
+@EXPORT_OK = qw($TIMEOUT %ERRORS &print_revision &support &usage
+		&output_of &output_and_error_of);
 
 #use strict;
 #use vars($TIMEOUT %ERRORS);
@@ -67,4 +68,29 @@
 	}
 }
 
+sub output_of {
+	local *CMD;
+	local $/ = undef;
+	if (open CMD, "-|", @_) {
+		return <CMD>;
+		close CMD;
+	}
+	return undef;
+}
+
+sub output_and_error_of {
+	local *CMD;
+	local $/ = undef;
+	my $pid = open CMD, "-|";
+	if (defined($pid)) {
+		if ($pid) {
+			return <CMD>;
+		} else {
+			open STDERR, ">&STDOUT" and exec @_;
+			exit(1);
+		}
+	}
+	return undef;
+}
+
 1;