« lib/App/ElasticSearch/Utilities/QueryString/Ranges.pm
↑ Summary
lib/App/ElasticSearch/Utilities/QueryString/Plugin.pm »
| 1 | #!/usr/local/bin/perl | ||
| 2 | # PODNAME: es-index-fields.pl | ||
| 3 | # ABSTRACT: Show information on the fields storage usage | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use App::ElasticSearch::Utilities qw(:all); | ||
| 8 | use CLI::Helpers qw(:all); | ||
| 9 | use Const::Fast; | ||
| 10 | use JSON::MaybeXS; | ||
| 11 | use Getopt::Long::Descriptive; | ||
| 12 | use Pod::Usage; | ||
| 13 | |||
| 14 | #------------------------------------------------------------------------# | ||
| 15 | # Argument Collection | ||
| 16 | const my %DEFAULT => ( | ||
| 17 | duration => 'transient', | ||
| 18 | top => 10, | ||
| 19 | ); | ||
| 20 | my ($opt,$usage) = describe_options('%c %o', | ||
| 21 | ['top|limit|size|n=i', "Show the top N fields, defaults to $DEFAULT{top}", | ||
| 22 | { default => $DEFAULT{top} }, | ||
| 23 | ], | ||
| 24 | ['no-meta-fields|N', "Disable showing meta fields starting with an underscore"], | ||
| 25 | [] , | ||
| 26 | ['help', 'Display this message', { shortcircuit => 1 }], | ||
| 27 | ['manual', 'Display full manual', { shortcircuit => 1 }], | ||
| 28 | ); | ||
| 29 | |||
| 30 | #------------------------------------------------------------------------# | ||
| 31 | # Documentations! | ||
| 32 | if( $opt->help ) { | ||
| 33 | print $usage->text; | ||
| 34 | exit 0; | ||
| 35 | } | ||
| 36 | pod2usage(-exitstatus => 0, -verbose => 2) if $opt->manual; | ||
| 37 | |||
| 38 | #------------------------------------------------------------------------# | ||
| 39 | my $json = JSON->new->pretty->utf8->canonical; | ||
| 40 | |||
| 41 | my %indices = map { $_ => (es_index_days_old($_) || 0) } es_indices(); | ||
| 42 | |||
| 43 | const my @FieldStores => qw( | ||
| 44 | doc_values | ||
| 45 | norms | ||
| 46 | stored_fields | ||
| 47 | term_vectors | ||
| 48 | points | ||
| 49 | ); | ||
| 50 | |||
| 51 | my %Fields = (); | ||
| 52 | |||
| 53 | foreach my $idx ( sort keys %indices ) { | ||
| 54 | output({clear=>1, color=>"cyan"}, "Getting field data for $idx"); | ||
| 55 | |||
| 56 | # Get Field Data | ||
| 57 | my $result; | ||
| 58 | eval { | ||
| 59 | $result = es_request('_disk_usage', { | ||
| 60 | method => 'POST', | ||
| 61 | index => $idx, | ||
| 62 | uri_param => { | ||
| 63 | run_expensive_tasks => 'true' | ||
| 64 | }, | ||
| 65 | }); | ||
| 66 | 1; | ||
| 67 | } or do { | ||
| 68 | my $err = $@; | ||
| 69 | output({indent=>1, color=>'red'}, "Request Failed: $err"); | ||
| 70 | }; | ||
| 71 | $result //= {}; | ||
| 72 | |||
| 73 | if( my $fields = $result->{$idx}{fields} ) { | ||
| 74 | my $by_size = sub { | ||
| 75 | $fields->{$b}{total_in_bytes} <=> $fields->{$a}{total_in_bytes} | ||
| 76 | }; | ||
| 77 | my $n = 0; | ||
| 78 | foreach my $field ( sort $by_size keys %{ $fields } ) { | ||
| 79 | # Skip meta fields | ||
| 80 | next if $opt->no_meta_fields && $field =~ /^_/; | ||
| 81 | |||
| 82 | # Collect field totals | ||
| 83 | my $data = $fields->{$field}; | ||
| 84 | $Fields{$field} += $data->{total_in_bytes}; | ||
| 85 | |||
| 86 | # Skip the output, but collect all the datas | ||
| 87 | $n++; | ||
| 88 | next if $n > $opt->top; | ||
| 89 | |||
| 90 | output({indent=>1,kv=>1,color=>color_pick($data->{total_in_bytes})}, $field => $data->{total}); | ||
| 91 | foreach my $k ( @FieldStores ) { | ||
| 92 | if( $data->{"${k}_in_bytes"} > 0 ) { | ||
| 93 | output({indent=>2,kv=>1,color=>color_pick($data->{"${k}_in_bytes"})}, $k => $data->{$k} ); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | else { | ||
| 99 | output({indent=>1, color=>'red'}, "Failed retrieving field storage information"); | ||
| 100 | } | ||
| 101 | |||
| 102 | if ( my $totals = $result->{$idx}{all_fields} ) { | ||
| 103 | output({clear=>1,indent=>1,color=>'cyan'}, "All Fields ($idx):"); | ||
| 104 | output({indent=>2,kv=>1,color=>color_pick($totals->{total_in_bytes})}, total => $totals->{total}); | ||
| 105 | |||
| 106 | foreach my $k ( @FieldStores ) { | ||
| 107 | if( $totals->{"${k}_in_bytes"} > 0 ) { | ||
| 108 | output({indent=>3,kv=>1,color=>color_pick($totals->{"${k}_in_bytes"})}, $k => $totals->{$k} ); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | output({clear=>1,color=>'yellow'}, "Totals for fields in all indexes"); | ||
| 115 | my $n = 0; | ||
| 116 | foreach my $k ( sort { $Fields{$b} <=> $Fields{$a} } keys %Fields ) { | ||
| 117 | output({indent=>1,kv=>1,color=>color_pick($Fields{$k})}, $k, es_human_size($Fields{$k})); | ||
| 118 | $n++; | ||
| 119 | last if $n >= $opt->top; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub color_pick { | ||
| 123 | my ($v) = @_; | ||
| 124 | return | ||
| 125 | $v > 1024 * 1024 * 1024 * 10 ? 'red' | ||
| 126 | : $v > 1024 * 1024 * 1024 ? 'yellow' | ||
| 127 | : 'green'; | ||
| 128 | } | ||
| 129 | |||
| 130 | __END__ |