From 487ed668f00ed4e65cb1bd44ed2ead290145d673 Mon Sep 17 00:00:00 2001 From: llavaud Date: Fri, 30 Dec 2016 22:34:41 +0100 Subject: [PATCH] initial commit --- kbt | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100755 kbt diff --git a/kbt b/kbt new file mode 100755 index 0000000..b2867a1 --- /dev/null +++ b/kbt @@ -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] + + 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