4 # check_disk.pl <host> <share> <user> <pass> [warn] [critical] [port]
6 # Nagios host script to get the disk usage from a SMB share
8 # Changes and Modifications
9 # =========================
10 # 7-Aug-1999 - Michael Anthon
11 # Created from check_disk.pl script provided with netsaint_statd (basically
12 # cause I was too lazy (or is that smart?) to write it from scratch)
13 # 8-Aug-1999 - Michael Anthon
14 # Modified [warn] and [critical] parameters to accept format of nnn[M|G] to
15 # allow setting of limits in MBytes or GBytes. Percentage settings for large
16 # drives is a pain in the butt
17 # 2-May-2002 - SGhosh fix for embedded perl
25 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);
26 use vars qw($PROGNAME);
28 use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
33 $PROGNAME = "check_disk_smb";
39 Getopt::Long::Configure('bundling');
41 ("v" => \$verbose, "verbose" => \$verbose,
42 "P=s" => \$opt_P, "port=s" => \$opt_P,
43 "V" => \$opt_V, "version" => \$opt_V,
44 "h" => \$opt_h, "help" => \$opt_h,
45 "w=s" => \$opt_w, "warning=s" => \$opt_w,
46 "c=s" => \$opt_c, "critical=s" => \$opt_c,
47 "p=s" => \$opt_p, "password=s" => \$opt_p,
48 "u=s" => \$opt_u, "username=s" => \$opt_u,
49 "s=s" => \$opt_s, "share=s" => \$opt_s,
50 "W=s" => \$opt_W, "workgroup=s" => \$opt_W,
51 "H=s" => \$opt_H, "hostname=s" => \$opt_H,
52 "a=s" => \$opt_a, "address=s" => \$opt_a);
55 print_revision($PROGNAME,'@NP_VERSION@'); #'
59 if ($opt_h) {print_help(); exit $ERRORS{'OK'};}
61 my $smbclient = $utils::PATH_TO_SMBCLIENT;
65 ($opt_H) || ($opt_H = shift @ARGV) || usage("Host name not specified\n");
66 my $host = $1 if ($opt_H =~ /^([-_.A-Za-z0-9 ]+\$?)$/);
67 ($host) || usage("Invalid host: $opt_H\n");
69 ($opt_s) || ($opt_s = shift @ARGV) || usage("Share volume not specified\n");
70 my $share = $1 if ($opt_s =~ /^([-_.A-Za-z0-9]+\$?)$/);
71 ($share) || usage("Invalid share: $opt_s\n");
73 defined($opt_u) || ($opt_u = shift @ARGV) || ($opt_u = "guest");
74 my $user = $1 if ($opt_u =~ /^([-_.A-Za-z0-9\\]*)$/);
75 defined($user) || usage("Invalid user: $opt_u\n");
77 defined($opt_p) || ($opt_p = shift @ARGV) || ($opt_p = "");
78 my $pass = $1 if ($opt_p =~ /(.*)/);
80 ($opt_w) || ($opt_w = shift @ARGV) || ($opt_w = 85);
81 my $warn = $1 if ($opt_w =~ /^([0-9]{1,2}\%?|100\%?|[0-9]+[kMG])$/);
82 ($warn) || usage("Invalid warning threshold: $opt_w\n");
84 ($opt_c) || ($opt_c = shift @ARGV) || ($opt_c = 95);
85 my $crit = $1 if ($opt_c =~ /^([0-9]{1,2}\%?|100\%?|[0-9]+[kMG])$/);
86 ($crit) || usage("Invalid critical threshold: $opt_c\n");
88 # Execute the given command line and return anything it writes to STDOUT and/or
89 # STDERR. (This might be useful for other plugins, too, so it should possibly
90 # be moved to utils.pm.)
91 sub output_and_error_of {
94 my $pid = open CMD, "-|";
99 open STDERR, ">&STDOUT" and exec @_;
106 # split the type from the unit value
107 #Check $warn and $crit for type (%/M/G) and set up for tests
108 #P = Percent, K = KBytes
112 if ($opt_w =~ /^([0-9]+)\%?$/) {
115 } elsif ($opt_w =~ /^([0-9]+)k$/) {
118 } elsif ($opt_w =~ /^([0-9]+)M$/) {
121 } elsif ($opt_w =~ /^([0-9]+)G$/) {
123 $warn = $1 * 1048576;
125 if ($opt_c =~ /^([0-9]+)\%?$/) {
128 } elsif ($opt_c =~ /^([0-9]+)k$/) {
131 } elsif ($opt_c =~ /^([0-9]+)M$/) {
134 } elsif ($opt_c =~ /^([0-9]+)G$/) {
136 $crit = $1 * 1048576;
139 # check if both warning and critical are percentage or size
140 unless( ( $warn_type eq "P" && $crit_type eq "P" ) || ( $warn_type ne "P" && $crit_type ne "P" ) ){
141 $opt_w =~ s/\%/\%\%/g;
142 $opt_c =~ s/\%/\%\%/g;
143 usage("Both warning and critical should be same type- warning: $opt_w critical: $opt_c \n");
146 # verify warning is less than critical
147 if ( $warn_type eq "K") {
148 unless ( $warn > $crit) {
149 usage("Disk size: warning ($opt_w) should be greater than critical ($opt_c) \n");
152 unless ( $warn < $crit) {
153 $opt_w =~ s/\%/\%\%/g;
154 $opt_c =~ s/\%/\%\%/g;
155 usage("Percentage: warning ($opt_w) should be less than critical ($opt_c) \n");
159 my $workgroup = $1 if (defined($opt_W) && $opt_W =~ /(.*)/);
161 my $address = $1 if (defined($opt_a) && $opt_a =~ /(.*)/);
163 # end of options checking
171 # Just in case of problems, let's not hang Nagios
173 print "No Answer from Client\n";
174 exit $ERRORS{"UNKNOWN"};
178 # Execute an "ls" on the share using smbclient program
179 # get the results into $res
184 defined($workgroup) ? ("-W", $workgroup) : (),
185 defined($address) ? ("-I", $address) : (),
186 defined($opt_P) ? ("-p", $opt_P) : (),
190 print join(" ", @cmd) . "\n" if ($verbose);
191 $res = output_and_error_of(@cmd) or exit $ERRORS{"UNKNOWN"};
196 #Split $res into an array of lines
197 @lines = split /\n/, $res;
199 #Get the last line into $_
200 $_ = $lines[$#lines];
203 #Process the last line to get free space.
204 #If line does not match required regexp, return an UNKNOWN error
205 if (/\s*(\d*) blocks of size (\d*)\. (\d*) blocks available/) {
207 my ($avail) = ($3*$2)/1024;
208 my ($avail_bytes) = $avail;
209 my ($capper) = int(($3/$1)*100);
210 my ($mountpt) = "\\\\$host\\$share";
213 if (int($avail / 1024) > 0) {
214 $avail = int($avail / 1024);
215 if (int($avail /1024) > 0) {
216 $avail = (int(($avail / 1024)*100))/100;
217 $avail = $avail ."G";
219 $avail = $avail ."M";
222 $avail = $avail ."K";
225 #print ":$warn:$warn_type:\n";
226 #print ":$crit:$crit_type:\n";
227 #print ":$avail:$avail_bytes:$capper:$mountpt:\n";
229 if ((($warn_type eq "P") && (100 - $capper) < $warn) || (($warn_type eq "K") && ($avail_bytes > $warn))) {
230 $answer = "Disk ok - $avail ($capper%) free on $mountpt\n";
231 } elsif ((($crit_type eq "P") && (100 - $capper) < $crit) || (($crit_type eq "K") && ($avail_bytes > $crit))) {
233 $answer = "WARNING: Only $avail ($capper%) free on $mountpt\n";
236 $answer = "CRITICAL: Only $avail ($capper%) free on $mountpt\n";
239 $answer = "Result from smbclient not suitable\n";
242 if (/(Access denied|NT_STATUS_LOGON_FAILURE)/) {
243 $answer = "Access Denied\n";
247 if (/(Unknown host \w*|Connection.*failed)/) {
252 if (/(You specified an invalid share name|NT_STATUS_BAD_NETWORK_NAME)/) {
253 $answer = "Invalid share name \\\\$host\\$share\n";
262 print "$state\n" if ($verbose);
263 exit $ERRORS{$state};
266 print "Usage: $PROGNAME -H <host> -s <share> -u <user> -p <password>
267 -w <warn> -c <crit> [-W <workgroup>] [-P <port>] [-a <IP>]\n";
271 print_revision($PROGNAME,'@NP_VERSION@');
272 print "Copyright (c) 2000 Michael Anthon/Karl DeBisschop
274 Perl Check SMB Disk plugin for Nagios
280 NetBIOS name of the server
282 Share name to be tested
283 -W, --workgroup=STRING
284 Workgroup or Domain used (Defaults to \"WORKGROUP\")
286 IP-address of HOST (only necessary if HOST is in another network)
288 Username to log in to server. (Defaults to \"guest\")
289 -p, --password=STRING
290 Password to log in to server. (Defaults to an empty password)
291 -w, --warning=INTEGER or INTEGER[kMG]
292 Percent of used space at which a warning will be generated (Default: 85%)
294 -c, --critical=INTEGER or INTEGER[kMG]
295 Percent of used space at which a critical will be generated (Defaults: 95%)
297 Port to be used to connect to. Some Windows boxes use 139, others 445 (Defaults to smbclient default)
299 If thresholds are followed by either a k, M, or G then check to see if that
300 much disk space is available (kilobytes, Megabytes, Gigabytes)
302 Warning percentage should be less than critical
303 Warning (remaining) disk space should be greater than critical.