App-ElasticSearch-Utilities
view release on metacpan or search on metacpan
scripts/es-status.pl view on Meta::CPAN
#------------------------------------------------------------------------#
# Dispatch Table
my @DISPATCH = (
{ name => 'health', handler => \&handle_health },
{ name => 'node', handler => \&handle_node },
{ name => 'segments', handler => \&handle_segments },
{ name => 'settings', handler => \&handle_settings },
);
my @TODO=();
# Set the checks specified
foreach my $d (@DISPATCH) {
my $parm = $d->{name};
if( exists $opt{all} && $opt{all} ) {
push @TODO, $parm;
}
elsif( exists $opt{$parm} && defined $opt{$parm} ) {
push @TODO, $parm;
}
}
# Default is a check
push @TODO, 'health' unless scalar @TODO;
#------------------------------------------------------------------------#
# Documentations!
pod2usage(1) if $opt{help};
pod2usage(-exitstatus => 0, -verbose => 2) if $opt{manual};
#------------------------------------------------------------------------#
# Dispatch
my $ES = es_connect();
#------------------------------------------------------------------------#
# Dispatch
foreach my $action (@TODO) {
foreach my $dispatch (@DISPATCH) {
next unless $dispatch->{name} eq $action;
$dispatch->{handler}->();
}
}
#------------------------------------------------------------------------#
exit 0;
#------------------------------------------------------------------------#
# Query functions
sub handle_health {
my $stats = es_request('_cluster/health');
my $state = es_request('_cluster/state/master_node,nodes', {});
output({clear=>1,color=>"cyan"}, "Cluster Health Check", "-="x20);
if( defined $state && exists $state->{master_node} && exists $state->{nodes}{$state->{master_node}}) {
output({kv=>1,color=>'cyan'}, master => $state->{nodes}{$state->{master_node}}->{name});
}
output({kv=>1,color=>"cyan"}, "name", $stats->{cluster_name});
output({kv=>1,color=>$stats->{status}}, "health", $stats->{status});
output({kv=>1}, "nodes", $stats->{number_of_nodes});
if( $stats->{status} ne "green" ) {
output({kv=>1,color=>"red"}, "shards_unassigned", $stats->{unassigned_shards});
output({kv=>1,color=>"magenta"}, "shards_relocating", $stats->{relocating_shards});
output({kv=>1,color=>"yellow"}, "shards_initializing", $stats->{initializing_shards});
}
else {
verbose({kv=>1}, "shards_unassigned", $stats->{unassigned_shards});
verbose({kv=>1}, "shards_relocating", $stats->{relocating_shards});
verbose({kv=>1}, "shards_initializing", $stats->{initializing_shards});
}
}
sub handle_segments {
output({clear=>1,color=>"cyan"}, "Index Segmentation Check", "-="x20);
my @indexes = es_indices();
foreach my $index ( sort @indexes ) {
output({color=>"cyan"},"$index:");
my $stats = es_index_segments($index);
my $shards = 0;
my $segments = 0;
my $index_size = 0;
foreach my $id (sort keys %{ $stats->{indices}{$index}{shards} } ) {
$shards++;
verbose({kv=>1,color=>"magenta",indent=>1}, "shard", $id);
my $shard = $stats->{indices}{$index}{shards}{$id}[0];
my $color = $shard->{num_search_segments} > 1 ? 'yellow' : 'green';
$segments += $shard->{num_search_segments};
verbose({kv=>1,color=>$color,indent=>2}, "segments", $shard->{num_search_segments} );
my $size = 0;
foreach my $seg ( keys %{ $shard->{segments} }) {
$size += $shard->{segments}{$seg}{size_in_bytes};
}
verbose({kv=>1,indent=>2}, "size_bytes", $size);
my @units = qw(kb mb gb tb);
my $unit = 'b';
my $size_short = $size;
while( $size_short > 1024 && @units ) {
$size_short /= 1024;
$unit = shift @units;
}
verbose({kv=>1,indent=>2}, "size", sprintf("%.2f %s", $size_short, $unit) );
$index_size += $size;
}
my $ratio = $shards > 0 ? sprintf("%.2f", $segments / $shards ) : 0;
my $color = $ratio == 1 ? "green" : "yellow";
output({kv=>1,color=>$color,indent=>1}, "segments_to_shards", $ratio);
# Index size
my @units = qw(kb mb gb tb);
my $unit = 'b';
my $size_short = $index_size;
while( $size_short > 1024 && @units ) {
$size_short /= 1024;
$unit = shift @units;
}
verbose({kv=>1,indent=>1}, "index_size", sprintf("%.2f %s", $size_short, $unit) );
verbose({kv=>1,indent=>1,level=>2}, "index_size_bytes", $index_size );
}
}
sub handle_node {
my $stats = es_node_stats();
output({clear=>1,color=>"cyan"}, "Node Status Check", "-="x20);
my $node_id = (keys %{ $stats->{nodes} })[0];
my $node = $stats->{nodes}{$node_id};
output({kv=>1,color=>"cyan"}, "name", $node->{name});
output({kv=>1}, "index_size", $node->{indices}{store}{size} );
verbose({kv=>1}, "index_size_bytes", $node->{indices}{store}{size_in_bytes} );
( run in 0.596 second using v1.01-cache-2.11-cpan-71847e10f99 )