-
Notifications
You must be signed in to change notification settings - Fork 0
/
elkclusterstats
executable file
·126 lines (111 loc) · 5.32 KB
/
elkclusterstats
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
#!/usr/bin/perl
use strict;
use warnings;
use JSON::XS;
use YAML::Tiny;
use Data::Dumper;
use Munin::Plugin;
use LWP::UserAgent;
use JSON qw (decode_json);
# use Scalar::Util qw/reftype/;
use vars qw ($config $yamlconfig $json $debug %infohash1 %infohash2 %infohash3 $key $val);
$debug = 1;
if (@ARGV && $ARGV[0] eq "config") {
$config = 1;
}
# Open the config
my $yaml = YAML::Tiny->read ( '/usr/local/etc/elkclusterstats.yml');
# Get a reference to the first document
my $yamlconfig = $yaml->[0];
my $statsurl = 'http://localhost:9200/_cluster/stats';
my $ua = LWP::UserAgent->new;
my $response = $ua->get (${statsurl}) or print ("WARNING: Could not read stats from $statsurl");
if ($response->is_success) {
$json = JSON::XS->new->decode ($response->content);
} else {
print "WARNING: ", $response->status_line, "\n";
}
my $cluster_name = $json->{'cluster_name'};
my $cluster_name_config = clean_fieldname ($cluster_name);
my @keys = qw (indices nodes);
foreach my $key1 (@keys) {
if ($yamlconfig->{$key1}) { # We have YAML config for this section
if (ref $json->{$key1} eq ref {}) { # Is it a hash?
foreach my $key2 (keys %{$json->{$key1}}) {
if ($yamlconfig->{$key1}->{$key2}) { # We have YAML config for this section, so we include it
if (ref $json->{$key1}->{$key2} eq ref {}) {
# print " - $json->{'nodes'}->{$node}->{$key}->{$key2} is a hash\n";
foreach my $key3 (keys %{$json->{$key1}->{$key2}}) {
# print "nodes -> $node -> $key -> $key2 -> $key3\n";
if ($yamlconfig->{$key1}->{$key2}->{$key3}) {
if (ref $json->{$key1}->{$key2}->{$key3} eq ref {}) { # Is it a hash?
# print " - - $json->{'nodes'}->{$node}->{$key}->{$key2}->{$key3} is a hash\n";
} else {
if ($yamlconfig->{$key1}->{$key2}->{$key3}->{'munin_title'}) {
$infohash3{$key1}->{$key2}->{$key3}->{'label'} = $key3;
$infohash3{$key1}->{$key2}->{$key3}->{'value'} = $json->{$key1}->{$key2}->{$key3};
$infohash3{$key1}->{$key2}->{$key3}->{'title'} = $yamlconfig->{$key1}->{$key2}->{$key3}->{'munin_title'};
$infohash3{$key1}->{$key2}->{$key3}->{'type'} = $yamlconfig->{$key1}->{$key2}->{$key3}->{'munin_type'} if $yamlconfig->{$key1}->{$key2}->{$key3}->{'munin_type'};
$infohash3{$key1}->{$key2}->{$key3}->{'vlabel'} = $yamlconfig->{$key1}->{$key2}->{$key3}->{'munin_vlabel'} if $yamlconfig->{$key1}->{$key2}->{$key3}->{'munin_vlabel'};
}
}
}
}
} else { # $key2 is not a hash, checking for name
if ($yamlconfig->{$key1}->{$key2}->{'munin_title'}) {
$infohash2{$key1}->{$key2}->{'title'} = $yamlconfig->{$key1}->{$key2}->{'munin_title'};
$infohash2{$key1}->{$key2}->{'type'} = $yamlconfig->{$key1}->{$key2}->{'munin_type'} if $yamlconfig->{$key1}->{$key2}->{'munin_type'};
$infohash2{$key1}->{$key2}->{'vlabel'} = $yamlconfig->{$key1}->{$key2}->{'munin_vlabel'} if $yamlconfig->{$key1}->{$key2}->{'munin_vlabel'};
$infohash2{$key1}->{$key2}->{'label'} = $key2;
$infohash2{$key1}->{$key2}->{'value'} = $json->{$key1}->{$key2};
}
}
}
}
} else { # key is not a hash
if ($yamlconfig->{$key1}->{'munin_title'}) {
$infohash1{$key1}->{'title'} = $yamlconfig->{$key1}->{'munin_title'};
$infohash1{$key1}->{'type'} = $yamlconfig->{$key1}->{'munin_type'} if $yamlconfig->{$key1}->{'munin_type'};
$infohash1{$key1}->{'vlabel'} = $yamlconfig->{$key1}->{'munin_vlabel'} if $yamlconfig->{$key1}->{'munin_vlabel'};
$infohash1{$key1}->{'label'} = $key1;
$infohash1{$key1}->{'value'} = $json->{$key1};
}
}
}
}
foreach my $key1 (keys %infohash2) {
foreach my $key2 (keys %{$infohash2{$key1}}) {
print "\nmultigraph elkclusterstats__${key1}__${key2}\n";
my $tmptype = "";
if ($config) {
my $tmptitle = $infohash2{$key1}->{$key2}->{'title'} if $infohash2{$key1}->{$key2}->{'title'};
$tmptype = $infohash2{$key1}->{$key2}->{'type'} if $infohash2{$key1}->{$key2}->{'type'};
print "graph_title ", $cluster_name, " cluster: ", $key1, " - ", $tmptitle, "\n";
print "graph_category Elasticsearch\n";
print "graph_vlabel ", $infohash2{$key1}->{$key2}->{'vlabel'}, "\n" if $infohash2{$key1}->{$key2}->{'vlabel'};
print $key2, ".label ", $infohash2{$key1}->{$key2}->{'label'}, "\n";
print $key2, ".type ", $tmptype, "\n" if $tmptype;
} else {
print $key2, ".value ", $infohash2{$key1}->{$key2}->{'value'}, "\n";
}
}
}
foreach my $key1 (keys %infohash3) {
foreach my $key2 (keys %{$infohash3{$key1}}) {
foreach my $key3 (keys %{$infohash3{$key1}->{$key2}}) {
print "\nmultigraph elkclusterstats__${key1}__${key2}__${key3}\n";
my $tmptype = "";
if ($config) {
my $tmptitle = $infohash3{$key1}->{$key2}->{$key3}->{'title'} if $infohash3{$key1}->{$key2}->{$key3}->{'title'};
$tmptype = $infohash3{$key1}->{$key2}->{$key3}{'type'} if $infohash3{$key1}->{$key2}->{$key3}->{'type'};
print "graph_title ", $cluster_name, " cluster: ", $key1, " - ", $key2, " - ", $tmptitle, "\n";
print "graph_category Elasticsearch\n";
print "graph_vlabel ", $infohash3{$key1}->{$key2}->{$key3}->{'vlabel'}, "\n" if $infohash3{$key1}->{$key2}->{$key3}->{'vlabel'};
print $key3, ".label ", $infohash3{$key1}->{$key2}->{$key3}->{'label'}, "\n";
print $key3, ".type ", $tmptype, "\n" if $tmptype;
} else {
print $key3, ".value ", $infohash3{$key1}->{$key2}->{$key3}->{'value'}, "\n";
}
}
}
}