|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# |
| 4 | +# Script to setup the default index pattern in kibana |
| 5 | +# |
| 6 | + |
| 7 | +set -ue |
| 8 | + |
| 9 | +INDEX_ID="logstash-*" |
| 10 | + |
| 11 | +API_HOSTNAME="$KIBANA_SERVICE_HOST:$KIBANA_SERVICE_PORT" |
| 12 | + |
| 13 | +COUNTDOWN=10 |
| 14 | +SLEEP_DELAY=5 |
| 15 | + |
| 16 | +set_index_pattern() |
| 17 | +{ |
| 18 | + curl -s -o /dev/null -w "%{http_code}" \ |
| 19 | + -XPOST \ |
| 20 | + -H "Content-type: application/json" \ |
| 21 | + -H "kbn-xsrf: anything" \ |
| 22 | + "http://$API_HOSTNAME/api/saved_objects/index-pattern/$INDEX_ID" \ |
| 23 | + -d "{\"attributes\": {\"title\": \"$INDEX_ID\", \"timeFieldName\": \"@timestamp\"}}" |
| 24 | +} |
| 25 | + |
| 26 | +set_default_index() |
| 27 | +{ |
| 28 | + curl -s -o /dev/null -w "%{http_code}" \ |
| 29 | + -XPOST \ |
| 30 | + -H "Content-type: application/json" \ |
| 31 | + -H "kbn-xsrf: anything" \ |
| 32 | + "http://$API_HOSTNAME/api/kibana/settings/defaultIndex" \ |
| 33 | + -d "{\"value\": \"$INDEX_ID\"}" |
| 34 | +} |
| 35 | + |
| 36 | + |
| 37 | +# Wait for kibana service to be ready |
| 38 | +while [ "$COUNTDOWN" -gt 0 ]; do |
| 39 | + http_code=$(curl -s -o /dev/null -w "%{http_code}" "$API_HOSTNAME") |
| 40 | + if [ "$http_code" = "200" ]; then |
| 41 | + break |
| 42 | + else |
| 43 | + COUNTDOWN=$((COUNTDOWN - 1)) |
| 44 | + if [ "$COUNTDOWN" -le 0 ]; then |
| 45 | + echo "Error: Can't connect to the Kibana API server" |
| 46 | + exit 1 |
| 47 | + else |
| 48 | + sleep $SLEEP_DELAY |
| 49 | + fi |
| 50 | + fi |
| 51 | +done |
| 52 | + |
| 53 | +http_code=$(set_index_pattern) |
| 54 | + |
| 55 | +if [ "$http_code" = "409" ]; then |
| 56 | + echo "Index pattern '$INDEX_ID' is already registered" |
| 57 | +elif [ "$http_code" != "200" ]; then |
| 58 | + echo "Error: An error occurred when trying to set the Kibana index pattern (http code: $http_code)" |
| 59 | + exit 1 |
| 60 | +fi |
| 61 | + |
| 62 | +http_code=$(set_default_index) |
| 63 | + |
| 64 | +if [ "$http_code" != "200" ]; then |
| 65 | + echo "Error: An error occurred when trying to set the default Kibana index (http code: $http_code)" |
| 66 | + exit 1 |
| 67 | +fi |
| 68 | + |
| 69 | +echo "Successful registered '$INDEX_ID' as Kibana index pattern" |
0 commit comments