c68634b5559f3bb00d66ac28cefad7871aca4049
[scripts.git] / elastic / load.sh
1 #!/bin/bash
2 #
3 # from https://raw.githubusercontent.com/elastic/beats-dashboards/master/load.sh
4
5
6 # Usage examples:
7 # env KIBANA_INDEX='.kibana_env1' ./load.sh
8 # ./load.sh -url http://test.com:9200
9 # ./load.sh -url http://test.com:9200 -user admin:secret
10 # ./load.sh -url http://test.com:9200 -index .kibana-test
11
12 # The default value of the variable. Initialize your own variables here
13 ELASTICSEARCH=http://localhost:9200
14 CURL='curl --netrc'
15 KIBANA_INDEX=".kibana"
16
17 print_usage() {
18   echo "
19
20 Load the dashboards, visualizations and index patterns into the given
21 Elasticsearch instance.
22
23 Usage:
24   $(basename "$0") -url ${ELASTICSEARCH} -user admin:secret -index ${KIBANA_INDEX}
25
26 Options:
27   -h | -help
28     Print the help menu.
29   -l | -url
30     Elasticseacrh URL. By default is ${ELASTICSEARCH}.
31   -u | -user
32     Username and password for authenticating to Elasticsearch using Basic
33     Authentication. The username and password should be separated by a
34     colon (i.e. "admin:secret"). By default no username and password are
35     used.
36   -i | -index
37     Kibana index pattern where to save the dashboards, visualizations,
38     index patterns. By default is ${KIBANA_INDEX}.
39
40 " >&2
41 }
42
43 while [ "$1" != "" ]; do
44 case $1 in
45     -l | -url )
46         ELASTICSEARCH=$2
47         if [ "$ELASTICSEARCH" = "" ]; then
48             echo "Error: Missing Elasticsearch URL"
49             print_usage
50             exit 1
51         fi
52         ;;
53
54     -u | -user )
55         USER=$2
56         if [ "$USER" = "" ]; then
57             echo "Error: Missing username"
58             print_usage
59             exit 1
60         fi
61         CURL="${CURL} --user ${USER}"
62         ;;
63
64     -i | -index )
65         KIBANA_INDEX=$2
66         if [ "$KIBANA_INDEX" = "" ]; then
67             echo "Error: Missing Kibana index pattern"
68             print_usage
69             exit 1
70         fi
71         ;;
72
73     -h | -help )
74         print_usage
75         exit 0
76         ;;
77
78      *)
79         echo "Error: Unknown option $2"
80         print_usage
81         exit 1
82         ;;
83
84 esac
85 shift 2
86 done
87
88 DIR=.
89 echo "Loading dashboards to ${ELASTICSEARCH} in ${KIBANA_INDEX}"
90
91 # Workaround for: https://github.com/elastic/beats-dashboards/issues/94
92 #${CURL} -XPUT "${ELASTICSEARCH}/${KIBANA_INDEX}"
93 #${CURL} -XPUT "${ELASTICSEARCH}/${KIBANA_INDEX}/_mapping/search" -d'{"search": {"properties": {"hits": {"type": "integer"}, "version": {"type": "integer"}}}}'
94
95 for file in ${DIR}/search/*.json
96 do
97     NAME=`basename ${file} .json`
98     echo "Loading search ${NAME}:"
99     ${CURL} -XPUT ${ELASTICSEARCH}/${KIBANA_INDEX}/search/${NAME} \
100         -d @${file} || exit 1
101     echo
102 done
103
104 for file in ${DIR}/index-pattern/*.json
105 do
106     NAME=`awk '$1 == "\"title\":" {gsub(/[",]/, "", $2); print $2}' ${file}`
107     echo "Loading index pattern ${NAME}:"
108
109     ${CURL} -XPUT ${ELASTICSEARCH}/${KIBANA_INDEX}/index-pattern/${NAME} \
110         -d @${file} || exit 1
111     echo
112 done
113
114 for file in ${DIR}/dashboard/*.json
115 do
116     NAME=`basename ${file} .json`
117     echo "Loading dashboard ${NAME}:"
118     ${CURL} -XPUT ${ELASTICSEARCH}/${KIBANA_INDEX}/dashboard/${NAME} \
119         -d @${file} || exit 1
120     echo
121 done
122
123 for file in ${DIR}/visualization/*.json
124 do
125     NAME=`basename ${file} .json`
126     echo "Loading visualization ${NAME}:"
127     ${CURL} -XPUT ${ELASTICSEARCH}/${KIBANA_INDEX}/visualization/${NAME} \
128         -d @${file} || exit 1
129     echo
130 done
131
132 # Clear cache
133 ${CURL} -XPOST ${ELASTICSEARCH}/_cache/clear