script/es-status.pl untested

filter: Statementstmt 0.0% 0 / 190 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 266 CC 9 SCAR 24.6
CC
9
Cov
0%
handle_segments
20.0
handle_settings
20.0
handle_health
12.0
CRAP 11.7
1 #!/usr/local/bin/perl
2 # PODNAME: es-status.pl
3 # ABSTRACT: Simple ElaticSearch Status Checks
4 use strict;
5 use warnings;
6  
7 use Getopt::Long qw(:config no_ignore_case no_ignore_case_always);
8 use Pod::Usage;
9 use CLI::Helpers qw(:all);
10 use App::ElasticSearch::Utilities qw(:all);
11  
12 #------------------------------------------------------------------------#
13 # Argument Collection
14 my %opt;
15 GetOptions(\%opt,
16 # DISPATCH FUNCTIONS
17 'all',
18 'health',
19 'node',
20 'segments',
21 'settings',
22 # DOCS
23 'help|h',
24 'manual|m',
25 );
26  
27 #------------------------------------------------------------------------#
28 # Dispatch Table
29 my @DISPATCH = (
30 { name => 'health', handler => \&handle_health },
31 { name => 'node', handler => \&handle_node },
32 { name => 'segments', handler => \&handle_segments },
33 { name => 'settings', handler => \&handle_settings },
34  
35 );
36  
37 my @TODO=();
38 # Set the checks specified
39 foreach my $d (@DISPATCH) {
40 my $parm = $d->{name};
41 if( exists $opt{all} && $opt{all} ) {
42 push @TODO, $parm;
43 }
44 elsif( exists $opt{$parm} && defined $opt{$parm} ) {
45 push @TODO, $parm;
46 }
47 }
48 # Default is a check
49 push @TODO, 'health' unless scalar @TODO;
50  
51 #------------------------------------------------------------------------#
52 # Documentations!
53 pod2usage(1) if $opt{help};
54 pod2usage(-exitstatus => 0, -verbose => 2) if $opt{manual};
55  
56 #------------------------------------------------------------------------#
57 # Dispatch
58 my $ES = es_connect();
59  
60 #------------------------------------------------------------------------#
61 # Dispatch
62 foreach my $action (@TODO) {
63 foreach my $dispatch (@DISPATCH) {
64 next unless $dispatch->{name} eq $action;
65 $dispatch->{handler}->();
66 }
67 }
68  
69 #------------------------------------------------------------------------#
70 exit 0;
71 #------------------------------------------------------------------------#
72 # Query functions
73 sub handle_health {
74 my $stats = es_request('_cluster/health');
75 my $state = es_request('_cluster/state/master_node,nodes', {});
76  
77 output({clear=>1,color=>"cyan"}, "Cluster Health Check", "-="x20);
78 if( defined $state && exists $state->{master_node} && exists $state->{nodes}{$state->{master_node}}) {
79 output({kv=>1,color=>'cyan'}, master => $state->{nodes}{$state->{master_node}}->{name});
80 }
81 output({kv=>1,color=>"cyan"}, "name", $stats->{cluster_name});
82 output({kv=>1,color=>$stats->{status}}, "health", $stats->{status});
83 output({kv=>1}, "nodes", $stats->{number_of_nodes});
84  
85 if( $stats->{status} ne "green" ) {
86 output({kv=>1,color=>"red"}, "shards_unassigned", $stats->{unassigned_shards});
87 output({kv=>1,color=>"magenta"}, "shards_relocating", $stats->{relocating_shards});
88 output({kv=>1,color=>"yellow"}, "shards_initializing", $stats->{initializing_shards});
89 }
90 else {
91 verbose({kv=>1}, "shards_unassigned", $stats->{unassigned_shards});
92 verbose({kv=>1}, "shards_relocating", $stats->{relocating_shards});
93 verbose({kv=>1}, "shards_initializing", $stats->{initializing_shards});
94 }
95 }
96 sub handle_segments {
97 output({clear=>1,color=>"cyan"}, "Index Segmentation Check", "-="x20);
98  
99 my @indexes = es_indices();
100 foreach my $index ( sort @indexes ) {
101 output({color=>"cyan"},"$index:");
102 my $stats = es_index_segments($index);
103 my $shards = 0;
104 my $segments = 0;
105 my $index_size = 0;
106 foreach my $id (sort keys %{ $stats->{indices}{$index}{shards} } ) {
107 $shards++;
108 verbose({kv=>1,color=>"magenta",indent=>1}, "shard", $id);
109 my $shard = $stats->{indices}{$index}{shards}{$id}[0];
110 my $color = $shard->{num_search_segments} > 1 ? 'yellow' : 'green';
111 $segments += $shard->{num_search_segments};
112 verbose({kv=>1,color=>$color,indent=>2}, "segments", $shard->{num_search_segments} );
113 my $size = 0;
114 foreach my $seg ( keys %{ $shard->{segments} }) {
115 $size += $shard->{segments}{$seg}{size_in_bytes};
116 }
117 verbose({kv=>1,indent=>2}, "size_bytes", $size);
118 my @units = qw(kb mb gb tb);
119 my $unit = 'b';
120 my $size_short = $size;
121 while( $size_short > 1024 && @units ) {
122 $size_short /= 1024;
123 $unit = shift @units;
124 }
125 verbose({kv=>1,indent=>2}, "size", sprintf("%.2f %s", $size_short, $unit) );
126 $index_size += $size;
127 }
128 my $ratio = $shards > 0 ? sprintf("%.2f", $segments / $shards ) : 0;
129 my $color = $ratio == 1 ? "green" : "yellow";
130 output({kv=>1,color=>$color,indent=>1}, "segments_to_shards", $ratio);
131 # Index size
132 my @units = qw(kb mb gb tb);
133 my $unit = 'b';
134 my $size_short = $index_size;
135 while( $size_short > 1024 && @units ) {
136 $size_short /= 1024;
137 $unit = shift @units;
138 }
139 verbose({kv=>1,indent=>1}, "index_size", sprintf("%.2f %s", $size_short, $unit) );
140 verbose({kv=>1,indent=>1,level=>2}, "index_size_bytes", $index_size );
141 }
142 }
143  
144 sub handle_node {
145 my $stats = es_node_stats();
146 output({clear=>1,color=>"cyan"}, "Node Status Check", "-="x20);
147  
148 my $node_id = (keys %{ $stats->{nodes} })[0];
149 my $node = $stats->{nodes}{$node_id};
150 output({kv=>1,color=>"cyan"}, "name", $node->{name});
151 output({kv=>1}, "index_size", $node->{indices}{store}{size} );
152 verbose({kv=>1}, "index_size_bytes", $node->{indices}{store}{size_in_bytes} );
153 verbose({kv=>1}, "docs", $node->{indices}{docs}{count} );
154 output({kv=>1}, "open_fd", $node->{process}{open_file_descriptors} );
155  
156 # JVM Stats
157 output({kv=>1}, "jvm", '');
158 # Threads
159 output({indent=>1,kv=>1}, "threads_current", $node->{jvm}{threads}{count});
160 output({indent=>1,kv=>1}, "threads_peak", $node->{jvm}{threads}{peak_count});
161 # Memory
162 output({indent=>1,kv=>1}, "mem", '');
163 output({indent=>2,kv=>1,color=>"yellow"}, "heap_used", $node->{jvm}{mem}{heap_used});
164 verbose({indent=>2,kv=>1,color=>"yellow"}, "heap_used_bytes", $node->{jvm}{mem}{heap_used_in_bytes});
165 output({indent=>2,kv=>1}, "heap_committed", $node->{jvm}{mem}{heap_committed});
166 verbose({indent=>2,kv=>1}, "heap_committed_bytes", $node->{jvm}{mem}{heap_committed_in_bytes});
167 # GC
168 output({indent=>1,kv=>1}, "gc", '');
169 # GC Details
170 foreach my $collector ( keys %{ $node->{jvm}{gc}{collectors} } ) {
171 output({indent=>2,kv=>1}, $collector, '');
172 output({indent=>3,kv=>1}, "collections", $node->{jvm}{gc}{collectors}{$collector}{collection_count} );
173 output({indent=>3,kv=>1}, "time", $node->{jvm}{gc}{collectors}{$collector}{collection_time} );
174 output({indent=>3,kv=>1}, "time_ms", $node->{jvm}{gc}{collectors}{$collector}{collection_time_in_millis} );
175 }
176 output({kv=>1}, "requests", $node->{transport}{rx_count});
177 output({indent=>1,kv=>1}, "rx", $node->{transport}{rx_size});
178 verbose({indent=>1,kv=>1}, "rx_bytes", $node->{transport}{rx_size_in_bytes});
179 output({kv=>1}, "responses", $node->{transport}{tx_count});
180 output({indent=>1,kv=>1}, "tx", $node->{transport}{tx_size});
181 verbose({indent=>1,kv=>1}, "tx_bytes", $node->{transport}{tx_size_in_bytes});
182 }
183  
184 sub handle_settings {
185 my $stats = es_settings();
186 output({clear=>1,color=>"cyan"}, "Index Settings Check", "-="x20);
187  
188 my $colorize = sub {
189 my ($v) = shift;
190 return "green" if $v eq 'false';
191 return "yellow" if $v eq 'not set';
192 return "red";
193 };
194  
195 foreach my $index (sort keys %{ $stats } ) {
196 my %settings = %{ $stats->{$index}{settings} };
197 output({color=>'cyan'}, "$index:");
198 my $value = exists $settings{'index.auto_expand_replicas'} && defined $settings{'index.auto_expand_replicas'} ? $settings{'index.auto_expand_replicas'} : 'not set';
199 my $color = $colorize->($value);
200 output({indent=>1,kv=>1,color=>$color}, "auto_expand_replicas", $value);
201 verbose({indent=>1,kv=>1}, "replicas", $settings{'index.number_of_replicas'});
202 verbose({indent=>1,kv=>1}, "shards", $settings{'index.number_of_shards'});
203 }
204 }
205  
206 __END__