-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_sql
executable file
·269 lines (208 loc) · 8.7 KB
/
check_sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/perl
#
# check_sql a nagios check plugin for monitoring databases
#
# Copyright (C) 2007 Jesse Morgan
# BASED ON: check_oracle_generic, Copyright 2006 David Ligeret (david.ligeret at gmail.com)
#
# check_sql
# This file is part of morgnagplug
#
# check_sql is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# check_sql is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
use strict;
use Data::Dumper;
use Nagios::Plugin;
use DBI;
my $np = Nagios::Plugin->new(
usage => "Usage: %s --driver <driver> -q|f <value> [options]",
version => '2.0',
blurb => 'a check for sql',
url => 'https://morgnagplug.svn.sourceforge.net/',
license => 'GPL 2',
timeout => 15, # default value explictly defined
);
$np->add_arg(
'spec' => "driver|D=s",
'help' => "Type of DBI driver (case sensitive)",
'required' => 1,
);
$np->add_arg(
'spec' => "user|u=s",
'help' => "Username for the database",
);
$np->add_arg(
'spec' => "password|p=s",
'help' => "Password for the database",
);
$np->add_arg(
'spec' => "sid|s=s",
'help' => "Oracle SID/DB Name (required for oracle driver)",
);
$np->add_arg(
'spec' => "dsn=s",
'help' => "DSN (required for ODBC driver)",
);
$np->add_arg(
'spec' => "host|H=s",
'help' => "Host server",
);
$np->add_arg(
'spec' => "dbname=s",
'help' => "Name of the target database",
);
$np->add_arg(
'spec' => "query|q=s",
'help' => "SQL Query (should return a number)",
);
$np->add_arg(
'spec' => "file|f=s",
'help' => "file containt SQL (should return a number)",
);
$np->add_arg(
'spec' => "units|U=s",
'help' => "Units of returned value",
);
$np->add_arg(
'spec' => "port|P=s",
'help' => "port to connect to",
);
$np->add_arg(
'spec' => "warning|w=s",
'help' => "value needed to Warn (default is under)",
);
$np->add_arg(
'spec' => "critical|c=s",
'help' => "value needed to Crit (default is under)",
);
$np->add_arg(
'spec' => "type|t=s",
'help' => "type of threshold (return value is over|under|range|equal WARN or CRIT; default is 'under')
if VALUE is over 3, WARN;
if VALUE is under 3, WARN;
if VALUE is equal 3, WARN;
if VALUE is unequal 3, WARN;
if VALUE is inside (15,20), WARN;
if VALUE is outside (4,30), WARN;",
'default' => 'under',
);
$np->getopts;
alarm $np->opts->timeout;
###########################
# parameter sanity checks #
###########################
$np->nagios_exit(UNKNOWN, "No SID: SID is required for Oracle!") if ($np->opts->driver eq 'Oracle' and $np->opts->sid eq '');
$np->nagios_exit(UNKNOWN, "No DSN: DSN is required for ODBC!") if ($np->opts->driver eq 'ODBC' and $np->opts->dsn eq '');
# I'm not going to add sanity checking for types with crit and warns, please replace with common sense.
#####################################
# database connection sanity checks #
#####################################
my %attr = (PrintError => 0);
my $querystring;
my $dbstring="dbi:".$np->opts->driver.":";
# The following are appended to the dbstring in a reasonable order (if they exist)
$dbstring.='database='.$np->opts->dbname.";" if ($np->opts->dbname);
$dbstring.='host='.$np->opts->host.";" if ($np->opts->host);
$dbstring.='sid='.$np->opts->sid.";" if ($np->opts->sid);
$dbstring.='port='.$np->opts->port.";" if ($np->opts->port);
$dbstring.=$np->opts->dsn if ($np->opts->dsn);
print "$dbstring\n" if ($np->opts->verbose > 0);
#Connect to the Database
my $dbh = DBI->connect( $dbstring, $np->opts->user,$np->opts->password, \%attr) or $np->nagios_exit(UNKNOWN, "DB String: $DBI::errstr");
#######################
# Get the querystring #
#######################
# Prep the query, either from command line or from file. Fail if unable to locate.
if (defined $np->opts->query ){
$querystring=$np->opts->query;
}elsif (defined $np->opts->file and -e $np->opts->file ){
open QFILE, $np->opts->file;
$querystring=join ' ', <QFILE>;
close QFILE;
}else{
$np->nagios_exit(UNKNOWN, "No query or file provided, or file was not found!");
}
#########################################
# Execute the query and capture results #
#########################################
# Prepare and execute query
my $statement = $dbh->prepare($querystring ) or $np->nagios_exit(UNKNOWN, "Prepare error: $DBI::errstr");
$statement->execute()or $np->nagios_exit(UNKNOWN, "Execution error: $DBI::errstr");
# retrieve result
my $value = $statement->fetchrow_array();
$statement->finish();
####################
## Process Results #
####################
$np->add_perfdata( label => "Results", 'value' =>$value , warning => $np->opts->warning, critical => $np->opts->critical );
if ($np->opts->type eq 'under' ){
if ($np->opts->critical ne '' and $value <= $np->opts->critical ){
$np->nagios_exit(CRITICAL, "result: ($value) less than or equal to ".$np->opts->critical );
}elsif ($np->opts->warning ne '' and $value <= $np->opts->warning){
$np->nagios_exit(WARNING, "result: ($value) less than or equal to ".$np->opts->warning );
}else{
$np->nagios_exit(OK, "Success: $value ".$np->opts->units);
}
}elsif ($np->opts->type eq 'over' ){
if ($np->opts->critical ne '' and $value >= $np->opts->critical ){
$np->nagios_exit(CRITICAL, "result: ($value) greater than or equal to ".$np->opts->critical );
}elsif ($np->opts->warning ne '' and $value >= $np->opts->warning){
$np->nagios_exit(WARNING, "result: ($value) greater than or equal to ".$np->opts->warning );
}else{
$np->nagios_exit(OK, "Success: $value ".$np->opts->units);
}
}elsif ($np->opts->type eq 'equal' ){
if ($np->opts->critical ne '' and $value eq $np->opts->critical ){
$np->nagios_exit(CRITICAL, "result: ($value) equal to ".$np->opts->critical );
}elsif ($np->opts->warning ne '' and $value eq $np->opts->warning){
$np->nagios_exit(WARNING, "result: ($value) equal to ".$np->opts->warning );
}else{
$np->nagios_exit(OK, "Success: $value ".$np->opts->units);
}
}elsif ($np->opts->type eq 'unequal' ){
# usage: warn/crit if returned value is unequal to crit value i.e
# "value should always be X, if not, alert!"
if ($np->opts->critical ne '' and $value ne $np->opts->critical ){
$np->nagios_exit(CRITICAL, "result: ($value) unequal to ".$np->opts->critical );
}elsif ($np->opts->warning ne '' and $value ne $np->opts->warning){
$np->nagios_exit(WARNING, "result: ($value) unequal to ".$np->opts->warning );
}else{
$np->nagios_exit(OK, "Success: $value ".$np->opts->units);
}
}elsif ($np->opts->type eq 'outside' ){
my ($warnlow,$warnhigh)=split ",",$np->opts->warning;
my ($critlow,$crithigh)=split ",",$np->opts->critical;
if ($np->opts->critical ne '' and ($value <= $critlow or $value >=$crithigh) ){
$np->nagios_exit(CRITICAL, "result ($value) outside of range" );
}elsif ($np->opts->warning and ($value <= $warnlow or $value >=$warnhigh) ){
$np->nagios_exit(WARNING, "result ($value) outside of range" );
}else{
$np->nagios_exit(OK, "result: $value ".$np->opts->units );
}
#Not sure why anyone would care if something fell WITHIN range, but included for completeness
}elsif ($np->opts->type eq 'inside' ){
my ($warnlow,$warnhigh)=split ",",$np->opts->warning;
my ($critlow,$crithigh)=split ",",$np->opts->critical;
if ($np->opts->critical ne '' and ($value >= $critlow and $value <=$crithigh) ){
$np->nagios_exit(CRITICAL, "result ($value) inside of range" );
}elsif ($np->opts->warning and ($value >= $warnlow and $value <=$warnhigh) ){
$np->nagios_exit(WARNING, "result ($value) inside of range" );
}else{
$np->nagios_exit(OK, "result: $value ".$np->opts->units );
}
}else{
##not sure what, if anything, should go here.
$np->nagios_exit(UNKNOWN, "Query Type Error: no idea what '".$np->opts->type."' is...");
}
## If you get this, please report how you did it.
$np->nagios_exit(UNKNOWN, "WTF Error: I have no idea how you got here.");