Last active
August 29, 2015 14:10
-
-
Save rottenbytes/91764f4ac0946a5de305 to your computer and use it in GitHub Desktop.
quickly dump kibana dashboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "bundler/setup" | |
require "elasticsearch" | |
require "json" | |
require "getoptlong" | |
def help() | |
puts "Usage : dashboard_dumper.rb [options]" | |
puts "Options :" | |
puts " --host : specifies host to connect to. default is localhost" | |
puts " --port : port to connect to. default is 9200" | |
puts " --index : dashboard index name. default is kibana-int" | |
puts " --output-dir : output directory. default is './dashboards'" | |
puts " --user : username. default is foo" | |
puts " --password : password. default is bar" | |
puts " --help : this help" | |
exit 0 | |
end | |
opts = GetoptLong.new( | |
[ "--host", "-H", GetoptLong::REQUIRED_ARGUMENT ], | |
[ "--port", "-p", GetoptLong::REQUIRED_ARGUMENT ], | |
[ "--index", "-i", GetoptLong::REQUIRED_ARGUMENT ], | |
[ "--output-dir", "-o", GetoptLong::REQUIRED_ARGUMENT ], | |
[ "--user", "-u", GetoptLong::REQUIRED_ARGUMENT ], | |
[ "--password", "-W", GetoptLong::REQUIRED_ARGUMENT ], | |
[ "--help", "-h", GetoptLong::NO_ARGUMENT ] | |
) | |
host = "localhost" | |
port = 9200 | |
index = "kibana-int" | |
outputdir = "./dashboards" | |
user = "foo" | |
password = "bar" | |
opts.each do |opt, arg| | |
case opt | |
when "--host" | |
host = arg | |
when "--port" | |
port = arg.to_i | |
when "--index" | |
index = arg | |
when "--output-dir" | |
outputdir = arg | |
when "--user" | |
user = arg | |
when "--password" | |
password = arg | |
when "--help" | |
help() | |
end | |
end | |
Dir::mkdir(outputdir) unless Dir.exists?(outputdir) | |
client = Elasticsearch::Client.new hosts: [ { host: host, port: port, user: user, password: password } ] | |
begin | |
rslt = client.search(index: index, body: { query: { match: { _type: "dashboard" } }}) | |
rescue Exception => e | |
puts "An error occured : #{e.message}" | |
exit 1 | |
end | |
if rslt.has_key?("hits") | |
rslt["hits"]["hits"].each do |dash| | |
name = dash["_id"].gsub(" ","_").downcase | |
File.open(File.join(outputdir, "#{name}.json"), "w") do |fp| | |
fp.write(JSON.pretty_generate(JSON.parse(dash["_source"]["dashboard"]))) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment