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