script/es-index-blocks.pl untested

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 8 Podpod n/a totaltotal 0.0% 0 / 97 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2 # PODNAME: es-index-blocks.pl
3 # ABSTRACT: Report and manage index blocks
4 use strict;
5 use warnings;
6  
7 use App::ElasticSearch::Utilities qw(es_request es_node_stats es_index_stats es_index_strip_date es_flatten_hash);
8 use CLI::Helpers qw(:all);
9 use Getopt::Long::Descriptive;
10 use IO::Socket::INET;
11 use Pod::Usage;
12 use Ref::Util qw(is_hashref is_arrayref);
13  
14 #------------------------------------------------------------------------#
15 # Argument Collection
16 my ($opt,$usage) = describe_options('%c %o',
17 ['remove-blocks|remove', "Remove discovered blocks, default is to just report."],
18 [],
19 ['For complete options, see the manual.'],
20 [],
21 ['help', "Display this message and exit.", { shortcircuit => 1 }],
22 ['manual', "Display complete man page.", { shortcircuit => 1 }],
23 );
24 #------------------------------------------------------------------------#
25 # Documentations!
26 if( $opt->help ) {
27 print $usage->text;
28 exit;
29 }
30 if( $opt->manual ) {
31 pod2usage(-exitstatus => 0, -verbose => 2);
32 }
33  
34 #------------------------------------------------------------------------#
35 # Get Index Blocks
36 my $result = es_request('_settings/index.blocks.*', { index => '_all' });
37 my %blocks=();
38 my @all_indices=();
39 foreach my $idx ( keys %{ $result } ) {
40 push @all_indices, $idx;
41 if( $result->{$idx}{settings} ) {
42 my $settings = es_flatten_hash( $result->{$idx}{settings} );
43 foreach my $block ( keys %{ $settings } ) {
44 my $value = $settings->{$block};
45 if( lc $value eq 'true') {
46 push @{ $blocks{$block} }, $idx;
47 }
48 }
49 }
50 }
51  
52 #------------------------------------------------------------------------#
53 # Report Blocks
54 if( my @blocks = keys %blocks ) {
55 foreach my $block ( sort @blocks ) {
56 output({color=>'cyan',clear=>1}, "Index block: $block");
57 foreach my $index (sort @{ $blocks{$block} }) {
58 output({data=>1}, "$index is $block");
59 if( $opt->remove_blocks ) {
60 eval {
61 my $result = es_request('_settings',
62 { index => $index, method => 'PUT' },
63 { $block => 'false' },
64 );
65 die "not acknowledged" unless $result->{acknowledged};
66 output({color=>'green',indent=>1}, "$block removed.");
67 1;
68 } or do {
69 my $err = $@;
70 output({color=>'red',indent=>1}, "ERROR removing $block from $index: $err");
71 };
72 }
73 }
74 }
75 }
76 else {
77 output({color=>'green'}, "No blocks discovered on any indices.");
78 exit 0;
79 }
80  
81 __END__