blob: e8ff43d7a489bc07a4bcdeac7cbafe1eade6b370 [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}
Hung-Wei Chiu8676d082022-02-24 14:11:03 -080017 skip_tls=${SKIP_TLS:true}
18 auth_username=${AUTH_USERNAME}
19 auth_password=${AUTH_PASSWORD}
Hung-Wei Chiu1887b932021-04-05 16:59:28 -070020 dest_dir=${DEST_DIR}
21 monitor_dir=${MONITOR_DIR}
22 setting_dir=${SETTING_DIR}
23 temp_dir=${OUTPUT_DIR:-/opendistro_temp}
24
Hung-Wei Chiu40bf9bf2021-06-22 19:15:51 -070025 cleanup() {
26 echo "Cleaning up..."
27 exit
28 }
29
Hung-Wei Chiu1887b932021-04-05 16:59:28 -070030 #return destination id if exist
31 check_destination() { name=$1
32 curl -s -H "content-type: application/json" "$host/_opendistro/_alerting/destinations" | jq -r ".destinations[] | select(.name == \"$name\").id"
33 }
34
35 update_destination() {
36 echo "update"
37 file=$1
38 id=$2
39 #compress the json file to reduce the http size
40 new_file="$temp_dir/$file"
41 jq -c '' < "$file" > "$new_file"
42 curl -s -X PUT -H "content-type: application/json" -d "@$new_file" "$host/_opendistro/_alerting/destinations/$id"
43 rm "$new_file"
44 echo " "
45 }
46
47 create_destination() {
48 echo "create"
49 file=$1
50 new_file="$temp_dir/$file"
51 jq -c '' < "$file" > "$new_file"
52 curl -s -X POST -H "content-type: application/json" -d "@$new_file" "$host/_opendistro/_alerting/destinations"
53 rm "$new_file"
54 echo " "
55 }
56
57 #return cluster_id if exist
58 check_monitor() {
59 name=$1
60 curl -s -H "content-type: application/json" \
61 --data "{\"query\":{\"match\":{ \"monitor.name\": \"$name\"}}}" \
62 "$host/_opendistro/_alerting/monitors/_search" | jq -r '.hits.hits[0]._id'
63 }
64
65 create_monitor() {
66 file=$1
67 dest_id=$2
68 new_file="$temp_dir/$file"
69 echo "create monitor"
70 jq -c ".triggers[0].actions[0].destination_id=\"$dest_id\"" "$file" > "$new_file"
71 curl -s -X POST -H "content-type: application/json" -d "@$new_file" "$host/_opendistro/_alerting/monitors/"
72 rm "$new_file"
73 echo " "
74 }
75
76 update_monitor() {
77 file=$1
78 dest_id=$2
79 monitor_id=$3
80 new_file="$temp_dir/$file"
81 echo "update monitor"
82 jq -c ".triggers[0].actions[0].destination_id=\"$dest_id\"" "$file" > "$new_file"
83 curl -s -X PUT -H "content-type: application/json" -d "@$new_file" "$host/_opendistro/_alerting/monitors/$monitor_id"
84 rm "$new_file"
85 echo " "
86 }
87
88 # handle the destination
89 handle_destination() {
90 dir=$dest_dir
91 mkdir -p "$temp_dir/$dir"
92 config_file=$dir/config.json
93 for k in $(jq ' keys | .[]' "$config_file"); do
94 file=$dir/$(jq -r ".[$k].file" "$config_file");
95
96 name=$(jq -r '.name' "$file")
97 id=$(check_destination "$name")
98 if [ -z "$id" ]; then
99 create_destination "$file"
100 else
101 update_destination "$file" "$id"
102 fi
103 done
104 }
105
106 # handle the monitors
107 handle_monitor() {
108 dir=$monitor_dir
109 mkdir -p "$temp_dir/$dir"
110 config_file=$dir/config.json
111 for k in $(jq ' keys | .[]' "$config_file"); do
112 file=$dir/$(jq -r ".[$k].file" "$config_file");
113 dest_name=$(jq -r ".[$k].destination" "$config_file");
Hung-Wei Chiub2220502021-07-07 20:39:40 -0700114 format=$(jq -r ".[$k].format" "$config_file");
Hung-Wei Chiu1887b932021-04-05 16:59:28 -0700115
116 dest_id=$(check_destination "$dest_name")
117 if [ -z "$dest_id" ]; then
118 echo "destination doesn't exist, skip this monitor"
119 continue
120 fi
121
122 name=$(jq -r '.name' "$file")
Hung-Wei Chiub2220502021-07-07 20:39:40 -0700123
124 ## Replace the slack output format
125 FILENAME=$(mktemp)
126 jq --arg slack "${format}" '.triggers[0].actions[0].message_template.source=$slack' "$file" > "$FILENAME"
Hung-Wei Chiu1887b932021-04-05 16:59:28 -0700127 monitor_id=$(check_monitor "$name")
128 if [ "$monitor_id" = "null" ]; then
Hung-Wei Chiub2220502021-07-07 20:39:40 -0700129 create_monitor "$FILENAME" "$dest_id"
Hung-Wei Chiu1887b932021-04-05 16:59:28 -0700130 else
Hung-Wei Chiub2220502021-07-07 20:39:40 -0700131 update_monitor "$FILENAME" "$dest_id" "$monitor_id"
Hung-Wei Chiu1887b932021-04-05 16:59:28 -0700132 fi
133 done
134
135 }
136
137 handle_settings() {
138 dir=$setting_dir
139 mkdir -p "$temp_dir/$dir"
140 config_file=$dir/config.json
Hung-Wei Chiu8676d082022-02-24 14:11:03 -0800141
142 curl_opts=""
143
144 if [ ! -z $auth_username ]; then
145 curl_opts="$curl_opts -u $auth_username:$auth_password"
146 fi
147
148 if [ $skip_tls ]; then
149 curl_opts="$curl_opts -k"
150 fi
151
152
Hung-Wei Chiu1887b932021-04-05 16:59:28 -0700153 for k in $(jq ' keys | .[]' "$config_file"); do
154 file=$dir/$(jq -r ".[$k].file" "$config_file");
Hung-Wei Chiu8676d082022-02-24 14:11:03 -0800155 type=$(jq -r ".[$k].type" "$config_file");
Hung-Wei Chiu1887b932021-04-05 16:59:28 -0700156 url=$(jq -r ".[$k].url" "$config_file");
157
158 new_file="$temp_dir/$file"
159 jq -c '' < "$file" > "$new_file"
Hung-Wei Chiu8676d082022-02-24 14:11:03 -0800160 curl -s $curl_opts -X $type -H "content-type: application/json" -d "@$new_file" "$host/$url"
Hung-Wei Chiu1887b932021-04-05 16:59:28 -0700161 rm "$new_file"
162 echo " "
163 done
164 }
165
166 if [ ! -z $setting_dir ]; then
167 echo "handle setting"
168 handle_settings
169 fi
170 if [ ! -z $dest_dir ]; then
171 echo "handle destination"
172 handle_destination
173 fi
174 if [ ! -z $monitor_dir ]; then
175 echo "handle monitor"
176 handle_monitor
177 fi
178
Hung-Wei Chiu40bf9bf2021-06-22 19:15:51 -0700179 trap cleanup INT TERM
180 sleep infinity