blob: f28c55a1a66f48bc4c86fa9e487d668e0e51e290 [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");
Hung-Wei Chiub2220502021-07-07 20:39:40 -0700111 format=$(jq -r ".[$k].format" "$config_file");
Hung-Wei Chiu1887b932021-04-05 16:59:28 -0700112
113 dest_id=$(check_destination "$dest_name")
114 if [ -z "$dest_id" ]; then
115 echo "destination doesn't exist, skip this monitor"
116 continue
117 fi
118
119 name=$(jq -r '.name' "$file")
Hung-Wei Chiub2220502021-07-07 20:39:40 -0700120
121 ## Replace the slack output format
122 FILENAME=$(mktemp)
123 jq --arg slack "${format}" '.triggers[0].actions[0].message_template.source=$slack' "$file" > "$FILENAME"
Hung-Wei Chiu1887b932021-04-05 16:59:28 -0700124 monitor_id=$(check_monitor "$name")
125 if [ "$monitor_id" = "null" ]; then
Hung-Wei Chiub2220502021-07-07 20:39:40 -0700126 create_monitor "$FILENAME" "$dest_id"
Hung-Wei Chiu1887b932021-04-05 16:59:28 -0700127 else
Hung-Wei Chiub2220502021-07-07 20:39:40 -0700128 update_monitor "$FILENAME" "$dest_id" "$monitor_id"
Hung-Wei Chiu1887b932021-04-05 16:59:28 -0700129 fi
130 done
131
132 }
133
134 handle_settings() {
135 dir=$setting_dir
136 mkdir -p "$temp_dir/$dir"
137 config_file=$dir/config.json
138 for k in $(jq ' keys | .[]' "$config_file"); do
139 file=$dir/$(jq -r ".[$k].file" "$config_file");
140 url=$(jq -r ".[$k].url" "$config_file");
141
142 new_file="$temp_dir/$file"
143 jq -c '' < "$file" > "$new_file"
144 curl -s -X POST -H "content-type: application/json" -d "@$new_file" "$host/$url"
145 rm "$new_file"
146 echo " "
147 done
148 }
149
150 if [ ! -z $setting_dir ]; then
151 echo "handle setting"
152 handle_settings
153 fi
154 if [ ! -z $dest_dir ]; then
155 echo "handle destination"
156 handle_destination
157 fi
158 if [ ! -z $monitor_dir ]; then
159 echo "handle monitor"
160 handle_monitor
161 fi
162
Hung-Wei Chiu40bf9bf2021-06-22 19:15:51 -0700163 trap cleanup INT TERM
164 sleep infinity