forked from paregupt/ucs_traffic_monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade_utm.sh
executable file
·291 lines (259 loc) · 11.6 KB
/
upgrade_utm.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/bin/bash
# Initial Version coded on 26-Jul-2020 by Paresh (with Kiara)
# Run this file from the
UTM_VERSION=5
UTM_DIR=/usr/local/telegraf
GRAFANA_IMG_DIR=/usr/share/grafana/public/img
GRAFANA_PLUGIN_DIR=/var/lib/grafana/plugins
# Grafana uid is globally unique. This is used for sharing and maintaining links between dashboards
# Dashboard id is unique per installation
# Folder id is used as per existing folder
declare -A utm_dashboard_arr
utm_dashboard_arr=( ["locations"]="ri2OFp4Wz" ["domain_overview"]="Inte2EIWk" ["domain_traffic"]="W7LSukHWz" ["chassis_traffic"]="KOM8ZHNWz" ["service_profile"]="Z0M_N1vWz" ["ingress_congestion"]="Sve32sDZk" ["chassis_pause"]="SVO-VNiWk" ["local_sys"]="9CXO3jTWz")
declare -A fid_arr
declare -A db_id_arr
date_suffix=$(date +"%M%H%m%d%y")
KIARA_BIRTH=1528354800
TODAY=$(date +%s)
DIFF=$(( $TODAY - $KIARA_BIRTH ))
KIARA_AGE=$(( $DIFF/( 60 * 60 * 24 * 365) ))
echo ""
echo "---------------------------"
echo "Hi there. I am Kiara."
echo "I am $KIARA_AGE years old and I can help you in upgrading your UTM installation."
echo "I learned it with my daddy while he was working on it."
echo "---------------------------"
echo ""
echo "---------------------------"
read -p "May I continue? (y/n):" -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Nn]$ ]]
then
echo "---------------------------"
echo "Bye - Kiara."
echo ""
exit
fi
if ! command -v jq &> /dev/null
then
echo "I need jq to proceed. Hint: yum install jq"
echo "---------------------------"
echo "Bye - Kiara."
echo ""
exit
fi
echo "---------------------------"
echo "I can upgrade the complete UTM app or just the UI dashboards."
read -p "What do you want me to do? (a(ll) for total upgrade, any other key to upgrade UI dashboards only):" -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Aa]$ ]] ; then
echo "---------------------------"
echo "Alright! Let's get started. Upgrading UTM will take just a few minutes."
echo "First, I need some new packages. I can download them if this machine can access the Internet"
echo ""
echo "If this machine can't reach the Internet, please:"
echo " 1. Download and install https://dl.grafana.com/oss/release/grafana-7.5.7-1.x86_64.rpm"
echo " 1.1. Earlier versions of Grafana will work with reduced functionality"
echo " 2. Download and install https://grafana.com/api/plugins/agenty-flowcharting-panel/versions/0.9.0/download"
echo " 2.1. Minimum required version 0.9"
echo " 3. Download and install https://grafana.com/api/plugins/michaeldmoore-multistat-panel/versions/1.7.1/download"
echo " 3.1. Minimum required version 1.4.1. UTM v0.6 onwards doesn't need Multistat panel"
echo " 4. Restart Grafana: systemctl restart grafana-server"
echo " 5. Download and install wget https://dl.influxdata.com/telegraf/releases/telegraf-1.18.3-1.x86_64.rpm"
echo " 6. Restart Telegraf: systemctl restart telegraf"
echo "---------------------------"
echo ""
read -p "May I access the Internet now? (y to keep me going, n to install them manually):" -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "---------------------------"
echo "Downloading and upgrading Grafana ..."
if wget https://dl.grafana.com/oss/release/grafana-7.5.7-1.x86_64.rpm ; then
if yum -y install grafana-7.5.7-1.x86_64.rpm ; then
echo "Grafana upgrade done"
else
echo "I could not upgrade Grafana"
fi
echo ""
else
echo "---------------------------"
echo "I could not make that work"
fi
echo "---------------------------"
echo "Downloading and upgrading Telegraf ..."
if wget https://dl.influxdata.com/telegraf/releases/telegraf-1.18.3-1.x86_64.rpm ; then
if yum -y localinstall telegraf-1.18.3-1.x86_64.rpm ; then
echo "Telegraf upgrade done"
systemctl restart telegraf
else
echo "I could not upgrade Telegraf"
fi
echo ""
else
echo "---------------------------"
echo "I could not make that work"
fi
echo "---------------------------"
echo "Downloading and upgrading other packages ..."
echo "---------------------------"
#if git clone https://github.com/algenty/grafana-flowcharting.git ; then
if grafana-cli plugins install agenty-flowcharting-panel ; then
echo "flowcharting upgrade done"
else
echo "---------------------------"
echo "I could not make that work"
fi
echo "---------------------------"
#if git clone https://github.com/michaeldmoore/michaeldmoore-multistat-panel.git ; then
if grafana-cli plugins install michaeldmoore-multistat-panel ; then
echo "multistat upgrade done"
else
echo "---------------------------"
echo "I could not make that work"
fi
fi
echo "---------------------------"
echo ""
sleep 2
echo "---------------------------"
echo "Upgrading UTM images ..."
sleep 2
mkdir -p $GRAFANA_IMG_DIR/utm
if cp images/* $GRAFANA_IMG_DIR/utm/ ; then
echo "Images upgrade done"
else
echo "I could not copy UTM images. You can move UTM images to $GRAFANA_IMG_DIR/utm/ later"
fi
echo "---------------------------"
echo " "
sleep 2
echo "---------------------------"
echo "Upgrading UTM receiver ..."
sleep 2
utm_old_ver=$(grep -Po '(?<=__version__ =).*' $UTM_DIR/ucs_traffic_monitor.py | awk -F'"' '{print $2}')
utm_new_ver=$(grep -Po '(?<=__version__ =).*' telegraf/ucs_traffic_monitor.py | awk -F'"' '{print $2}')
echo "Your existing UTM version is $utm_old_ver"
echo "First, let me take a backup ..."
sleep 2
if cp $UTM_DIR/ucs_traffic_monitor.py $UTM_DIR/ucs_traffic_monitor_$utm_old_ver.py ; then
echo "Done: $UTM_DIR/ucs_traffic_monitor_$utm_old_ver.py"
else
echo "I could not take a backup of your existing UTM receiver. Still trying to continue..."
fi
echo "Upgrading UTM receiver ..."
sleep 2
if cp telegraf/ucs_traffic_monitor.py $UTM_DIR/ucs_traffic_monitor.py ; then
echo "Your UTM receiver is now upgraded to version $utm_new_ver"
else
echo "---------------------------"
echo "I could not upgrade the UTM receiver."
fi
echo "---------------------------"
echo " "
sleep 2
echo "---------------------------"
echo "In the earlier releases of UTM, disable_sanitize_html flag was enabled. It is not required anymore."
echo "I am turning this off."
sleep 2
sed -i '/^disable_sanitize_html*/s/^/;/' /etc/grafana/grafana.ini
echo "Done"
echo "---------------------------"
echo " "
sleep 2
fi
read -p "Are you ready to upgrade UI dashboards: (y/n)" -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]] ; then
echo "---------------------------"
echo "I need Grafana Credentials."
echo "Hint:admin/Utm_12345"
echo "---------------------------"
testLoginValue="false"
while [[ "${testLoginValue}" == "false" ]]; do
unset GRAFANA_USER
unset GRAFANA_PASSWORD
while [ -z "$GRAFANA_USER" ]; do
read -p "Grafana User:" GRAFANA_USER
done
while [ -z "$GRAFANA_PASSWORD" ]; do
read -s -p "Password:" GRAFANA_PASSWORD
done
echo " "
auth_msg=$(curl -s -X GET -H "Accept: application/json" -H "Content-Type: application/json" http://$GRAFANA_USER:$GRAFANA_PASSWORD@localhost:3000/api/org | jq '.message')
if [[ "$auth_msg" == *"Invalid"* ]]; then
echo "$auth_msg"
echo "Please try again"
else
testLoginValue="true"
echo ""
fi
done
echo "---------------------------"
echo "First, let's backup your existing UTM dashboards"
sleep 2
mkdir -p $UTM_DIR/grafana/dashboards_${date_suffix}
for item in "${!utm_dashboard_arr[@]}" ; do
curl -s -X GET -H "Accept: application/json" -H "Content-Type: application/json" http://$GRAFANA_USER:$GRAFANA_PASSWORD@localhost:3000/api/dashboards/uid/${utm_dashboard_arr[$item]} > $UTM_DIR/grafana/dashboards_${date_suffix}/${item}.json
fid_arr+=( ["${item}"]="$(jq -r '.meta.folderId' $UTM_DIR/grafana/dashboards_${date_suffix}/${item}.json)" )
db_id_arr+=( ["${item}"]="$(jq -r '.dashboard.id' $UTM_DIR/grafana/dashboards_${date_suffix}/${item}.json)" )
done
#for key in ${!fid_arr[@]}; do
# echo ${key} ${fid_arr[${key}]}
#done
echo "I have taken a backup of your existing UTM dashboards in $UTM_DIR/grafana/dashboards_${date_suffix}"
echo "---------------------------"
sleep 2
#Add at the beginning of file: sed -i '1 i\{ "dashboard":' 1.json
#Add at the end of the file: sed -i -e '$a,\n"folderId":1,\n"overwrite":true\n}'
#Delete a key: jq 'del(.meta)' json
#Update key value jq '.dashboard.version = 4' json
#Add keys jq '. + { "folderId": 1, "overwrite":true }'
# All together jq 'del(.meta)' input.json | jq '.dashboard.version = 4' | jq '. + { "folderId": 1, "overwrite":true }' > 1.json
echo "Now, I will try to upgrade UTM dashboards. "
read -p "Are you sure: (y/n)" -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]] ; then
locations_uid=${utm_dashboard_arr["locations"]}
locations_id=1
#for i in $(ls grafana/dashboards/*.json)
for item in "${!utm_dashboard_arr[@]}" ; do
cp grafana/dashboards/${item}.json /tmp/${item}.json
# Use the same folderId and id from the existing dashboards
fid=${fid_arr[${item}]}
db_id=${db_id_arr[${item}]}
printf 'Upgrading %-25s -- in Folder %-3s -- with ID %-3s -- to UTM version %-3s\n' "$item" "$fid" "$db_id" "$UTM_VERSION"
# update UTM version
jq '{"dashboard": .}' /tmp/${item}.json > /tmp/${item}_1.json
jq ".dashboard.version = $UTM_VERSION" /tmp/${item}_1.json > /tmp/${item}.json
# use the existing dashboard id
jq ".dashboard.id = $db_id" /tmp/${item}.json > /tmp/${item}_1.json
# add folderId and overwrite flag
jq ". + { "folderId": $fid, "overwrite":true }" /tmp/${item}_1.json > /tmp/${item}.json
location_db_id=$(curl -s -X POST -H "Accept: application/json" -H "Content-Type: application/json" -d @/tmp/${item}.json http://$GRAFANA_USER:$GRAFANA_PASSWORD@localhost:3000/api/dashboards/db | jq -r '. | select(.. | .uid? == '\"$locations_uid\"').id')
if [ -z "$location_db_id" ] ; then
echo "."
else
echo "."
locations_id=$location_db_id
fi
done
echo "Done"
echo ""
echo "UTM dashboards upgraded"
echo "---------------------------"
sleep 2
echo "Update home dashboard to ID $locations_id ..."
curl -s -X PUT -H "Accept: application/json" -H "Content-Type: application/json" -d "{\"theme\":\"\",\"homeDashboardId\":$locations_id,\"timezone\":\"\"}" http://$GRAFANA_USER:$GRAFANA_PASSWORD@localhost:3000/api/user/preferences
echo ""
sleep 2
echo "That is all."
fi
fi
echo "---------------------------"
echo "..."
systemctl restart grafana-server
echo "---------------------------"
echo "Bye - Kiara."
echo "---------------------------"
sleep 1