forked from rbramley/Opsview-solr-checks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_solr
executable file
·297 lines (246 loc) · 8.15 KB
/
check_solr
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Std;
use LWP::UserAgent;
use XML::XPath;
#
# Check Solr status/statistics
# e.g. check_solr -H localhost
#
# (c) 2011 Robin Bramley, Ixxus Ltd.
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved. This file is offered as-is,
# without any warranty.
#
# About the check
my $script = "check_solr";
my $script_version = "0.1";
my $script_description = "Extracts information from Apache Solr and performs checks on the results";
# Nagios return values
# OK = 0
# WARNING = 1
# CRITICAL = 2
# UNKNOWN = 3
my $retStr = "plugin error";
my $perfData = "";
my @alertStrs = ("OK", "WARNING", "CRITICAL", "UNKNOWN");
my $exitCode = 3;
# variables
my %opts;
getopts('shH:p:u:o:w:c:n:', \%opts);
# secure flag; host; port; url portion; option (ping/cache/response/numdocs); warning; critical; handler name
my $protocol = 'http';
my $hostname;
my $port = 8983;
my $path = '/solr/admin/ping';
my $metric = 'ping';
my $handlerName = 'standard';
my $warning;
my $critical;
## process command line arguments
if ($opts{'h'}) {
usage();
exit 0;
}
# secure flag
if ($opts{'s'}) {
$protocol = 'https';
}
# Hostname
if ($opts{'H'}) {
$hostname = $opts{'H'};
}
else {
print 'No hostname specified\n';
usage();
exit 3;
}
# User specified port number
if ($opts{'p'}) {
if ( $opts{'p'} !~ /[0-9]+/ ) {
print 'Specify port number as an integer\n';
exit 3;
}
$port = $opts{'p'};
}
# metric option
if ($opts{'o'}) {
if ( $opts{'o'} eq 'ping' ) {
# path and metric already defaulted to ping
}
elsif ( $opts{'o'} eq 'cache' || $opts{'o'} eq 'response' || $opts{'o'} eq 'numdocs' || $opts{'o'} eq 'updates' ) {
$metric = $opts{'o'};
$path = '/solr/admin/stats.jsp';
}
else {
print "Unsupported option $opts{'o'}\n";
usage();
exit 3;
}
}
# specified query handler name
if ( $opts{'n'} ) {
$handlerName = $opts{'n'};
}
# specified path portion of the URL
if ( $opts{'u'} ) {
$path = $opts{'u'};
}
my $url = "${protocol}://${hostname}:${port}${path}";
#print "URL: $url\n";
# thresholds
if ( $opts{'w'} ) {
$warning = $opts{'w'};
}
if ( $opts{'c'} ) {
$critical = $opts{'c'};
}
# Make the GET request to Solr
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new( GET => $url );
my $res = $ua->request($req);
# if we have a HTTP 200 OK response
if ( $res->is_success ) {
# get content as XML
my $xp = XML::XPath->new( xml => $res->content );
if ( $metric eq 'ping' ) {
$retStr = "Solr ping status";
if ( $xp->exists("/response[str='OK']/str[\@name='status']") ) {
# We've got <str name="status">OK</str>
$exitCode = 0;
}
else {
$exitCode = 2;
}
}
else {
# these are from stats.jsp
my $core = $xp->getNodeText('/solr/core')->string_value();
my $schema = $xp->getNodeText('/solr/schema')->string_value();
$retStr = "Solr Core: $core - schema: $schema";
if ( $metric eq 'numdocs' ) {
# process the CORE searcher entry
if ( $xp->exists("/solr/solr-info/CORE/entry[normalize-space(name)='searcher']") ) {
my $numDocs = $xp->getNodeText("/solr/solr-info/CORE/entry[normalize-space(name)='searcher']/stats/stat[\@name='numDocs']")->string_value();;
# Clean up the strings
$numDocs = trim($numDocs);
$exitCode = 0;
# append to output
$retStr = $retStr . " - numDocs=${numDocs}";
$perfData = $perfData . "numDocs=${numDocs};;;; ";
}
else {
# exit code stays as UNKNOWN
$retStr = $retStr . " - Core 'searcher' not found!"
}
}
elsif ( $metric eq 'cache' ) {
my $n = 1;
# loop through the CACHE entries
while ( $xp->exists("/solr/solr-info/CACHE/entry[$n]") ) {
my $name = $xp->getNodeText("/solr/solr-info/CACHE/entry[$n]/name")->string_value();
# Don't bother if it doesn't have a hit ratio (e.g. fieldCache which is managed by Lucene)
if ( $xp->exists("/solr/solr-info/CACHE/entry[$n]/stats/stat[\@name='hitratio']") ) {
my $hitratio = $xp->getNodeText("/solr/solr-info/CACHE/entry[$n]/stats/stat[\@name='hitratio']")->string_value();
# Clean up the strings
$name = trim($name);
$hitratio = trim($hitratio);
# append to performance data
$perfData = $perfData . "'$name hit ratio'=$hitratio\%;;;; ";
}
$n++;
}
$exitCode = 0;
}
elsif ( $metric eq 'response' ) {
# process the specific named QUERYHANDLER stats
if ( $xp->exists("/solr/solr-info/QUERYHANDLER/entry[normalize-space(name)='$handlerName']") ) {
my $avgResponseTime = $xp->getNodeText("/solr/solr-info/QUERYHANDLER/entry[normalize-space(name)='$handlerName']/stats/stat[\@name='avgTimePerRequest']")->string_value();;
my $requests = $xp->getNodeText("/solr/solr-info/QUERYHANDLER/entry[normalize-space(name)='$handlerName']/stats/stat[\@name='requests']")->string_value();;
my $qps = $xp->getNodeText("/solr/solr-info/QUERYHANDLER/entry[normalize-space(name)='$handlerName']/stats/stat[\@name='avgRequestsPerSecond']")->string_value();;
# Clean up the string
$requests = trim($requests);
if ($requests == 0) {
$retStr = $retStr . " - Query handler ${handlerName} has had no requests.";
$exitCode = 3;
} else {
$avgResponseTime = trim($avgResponseTime);
$qps = trim($qps);
# threshold checks
if ( $critical && $avgResponseTime >= $critical ) {
$exitCode = 2;
}
elsif ( $warning && $avgResponseTime >= $warning ) {
$exitCode = 1;
}
else {
$exitCode = 0;
}
# append to output
$retStr = $retStr . " - ${handlerName} avgResponseTime=${avgResponseTime}";
$perfData = $perfData . "'${handlerName} avgResponseTime'=${avgResponseTime}ms;;;; '${handlerName} requests'=${requests}c;;;; '${handlerName} qps'=${qps};;;; ";
}
}
else {
# exit code stays as UNKNOWN
$retStr = $retStr . " - Query handler ${handlerName} not found!"
}
}
elsif ( $metric eq 'updates' ) {
# get the UPDATEHANDLER stats
if ( $xp->exists("/solr/solr-info/UPDATEHANDLER/entry[normalize-space(name)='updateHandler']") ) {
# Stats to gather
my @updateStats = ('cumulative_adds', 'commits', 'optimizes', 'docsPending', 'deletesById', 'deletesByQuery');
foreach my $stat ( @updateStats ) {
my $value = $xp->getNodeText("/solr/solr-info/UPDATEHANDLER/entry[normalize-space(name)='updateHandler']/stats/stat[\@name='$stat']")->string_value();
$value = trim($value);
# append to output
$retStr = $retStr . "; ${stat}=${value}";
$perfData = $perfData . "${stat}=${value};;;; ";
}
$exitCode = 0;
}
else {
# exit code stays as UNKNOWN
$retStr = $retStr . " - Query handler ${handlerName} not found!"
}
}
}
}
else {
$retStr = $res->status_line;
$exitCode = 2;
}
# and exit
print $alertStrs[$exitCode] . " - $retStr | $perfData\n";
exit $exitCode;
# Display the usage message
sub usage {
print <<EOF
------------------------------------------------------------------------------
$script $script_version
$script_description
Usage: $script -H <hostname>
Options: -s Use secure transport (https)
-H Hostname or IP address of Solr server
-p Solr port number (if not supplied defaults to port 8983)
-u URL path (defaults to /solr/admin/ping
or /solr/admin/stats.jsp depending on option)
-o Option: ping/cache/response/numdocs/updates
(defaults to 'ping')
-n Handler name for response statistics (defaults to 'standard')
-w Warning criteria (Integer - used by response)
-c Critical criteria (Integer - used by response)
------------------------------------------------------------------------------
Copyright (c) 2011 Robin Bramley, Ixxus Limited.
------------------------------------------------------------------------------
EOF
}
# trim leading & trailing spaces
sub trim {
my ($arg) = @_;
$arg =~ s/^\s+|\s+$//g;
return $arg;
}