blob: 77715c784fb24c41f430243ef33ad2fb1b5fe623 [file] [log] [blame]
Hung-Wei Chiu1887b932021-04-05 16:59:28 -07001# Copyright 2021-present Open Networking Foundation
2# SPDX-License-Identifier: LicenseRef-ONF-Member-Only-1.0
3#
4apiVersion: v1
5kind: ConfigMap
6metadata:
7 name: "{{ .Release.Name }}-scripts"
8 labels:
9 app: onos
10 chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
11 release: "{{ .Release.Name }}"
12 heritage: "{{ .Release.Service }}"
13data:
14 update.sh: |
15 #!/bin/bash
16 host=${ES_URL:-localhost:9201}
17 dest_dir=${DEST_DIR}
18 monitor_dir=${MONITOR_DIR}
19 setting_dir=${SETTING_DIR}
20 temp_dir=${OUTPUT_DIR:-/opendistro_temp}
21
Hung-Wei Chiu40bf9bf2021-06-22 19:15:51 -070022 cleanup() {
23 echo "Cleaning up..."
24 exit
25 }
26
Hung-Wei Chiu1887b932021-04-05 16:59:28 -070027 #return destination id if exist
28 check_destination() { name=$1
29 curl -s -H "content-type: application/json" "$host/_opendistro/_alerting/destinations" | jq -r ".destinations[] | select(.name == \"$name\").id"
30 }
31
32 update_destination() {
33 echo "update"
34 file=$1
35 id=$2
36 #compress the json file to reduce the http size
37 new_file="$temp_dir/$file"
38 jq -c '' < "$file" > "$new_file"
39 curl -s -X PUT -H "content-type: application/json" -d "@$new_file" "$host/_opendistro/_alerting/destinations/$id"
40 rm "$new_file"
41 echo " "
42 }
43
44 create_destination() {
45 echo "create"
46 file=$1
47 new_file="$temp_dir/$file"
48 jq -c '' < "$file" > "$new_file"
49 curl -s -X POST -H "content-type: application/json" -d "@$new_file" "$host/_opendistro/_alerting/destinations"
50 rm "$new_file"
51 echo " "
52 }
53
54 #return cluster_id if exist
55 check_monitor() {
56 name=$1
57 curl -s -H "content-type: application/json" \
58 --data "{\"query\":{\"match\":{ \"monitor.name\": \"$name\"}}}" \
59 "$host/_opendistro/_alerting/monitors/_search" | jq -r '.hits.hits[0]._id'
60 }
61
62 create_monitor() {
63 file=$1
64 dest_id=$2
65 new_file="$temp_dir/$file"
66 echo "create monitor"
67 jq -c ".triggers[0].actions[0].destination_id=\"$dest_id\"" "$file" > "$new_file"
68 curl -s -X POST -H "content-type: application/json" -d "@$new_file" "$host/_opendistro/_alerting/monitors/"
69 rm "$new_file"
70 echo " "
71 }
72
73 update_monitor() {
74 file=$1
75 dest_id=$2
76 monitor_id=$3
77 new_file="$temp_dir/$file"
78 echo "update monitor"
79 jq -c ".triggers[0].actions[0].destination_id=\"$dest_id\"" "$file" > "$new_file"
80 curl -s -X PUT -H "content-type: application/json" -d "@$new_file" "$host/_opendistro/_alerting/monitors/$monitor_id"
81 rm "$new_file"
82 echo " "
83 }
84
85 # handle the destination
86 handle_destination() {
87 dir=$dest_dir
88 mkdir -p "$temp_dir/$dir"
89 config_file=$dir/config.json
90 for k in $(jq ' keys | .[]' "$config_file"); do
91 file=$dir/$(jq -r ".[$k].file" "$config_file");
92
93 name=$(jq -r '.name' "$file")
94 id=$(check_destination "$name")
95 if [ -z "$id" ]; then
96 create_destination "$file"
97 else
98 update_destination "$file" "$id"
99 fi
100 done
101 }
102
103 # handle the monitors
104 handle_monitor() {
105 dir=$monitor_dir
106 mkdir -p "$temp_dir/$dir"
107 config_file=$dir/config.json
108 for k in $(jq ' keys | .[]' "$config_file"); do
109 file=$dir/$(jq -r ".[$k].file" "$config_file");
110 dest_name=$(jq -r ".[$k].destination" "$config_file");
111
112 dest_id=$(check_destination "$dest_name")
113 if [ -z "$dest_id" ]; then
114 echo "destination doesn't exist, skip this monitor"
115 continue
116 fi
117
118 name=$(jq -r '.name' "$file")
119 monitor_id=$(check_monitor "$name")
120 if [ "$monitor_id" = "null" ]; then
121 create_monitor "$file" "$dest_id"
122 else
123 update_monitor "$file" "$dest_id" "$monitor_id"
124 fi
125 done
126
127 }
128
129 handle_settings() {
130 dir=$setting_dir
131 mkdir -p "$temp_dir/$dir"
132 config_file=$dir/config.json
133 for k in $(jq ' keys | .[]' "$config_file"); do
134 file=$dir/$(jq -r ".[$k].file" "$config_file");
135 url=$(jq -r ".[$k].url" "$config_file");
136
137 new_file="$temp_dir/$file"
138 jq -c '' < "$file" > "$new_file"
139 curl -s -X POST -H "content-type: application/json" -d "@$new_file" "$host/$url"
140 rm "$new_file"
141 echo " "
142 done
143 }
144
145 if [ ! -z $setting_dir ]; then
146 echo "handle setting"
147 handle_settings
148 fi
149 if [ ! -z $dest_dir ]; then
150 echo "handle destination"
151 handle_destination
152 fi
153 if [ ! -z $monitor_dir ]; then
154 echo "handle monitor"
155 handle_monitor
156 fi
157
Hung-Wei Chiu40bf9bf2021-06-22 19:15:51 -0700158 trap cleanup INT TERM
159 sleep infinity