forked from stagemonitor/stagemonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExportKibanaDashboard.groovy
62 lines (57 loc) · 2.07 KB
/
ExportKibanaDashboard.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
def cli = new CliBuilder(usage: "groovy ExportKibanaDashboard.groovy")
cli.with {
width = 160
_(longOpt: 'url', args: 1, 'The elasticsearch url (defaults to http://localhost:9200)')
_(longOpt: 'type', args: 1, required: false, argName: 'index-pattern|search|dashboard|all', 'Which type to export')
_(longOpt: 'id', args: 1, required: false, 'The id of the type to export')
_(longOpt: 'curl', 'If set, exports as curl script')
_(longOpt: 'format', args: 1, 'bulk|json')
}
def options = cli.parse(args)
if (!options) return
def addr = options.url ?: "http://localhost:9200"
def format = options.format ?: 'bulk'
if (options.type) {
exportTypes(addr, options.type, format)
} else {
["index-pattern", "search", "dashboard"].each { exportTypes(addr, it, format) }
}
private void exportTypes(addr, type, format) {
println "# Exporting ${type}s"
new File("export/$type").mkdirs()
def dashboards = new JsonSlurper().parse("$addr/.kibana/${type}/_search?q=*&size=100".toURL())
dashboards.hits.hits.each {
def title = it._source.title.replace(" ", "-")
println " - $title"
def out = new FileOutputStream("export/$type/$title.$format")
if (format == "json") {
out << "[\n"
}
exportKibanaType(addr, type, it._id, out, format)
if (format == "json") {
out << "]"
}
}
}
def exportKibanaType(String addr, String type, String id, OutputStream out, String format, boolean firstInvocation = true) {
def hit = new JsonSlurper().parse("$addr/.kibana/$type/${URLEncoder.encode(id, "UTF-8")}".toURL())
if (format == "bulk") {
out << new JsonBuilder([index: [_index: hit._index, _type: hit._type, _id: hit._id, _version: 1, _version_type: "external_gte"]]).toString()
out << "\n"
out << new JsonBuilder(hit._source).toString()
out << "\n"
} else if (format == "json") {
if (!firstInvocation) {
out << ","
}
out << new JsonBuilder(hit).toString()
out << "\n"
}
if (type == "dashboard") {
new JsonSlurper().parseText(hit._source.panelsJSON).each {
exportKibanaType(addr, it.type, it.id, out, format, false)
}
}
}