forked from elastic/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_watch.sh
More file actions
executable file
·68 lines (59 loc) · 1.83 KB
/
load_watch.sh
File metadata and controls
executable file
·68 lines (59 loc) · 1.83 KB
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
62
63
64
65
66
67
if [ -z "$1" ] ; then
echo "USAGE: load_watch.sh <watch_name> <optional_username> <optional_password> <optional_endpoint>:<optional_port>"
echo "eg: ./load_watch.sh port_scan elastic changeme my_remote_cluster.mydomain:9200"
echo -e
echo "Defaults: elastic changeme localhost:9200"
exit 1
fi
username=elastic
if [ "$2" ] ; then
username=$2
fi
password=changeme
if [ "$3" ] ; then
password=$3
fi
port=9200
endpoint=localhost
if [ "$4" ] ; then
if ":" in $4; then
endpoint=${4%":"*} # extractthe host value from the provided endpoint
port=${4#*":"} # extract the port value if provided in endpoint:port format
if [ "$port" == "" ]; then
# if port is blank, due to endpoint provided as localhost: or no port providedthen use default port
port=9200
fi
else
endpoint=$4
fi
fi
# test if provided watch name is correct/exists
if [ ! -d "$1" ]; then
echo "Watch scripts dir $1 doesn't appear to exist in $PWD"
exit 1
fi
echo "Loading $1 scripts"
shopt -s nullglob
for script in $1/scripts/*.json
do
filename=$(basename "$script")
scriptname="${filename%.*}"
echo $scriptname
es_response=$(curl -s -X POST $endpoint:$port/_scripts/painless/$scriptname -u $username:$password -d @$script)
if [ 0 -eq $? ] && [ $es_response = '{"acknowledged":true}' ]; then
echo "Loading $scriptname script...OK"
else
echo "Loading $scriptname script...FAILED"
exit 1
fi
done
echo "Loading $1 watch "
curl -s -o /dev/null -X DELETE $endpoint:$port/_xpack/watcher/watch/$1 -u $username:$password
es_response=$(curl --w "%{http_code}" -s -o /dev/null -X PUT $endpoint:$port/_xpack/watcher/watch/$1 -u $username:$password -d @$1/watch.json)
if [ 0 -eq $? ] && [ $es_response = "201" ]; then
echo "Loading $2 watch...OK"
exit 0
else
echo "Loading $2 watch...FAILED"
exit 1
fi