summaryrefslogtreecommitdiffstats
path: root/contrib
diff options
context:
space:
mode:
Diffstat (limited to 'contrib')
-rwxr-xr-xcontrib/check_sybase370
1 files changed, 0 insertions, 370 deletions
diff --git a/contrib/check_sybase b/contrib/check_sybase
deleted file mode 100755
index 74cf17a..0000000
--- a/contrib/check_sybase
+++ /dev/null
@@ -1,370 +0,0 @@
1#!/usr/bin/perl
2# check_sybase
3# A nagios plugin that connects to a Sybase database and checks free space.
4#
5# Copyright 2004-2005 Simon Bellwood, NetMan Network Management and IT
6# Services GmbH
7# Portions Copyright 2001 Michael Peppler.
8# License: GPL
9#
10# Bugs and feedback to simon.bellwood@NOSPAM.net-man.at
11# Latest version available from:
12# http://www.net-man.at/software/check_sybase-LATEST.zip
13#
14# Revision history:
15# 0.1 01-OCT-2004 Initial version.
16# 0.2 08-NOV-2004 Initial release.
17# 0.3 13-JAN-2005 Fixed lib path, improved timeouts.
18# 0.4 26-JAN-2005 Added loginTimeout.
19# 0.5 04-FEB-2005 Fixed dates in history. Oops.
20# 0.6 29-MAR-2005 Added --explain option.
21# 0.7 08-APR-2005 Added initial performance data support.
22my $VERSION = "0.7";
23
24#use warnings;
25use strict;
26use DBI;
27use Getopt::Long;
28use lib qw( /usr/lib/nagios/plugins/ /usr/local/nagios/libexec/ );
29use utils qw(%ERRORS &print_revision &support &usage $TIMEOUT);
30
31
32my $PROGNAME = "check_sybase";
33my $DEFAULT_CHECKTYPE = "FREESPACE";
34my $DEFAULT_WARNING = "25";
35my $DEFAULT_CRITICAL = "10";
36my $DEFAULT_TIMEOUT = "30";
37
38my ($user, $pass, $dbsvr, $dbname, $config, $checktype, $explain,
39 $warn, $crit, $timeout, $help, $version);
40
41my $options_okay = GetOptions(
42 "U|user=s" => \$user,
43 "P|pass:s" => \$pass, # ":" means optional
44 "S|dbsvr=s" => \$dbsvr,
45 "D|dbname=s" => \$dbname,
46 "config=s" => \$config,
47 "checktype=s" => \$checktype,
48 "explain" => \$explain,
49 "w|warning=i" => \$warn,
50 "c|critical=i" => \$crit,
51 "t|timeout=i" => \$timeout,
52 "h|help" => \$help,
53 "V|version" => \$version
54);
55
56
57if (! $options_okay) # Bad option passed
58{
59 &help;
60 &nunk("Bad command line option passed!");
61}
62
63# Use defaults, if needed
64$warn = $warn || $DEFAULT_WARNING;
65$crit = $crit || $DEFAULT_CRITICAL;
66$checktype = $checktype || $DEFAULT_CHECKTYPE;
67$timeout = $timeout || $TIMEOUT || $DEFAULT_TIMEOUT;
68
69if ($help)
70{
71 &help;
72 &nok;
73}
74
75if ($version)
76{
77 print_revision($PROGNAME,"\$Revision$VERSION \$");
78 &nok;
79}
80
81if ($config) # Read any of "user", "pass", "dbsvr", "dbname" from config file
82{
83 &read_config;
84}
85
86# Some more descriptive syntax checks
87my $syntax_error;
88$syntax_error .= "No dbsvr given! " unless $dbsvr;
89$syntax_error .= "No dbname given! " unless $dbname;
90$syntax_error .= "No user given! " unless $user;
91$syntax_error .= "Bad checktype given!"
92 unless $checktype =~ m/^CONNECT|FREESPACE$/;
93&nunk($syntax_error) if $syntax_error;
94
95
96# Just in case of problems, let's not hang Nagios
97$SIG{'ALRM'} = sub {
98 &nunk("Timeout: no response from dbsvr $dbsvr within $timeout seconds");
99};
100alarm($timeout);
101
102
103# Decide on what we are checking
104if ($checktype eq "CONNECT")
105{
106 &connect;
107}
108elsif ($checktype eq "FREESPACE")
109{
110 &check_space;
111}
112
113my $dbh;
114my $is_connected;
115sub connect
116{
117 $dbh = DBI->connect("dbi:Sybase:server=$dbsvr;database=$dbname;".
118 "timeout=$timeout,loginTimeout=$timeout", $user, $pass)
119 or &ncrit("Could not connect to '$dbname' on '$dbsvr'");
120
121 # Report success for a check of type CONNECT
122 &nok("Connect okay") if $checktype ne "FREESPACE";
123}
124
125sub disconnect
126{
127 $dbh->disconnect if $is_connected;
128 $is_connected = 0;
129}
130
131sub check_space
132{
133 &connect;
134
135 # Most of this sub based on Michael Peppler's check-space.pl
136 # For debugging purposes, more values are collected than needed.
137
138 $dbh->{syb_do_proc_status} = 1;
139
140 my $dbinfo;
141
142 # First check space in the database
143 my $sth = $dbh->prepare("sp_spaceused")
144 or &nunk("Failed to call sp_spaceused on '$dbsvr'");
145 $sth->execute
146 or &nunk("Failed to call sp_spaceused on '$dbsvr'");
147 do {
148 while(my $d = $sth->fetch)
149 {
150 if($d->[0] =~ /$dbname/)
151 {
152 # Grab "database_size"
153 $d->[1] =~ s/[^\d.]//g;
154 $dbinfo->{size} = $d->[1];
155 }
156 else
157 {
158 # Reserved, data, index, unused
159 foreach (@$d)
160 {
161 s/\D//g;
162 }
163
164 # Grab "reserved", "data", "index"
165 $dbinfo->{reserved} = $d->[0] / 1024;
166 $dbinfo->{data} = $d->[1] / 1024;
167 $dbinfo->{index} = $d->[2] / 1024;
168 $dbinfo->{unused} = $d->[3] / 1024;
169 }
170 }
171 } while($sth->{syb_more_results});
172
173 &explain("db size: ".$dbinfo->{size});
174 &explain("reserved: ".$dbinfo->{reserved});
175 &explain(" data: ".$dbinfo->{data});
176 &explain(" index: ".$dbinfo->{index});
177 &explain(" unused: ".$dbinfo->{unused});
178
179 # Get the actual device usage from sp_helpdb to get the free log space
180 $sth = $dbh->prepare("sp_helpdb $dbname")
181 or &nunk("Failed to call sp_helpdb $dbname on '$dbsvr'");
182 $sth->execute
183 or &nunk("Failed to call sp_helpdb $dbname on '$dbsvr'");
184 do {
185 while(my $d = $sth->fetch)
186 {
187 # Look for "usage" column with value "log only"
188 if($d->[2] && $d->[2] =~ /log only/)
189 {
190 # Grab "size", add it to our log size
191 $d->[1] =~ s/[^\d\.]//g;
192 $dbinfo->{log} += $d->[1];
193 }
194
195 # Look for "device fragments" column with "log only"
196 # followed by a number.
197 if($d->[0] =~ /log only .* (\d+)/)
198 {
199 $dbinfo->{logfree} = $1 / 1024;
200 }
201 }
202 } while($sth->{syb_more_results});
203
204 &explain("log: ".$dbinfo->{log});
205 &explain("logfree: ".$dbinfo->{logfree});
206
207 # Subtract the log size from the database size
208 $dbinfo->{realsize} = $dbinfo->{size} - $dbinfo->{log};
209 &explain("realsize (i.e. size - log) = ".$dbinfo->{realsize});
210
211 # The "reserved" space is free for use by the table that freed it, so
212 # it is not truly free space. To be safe, our calculation ignores it.
213 my $free = ($dbinfo->{realsize} - $dbinfo->{reserved})/$dbinfo->{realsize};
214 $free = sprintf("%.2f", $free*100);
215
216 &explain("(realsize-reserved)/realsize = $free%");
217 &explain("For safety, this calculation assumes no log space reuse. ".
218 "Because of this, you may get negative values.");
219
220
221 if ($free < $crit)
222 {
223 &ncrit("Free space is $free%! (critical threshold is $crit%)".
224 "|free_space=$free%");
225 }
226
227 if ($free < $warn)
228 {
229 &nwarn("Free space is $free%! (warning threshold is $warn%)".
230 "|free_space=$free%");
231 }
232
233
234 &nok("Free space within thresholds ($free% free)".
235 "|free_space=$free%");
236}
237
238sub read_config
239{
240 open (CONFIG, "<$config")
241 or &nunk("Failed to open config file '$config': $!");
242 while (<CONFIG>)
243 {
244 chomp;
245 next if m/^#/; # skip comments
246 next if m/^$/; # skip blanks
247
248 # Each case-insensitive argument can be followed by an optional
249 # colon, then must be followed by whitespace and the value.
250 # Options in the config file override those given on the
251 # command line, but don't rely on this!
252
253 if (m/USER:?\s+(\S+)/i)
254 {
255 $user = $1;
256 }
257 elsif (m/PASS:?\s+(\S+)/i)
258 {
259 $pass = $1;
260 }
261 elsif (m/DBSVR:?\s+(\S+)/i)
262 {
263 $dbsvr = $1;
264 }
265 elsif (m/DBNAME:?\s+(\S+)/i)
266 {
267 $dbname = $1;
268 }
269 else
270 {
271 &nunk("Invalid line $. in config file '$config'");
272 }
273 }
274 close (CONFIG);
275}
276
277sub help
278{
279 print <<_HELP_;
280Usage: $PROGNAME OPTIONS
281A nagios plugin that connects to a Sybase database and checks free space.
282
283Mandatory arguments to long options are mandatory for short options too.
284 -U, --user Username to connect to database.
285 -P, --pass Password to connect to database.
286 -S, --dbsvr Database server (as in the interfaces file).
287 -D, --dbname Database name to check.
288 --config=FILE Config file (see SECURITY below)
289 --checktype=TYPE Type of check to run (see TYPEs below)
290 --explain Explains how we calculated the free space.
291 -w, --warning Warning threshold, in percent (default 25)
292 -c, --critical Critical threshold, in percent (default 10)
293 -t, --timeout Timeout value, in seconds (default 30)
294 -h, --help This help message
295 -V, --version Version information ($VERSION)
296
297Examples:
298 $PROGNAME -U sa -P secret -S bigbox -D orders
299 $PROGNAME --config=/secure/nagios-sybase.cfg --checktype=CONNECT
300
301TYPEs
302 There are two types of checks you can run:
303 --checktype=CONNECT
304 Checks just the connection to the database.
305 --checktype=FREESPACE
306 (Default) Checks both the connection to the database and the free space.
307
308SECURITY - Using a config file
309 Since a "ps ax" will reveal your database username and password, you can
310 instead specify them in a config file. Pass the config file with --config.
311 The format of the file is:
312 USER value
313 PASS value
314 You can also specify a DBSVR and DBNAME in the file. Comments (#) and blank
315 lines are ignored. Use whitespace to separate argument and value.
316_HELP_
317
318}
319
320sub explain
321{
322 return unless $explain;
323
324 my $msg = shift;
325 print "$msg\n";
326}
327
328
329
330# Some wrappers..
331
332# Returns code 0, OK
333sub nok
334{
335 my $msg = shift;
336 print "OK: $msg\n" if $msg;
337
338 &disconnect;
339 exit $ERRORS{OK};
340}
341
342# Returns code 1, Warning
343sub nwarn
344{
345 my $msg = shift;
346 print "WARNING: $msg\n";
347
348 &disconnect;
349 exit $ERRORS{WARNING};
350}
351
352# Returns code 2, Critical
353sub ncrit
354{
355 my $msg = shift;
356 print "CRITICAL: $msg\n";
357
358 &disconnect;
359 exit $ERRORS{CRITICAL};
360}
361
362# Returns code 3, Unknown
363sub nunk
364{
365 my $msg = shift;
366 print "ERROR: $msg\n";
367
368 &disconnect;
369 exit $ERRORS{UNKNOWN};
370}