Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
llavaud committed Dec 30, 2016
0 parents commit 487ed66
Showing 1 changed file with 130 additions and 0 deletions.
130 changes: 130 additions & 0 deletions kbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#!/usr/bin/env perl

use 5.010;

use strict;
use warnings;

use Getopt::Long qw/GetOptions HelpMessage/;
use HTTP::Request;
use IO::File;
use JSON qw/to_json from_json/;
use LWP::UserAgent;

GetOptions(
'help' => sub { HelpMessage(0) },
'host=s' => \(my $host = 'localhost:5601'),
'index=s' => \(my $index = '.kibana'),
'output=s' => \(my $output = 'kbt_export.json'),
'type=s' => \(my $type = 'all'),
) or HelpMessage(1);

$host =~ s/^([\d\.]+)(?!\:\d+)$/$1:5601/;

my $cmd = $ARGV[0];

HelpMessage(1) unless $cmd;

if ($cmd eq 'list') {
&list(0);
}
elsif ($cmd eq 'export') {
&export();
}
else {
HelpMessage(1);
}

sub list {
my $export = shift;

my $ids = {};

foreach my $t ('search', 'visualization', 'dashboard') {

next if ($type ne 'all' and $t ne $type);

print '* '.uc $t.":\n" if $type eq 'all' and $export == 0;
my $request = HTTP::Request->new(POST => "http://$host/elasticsearch/$index/$t/_search");
$request->header('kbn-version' => '5.1.1');
$request->content('{"query":{"match_all":{}}}');

my $ua = LWP::UserAgent->new;
my $response = $ua->request($request);

if ($response->is_success) {
my $json = from_json($response->decoded_content);
foreach (@{ $json->{'hits'}->{'hits'} }) {
if ($export == 0) {
print "$_->{'_id'}\n";
}
elsif ($export == 1) {
push @{ $ids->{'docs'} }, { '_id' => $_->{'_id'}, '_type' => $t };
}
}
}
else {
print STDERR $response->status_line."\n";
}
print "\n" if $type eq 'all' and $export == 0;
}

return ($export == 1) ? $ids : 1;
}

sub export {
my $ids = &list(1);

my $json = to_json($ids);

my $request = HTTP::Request->new(POST => "http://$host/elasticsearch/$index/_mget");
$request->header('kbn-version' => '5.1.1');
$request->content($json);

my $ua = LWP::UserAgent->new;
my $response = $ua->request($request);

my $fh = IO::File->new($output, q{>});

if ($response->is_success) {
print $fh $response->decoded_content;
}
else {
print STDERR $response->status_line."\n";
}

close $fh;

return 1;
}

=pod
=head1 NAME
kbt - Kibana backup Tool, export kibana data
=head1 SYNOPSIS
kbt [OPTIONS] <COMMAND>
OPTIONS
--type TYPE Type of kibana resources {search|visualization|dashboard} (defaults to all)
--output FILE Backup file (defaults to kbt_export.json)
--host IP:PORT Ip address of kibana instance (defaults to localhost:5601)
--index INDEX Kibana index (defaults to .kibana)
--help Print this help
COMMAND
list list resource's id
export export resources
=head1 AUTHOR
Laurent Lavaud
=head1 VERSION
0.01
=cut

0 comments on commit 487ed66

Please sign in to comment.