blob: d6db03b306797a2ef6141b91b1d7f1013797c8d8 [file] [log] [blame]
Hung-Wei Chiu1887b932021-04-05 16:59:28 -07001# Copyright 2021-present Open Networking Foundation
Scott Baker4cad5ee2022-06-09 14:17:35 -07002# SPDX-License-Identifier: Apache-2.0
Hung-Wei Chiu1887b932021-04-05 16:59:28 -07003#
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}
Hung-Wei Chiu2e439de2022-02-25 20:42:56 -080023 temp_dir=${OUTPUT_DIR:-/opensearch_temp}
Hung-Wei Chiu1887b932021-04-05 16:59:28 -070024
Hung-Wei Chiu6f7633f2022-02-24 15:18:59 -080025 curl_opts=""
26 if [ ! -z $auth_username ]; then
27 curl_opts="$curl_opts -u $auth_username:$auth_password"
28 fi
29
30 if [ $skip_tls ]; then
31 curl_opts="$curl_opts -k"
32 fi
33
Hung-Wei Chiu40bf9bf2021-06-22 19:15:51 -070034 cleanup() {
35 echo "Cleaning up..."
36 exit
37 }
38
Hung-Wei Chiu1887b932021-04-05 16:59:28 -070039 #return destination id if exist
40 check_destination() { name=$1
Hung-Wei Chiud7fd4d32022-02-25 21:53:47 -080041 curl -s $curl_opts -H "content-type: application/json" "$host/_plugins/_alerting/destinations" | jq -r ".destinations[] | select(.name == \"$name\").id"
Hung-Wei Chiu1887b932021-04-05 16:59:28 -070042 }
43
44 update_destination() {
45 echo "update"
46 file=$1
47 id=$2
48 #compress the json file to reduce the http size
49 new_file="$temp_dir/$file"
50 jq -c '' < "$file" > "$new_file"
Hung-Wei Chiud7fd4d32022-02-25 21:53:47 -080051 curl -s $curl_opts -X PUT -H "content-type: application/json" -d "@$new_file" "$host/_plugins/_alerting/destinations/$id"
Hung-Wei Chiu1887b932021-04-05 16:59:28 -070052 rm "$new_file"
53 echo " "
54 }
55
56 create_destination() {
57 echo "create"
58 file=$1
59 new_file="$temp_dir/$file"
60 jq -c '' < "$file" > "$new_file"
Hung-Wei Chiud7fd4d32022-02-25 21:53:47 -080061 curl -s $curl_opts -X POST -H "content-type: application/json" -d "@$new_file" "$host/_plugins/_alerting/destinations"
Hung-Wei Chiu1887b932021-04-05 16:59:28 -070062 rm "$new_file"
63 echo " "
64 }
65
66 #return cluster_id if exist
67 check_monitor() {
68 name=$1
Hung-Wei Chiu6f7633f2022-02-24 15:18:59 -080069 curl -s $curl_opts -H "content-type: application/json" \
Hung-Wei Chiu1887b932021-04-05 16:59:28 -070070 --data "{\"query\":{\"match\":{ \"monitor.name\": \"$name\"}}}" \
Hung-Wei Chiud7fd4d32022-02-25 21:53:47 -080071 "$host/_plugins/_alerting/monitors/_search" | jq -r '.hits.hits[0]._id'
Hung-Wei Chiu1887b932021-04-05 16:59:28 -070072 }
73
74 create_monitor() {
75 file=$1
76 dest_id=$2
77 new_file="$temp_dir/$file"
78 echo "create monitor"
79 jq -c ".triggers[0].actions[0].destination_id=\"$dest_id\"" "$file" > "$new_file"
Hung-Wei Chiud7fd4d32022-02-25 21:53:47 -080080 curl -s $curl_opts -X POST -H "content-type: application/json" -d "@$new_file" "$host/_plugins/_alerting/monitors/"
Hung-Wei Chiu1887b932021-04-05 16:59:28 -070081 rm "$new_file"
82 echo " "
83 }
84
85 update_monitor() {
86 file=$1
87 dest_id=$2
88 monitor_id=$3
89 new_file="$temp_dir/$file"
90 echo "update monitor"
91 jq -c ".triggers[0].actions[0].destination_id=\"$dest_id\"" "$file" > "$new_file"
Hung-Wei Chiud7fd4d32022-02-25 21:53:47 -080092 curl -s $curl_opts -X PUT -H "content-type: application/json" -d "@$new_file" "$host/_plugins/_alerting/monitors/$monitor_id"
Hung-Wei Chiu1887b932021-04-05 16:59:28 -070093 rm "$new_file"
94 echo " "
95 }
96
97 # handle the destination
98 handle_destination() {
99 dir=$dest_dir
100 mkdir -p "$temp_dir/$dir"
101 config_file=$dir/config.json
102 for k in $(jq ' keys | .[]' "$config_file"); do
103 file=$dir/$(jq -r ".[$k].file" "$config_file");
104
105 name=$(jq -r '.name' "$file")
106 id=$(check_destination "$name")
107 if [ -z "$id" ]; then
108 create_destination "$file"
109 else
110 update_destination "$file" "$id"
111 fi
112 done
113 }
114
115 # handle the monitors
116 handle_monitor() {
117 dir=$monitor_dir
118 mkdir -p "$temp_dir/$dir"
119 config_file=$dir/config.json
120 for k in $(jq ' keys | .[]' "$config_file"); do
121 file=$dir/$(jq -r ".[$k].file" "$config_file");
122 dest_name=$(jq -r ".[$k].destination" "$config_file");
Hung-Wei Chiub2220502021-07-07 20:39:40 -0700123 format=$(jq -r ".[$k].format" "$config_file");
Hung-Wei Chiu1887b932021-04-05 16:59:28 -0700124
125 dest_id=$(check_destination "$dest_name")
126 if [ -z "$dest_id" ]; then
127 echo "destination doesn't exist, skip this monitor"
128 continue
129 fi
130
131 name=$(jq -r '.name' "$file")
Hung-Wei Chiub2220502021-07-07 20:39:40 -0700132
133 ## Replace the slack output format
134 FILENAME=$(mktemp)
135 jq --arg slack "${format}" '.triggers[0].actions[0].message_template.source=$slack' "$file" > "$FILENAME"
Hung-Wei Chiu1887b932021-04-05 16:59:28 -0700136 monitor_id=$(check_monitor "$name")
137 if [ "$monitor_id" = "null" ]; then
Hung-Wei Chiub2220502021-07-07 20:39:40 -0700138 create_monitor "$FILENAME" "$dest_id"
Hung-Wei Chiu1887b932021-04-05 16:59:28 -0700139 else
Hung-Wei Chiub2220502021-07-07 20:39:40 -0700140 update_monitor "$FILENAME" "$dest_id" "$monitor_id"
Hung-Wei Chiu1887b932021-04-05 16:59:28 -0700141 fi
142 done
143
144 }
145
146 handle_settings() {
147 dir=$setting_dir
148 mkdir -p "$temp_dir/$dir"
149 config_file=$dir/config.json
Hung-Wei Chiu8676d082022-02-24 14:11:03 -0800150
Hung-Wei Chiu1887b932021-04-05 16:59:28 -0700151 for k in $(jq ' keys | .[]' "$config_file"); do
152 file=$dir/$(jq -r ".[$k].file" "$config_file");
Hung-Wei Chiu8676d082022-02-24 14:11:03 -0800153 type=$(jq -r ".[$k].type" "$config_file");
Hung-Wei Chiu1887b932021-04-05 16:59:28 -0700154 url=$(jq -r ".[$k].url" "$config_file");
155
156 new_file="$temp_dir/$file"
157 jq -c '' < "$file" > "$new_file"
Hung-Wei Chiu6f7633f2022-02-24 15:18:59 -0800158 curl -s $curl_opts $curl_opts -X $type -H "content-type: application/json" -d "@$new_file" "$host/$url"
Hung-Wei Chiu1887b932021-04-05 16:59:28 -0700159 rm "$new_file"
160 echo " "
161 done
162 }
163
164 if [ ! -z $setting_dir ]; then
165 echo "handle setting"
166 handle_settings
167 fi
168 if [ ! -z $dest_dir ]; then
169 echo "handle destination"
170 handle_destination
171 fi
172 if [ ! -z $monitor_dir ]; then
173 echo "handle monitor"
174 handle_monitor
175 fi
176
Hung-Wei Chiu40bf9bf2021-06-22 19:15:51 -0700177 trap cleanup INT TERM
178 sleep infinity