1 #! /usr/bin/perl -w
2 #
3 #
4 # check_disk.pl <host> <share> <user> <pass> [warn] [critical] [port]
5 #
6 # Nagios host script to get the disk usage from a SMB share
7 #
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
18 #
19 # $Id$
20 #
22 require 5.004;
23 use POSIX;
24 use strict;
25 use Getopt::Long;
26 use vars qw($opt_V $opt_h $opt_H $opt_s $opt_W $opt_u $opt_p $opt_w $opt_c $verbose);
27 use vars qw($PROGNAME);
28 use lib utils.pm ;
29 use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
31 sub print_help ();
32 sub print_usage ();
34 $PROGNAME = "check_disk_smb";
36 $ENV{'PATH'}='';
37 $ENV{'BASH_ENV'}='';
38 $ENV{'ENV'}='';
40 Getopt::Long::Configure('bundling');
41 GetOptions
42 ("v" => \$verbose, "verbose" => \$verbose,
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);
53 if ($opt_V) {
54 print_revision($PROGNAME,'$Revision$'); #'
55 exit $ERRORS{'OK'};
56 }
58 if ($opt_h) {print_help(); exit $ERRORS{'OK'};}
60 my $smbclient= "$utils::PATH_TO_SMBCLIENT " ;
61 my $smbclientoptions="";
64 ($opt_H) || ($opt_H = shift) || usage("Host name not specified\n");
65 my $host = $1 if ($opt_H =~ /([-_.A-Za-z0-9]+)/);
66 ($host) || usage("Invalid host: $opt_H\n");
68 ($opt_s) || ($opt_s = shift) || usage("Share volume not specified\n");
69 my $share = $1 if ($opt_s =~ /([-_.A-Za-z0-9]+)/);
70 ($share) || usage("Invalid share: $opt_s\n");
72 ($opt_u) || ($opt_u = shift) || ($opt_u = "guest");
73 my $user = $1 if ($opt_u =~ /([-_.A-Za-z0-9]+)/);
74 ($user) || usage("Invalid user: $opt_u\n");
76 ($opt_p) || ($opt_p = shift) || ($opt_p = "guest");
77 my $pass = $1 if ($opt_p =~ /(.*)/);
79 ($opt_w) || ($opt_w = shift) || ($opt_w = 85);
80 my $warn = $1 if ($opt_w =~ /([0-9]{1,2}\%?|100\%?|[0-9]+[kmKM])+/);
81 ($warn) || usage("Invalid warning threshold: $opt_w\n");
83 ($opt_c) || ($opt_c = shift) || ($opt_c = 95);
84 my $crit = $1 if ($opt_c =~ /([0-9]{1,2}\%?|100\%?|[0-9]+[kmKM])/);
85 ($crit) || usage("Invalid critical threshold: $opt_c\n");
87 my $workgroup = $1 if (defined($opt_W) && $opt_W =~ /(.*)/);
89 my $state = "OK";
90 my $answer = undef;
91 my $res = undef;
92 my @lines = undef;
94 # Just in case of problems, let's not hang Nagios
95 $SIG{'ALRM'} = sub {
96 print "No Answer from Client\n";
97 exit $ERRORS{"UNKNOWN"};
98 };
99 alarm($TIMEOUT);
101 # Execute an "ls" on the share using smbclient program
102 # get the results into $res
103 if (defined($workgroup)) {
104 $res = qx/$smbclient \/\/$host\/$share $pass -W $workgroup -U $user $smbclientoptions -c ls/;
105 } else {
106 print "$smbclient " . "\/\/$host\/$share" ." $pass -U $user $smbclientoptions -c ls\n" if ($verbose);
107 $res = qx/$smbclient \/\/$host\/$share $pass -U $user $smbclientoptions -c ls/;
108 }
109 #Turn off alarm
110 alarm(0);
112 #Split $res into an array of lines
113 @lines = split /\n/, $res;
115 #Get the last line into $_
116 $_ = $lines[$#lines];
117 #print "$_\n";
119 #Process the last line to get free space.
120 #If line does not match required regexp, return an UNKNOWN error
121 if (/\s*(\d*) blocks of size (\d*)\. (\d*) blocks available/) {
123 my ($avail) = ($3*$2)/1024;
124 my ($avail_bytes) = $avail;
125 my ($capper) = int(($3/$1)*100);
126 my ($mountpt) = "\\\\$host\\$share";
128 #Check $warn and $crit for type (%/M/G) and set up for tests
129 #P = Percent, K = KBytes
130 my $warn_type;
131 my $crit_type;
132 if ($warn =~ /^([0-9]+$)/) {
133 $warn_type = "P";
134 } elsif ($warn =~ /^([0-9]+)k$/) {
135 my ($warn_type) = "K";
136 $warn = $1;
137 } elsif ($warn =~ /^([0-9]+)M$/) {
138 $warn_type = "K";
139 $warn = $1 * 1024;
140 } elsif ($warn =~ /^([0-9]+)G$/) {
141 $warn_type = "K";
142 $warn = $1 * 1048576;
143 }
144 if ($crit =~ /^([0-9]+$)/) {
145 $crit_type = "P";
146 } elsif ($crit =~ /^([0-9]+)k$/) {
147 $crit_type = "K";
148 $crit = $1;
149 } elsif ($crit =~ /^([0-9]+)M$/) {
150 $crit_type = "K";
151 $crit = $1 * 1024;
152 } elsif ($crit =~ /^([0-9]+)G$/) {
153 $crit_type = "K";
154 $crit = $1 * 1048576;
155 }
157 if (int($avail / 1024) > 0) {
158 $avail = int($avail / 1024);
159 if (int($avail /1024) > 0) {
160 $avail = (int(($avail / 1024)*100))/100;
161 $avail = $avail."G";
162 } else {
163 $avail = $avail."M";
164 }
165 } else {
166 $avail = $avail."K";
167 }
169 #print ":$warn:$warn_type:\n";
170 #print ":$crit:$crit_type:\n";
171 #print ":$avail:$avail_bytes:$capper:$mountpt:\n";
172 if ((($warn_type eq "P") && (100 - $capper) < $warn) || (($warn_type eq "K") && ($avail_bytes > $warn))) {
173 $answer = "Disk ok - $avail ($capper%) free on $mountpt\n";
174 } elsif ((($crit_type eq "P") && (100 - $capper) < $crit) || (($crit_type eq "K") && ($avail_bytes > $crit))) {
175 $state = "WARNING";
176 $answer = "Only $avail ($capper%) free on $mountpt\n";
177 } else {
178 $state = "CRITICAL";
179 $answer = "Only $avail ($capper%) free on $mountpt\n";
180 }
181 } else {
182 $answer = "Result from smbclient not suitable\n";
183 $state = "UNKNOWN";
184 foreach (@lines) {
185 if (/Access denied/) {
186 $answer = "Access Denied\n";
187 $state = "CRITICAL";
188 last;
189 }
190 if (/(Unknown host \w*)/) {
191 $answer = "$1\n";
192 $state = "CRITICAL";
193 last;
194 }
195 if (/(You specified an invalid share name)/) {
196 $answer = "Invalid share name \\\\$host\\$share\n";
197 $state = "CRITICAL";
198 last;
199 }
200 }
201 }
204 print $answer;
205 print "$state\n" if ($verbose);
206 exit $ERRORS{$state};
208 sub print_usage () {
209 print "Usage: $PROGNAME -H <host> -s <share> -u <user> -p <password>
210 -w <warn> -c <crit> [-W <workgroup>]\n";
211 }
213 sub print_help () {
214 print_revision($PROGNAME,'$Revision$');
215 print "Copyright (c) 2000 Michael Anthon/Karl DeBisschop
217 Perl Check SMB Disk plugin for Nagios
219 ";
220 print_usage();
221 print "
222 -H, --hostname=HOST
223 NetBIOS name of the server
224 -s, --share=STRING
225 Share name to be tested
226 -W, --workgroup=STRING
227 Workgroup or Domain used (Defaults to \"WORKGROUP\")
228 -u, --user=STRING
229 Username to log in to server. (Defaults to \"guest\")
230 -p, --password=STRING
231 Password to log in to server. (Defaults to \"guest\")
232 -w, --warning=INTEGER
233 Percent of used space at which a warning will be generated (Default: 85%)
235 -c, --critical=INTEGER
236 Percent of used space at which a critical will be generated (Defaults: 95%)
239 ";
240 support();
241 }