-
Notifications
You must be signed in to change notification settings - Fork 0
/
elasticsearch
executable file
·249 lines (230 loc) · 8.61 KB
/
elasticsearch
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
#!/usr/bin/perl -w
use strict;
use LWP;
use JSON qw/decode_json/;
my $HOST = exists $ENV{'host'} ? $ENV{'host'} : "127.0.0.1";
my $PORT = exists($ENV{'PORT'}) ? $ENV{'PORT'} : 9200;
my $GRAPH_ALL_NODES = exists($ENV{'GRAPH_ALL_NODES'}) ? $ENV{'GRAPH_ALL_NODES'} : 0;
# default graph params
my %graph_default = (
'graph_args' => '--base 1024',
'graph_category' => 'elasticsearch',
'graph_vlabel' => 'bytes',
);
# strategy based on symlink name
my %plugins = (
'elasticsearch_cache' => {
'path_all' => '/_cluster/nodes/stats',
'path' => '/_cluster/nodes/_local/stats',
'graph' => {
'graph_title' => 'ElasticSearch cache',
},
'values' => {
'field_size' => { 'value' => 'indices.cache.field_size_in_bytes', 'name' => 'Field size', 'type' => 'GAUGE', },
'filter_size' => { 'value' => 'indices.cache.filter_size_in_bytes', 'name' => 'Filter size', 'type' => 'GAUGE', },
},
},
'elasticsearch_docs' => {
'path_all' => '/_cluster/nodes/stats',
'path' => '/_cluster/nodes/_local/stats',
'graph' => {
'graph_args' => '--base 1000',
'graph_title' => 'ElasticSearch documents',
'graph_vlabel' => 'docs',
},
'values' => {
'num_docs' => { 'value' => 'indices.docs.count', 'name' => 'num of documents', 'type' => 'GAUGE', },
},
},
'elasticsearch_index_size' => {
'path_all' => '/_cluster/nodes/stats',
'path' => '/_cluster/nodes/_local/stats',
'graph' => {
'graph_title' => 'ElasticSearch index size',
},
'values' => {
'index_size' => { 'value' => 'indices.store.size_in_bytes', 'name' => 'Index size', 'type' => 'GAUGE', },
},
},
'elasticsearch_jvm_memory' => {
'path_all' => '/_cluster/nodes/stats',
'path' => '/_nodes/_local/jvm/stats',
'graph' => {
'graph_title' => 'ElasticSearch jvm memory',
},
'values' => {
'heap_used_in_bytes' => { 'value' => 'jvm.mem.heap_used_in_bytes', 'name' => 'Heap used size', 'type' => 'GAUGE', },
'heap_committed_in_bytes' => { 'value' => 'jvm.mem.heap_committed_in_bytes', 'name' => 'Heap committed size', 'type' => 'GAUGE', },
'non_heap_used_in_bytes' => { 'value' => 'jvm.mem.non_heap_used_in_bytes', 'name' => 'Non-heap used size', 'type' => 'GAUGE', },
'non_heap_committed_in_bytes' => { 'value' => 'jvm.mem.non_heap_committed_in_bytes', 'name' => 'Non-heap committed size', 'type' => 'GAUGE', },
},
},
'elasticsearch_jvm_pool_memory' => {
'path_all' => '/_cluster/nodes/stats',
'path' => '/_nodes/_local/jvm/stats',
'graph' => {
'graph_title' => 'ElasticSearch jvm pool memory',
},
'values' => {
'used_in_bytes' => { 'value' => 'jvm.mem.pools.Code Cache.used_in_bytes', 'name' => 'Pool used size', 'type' => 'GAUGE', },
'max_in_bytes' => { 'value' => 'jvm.mem.pools.Code Cache.max_in_bytes', 'name' => 'Pool max size', 'type' => 'GAUGE', },
'peak_used_in_bytes' => { 'value' => 'jvm.mem.pools.Code Cache.peak_used_in_bytes', 'name' => 'Pool peak used size', 'type' => 'GAUGE', },
'peak_max_in_bytes' => { 'value' => 'jvm.mem.pools.Code Cache.peak_max_in_bytes', 'name' => 'Pool peak max size', 'type' => 'GAUGE', },
},
},
'elasticsearch_jvm_threads' => {
'path_all' => '/_cluster/nodes/stats',
'path' => '/_nodes/_local/jvm/stats',
'graph' => {
'graph_args' => '--base 1000',
'graph_title' => 'ElasticSearch jvm threads',
'graph_vlabel' => 'threads',
},
'values' => {
'count' => { 'value' => 'jvm.threads.count', 'name' => 'Num threads', 'type' => 'GAUGE', },
'peak_count' => { 'value' => 'jvm.threads.peak_count', 'name' => 'Peak threads', 'type' => 'GAUGE', },
},
},
);
# determine which plugin
my $plugin = getPluginDetails($0, \%plugins, \%graph_default);
if (! defined($plugin)) {
die("invalid plugin [$0] is not properly configured!\n");
}
# mix-in run-time settings
$plugin->{host} = $HOST;
$plugin->{port} = $PORT;
# this determines whether we chart just for the local node or whether we chart all nodes in the cluster (default: local only)
$plugin->{graph_all} = $GRAPH_ALL_NODES;
# user agent
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
# determine run mode
if (exists($ARGV[0]) && ($ARGV[0] eq 'config')) {
doOutputConfig($plugin, $ua);
} else {
doOutputData($plugin, $ua);
}
exit;
# output values for a single node for the provided plugin context
sub outputNodeValues {
my $plugin = shift;
my $data = shift;
my $keyPrefix = shift;
foreach my $key (keys(%{$plugin->{values}})) {
my @nestedKeys = split(/\./, $plugin->{values}->{$key}->{value});
my $ptr = $data;
foreach my $nestedKey (@nestedKeys) {
if (exists($ptr->{$nestedKey})) {
$ptr = $ptr->{$nestedKey};
} else {
$ptr = 'U';
last;
}
}
print STDOUT (defined($keyPrefix) ? ($keyPrefix . '_') : ''), $key, ".value ", $ptr, "\n";
}
}
# fetch and output plugin data
sub doOutputData {
my $plugin = shift;
my $ua = shift;
my $uri = getUriForPlugin($plugin);
my $data = getUriAndDecode($ua, $uri);
if (! $plugin->{graph_all}) {
my @keys = keys(%{$data->{nodes}});
my $key = $keys[0];
my $nodeData = $data->{nodes}->{$key};
outputNodeValues($plugin, $nodeData);
} else {
foreach my $nodeKey (keys(%{$data->{nodes}})) {
my $keyPrefix = $nodeKey;
$keyPrefix =~ s/\W/_/g;
my $nodeData = $data->{nodes}->{$nodeKey};
outputNodeValues($plugin, $nodeData, $keyPrefix);
}
}
}
# merge plugin-specific config with defaults
sub mergeConfig {
my $h1 = shift;
my $h2 = shift;
my %final = %{$h1};
foreach my $key (keys(%{$h2})) {
$final{$key} = $h2->{$key};
}
return \%final;
}
# determine uri for plugin context
sub getUriForPlugin {
my $plugin = shift;
return 'http://' . $plugin->{host} . ':' . $plugin->{port} . ($plugin->{graph_all} ? $plugin->{path_all} : $plugin->{path});
}
# fetch a uri and json decode
sub getUriAndDecode {
my $ua = shift;
my $uri = shift;
my $res = $ua->get($uri, 'Content-Type' => 'application/json');
if (! $res->is_success) {
die("request for [$uri] returned " . $res->code . "\n");
}
my $data = decode_json($res->content);
if (! defined($data)) {
die("unable to parse response from [$uri]\n");
}
return $data;
}
# get list of es nodes as an array ref of hash refs containing keys 'key' and 'name'
sub getElasticSearchNodes {
my $plugin = shift;
my $ua = shift;
my $uri = getUriForPlugin($plugin);
my $data = getUriAndDecode($ua, $uri);
my @nodes = ();
if (exists($data->{nodes})) {
foreach my $nodeKey (keys(%{$data->{nodes}})) {
my $node = $data->{nodes}->{$nodeKey};
$nodeKey =~ s/\W/_/g;
push(@nodes, { 'key' => $nodeKey, 'name' => $node->{name} });
}
}
return \@nodes;
}
# output graph config
sub doOutputConfig {
my $plugin = shift;
my $ua = shift;
foreach my $key (sort(keys(%{$plugin->{graph}}))) {
print $key, " ", $plugin->{graph}->{$key}, "\n";
}
if (! $plugin->{graph_all}) {
foreach my $key (keys(%{$plugin->{values}})) {
print $key, ".label ", $plugin->{values}->{$key}->{name}, "\n";
print $key, ".type ", uc($plugin->{values}->{$key}->{type}), "\n";
}
} else {
my $nodes = getElasticSearchNodes($plugin, $ua);
foreach my $node (@{$nodes}) {
my $nodeKey = $node->{key};
foreach my $key (keys(%{$plugin->{values}})) {
print $nodeKey, '_', $key, ".label ", $node->{name}, " ", $plugin->{values}->{$key}->{name}, "\n";
print $nodeKey, '_', $key, ".type ", uc($plugin->{values}->{$key}->{type}), "\n";
}
}
}
}
# determine which plugin is being called based on the filename
sub getPluginDetails {
my $fileName = shift;
my $plugins = shift;
my $graph_default = shift;
my @fileParts = split(/\//, $fileName);
my $baseName = $fileParts[-1];
if (exists($plugins->{$baseName})) {
my $plugin = $plugins->{$baseName};
$plugin->{graph} = exists($plugin->{graph}) ? mergeConfig($graph_default, $plugin->{graph}) : $graph_default;
return $plugin;
}
return undef;
}
exit;