forked from tegud/elasticsearch-icinga-check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-elasticsearch-metrics.pl
executable file
·232 lines (214 loc) · 5.96 KB
/
check-elasticsearch-metrics.pl
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
#!/usr/bin/perl
use LWP::UserAgent;
use JSON::XS;
use Getopt::Std;
use Time::Piece;
use DateTime::Format::Strptime;
my $args = "drc:w:s:a:t:f:q:h:p:x:n:i:m:";
getopts("$args", \%opt);
if(!defined $opt{s}){
return inputError('s');
}
if(!defined $opt{c} ){
return inputError('c');
}
if(!defined $opt{w}){
return inputError('w');
}
if(!defined $opt{q}){
return inputError('q');
}
if(!defined $opt{h}){
return inputError('h');
}
if(!defined $opt{p}){
$opt{p} = 9200;
}
if(!defined $opt{i}){
$opt{i} = 2;
}
my $indexCount = 1;
my $fromTime = "now-$opt{s}s";
my $critical = $opt{c};
my $warning = $opt{w};
my $reverse = $opt{r};
my $aggregationName = $opt{a};
my $aggregationType = $opt{t};
my $field = $opt{f};
my $query = $opt{q};
my $host = $opt{h};
my $port = $opt{p};
my $indexPattern = $opt{n};
my $earliestIndexCount = $opt{i};
my $timeFormat = $opt{o};
my $hasDays = defined $opt{d};
my $hasAggregation = $aggregationName && $aggregationType && $field;
my $reqContent = "";
my $reqUrl = "";
makeElasticsearchRequest();
sub makeElasticsearchRequest {
my $ua = LWP::UserAgent->new;
$ua->agent("Icinga Check/0.1 ");
my $indices = buildIndices();
$reqUrl = "http://$host:$port/$indices/_search";
my $req = HTTP::Request->new(POST => $reqUrl);
$req->content_type('application/json');
$reqContent = "{
\"size\": 0,
\"query\": {
\"filtered\": {
\"query\": {
\"query_string\": {
\"query\": \"$query\",
\"analyze_wildcard\": true
}
},
\"filter\": {
\"bool\": {
\"must\": [
{
\"range\": {
\"\@timestamp\": {
\"gte\": \"$fromTime\"
}
}
}
],
\"must_not\": []
}
}
}
}";
if($hasAggregation){
$reqContent = "$reqContent,\"aggs\": {
\"$aggregationName\": {
\"$aggregationType\": {
\"field\": \"$field\"
}
}
}";
}
$reqContent = "$reqContent }";
$req->content($reqContent);
my $res = $ua->request($req);
parseElasticsearchResponse($res);
}
sub buildIndices {
my $now = gmtime;
$year = $now->year;
$month = $now->mon;
$day = $now->mday;
my $pattern = "%Y/%m/%d";
my $parser = DateTime::Format::Strptime->new(
pattern => $pattern,
on_error => 'croak',
);
my $date = "$year/$month/$day";
my $parsedDate = $parser->parse_datetime($date);
my $indexCount = 1;
my $index;
my @indexPatterns = ("metrics-{yyyy}.{mm}.{dd}");
if($hasDays) {
@indexPatterns = ($indexPattern);
}
while ($indexCount <= $earliestIndexCount) {
foreach my $indexPattern (@indexPatterns) {
my $year = $parsedDate->year;
my $month = $parsedDate->month;
if($month < 10){
$month = "0$month"
}
my $day = $parsedDate->day;
if($day < 10){
$day = "0$day"
}
my $patternToAppend = $indexPattern;
$patternToAppend =~ s/{yyyy}/$year/g;
$patternToAppend =~ s/{mm}/$month/g;
$patternToAppend =~ s/{dd}/$day/g;
$patternToAppend = "$patternToAppend,";
$index = "$index$patternToAppend";
}
$parsedDate->subtract(days => 1);
$indexCount++;
}
chop($index);
return $index
}
sub parseElasticsearchResponse {
my ($res) = @_;
if ($res->is_success) {
my $resContent = $res->content;
my %parsed = %{decode_json $resContent};
my $value = -1;
if($hasAggregation){
my %aggregations = %{$parsed{aggregations}};
my %aggValue = %{$aggregations{$aggregationName}};
$value = $aggValue{value};
} else {
my %hits = %{$parsed{hits}};
$value = $hits{total};
}
my $alertStatus = getAlertStatus($value);
print "\nExited with: $alertStatus, Current Value: $value, Critical: $critical, Warning: $warning\n";
exit $alertStatus;
}
else {
print $res->status_line, " from elasticsearch\n";
print $res->content, "\n";
print "made to:", $reqUrl, "\n";
print "request body:\n", $reqContent, "\n";
exit 3;
}
}
sub getAlertStatus {
my ($esvalue) = @_;
if($reverse){
if($esvalue <= $critical){
return 2;
}
if($esvalue <= $warning){
return 1;
}
}
else {
if($esvalue >= $critical){
return 2;
}
if($esvalue >= $warning){
return 1;
}
}
return 0;
}
sub help {
print "\nObtains metrics from elasticsearch to power Icinga alerts\n";
print "\nUsage: check-elasticsearch-metrics.pl [OPTIONS]\n";
print "\nRequired Settings:\n";
print "\t-c [threshold]: critical threshold\n";
print "\t-w [threshold]: warning threshold\n";
print "\t-s [seconds]: number of seconds from now to check\n";
print "\t-q [query_string]: the query to run in elasticsearch\n";
print "\t-h [host]: elasticsearch host\n";
print "\t-i [number_of_indices]: the number of indices to go back through, defaults to 2\n";
print "\t-n [index_pattern]: the pattern expects months and years and can take a prefix and days, e.g: metrics-{yyyy}.{mm}\n\n";
print "\tOptional Settings:\n";
print "\t-?: this help message\n";
print "\t-r: reverse threshold (so amounts below threshold values will alert)\n";
print "\t-p [port]: elasticsearch port (defaults to 9200)\n";
print "\t-a [name]: aggregation name\n";
print "\t-t [type]: aggregation type\n";
print "\t-f [field_name]: the name of the field to aggregate\n";
print "\t-d: include the day in elasticsearch index\n\n";
print "Error codes:\n";
print "\t0: Everything OK, check passed\n";
print "\t1: Warning threshold breached\n";
print "\t2: Critical threshold breached\n";
print "\t3: Unknown, encountered an error querying elasticsearch\n";
}
sub inputError {
my ($option) = @_;
print STDERR "\n\n\t\tMissing required parameter \"$option\"\n\n";
help();
exit 3;
}