f46466a2abafbc9618937940c55485c6e84d8903
[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 BEAT_CONFIG=".beatconfig"
17
18 print_usage() {
19   echo "
20
21 Load the dashboards, visualizations and index patterns into the given
22 Elasticsearch instance.
23
24 Usage:
25   $(basename "$0") -url ${ELASTICSEARCH} -user admin:secret -index ${KIBANA_INDEX}
26
27 Options:
28   -h | -help
29     Print the help menu.
30   -l | -url
31     Elasticseacrh URL. By default is ${ELASTICSEARCH}.
32   -u | -user
33     Username and password for authenticating to Elasticsearch using Basic
34     Authentication. The username and password should be separated by a
35     colon (i.e. "admin:secret"). By default no username and password are
36     used.
37   -i | -index
38     Kibana index pattern where to save the dashboards, visualizations,
39     index patterns. By default is ${KIBANA_INDEX}.
40
41 " >&2
42 }
43
44 while [ "$1" != "" ]; do
45 case $1 in
46     -l | -url )
47         ELASTICSEARCH=$2
48         if [ "$ELASTICSEARCH" = "" ]; then
49             echo "Error: Missing Elasticsearch URL"
50             print_usage
51             exit 1
52         fi
53         ;;
54
55     -u | -user )
56         USER=$2
57         if [ "$USER" = "" ]; then
58             echo "Error: Missing username"
59             print_usage
60             exit 1
61         fi
62         CURL="${CURL} --user ${USER}"
63         ;;
64
65     -i | -index )
66         KIBANA_INDEX=$2
67         if [ "$KIBANA_INDEX" = "" ]; then
68             echo "Error: Missing Kibana index pattern"
69             print_usage
70             exit 1
71         fi
72         ;;
73
74     -h | -help )
75         print_usage
76         exit 0
77         ;;
78
79      *)
80         echo "Error: Unknown option $2"
81         print_usage
82         exit 1
83         ;;
84
85 esac
86 shift 2
87 done
88
89 if [ -f ${BEAT_CONFIG} ]; then
90   for ln in `cat ${BEAT_CONFIG}`; do
91     BUILD_STRING="${BUILD_STRING}s/${ln}/g;"
92   done
93   SED_STRING=`echo ${BUILD_STRING} | sed 's/;$//'`
94 fi
95 # Failsafe
96 if [ -z ${SED_STRING} ]; then
97   SED_STRING="s/packetbeat-/packetbeat-/g;s/filebeat-/filebeat-/g;s/topbeat-/topbeat-/g;s/winlogonbeat-/winlogonbeat-/g"
98 fi
99
100 DIR=.
101 echo "Loading dashboards to ${ELASTICSEARCH} in ${KIBANA_INDEX}"
102
103 # Workaround for: https://github.com/elastic/beats-dashboards/issues/94
104 ${CURL} -XPUT "${ELASTICSEARCH}/${KIBANA_INDEX}"
105 ${CURL} -XPUT "${ELASTICSEARCH}/${KIBANA_INDEX}/_mapping/search" -d'{"search": {"properties": {"hits": {"type": "integer"}, "version": {"type": "integer"}}}}'
106
107 TMP_SED_FILE="${DIR}/search/tmp_search.json"
108 for file in ${DIR}/search/*.json
109 do
110     NAME=`basename ${file} .json`
111     echo "Loading search ${NAME}:"
112     sed ${SED_STRING} ${file} > ${TMP_SED_FILE}
113     ${CURL} -XPUT ${ELASTICSEARCH}/${KIBANA_INDEX}/search/${NAME} \
114         -d @${TMP_SED_FILE} || exit 1
115     echo
116 done
117 rm ${TMP_SED_FILE}
118
119 for file in ${DIR}/index-pattern/*.json
120 do
121     NAME=`awk '$1 == "\"title\":" {gsub(/[",]/, "", $2); print $2}' ${file}`
122     echo "Loading index pattern ${NAME}:"
123
124     ${CURL} -XPUT ${ELASTICSEARCH}/${KIBANA_INDEX}/index-pattern/${NAME} \
125         -d @${file} || exit 1
126     echo
127 done
128
129 for file in ${DIR}/dashboard/*.json
130 do
131     NAME=`basename ${file} .json`
132     echo "Loading dashboard ${NAME}:"
133     ${CURL} -XPUT ${ELASTICSEARCH}/${KIBANA_INDEX}/dashboard/${NAME} \
134         -d @${file} || exit 1
135     echo
136 done
137
138 for file in ${DIR}/visualization/*.json
139 do
140     NAME=`basename ${file} .json`
141     echo "Loading visualization ${NAME}:"
142     ${CURL} -XPUT ${ELASTICSEARCH}/${KIBANA_INDEX}/visualization/${NAME} \
143         -d @${file} || exit 1
144     echo
145 done
146
147 # Clear cache
148 ${CURL} -XPOST ${ELASTICSEARCH}/_cache/clear