Created
March 14, 2018 14:54
-
-
Save mkutz/be2afae8c51e9838a247afb3a7b0fc9f to your computer and use it in GitHub Desktop.
A command line script to open Kibana according to given parameters.
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 groovy | |
/* | |
* define CLI | |
*/ | |
def cli = new CliBuilder(usage: "open-log [options]", ) | |
final String DEFAULT_ENVIRONMENT = "prod" | |
cli.e(longOpt: "environment", args: 1, argName: "environment", | |
"Environment whose logs should be displayed (either int, pre or prod).\nDefault: ${DEFAULT_ENVIRONMENT}") | |
final String DEFAULT_TIME = "24h" | |
cli.t(longOpt: "time", args: 1, argName: "time", | |
"Age of the oldest log to be displayed (e.g. 24h or 15m or 7d).\nDefault: ${DEFAULT_TIME}") | |
final List<String> STANDARD_COLUMNS = ["loglevel", "msg"] | |
cli.c(longOpt: "columns", args: 20, argName: "columns", valueSeparator: ",", | |
"Comma separated list of additional columns to be displayed (e.g. \"log_type,host,market_code\").\n" + | |
"Default: ${STANDARD_COLUMNS.join(",")}") | |
final String SERVICE_NAME = new File(".").canonicalFile.name | |
cli.s(longOpt: "services", args: 10, argName: "services", valueSeparator: ",", | |
"Comma separated list of additional services whose logs should be displayed.") | |
cli.q(longOpt: "query", args: 1, argName: "query", | |
"Query string to look for.") | |
cli.h(longOpt: "help", "Print this message.") | |
/* | |
* parse arguments | |
*/ | |
OptionAccessor options = cli.parse(args) | |
if (options.h) { | |
cli.usage() | |
System.exit(0) | |
} | |
String environment = options.e ?: DEFAULT_ENVIRONMENT | |
List<String> services = [SERVICE_NAME] + (options.ss ?: []) | |
List<String> columns = STANDARD_COLUMNS + (services.size() > 1 ? ["service"] : []) + (options.cs ?: []) | |
String time = options.t ?: DEFAULT_TIME | |
String query = options.q ?: "" | |
/* | |
* assamble URL | |
*/ | |
String servicesFilterPart = "('\$state':(store:appState)," + | |
"meta:(alias:!n,disabled:!f,index:'technical-*',key:service,negate:!f,params:!(${services.join(",")}),type:phrases,value:'${services.join(",%20")}')," + | |
"query:(bool:(minimum_should_match:1,should:!(${services.collect { "(match_phrase:(service:${it}))" }.join(",")}))))" | |
String accessLogFilterPart = "('\$state':(store:appState)," + | |
"meta:(alias:!n,disabled:!f,index:'technical-*',key:log_type,negate:!t,type:phrase,value:access)," + | |
"query:(match:(log_type:(query:access,type:phrase))))" | |
String debugFilterPart = "('\$state':(store:appState)," + | |
"meta:(alias:!n,disabled:!f,index:'technical-*',key:loglevel,negate:!t,type:phrase,value:DEBUG)," + | |
"query:(match:(loglevel:(query:DEBUG,type:phrase))))" | |
String infoFilterPart = "('\$state':(store:appState)," + | |
"meta:(alias:!n,disabled:!f,index:'technical-*',key:loglevel,negate:!t,type:phrase,value:INFO)," + | |
"query:(match:(loglevel:(query:INFO,type:phrase))))" | |
String warnFilterPart = "('\$state':(store:appState)," + | |
"meta:(alias:!n,disabled:!t,index:'technical-*',key:loglevel,negate:!t,type:phrase,value:WARN)," + | |
"query:(match:(loglevel:(query:WARN,type:phrase))))" | |
String errorFilterPart = "('\$state':(store:appState)," + | |
"meta:(alias:!n,disabled:!t,index:'technical-*',key:loglevel,negate:!t,type:phrase,value:ERROR)," + | |
"query:(match:(loglevel:(query:ERROR,type:phrase))))" | |
String queryPart = (query ? "query:(query_string:(analyze_wildcard:!t,query:'%22${query}%22'))" : "query:(match_all:())") | |
String g = "_g=(time:(from:now-${time},mode:quick,to:now))" | |
String a = "_a=(columns:!(${columns.join(",")})," + | |
"filters:!(${servicesFilterPart},${accessLogFilterPart},${debugFilterPart},${infoFilterPart},${warnFilterPart},${errorFilterPart})," + | |
"index:'technical-*',interval:auto,${queryPart},sort:!('@timestamp',desc))" | |
String url = "https://logging-${environment}.rewe-digital.com/app/kibana#/discover?${g}&${a}" | |
/* | |
* open URL | |
*/ | |
String command = [ | |
"windows": "start", | |
"linux": "xdg-open", | |
"darwin": "open" | |
].find { key, value -> System.properties["os.name"].toLowerCase().contains(key) }.value | |
Process process = "${command} ${url}".execute() | |
process.consumeProcessOutput(System.out, System.err) | |
process.waitFor() | |
System.exit(process.exitValue()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment