summaryrefslogtreecommitdiffstats
path: root/plugins-scripts/check_disk_smb.pl
diff options
context:
space:
mode:
authorEthan Galstad <egalstad@users.sourceforge.net>2002-02-28 06:42:51 (GMT)
committerEthan Galstad <egalstad@users.sourceforge.net>2002-02-28 06:42:51 (GMT)
commit44a321cb8a42d6c0ea2d96a1086a17f2134c89cc (patch)
treea1a4d9f7b92412a17ab08f34f04eec45433048b7 /plugins-scripts/check_disk_smb.pl
parent54fd5d7022ff2d6a59bc52b8869182f3fc77a058 (diff)
downloadmonitoring-plugins-44a321cb8a42d6c0ea2d96a1086a17f2134c89cc.tar.gz
Initial revision
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@2 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins-scripts/check_disk_smb.pl')
-rwxr-xr-xplugins-scripts/check_disk_smb.pl240
1 files changed, 240 insertions, 0 deletions
diff --git a/plugins-scripts/check_disk_smb.pl b/plugins-scripts/check_disk_smb.pl
new file mode 100755
index 0000000..d1b0b3d
--- /dev/null
+++ b/plugins-scripts/check_disk_smb.pl
@@ -0,0 +1,240 @@
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
18BEGIN {
19 if ($0 =~ m/^(.*?)[\/\\]([^\/\\]+)$/) {
20 $runtimedir = $1;
21 $PROGNAME = $2;
22 }
23}
24
25require 5.004;
26use POSIX;
27use strict;
28use Getopt::Long;
29use vars qw($opt_V $opt_h $opt_H $opt_s $opt_W $opt_u $opt_p $opt_w $opt_c $verbose);
30use vars qw($PROGNAME);
31use lib $main::runtimedir;
32use utils qw($TIMEOUT %ERRORS &print_revision &support &usage);
33
34sub print_help ();
35sub print_usage ();
36
37$ENV{'PATH'}='';
38$ENV{'BASH_ENV'}='';
39$ENV{'ENV'}='';
40
41Getopt::Long::Configure('bundling');
42GetOptions
43 ("v" => \$verbose, "verbose" => \$verbose,
44 "V" => \$opt_V, "version" => \$opt_V,
45 "h" => \$opt_h, "help" => \$opt_h,
46 "w=s" => \$opt_w, "warning=s" => \$opt_w,
47 "c=s" => \$opt_c, "critical=s" => \$opt_c,
48 "p=s" => \$opt_p, "password=s" => \$opt_p,
49 "u=s" => \$opt_u, "username=s" => \$opt_u,
50 "s=s" => \$opt_s, "share=s" => \$opt_s,
51 "W=s" => \$opt_W, "workgroup=s" => \$opt_W,
52 "H=s" => \$opt_H, "hostname=s" => \$opt_H);
53
54if ($opt_V) {
55 print_revision($PROGNAME,'$Revision$'); #'
56 exit $ERRORS{'OK'};
57}
58
59if ($opt_h) {print_help(); exit $ERRORS{'OK'};}
60
61my $smbclient="/usr/bin/smbclient";
62my $smbclientoptions="";
63
64($opt_H) || ($opt_H = shift) || usage("Host name not specified\n");
65my $host = $1 if ($opt_H =~ /([-_.A-Za-z0-9]+)/);
66($host) || usage("Invalid host: $opt_H\n");
67
68($opt_s) || ($opt_s = shift) || usage("Share volume not specified\n");
69my $share = $1 if ($opt_s =~ /([-_.A-Za-z0-9]+)/);
70($share) || usage("Invalid share: $opt_s\n");
71
72($opt_u) || ($opt_u = shift) || ($opt_u = "guest");
73my $user = $1 if ($opt_u =~ /([-_.A-Za-z0-9]+)/);
74($user) || usage("Invalid user: $opt_u\n");
75
76($opt_p) || ($opt_p = shift) || ($opt_p = "guest");
77my $pass = $1 if ($opt_p =~ /(.*)/);
78
79($opt_w) || ($opt_w = shift) || ($opt_w = 85);
80my $warn = $1 if ($opt_w =~ /([0-9]{1,2}\%?|100\%?|[0-9]+[kmKM])+/);
81($warn) || usage("Invalid warning threshold: $opt_w\n");
82
83($opt_c) || ($opt_c = shift) || ($opt_c = 95);
84my $crit = $1 if ($opt_c =~ /([0-9]{1,2}\%?|100\%?|[0-9]+[kmKM])/);
85($crit) || usage("Invalid critical threshold: $opt_c\n");
86
87my $workgroup = $1 if (defined($opt_W) && $opt_W =~ /(.*)/);
88
89my $state = "OK";
90my $answer = undef;
91my $res = undef;
92my @lines = undef;
93
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};
99alarm($TIMEOUT);
100
101# Execute an "ls" on the share using smbclient program
102# get the results into $res
103if (defined($workgroup)) {
104 $res = qx/$smbclient \/\/$host\/$share $pass -W $workgroup -U $user $smbclientoptions -c ls/;
105} else {
106 $res = qx/$smbclient \/\/$host\/$share $pass -U $user $smbclientoptions -c ls/;
107}
108#Turn off alarm
109alarm(0);
110
111#Split $res into an array of lines
112@lines = split /\n/, $res;
113
114#Get the last line into $_
115$_ = $lines[$#lines];
116#print "$_\n";
117
118#Process the last line to get free space.
119#If line does not match required regexp, return an UNKNOWN error
120if (/\s*(\d*) blocks of size (\d*)\. (\d*) blocks available/) {
121
122 my ($avail) = ($3*$2)/1024;
123 my ($avail_bytes) = $avail;
124 my ($capper) = int(($3/$1)*100);
125 my ($mountpt) = "\\\\$host\\$share";
126
127 #Check $warn and $crit for type (%/M/G) and set up for tests
128 #P = Percent, K = KBytes
129 my $warn_type;
130 my $crit_type;
131 if ($warn =~ /^([0-9]+$)/) {
132 $warn_type = "P";
133 } elsif ($warn =~ /^([0-9]+)k$/) {
134 my ($warn_type) = "K";
135 $warn = $1;
136 } elsif ($warn =~ /^([0-9]+)M$/) {
137 $warn_type = "K";
138 $warn = $1 * 1024;
139 } elsif ($warn =~ /^([0-9]+)G$/) {
140 $warn_type = "K";
141 $warn = $1 * 1048576;
142 }
143 if ($crit =~ /^([0-9]+$)/) {
144 $crit_type = "P";
145 } elsif ($crit =~ /^([0-9]+)k$/) {
146 $crit_type = "K";
147 $crit = $1;
148 } elsif ($crit =~ /^([0-9]+)M$/) {
149 $crit_type = "K";
150 $crit = $1 * 1024;
151 } elsif ($crit =~ /^([0-9]+)G$/) {
152 $crit_type = "K";
153 $crit = $1 * 1048576;
154 }
155
156 if (int($avail / 1024) > 0) {
157 $avail = int($avail / 1024);
158 if (int($avail /1024) > 0) {
159 $avail = (int(($avail / 1024)*100))/100;
160 $avail = $avail."G";
161 } else {
162 $avail = $avail."M";
163 }
164 } else {
165 $avail = $avail."K";
166 }
167
168#print ":$warn:$warn_type:\n";
169#print ":$crit:$crit_type:\n";
170#print ":$avail:$avail_bytes:$capper:$mountpt:\n";
171 if ((($warn_type eq "P") && (100 - $capper) < $warn) || (($warn_type eq "K") && ($avail_bytes > $warn))) {
172 $answer = "Disk ok - $avail ($capper%) free on $mountpt\n";
173 } elsif ((($crit_type eq "P") && (100 - $capper) < $crit) || (($crit_type eq "K") && ($avail_bytes > $crit))) {
174 $state = "WARNING";
175 $answer = "Only $avail ($capper%) free on $mountpt\n";
176 } else {
177 $state = "CRITICAL";
178 $answer = "Only $avail ($capper%) free on $mountpt\n";
179 }
180} else {
181 $answer = "Result from smbclient not suitable\n";
182 $state = "UNKNOWN";
183 foreach (@lines) {
184 if (/Access denied/) {
185 $answer = "Access Denied\n";
186 $state = "CRITICAL";
187 last;
188 }
189 if (/(Unknown host \w*)/) {
190 $answer = "$1\n";
191 $state = "CRITICAL";
192 last;
193 }
194 if (/(You specified an invalid share name)/) {
195 $answer = "Invalid share name \\\\$host\\$share\n";
196 $state = "CRITICAL";
197 last;
198 }
199 }
200}
201
202
203print $answer;
204print "$state\n" if ($verbose);
205exit $ERRORS{$state};
206
207sub print_usage () {
208 print "Usage: $PROGNAME -H <host> -s <share> -u <user> -p <password>
209 -w <warn> -c <crit> [-W <workgroup>]\n";
210}
211
212sub print_help () {
213 print_revision($PROGNAME,'$Revision$');
214 print "Copyright (c) 2000 Michael Anthon/Karl DeBisschop
215
216Perl Check SMB Disk plugin for Nagios
217
218";
219 print_usage();
220 print "
221-H, --hostname=HOST
222 NetBIOS name of the server
223-s, --share=STRING
224 Share name to be tested
225-W, --workgroup=STRING
226 Workgroup or Domain used (Defaults to \"WORKGROUP\")
227-u, --user=STRING
228 Username to log in to server. (Defaults to \"guest\")
229-p, --password=STRING
230 Password to log in to server. (Defaults to \"guest\")
231-w, --warning=INTEGER
232 Percent of used space at which a warning will be generated (Default: 85%)
233
234-c, --critical=INTEGER
235 Percent of used space at which a critical will be generated (Defaults: 95%)
236
237
238";
239 support();
240}