forked from HariSekhon/Nagios-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_impala_metrics.pl
executable file
·224 lines (199 loc) · 6.79 KB
/
check_impala_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
#!/usr/bin/perl -T
# nagios: -epn
#
# Author: Hari Sekhon
# Date: 2013-07-29 19:19:45 +0100 (Mon, 29 Jul 2013)
#
# https://github.com/harisekhon/nagios-plugins
#
# License: see accompanying LICENSE file
#
$DESCRIPTION = "Nagios Plugin to fetch metrics from a given Impalad/StateStore debug UI";
$VERSION = "0.3.1";
use strict;
use warnings;
BEGIN {
use File::Basename;
use lib dirname(__FILE__) . "/lib";
}
use HariSekhonUtils;
use LWP::Simple '$ua';
use JSON::XS;
$ua->agent("Hari Sekhon $progname version $main::VERSION");
my $default_port = 25000;
$port = $default_port;
my $metrics;
my $all_metrics = 0;
%options = (
"H|host=s" => [ \$host, "Impalad or StateStore to connect to" ],
"P|port=s" => [ \$port, "Impalad or StateStore debug UI port (defaults to $default_port for Impalad, specify 25010 for StateStore)" ],
"m|metrics=s" => [ \$metrics, "Metrics to fetch, comma separated. If one metric is given then warning and critical thresholds may optionally be applied to that metric" ],
"a|all-metrics" => [ \$all_metrics, "Grab all metrics. Useful if you don't know what to monitor yet or just want to graph everything" ],
"w|warning=s" => [ \$warning, "Warning threshold or ran:ge (inclusive)" ],
"c|critical=s" => [ \$critical, "Critical threshold or ran:ge (inclusive)" ],
);
@usage_order = qw/host port metrics all-metrics warning critical/;
get_options();
my $metric_regex = '[A-Za-z][\w\.-]+';
$host = validate_host($host);
$host = validate_resolvable($host);
$port = validate_port($port);
my %stats;
my @stats;
if($all_metrics){
defined($metrics) and usage "cannot specify --all-metrics and specific --metrics at the same time!";
} else {
defined($metrics) or usage "no metrics specified";
foreach my $metric (split(/\s*[,\s]\s*/, $metrics)){
$metric =~ /^$metric_regex$/io or usage "invalid metric '$metric' given, must be alphanumeric, may contain dashes, dots and underscores";
grep(/^$metric$/, @stats) or push(@stats, $metric);
}
@stats or usage "no valid metrics specified";
@stats = uniq_array @stats;
}
validate_thresholds();
vlog2;
set_timeout();
set_http_timeout($timeout - 1);
$ua->show_progress(1) if $debug;
$status = "OK";
# Impala 1.0 / 1.0.1 doesn't currently support &raw on this URI handler
#my $url = "http://$host:$port/metrics";
# switched to /jsonmetrics
# Impala 1.0 / 1.0.1 doesn't currently support &raw on this URI handler
# Impala since CDH 5.4 switched to jsonmetrics?json
my $url = "http://$host:$port/jsonmetrics?json";
my $content = curl $url, "Impala debug UI metrics";
sub check_stats_parsed(){
if($all_metrics){
if(scalar keys %stats == 0){
quit "UNKNOWN", "no stats collected from /metrics page (daemon recently started?)";
}elsif(scalar keys %stats < 5){
quit "UNKNOWN", "<5 stats collected from /metrics page (daemon recently started?). This could also be an error, try running with -vvv to see what the deal is";
}
foreach(sort keys %stats){
vlog2 "stats $_ = $stats{$_}";
}
} else {
foreach(@stats){
unless(defined($stats{$_})){
vlog2;
quit "UNKNOWN", "failed to find $_ in output from '$host:$port'";
}
vlog2 "stats $_ = $stats{$_}";
}
}
vlog2;
}
sub parse_stats(){
#isJson($content) or quit "CRITICAL", "invalid json returned by '$host:$port'";
my $json;
try{
$json = decode_json $content;
};
catch{
quit "CRITICAL", "invalid json returned by '$host:$port'";
};
if($debug){
use Data::Dumper;
print Dumper($json);
print "\n";
}
if($all_metrics){
foreach my $metric (keys %{$json}){
if(isFloat($json->{$metric})){
$stats{$metric} = $json->{$metric};
}
}
} else {
foreach my $metric (@stats){
defined($json->{$metric}) or quit "UNKNOWN", "metric '$metric' not found in output from Impala";
if(isHash($json->{$metric})){
defined($json->{$metric}{"last"}) or quit "UNKNOWN", "unrecognized metric/format detected for '$metric', ask author Hari Sekhon for an update";
if(isFloat($json->{$metric}{"last"})){
$stats{$metric} = $json->{$metric}{"last"};
}
} else {
if(isFloat($json->{$metric})){
$stats{$metric} = $json->{$metric};
} else {
quit "UNKNOWN", "given metric '$metric' is not a float, cannot be used";
}
}
}
}
check_stats_parsed();
}
# ============================================================================ #
vlog2 "parsing metrics from '$host:$port'\n";
parse_stats();
# ============================================================================ #
#foreach(split("\n", $content)){
# if(/($metric_regex)\s*:\s*(\d+)\s*$/io){
# $stats{$1} = $2;
# } elsif(/($metric_regex):\s+(?:\w+:\s+\d+,\s+)*last\s*:\s*(\d+)\s*/io){
# # to catch last heartbeat stats
# $stats{$1} = $2;
# }
#}
#unless(%stats){
# quit "UNKNOWN", "no metrics found!";
#}
#
#my @metrics_not_found;
#if(@metrics){
# if(scalar @metrics eq 1){
# defined($metrics[0]) or quit "CRITICAL", "metric '$metrics[0] not found";
# $msg = "$metrics[0]=$stats{$metrics[0]}";
# check_thresholds($stats{$metrics[0]});
# $msg .= " | $metrics[0]=$stats{$metrics[0]}";
# quit $status, $msg;
# } else {
# foreach(@metrics){
# unless(defined($stats{$_})){
# push(@metrics_not_found, $_);
# next;
# }
# $msg .= "$_=$stats{$_} ";
# }
# }
#} else {
# foreach(sort keys %stats){
# $msg .= "$_=$stats{$_} ";
# }
#}
#
#$msg .= "| $msg";
#$msg =~ s/ $//;
#
#if(@metrics_not_found){
# critical;
# $msg = "metrics not found: " . join(",", @metrics_not_found) . " -- $msg";
#}
# ============================================================================ #
$msg = "";
if($all_metrics){
foreach(sort keys %stats){
$msg .= "$_=$stats{$_} ";
}
} else {
foreach(@stats){
$msg .= "$_=$stats{$_} ";
}
}
if($all_metrics){
$msg .= "| ";
foreach(sort keys %stats){
$msg .= "$_=$stats{$_} ";
}
} elsif(!$all_metrics and scalar @stats == 1){
$msg =~ s/ $//;
check_thresholds($stats{$stats[0]});
$msg .= " | $stats[0]=$stats{$stats[0]};" . ($thresholds{warning}{upper} ? $thresholds{warning}{upper} : "") . ";" . ($thresholds{critical}{upper} ? $thresholds{critical}{upper} : "");
} else {
$msg .= "| ";
foreach(sort keys %stats){
$msg .= "$_=$stats{$_} ";
}
}
quit $status, $msg;