1 #! /usr/bin/perl -wT
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 FindBin;
29 use lib "$FindBin::Bin";
30 use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
32 sub print_help ();
33 sub print_usage ();
35 $ENV{'PATH'}='';
36 $ENV{'BASH_ENV'}='';
37 $ENV{'ENV'}='';
39 Getopt::Long::Configure('bundling');
40 GetOptions
41 ("v" => \$verbose, "verbose" => \$verbose,
42 "V" => \$opt_V, "version" => \$opt_V,
43 "h" => \$opt_h, "help" => \$opt_h,
44 "w=s" => \$opt_w, "warning=s" => \$opt_w,
45 "c=s" => \$opt_c, "critical=s" => \$opt_c,
46 "p=s" => \$opt_p, "password=s" => \$opt_p,
47 "u=s" => \$opt_u, "username=s" => \$opt_u,
48 "s=s" => \$opt_s, "share=s" => \$opt_s,
49 "W=s" => \$opt_W, "workgroup=s" => \$opt_W,
50 "H=s" => \$opt_H, "hostname=s" => \$opt_H);
52 if ($opt_V) {
53 print_revision($PROGNAME,'$Revision$'); #'
54 exit $ERRORS{'OK'};
55 }
57 if ($opt_h) {print_help(); exit $ERRORS{'OK'};}
59 my $smbclient="/usr/bin/smbclient";
60 my $smbclientoptions="";
62 ($opt_H) || ($opt_H = shift) || usage("Host name not specified\n");
63 my $host = $1 if ($opt_H =~ /([-_.A-Za-z0-9]+)/);
64 ($host) || usage("Invalid host: $opt_H\n");
66 ($opt_s) || ($opt_s = shift) || usage("Share volume not specified\n");
67 my $share = $1 if ($opt_s =~ /([-_.A-Za-z0-9]+)/);
68 ($share) || usage("Invalid share: $opt_s\n");
70 ($opt_u) || ($opt_u = shift) || ($opt_u = "guest");
71 my $user = $1 if ($opt_u =~ /([-_.A-Za-z0-9]+)/);
72 ($user) || usage("Invalid user: $opt_u\n");
74 ($opt_p) || ($opt_p = shift) || ($opt_p = "guest");
75 my $pass = $1 if ($opt_p =~ /(.*)/);
77 ($opt_w) || ($opt_w = shift) || ($opt_w = 85);
78 my $warn = $1 if ($opt_w =~ /([0-9]{1,2}\%?|100\%?|[0-9]+[kmKM])+/);
79 ($warn) || usage("Invalid warning threshold: $opt_w\n");
81 ($opt_c) || ($opt_c = shift) || ($opt_c = 95);
82 my $crit = $1 if ($opt_c =~ /([0-9]{1,2}\%?|100\%?|[0-9]+[kmKM])/);
83 ($crit) || usage("Invalid critical threshold: $opt_c\n");
85 my $workgroup = $1 if (defined($opt_W) && $opt_W =~ /(.*)/);
87 my $state = "OK";
88 my $answer = undef;
89 my $res = undef;
90 my @lines = undef;
92 # Just in case of problems, let's not hang Nagios
93 $SIG{'ALRM'} = sub {
94 print "No Answer from Client\n";
95 exit $ERRORS{"UNKNOWN"};
96 };
97 alarm($TIMEOUT);
99 # Execute an "ls" on the share using smbclient program
100 # get the results into $res
101 if (defined($workgroup)) {
102 $res = qx/$smbclient \/\/$host\/$share $pass -W $workgroup -U $user $smbclientoptions -c ls/;
103 } else {
104 $res = qx/$smbclient \/\/$host\/$share $pass -U $user $smbclientoptions -c ls/;
105 }
106 #Turn off alarm
107 alarm(0);
109 #Split $res into an array of lines
110 @lines = split /\n/, $res;
112 #Get the last line into $_
113 $_ = $lines[$#lines];
114 #print "$_\n";
116 #Process the last line to get free space.
117 #If line does not match required regexp, return an UNKNOWN error
118 if (/\s*(\d*) blocks of size (\d*)\. (\d*) blocks available/) {
120 my ($avail) = ($3*$2)/1024;
121 my ($avail_bytes) = $avail;
122 my ($capper) = int(($3/$1)*100);
123 my ($mountpt) = "\\\\$host\\$share";
125 #Check $warn and $crit for type (%/M/G) and set up for tests
126 #P = Percent, K = KBytes
127 my $warn_type;
128 my $crit_type;
129 if ($warn =~ /^([0-9]+$)/) {
130 $warn_type = "P";
131 } elsif ($warn =~ /^([0-9]+)k$/) {
132 my ($warn_type) = "K";
133 $warn = $1;
134 } elsif ($warn =~ /^([0-9]+)M$/) {
135 $warn_type = "K";
136 $warn = $1 * 1024;
137 } elsif ($warn =~ /^([0-9]+)G$/) {
138 $warn_type = "K";
139 $warn = $1 * 1048576;
140 }
141 if ($crit =~ /^([0-9]+$)/) {
142 $crit_type = "P";
143 } elsif ($crit =~ /^([0-9]+)k$/) {
144 $crit_type = "K";
145 $crit = $1;
146 } elsif ($crit =~ /^([0-9]+)M$/) {
147 $crit_type = "K";
148 $crit = $1 * 1024;
149 } elsif ($crit =~ /^([0-9]+)G$/) {
150 $crit_type = "K";
151 $crit = $1 * 1048576;
152 }
154 if (int($avail / 1024) > 0) {
155 $avail = int($avail / 1024);
156 if (int($avail /1024) > 0) {
157 $avail = (int(($avail / 1024)*100))/100;
158 $avail = $avail."G";
159 } else {
160 $avail = $avail."M";
161 }
162 } else {
163 $avail = $avail."K";
164 }
166 #print ":$warn:$warn_type:\n";
167 #print ":$crit:$crit_type:\n";
168 #print ":$avail:$avail_bytes:$capper:$mountpt:\n";
169 if ((($warn_type eq "P") && (100 - $capper) < $warn) || (($warn_type eq "K") && ($avail_bytes > $warn))) {
170 $answer = "Disk ok - $avail ($capper%) free on $mountpt\n";
171 } elsif ((($crit_type eq "P") && (100 - $capper) < $crit) || (($crit_type eq "K") && ($avail_bytes > $crit))) {
172 $state = "WARNING";
173 $answer = "Only $avail ($capper%) free on $mountpt\n";
174 } else {
175 $state = "CRITICAL";
176 $answer = "Only $avail ($capper%) free on $mountpt\n";
177 }
178 } else {
179 $answer = "Result from smbclient not suitable\n";
180 $state = "UNKNOWN";
181 foreach (@lines) {
182 if (/Access denied/) {
183 $answer = "Access Denied\n";
184 $state = "CRITICAL";
185 last;
186 }
187 if (/(Unknown host \w*)/) {
188 $answer = "$1\n";
189 $state = "CRITICAL";
190 last;
191 }
192 if (/(You specified an invalid share name)/) {
193 $answer = "Invalid share name \\\\$host\\$share\n";
194 $state = "CRITICAL";
195 last;
196 }
197 }
198 }
201 print $answer;
202 print "$state\n" if ($verbose);
203 exit $ERRORS{$state};
205 sub print_usage () {
206 print "Usage: $PROGNAME -H <host> -s <share> -u <user> -p <password>
207 -w <warn> -c <crit> [-W <workgroup>]\n";
208 }
210 sub print_help () {
211 print_revision($PROGNAME,'$Revision$');
212 print "Copyright (c) 2000 Michael Anthon/Karl DeBisschop
214 Perl Check SMB Disk plugin for Nagios
216 ";
217 print_usage();
218 print "
219 -H, --hostname=HOST
220 NetBIOS name of the server
221 -s, --share=STRING
222 Share name to be tested
223 -W, --workgroup=STRING
224 Workgroup or Domain used (Defaults to \"WORKGROUP\")
225 -u, --user=STRING
226 Username to log in to server. (Defaults to \"guest\")
227 -p, --password=STRING
228 Password to log in to server. (Defaults to \"guest\")
229 -w, --warning=INTEGER
230 Percent of used space at which a warning will be generated (Default: 85%)
232 -c, --critical=INTEGER
233 Percent of used space at which a critical will be generated (Defaults: 95%)
236 ";
237 support();
238 }