Hung-Wei Chiu | 1887b93 | 2021-04-05 16:59:28 -0700 | [diff] [blame] | 1 | # Copyright 2021-present Open Networking Foundation |
Scott Baker | 4cad5ee | 2022-06-09 14:17:35 -0700 | [diff] [blame] | 2 | # SPDX-License-Identifier: Apache-2.0 |
Hung-Wei Chiu | 1887b93 | 2021-04-05 16:59:28 -0700 | [diff] [blame] | 3 | # |
| 4 | apiVersion: v1 |
| 5 | kind: ConfigMap |
| 6 | metadata: |
| 7 | name: "{{ .Release.Name }}-scripts" |
| 8 | labels: |
| 9 | app: onos |
| 10 | chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" |
| 11 | release: "{{ .Release.Name }}" |
| 12 | heritage: "{{ .Release.Service }}" |
| 13 | data: |
| 14 | update.sh: | |
| 15 | #!/bin/bash |
| 16 | host=${ES_URL:-localhost:9201} |
Hung-Wei Chiu | 8676d08 | 2022-02-24 14:11:03 -0800 | [diff] [blame] | 17 | skip_tls=${SKIP_TLS:true} |
| 18 | auth_username=${AUTH_USERNAME} |
| 19 | auth_password=${AUTH_PASSWORD} |
Hung-Wei Chiu | 1887b93 | 2021-04-05 16:59:28 -0700 | [diff] [blame] | 20 | dest_dir=${DEST_DIR} |
| 21 | monitor_dir=${MONITOR_DIR} |
| 22 | setting_dir=${SETTING_DIR} |
Hung-Wei Chiu | 2e439de | 2022-02-25 20:42:56 -0800 | [diff] [blame] | 23 | temp_dir=${OUTPUT_DIR:-/opensearch_temp} |
Hung-Wei Chiu | 1887b93 | 2021-04-05 16:59:28 -0700 | [diff] [blame] | 24 | |
Hung-Wei Chiu | 6f7633f | 2022-02-24 15:18:59 -0800 | [diff] [blame] | 25 | 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 Chiu | 40bf9bf | 2021-06-22 19:15:51 -0700 | [diff] [blame] | 34 | cleanup() { |
| 35 | echo "Cleaning up..." |
| 36 | exit |
| 37 | } |
| 38 | |
Hung-Wei Chiu | 1887b93 | 2021-04-05 16:59:28 -0700 | [diff] [blame] | 39 | #return destination id if exist |
| 40 | check_destination() { name=$1 |
Hung-Wei Chiu | d7fd4d3 | 2022-02-25 21:53:47 -0800 | [diff] [blame] | 41 | curl -s $curl_opts -H "content-type: application/json" "$host/_plugins/_alerting/destinations" | jq -r ".destinations[] | select(.name == \"$name\").id" |
Hung-Wei Chiu | 1887b93 | 2021-04-05 16:59:28 -0700 | [diff] [blame] | 42 | } |
| 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 Chiu | d7fd4d3 | 2022-02-25 21:53:47 -0800 | [diff] [blame] | 51 | curl -s $curl_opts -X PUT -H "content-type: application/json" -d "@$new_file" "$host/_plugins/_alerting/destinations/$id" |
Hung-Wei Chiu | 1887b93 | 2021-04-05 16:59:28 -0700 | [diff] [blame] | 52 | 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 Chiu | d7fd4d3 | 2022-02-25 21:53:47 -0800 | [diff] [blame] | 61 | curl -s $curl_opts -X POST -H "content-type: application/json" -d "@$new_file" "$host/_plugins/_alerting/destinations" |
Hung-Wei Chiu | 1887b93 | 2021-04-05 16:59:28 -0700 | [diff] [blame] | 62 | rm "$new_file" |
| 63 | echo " " |
| 64 | } |
| 65 | |
| 66 | #return cluster_id if exist |
| 67 | check_monitor() { |
| 68 | name=$1 |
Hung-Wei Chiu | 6f7633f | 2022-02-24 15:18:59 -0800 | [diff] [blame] | 69 | curl -s $curl_opts -H "content-type: application/json" \ |
Hung-Wei Chiu | 1887b93 | 2021-04-05 16:59:28 -0700 | [diff] [blame] | 70 | --data "{\"query\":{\"match\":{ \"monitor.name\": \"$name\"}}}" \ |
Hung-Wei Chiu | d7fd4d3 | 2022-02-25 21:53:47 -0800 | [diff] [blame] | 71 | "$host/_plugins/_alerting/monitors/_search" | jq -r '.hits.hits[0]._id' |
Hung-Wei Chiu | 1887b93 | 2021-04-05 16:59:28 -0700 | [diff] [blame] | 72 | } |
| 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 Chiu | d7fd4d3 | 2022-02-25 21:53:47 -0800 | [diff] [blame] | 80 | curl -s $curl_opts -X POST -H "content-type: application/json" -d "@$new_file" "$host/_plugins/_alerting/monitors/" |
Hung-Wei Chiu | 1887b93 | 2021-04-05 16:59:28 -0700 | [diff] [blame] | 81 | 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 Chiu | d7fd4d3 | 2022-02-25 21:53:47 -0800 | [diff] [blame] | 92 | curl -s $curl_opts -X PUT -H "content-type: application/json" -d "@$new_file" "$host/_plugins/_alerting/monitors/$monitor_id" |
Hung-Wei Chiu | 1887b93 | 2021-04-05 16:59:28 -0700 | [diff] [blame] | 93 | 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 Chiu | b222050 | 2021-07-07 20:39:40 -0700 | [diff] [blame] | 123 | format=$(jq -r ".[$k].format" "$config_file"); |
Hung-Wei Chiu | 1887b93 | 2021-04-05 16:59:28 -0700 | [diff] [blame] | 124 | |
| 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 Chiu | b222050 | 2021-07-07 20:39:40 -0700 | [diff] [blame] | 132 | |
| 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 Chiu | 1887b93 | 2021-04-05 16:59:28 -0700 | [diff] [blame] | 136 | monitor_id=$(check_monitor "$name") |
| 137 | if [ "$monitor_id" = "null" ]; then |
Hung-Wei Chiu | b222050 | 2021-07-07 20:39:40 -0700 | [diff] [blame] | 138 | create_monitor "$FILENAME" "$dest_id" |
Hung-Wei Chiu | 1887b93 | 2021-04-05 16:59:28 -0700 | [diff] [blame] | 139 | else |
Hung-Wei Chiu | b222050 | 2021-07-07 20:39:40 -0700 | [diff] [blame] | 140 | update_monitor "$FILENAME" "$dest_id" "$monitor_id" |
Hung-Wei Chiu | 1887b93 | 2021-04-05 16:59:28 -0700 | [diff] [blame] | 141 | 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 Chiu | 8676d08 | 2022-02-24 14:11:03 -0800 | [diff] [blame] | 150 | |
Hung-Wei Chiu | 1887b93 | 2021-04-05 16:59:28 -0700 | [diff] [blame] | 151 | for k in $(jq ' keys | .[]' "$config_file"); do |
| 152 | file=$dir/$(jq -r ".[$k].file" "$config_file"); |
Hung-Wei Chiu | 8676d08 | 2022-02-24 14:11:03 -0800 | [diff] [blame] | 153 | type=$(jq -r ".[$k].type" "$config_file"); |
Hung-Wei Chiu | 1887b93 | 2021-04-05 16:59:28 -0700 | [diff] [blame] | 154 | url=$(jq -r ".[$k].url" "$config_file"); |
| 155 | |
| 156 | new_file="$temp_dir/$file" |
| 157 | jq -c '' < "$file" > "$new_file" |
Hung-Wei Chiu | 6f7633f | 2022-02-24 15:18:59 -0800 | [diff] [blame] | 158 | curl -s $curl_opts $curl_opts -X $type -H "content-type: application/json" -d "@$new_file" "$host/$url" |
Hung-Wei Chiu | 1887b93 | 2021-04-05 16:59:28 -0700 | [diff] [blame] | 159 | 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 Chiu | 40bf9bf | 2021-06-22 19:15:51 -0700 | [diff] [blame] | 177 | trap cleanup INT TERM |
| 178 | sleep infinity |