-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmm.sh
310 lines (241 loc) · 12.9 KB
/
pmm.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/bin/bash
# daniel.burgos@percona.com
#
clear
set -o pipefail
lockFile="/tmp/pmmsh.lock"
errorFile="/tmp/pmmsh.err"
logFile="/tmp/pmmsh.log"
scrapeyaml="scrape.yaml"
function logInfo (){
echo "[$(date +%y%m%d-%H:%M:%S)] $1" >> $logFile
}
function sendAlert () {
if [ -e "$errorFile" ]
then
alertMsg=$(cat $errorFile)
echo -e "${alertMsg}" #| mailx -s "[$HOSTNAME] ALERT " "${email}"
fi
}
function destructor () {
sendAlert
rm -f "$lockFile" "$errorFile"
}
# Setting TRAP in order to capture SIG and cleanup things
trap destructor EXIT INT TERM
function verifyExecution () {
local exitCode="$1"
local mustDie=${3-:"false"}
if [ $exitCode -ne "0" ]
then
msg="[ERROR] Failed execution. ${2}"
echo "$msg" >> ${errorFile}
if [ "$mustDie" == "true" ]; then
exit 1
else
return 1
fi
fi
return 0
}
function setLockFile () {
if [ -e "$lockFile" ]; then
trap - EXIT INT TERM
verifyExecution "1" "Script already running. $lockFile exists"
sendAlert
rm -f "$errorFile"
exit 2
else
touch "$lockFile"
fi
}
function installGum () {
which dpkg &> /dev/null
EXITSTATUS=$?
if [ $EXITSTATUS -eq 0 ]; then
curl -OL https://github.com/charmbracelet/gum/releases/download/v0.10.0/gum_0.10.0_amd64.deb
dpkg -i gum_0.10.0_amd64.deb
logInfo "[OK] Installed 'gum' bin"
return 0
fi
which rpm &> /dev/null
EXITSTATUS=$?
if [ $EXITSTATUS -eq 0 ]; then
curl -OL https://github.com/charmbracelet/gum/releases/download/v0.10.0/gum-0.10.0.x86_64.rpm
sudo rpm -i gum-0.10.0.x86_64.rpm
logInfo "[OK] Installed 'gum' bin"
return 0
fi
}
function installJq () {
which dpkg &> /dev/null
EXITSTATUS=$?
if [ $EXITSTATUS -eq 0 ]; then
sudo apt install -y jq
logInfo "[OK] Installed 'jq' bin"
return 0
fi
which rpm &> /dev/null
EXITSTATUS=$?
if [ $EXITSTATUS -eq 0 ]; then
sudo yum install -y jq
logInfo "[OK] Installed 'jq' bin"
return 0
fi
}
function verifyGum () {
which gum &> /dev/null
EXITSTATUS=$?
#verifyExecution "$EXITSTATUS" "Cannot find gum tool" false
if [ $EXITSTATUS -eq 0 ]; then
logInfo "[OK] Found 'gum' bin"
return 0
fi
installGum
}
function verifyJq () {
which jq &> /dev/null
EXITSTATUS=$?
#verifyExecution "$EXITSTATUS" "Cannot find gum tool" false
if [ $EXITSTATUS -eq 0 ]; then
logInfo "[OK] Found 'jq' bin"
return 0
fi
installJq
}
function installPMM () {
curl -L https://www.percona.com/get/pmm | /bin/bash
}
function pmmheader () {
echo '{{ Bold "Percona" }} {{ Italic "Labs" }} {{ Color "99" "0" " PMM CLI " }}' | gum format --theme=light -t template
I=$(gum style --padding "1 5" --border rounded --border-foreground 212 "PERCONA")
LOVE=$(gum style --padding "1 4" --border thick --border-foreground 57 "LABS")
BUBBLE=$(gum style --padding "1 8" --border normal --border-foreground 255 "PMM")
GUM=$(gum style --padding "1 5" --border double --border-foreground 240 "CLI")
I_LOVE=$(gum join "$I" "$LOVE")
BUBBLE_GUM=$(gum join "$BUBBLE" "$GUM")
gum join --align center --vertical "$I_LOVE" "$BUBBLE_GUM"
}
function getVersion () {
USER=$1
PASS=$2
URL=$3
VERVAL=$(curl -s -k -X 'GET' "https://${USER}:${PASS}@${URL}/v1/version" -H 'accept: application/json' | jq '.version' | awk -F. '{print $2}')
}
setLockFile
verifyGum
pmmheader
ACTION=$(gum choose "Install PMM" "Set a PMM replica" "Set a PMM Primary" Exit --cursor "> " --ordered --header="What would you like to do?" --cursor.foreground 99 --selected.foreground 99 --limit=1)
gum confirm "Confirms: $ACTION"
CONFIRMED=$?
if [ $CONFIRMED -eq 0 ]; then
if [[ $ACTION == "Install PMM" ]]; then
installPMM
fi
if [[ $ACTION == "Set a PMM replica" ]]; then
clear
echo "# PMM Replica" | gum format
echo "# Using VictoriaMetrics Federation" | gum format
echo ":candy:" | gum format -t emoji
PRIMARY=$(gum input --header="Host / IP of the Primary PMM server" --placeholder "192.168.1.10" --prompt.foreground="99" --cursor.foreground="99" --header.foreground="240")
PORT="443"
USER=$(gum input --header="Primary PMM USER" --value "admin" --placeholder "admin")
PASS=$(gum input --password --header="Primary PMM PASSWORD")
SCHEME="https"
clear
getVersion $USER $PASS $PRIMARY
echo '{{ Bold "A new scrap will be added" }}' | gum format -t template
# cat $scrapeyaml | gum format -t code -l yaml
export jobname=$JOBNAME
export scheme=$SCHEME
export user=$USER
export pass=$PASS
export host=$PRIMARY
export port=$PORT
envsubst < $scrapeyaml > /tmp/pepe
echo '{{ Bold "Creating: File with pmmserver IP/Host" }}' | gum format -t template
echo $PRIMARY > /tmp/pmmserver.txt
sudo docker cp -q /tmp/pmmserver.txt pmm-server:/srv/pmmserver.txt
echo '{{ Bold "Creating: Replica table on ClickHouse" }}' | gum format -t template
sudo docker cp -q metrics.sql pmm-server:/tmp/metrics.sql
sudo docker exec pmm-server chown pmm:pmm /tmp/metrics.sql
sudo docker exec pmm-server bash -c "clickhouse-client < /tmp/metrics.sql"
echo '{{ Bold "Creating: Scraper YAML file" }}' | gum format -t template
sudo docker cp -q /tmp/pepe pmm-server:/srv/prometheus/prometheus.base.yml
sudo docker exec pmm-server chown pmm:pmm /srv/prometheus/prometheus.base.yml
echo '{{ Bold "Creating: QAN and Inventory replica scripts" }}' | gum format -t template
sudo docker cp -q qanreplica.sh pmm-server:/srv/qanreplica.sh
sudo docker exec pmm-server chmod +x /srv/qanreplica.sh
sudo docker cp -q inventoryreplica.sh pmm-server:/srv/inventoryreplica.sh
sudo docker exec pmm-server chmod +x /srv/inventoryreplica.sh
echo '{{ Bold "Creating: QAN and Inventory supervisord ini files" }}' | gum format -t template
sudo docker cp -q qanreplica.ini pmm-server:/etc/supervisord.d/qanreplica.ini
sudo docker cp -q inventoryreplica.ini pmm-server:/etc/supervisord.d/inventoryreplica.ini
if [ $VERVAL -lt 40 ]; then
echo '{{ Bold "Creating: SQLite replica scripts" }}' | gum format -t template
sudo docker cp -q restoresqlitepmm.sh pmm-server:/srv/restoresqlitepmm.sh
sudo docker exec pmm-server chmod +x /srv/restoresqlitepmm.sh
sudo docker cp -q restoresqlitedash.sh pmm-server:/srv/restoresqlitedash.sh
sudo docker exec pmm-server chmod +x /srv/restoresqlitedash.sh
echo '{{ Bold "Creating: SQLite supervisord ini files" }}' | gum format -t template
sudo docker cp -q restoresqlitepmm.ini pmm-server:/etc/supervisord.d/restoresqlitepmm.ini
sudo docker cp -q restoresqlitedash.ini pmm-server:/etc/supervisord.d/restoresqlitedash.ini
fi
if [ $VERVAL -ge 40 ]; then
echo '{{ Bold "Creating: Grafana replica scripts" }}' | gum format -t template
sudo docker cp -q grafanareplica.sh pmm-server:/srv/grafanareplica.sh
sudo docker exec pmm-server chmod +x /srv/grafanareplica.sh
echo '{{ Bold "Creating: Grafana supervisord ini files" }}' | gum format -t template
sudo docker cp -q grafanareplica.ini pmm-server:/etc/supervisord.d/grafanareplica.ini
fi
gum spin --show-output --spinner monkey --title "Loading..." --title.foreground 99 -- sh -c 'sudo docker exec -d pmm-server supervisorctl restart pmm-agent &> /dev/null; sleep 5; echo "PMM Metrics Replica set"'
gum spin --show-output --spinner monkey --title "Loading..." --title.foreground 99 -- sh -c 'sudo docker exec -d pmm-server supervisorctl update &> /dev/null; sleep 5; echo "PMM QAN + Inventory Replica set"'
fi
if [[ $ACTION == "Set a PMM Primary" ]]; then
VERVAL=$(docker exec pmm-server pmm-admin -v | grep PMMVersion | awk -F: '{print $2}' | awk -F. '{print $2}')
echo $VERVAL
echo '{{ Bold "Creating: Inventory table on ClickHouse" }}' | gum format -t template
sudo docker cp -q pgpmm.sql pmm-server:/tmp/pgpmm.sql
sudo docker exec pmm-server chown pmm:pmm /tmp/pgpmm.sql
sudo docker exec pmm-server bash -c "clickhouse-client < /tmp/pgpmm.sql"
echo '{{ Bold "Creating: Inventory primary script" }}' | gum format -t template
sudo docker cp -q pmmpgdump.sh pmm-server:/srv/pmmpgdump.sh
sudo docker exec pmm-server chmod +x /srv/pmmpgdump.sh
echo '{{ Bold "Creating: Inventory supervisord ini files" }}' | gum format -t template
sudo docker cp -q pmmpgdump.ini pmm-server:/etc/supervisord.d/pmmpgdump.ini
if [ $VERVAL -lt 40 ]; then
echo '{{ Bold "Creating: sqlite tables on ClickHouse" }}' | gum format -t template
sudo docker cp -q sqlitepmm.sql pmm-server:/tmp/sqlitepmm.sql
sudo docker exec pmm-server chown pmm:pmm /tmp/sqlitepmm.sql
sudo docker exec pmm-server bash -c "clickhouse-client < /tmp/sqlitepmm.sql"
sudo docker cp -q sqlitedash.sql pmm-server:/tmp/sqlitedash.sql
sudo docker exec pmm-server chown pmm:pmm /tmp/sqlitedash.sql
sudo docker exec pmm-server bash -c "clickhouse-client < /tmp/sqlitedash.sql"
echo '{{ Bold "Creating: SQLite primary script" }}' | gum format -t template
sudo docker cp -q sqlitepmm.sh pmm-server:/srv/sqlitepmm.sh
sudo docker exec pmm-server chmod +x /srv/sqlitepmm.sh
sudo docker cp -q sqlitedash.sh pmm-server:/srv/sqlitedash.sh
sudo docker exec pmm-server chmod +x /srv/sqlitedash.sh
echo '{{ Bold "Creating: sqlite supervisord ini files" }}' | gum format -t template
sudo docker cp -q sqlitepmm.ini pmm-server:/etc/supervisord.d/sqlitepmm.ini
sudo docker cp -q sqlitedash.ini pmm-server:/etc/supervisord.d/sqlitedash.ini
fi
if [ $VERVAL -ge 40 ]; then
echo '{{ Bold "Creating: Grafana table on ClickHouse" }}' | gum format -t template
sudo docker cp -q pggrafana.sql pmm-server:/tmp/pggrafana.sql
sudo docker exec pmm-server chown pmm:pmm /tmp/pggrafana.sql
sudo docker exec pmm-server bash -c "clickhouse-client < /tmp/pggrafana.sql"
echo '{{ Bold "Creating: Grafana primary script" }}' | gum format -t template
sudo docker cp -q pmmgrafanadump.sh pmm-server:/srv/pmmgrafanadump.sh
sudo docker exec pmm-server chmod +x /srv/pmmgrafanadump.sh
echo '{{ Bold "Creating: grafana supervisord ini files" }}' | gum format -t template
sudo docker cp -q pmmgrafanadump.ini pmm-server:/etc/supervisord.d/pmmgrafanadump.ini
fi
echo '{{ Bold "Adding: ClickHouse Port Forwarding" }}' | gum format -t template
docker ps -a --format '{{.Names}}' | grep -q socatpmm
if [ $? -eq 1 ]; then
docker run --detach --restart unless-stopped --publish 9000:9000 --link pmm-server:target --name socatpmm alpine/socat tcp-listen:9000,fork,reuseaddr tcp-connect:target:9000
fi
gum spin --show-output --spinner monkey --title "Loading..." --title.foreground 99 -- sh -c 'sudo docker exec -d pmm-server supervisorctl update &> /dev/null; sleep 5; echo "PMM Primary Set"'
fi
fi