-
Notifications
You must be signed in to change notification settings - Fork 28
/
mysqlconfigurer.sh
666 lines (595 loc) · 27.7 KB
/
mysqlconfigurer.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
#!/bin/bash
# mysqlconfigurer.sh - Version 1.19.8.3
# (C) Releem, Inc 2022
# All rights reserved
export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
export EXTEND_TIMEOUT_USEC=18000000000
# Variables
MYSQLCONFIGURER_PATH="/opt/releem/conf/"
RELEEM_CONF_FILE="/opt/releem/releem.conf"
MYSQLCONFIGURER_FILE_NAME="z_aiops_mysql.cnf"
MYSQLTUNER_FILENAME=$MYSQLCONFIGURER_PATH"mysqltuner.pl"
MYSQLTUNER_REPORT=$MYSQLCONFIGURER_PATH"mysqltunerreport.json"
RELEEM_MYSQL_VERSION=$MYSQLCONFIGURER_PATH"mysql_version"
MYSQLCONFIGURER_CONFIGFILE="${MYSQLCONFIGURER_PATH}${MYSQLCONFIGURER_FILE_NAME}"
MYSQL_MEMORY_LIMIT=0
VERSION="1.19.8.3"
RELEEM_INSTALL_PATH=$MYSQLCONFIGURER_PATH"install.sh"
logfile="/var/log/releem-mysqlconfigurer.log"
# Set up a named pipe for logging
npipe=/tmp/$$.mysqlconfigurer.tmp
mknod $npipe p
# Log all output to a log for error checking
tee <$npipe $logfile &
exec 1>&-
exec 1>$npipe 2>&1
function on_exit() {
curl -s -L -d @$logfile -H "x-releem-api-key: $RELEEM_API_KEY" -H "Content-Type: application/json" -X POST https://api.releem.com/v2/events/configurer_log
rm -f $npipe
}
trap on_exit EXIT
function update_agent() {
trap - EXIT
/opt/releem/releem-agent start > /dev/null || true
NEW_VER=$(curl -s -L https://releem.s3.amazonaws.com/v2/current_version_agent)
if [ "$NEW_VER" != "$VERSION" ]; then
if [ "$(printf '%s\n' "$NEW_VER" "$VERSION" | sort -V | head -n1)" = "$VERSION" ];
then
printf "\033[37m\n * Updating script \e[31;1m%s\e[0m -> \e[32;1m%s\e[0m\n" "$VERSION" "$NEW_VER"
curl -s -L https://releem.s3.amazonaws.com/v2/install.sh > "$RELEEM_INSTALL_PATH"
RELEEM_API_KEY=$RELEEM_API_KEY exec bash "$RELEEM_INSTALL_PATH" -u
/opt/releem/releem-agent --event=agent_updated > /dev/null
fi
fi
}
function non_blocking_wait() {
PID=$1
if [ ! -d "/proc/$PID" ]; then
wait $PID
CODE=$?
else
CODE=150
fi
return $CODE
}
function wait_restart() {
sleep 1
flag=0
spin[0]="-"
spin[1]="\\"
spin[2]="|"
spin[3]="/"
printf "\033[37m\n Waiting for mysql service to start 1200 seconds ${spin[0]}"
while /bin/true; do
PID=$1
non_blocking_wait $PID
CODE=$?
if [ $CODE -ne 150 ]; then
printf "\033[0m\n PID $PID terminated with exit code $CODE"
if [ $CODE -eq 0 ]; then
RETURN_CODE=0
else
RETURN_CODE=7
fi
break
fi
flag=$(($flag + 1))
if [ $flag == 1200 ]; then
RETURN_CODE=6
break
fi
i=`expr $flag % 4`
printf "\b${spin[$i]}"
sleep 1
done
printf "\033[0m\n"
return $RETURN_CODE
}
function check_mysql_version() {
if [ -f $MYSQLTUNER_REPORT ]; then
mysql_version=$(grep -o '"Version":"[^"]*' $MYSQLTUNER_REPORT | grep -o '[^"]*$')
elif [ -f "$RELEEM_MYSQL_VERSION" ]; then
mysql_version=$(cat $RELEEM_MYSQL_VERSION)
else
printf "\033[37m\n * Please try again later or run Releem Agent manually:\033[0m"
printf "\033[32m\n /opt/releem/releem-agent -f \033[0m\n\n"
exit 1;
fi
if [ -z $mysql_version ]; then
printf "\033[37m\n * Please try again later or run Releem Agent manually:\033[0m"
printf "\033[32m\n /opt/releem/releem-agent -f \033[0m\n\n"
exit 1;
fi
requiredver="5.6.8"
if [ "$(printf '%s\n' "$mysql_version" "$requiredver" | sort -V | head -n1)" = "$requiredver" ]; then
return 0
else
return 1
fi
}
function releem_rollback_config() {
printf "\033[31m\n * Rolling back MySQL configuration!\033[0m\n"
if ! check_mysql_version; then
printf "\033[31m\n * MySQL version is lower than 5.6.7. Check the documentation https://github.com/Releem/mysqlconfigurer#how-to-apply-the-recommended-configuration for applying the configuration. \033[0m\n"
exit 2
fi
if [ -z "$RELEEM_MYSQL_CONFIG_DIR" -o ! -d "$RELEEM_MYSQL_CONFIG_DIR" ]; then
printf "\033[37m\n * MySQL configuration directory is not found.\033[0m"
printf "\033[37m\n * Try to reinstall Releem Agent, and please set the my.cnf location.\033[0m"
exit 3;
fi
FLAG_RESTART_SERVICE=1
if [ -z "$RELEEM_RESTART_SERVICE" ]; then
read -p "Please confirm restart MySQL service? (Y/N) " -n 1 -r
echo # move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
printf "\033[37m\n * A confirmation to restart the service has not been received. Releem recommended configuration has not been roll back.\033[0m\n"
FLAG_RESTART_SERVICE=0
fi
elif [ "$RELEEM_RESTART_SERVICE" -eq 0 ]; then
FLAG_RESTART_SERVICE=0
fi
if [ "$FLAG_RESTART_SERVICE" -eq 0 ]; then
exit 5
fi
printf "\033[31m\n * Deleting a configuration file... \033[0m\n"
rm -rf $RELEEM_MYSQL_CONFIG_DIR/$MYSQLCONFIGURER_FILE_NAME
#echo "----Test config-------"
if [ -f "${MYSQLCONFIGURER_PATH}${MYSQLCONFIGURER_FILE_NAME}.bkp" ]; then
printf "\033[31m\n * Restoring a backup copy of the configuration file ${MYSQLCONFIGURER_PATH}${MYSQLCONFIGURER_FILE_NAME}.bkp... \033[0m\n"
cp -f "${MYSQLCONFIGURER_PATH}${MYSQLCONFIGURER_FILE_NAME}.bkp" "${RELEEM_MYSQL_CONFIG_DIR}/${MYSQLCONFIGURER_FILE_NAME}"
fi
if [ -z "$RELEEM_MYSQL_RESTART_SERVICE" ]; then
printf "\033[37m\n * The command to restart the MySQL service was not found. Try to reinstall Releem Agent.\033[0m"
exit 4;
fi
printf "\033[31m\n * Restarting with command '$RELEEM_MYSQL_RESTART_SERVICE'...\033[0m\n"
eval "$RELEEM_MYSQL_RESTART_SERVICE" &
wait_restart $!
RESTART_CODE=$?
#if [[ $($mysqladmincmd ${connection_string} --user=${MYSQL_LOGIN} --password=${MYSQL_PASSWORD} ping 2>/dev/null || true) == "mysqld is alive" ]];
if [ $RESTART_CODE -eq 0 ];
then
printf "\n`date +%Y%m%d-%H:%M:%S`\033[32m The MySQL service restarted successfully!\033[0m\n"
rm -f "${MYSQLCONFIGURER_PATH}${MYSQLCONFIGURER_FILE_NAME}.bkp"
elif [ $RESTART_CODE -eq 6 ];
then
printf "\n`date +%Y%m%d-%H:%M:%S`\033[31m The MySQL service failed to restart in 1200 seconds! Check the MySQL error log! \033[0m\n"
elif [ $RESTART_CODE -eq 7 ];
then
printf "\n`date +%Y%m%d-%H:%M:%S`\033[31m The MySQL service failed to restart with error! Check the MySQL error log! \033[0m\n"
fi
/opt/releem/releem-agent --event=config_rollback > /dev/null
exit "${RESTART_CODE}"
}
function releem_ps_mysql() {
FLAG_CONFIGURE=1
status_ps=$($mysqlcmd ${connection_string} --user=${MYSQL_LOGIN} --password=${MYSQL_PASSWORD} -BNe "show global variables like 'performance_schema'" 2>/dev/null | awk '{print $2}')
if [ "$status_ps" != "ON" ]; then
FLAG_CONFIGURE=0
fi
status_slowlog=$($mysqlcmd ${connection_string} --user=${MYSQL_LOGIN} --password=${MYSQL_PASSWORD} -BNe "show global variables like 'slow_query_log'" 2>/dev/null | awk '{print $2}')
if [ "$status_slowlog" != "ON" ]; then
FLAG_CONFIGURE=0
fi
if [ -n "$RELEEM_MYSQL_CONFIG_DIR" -a -d "$RELEEM_MYSQL_CONFIG_DIR" ]; then
printf "\033[37m\n * Enabling Performance schema and SlowLog to collect metrics...\n\033[0m\n"
echo "### This configuration was recommended by Releem. https://releem.com" | $sudo_cmd tee "$RELEEM_MYSQL_CONFIG_DIR/collect_metrics.cnf" >/dev/null
echo "[mysqld]" | $sudo_cmd tee -a "$RELEEM_MYSQL_CONFIG_DIR/collect_metrics.cnf" >/dev/null
echo "performance_schema = 1" | $sudo_cmd tee -a "$RELEEM_MYSQL_CONFIG_DIR/collect_metrics.cnf" >/dev/null
echo "slow_query_log = 1" | $sudo_cmd tee -a "$RELEEM_MYSQL_CONFIG_DIR/collect_metrics.cnf" >/dev/null
if [ -n "$RELEEM_QUERY_OPTIMIZATION" -a "$RELEEM_QUERY_OPTIMIZATION" = true ]; then
performance_schema_history_size=$($mysqlcmd ${connection_string} --user=${MYSQL_LOGIN} --password=${MYSQL_PASSWORD} -BNe "show global variables like 'performance_schema_events_statements_history_size'" 2>/dev/null | awk '{print $2}')
if [ "$performance_schema_history_size" != "500" ]; then
FLAG_CONFIGURE=0
fi
echo "performance-schema-consumer-events-statements-history = ON" | $sudo_cmd tee -a "$RELEEM_MYSQL_CONFIG_DIR/collect_metrics.cnf" >/dev/null
echo "performance-schema-consumer-events-statements-current = ON" | $sudo_cmd tee -a "$RELEEM_MYSQL_CONFIG_DIR/collect_metrics.cnf" >/dev/null
echo "performance_schema_events_statements_history_size = 500" | $sudo_cmd tee -a "$RELEEM_MYSQL_CONFIG_DIR/collect_metrics.cnf" >/dev/null
fi
chmod 644 $RELEEM_MYSQL_CONFIG_DIR/collect_metrics.cnf
else
printf "\033[31m\n MySQL configuration directory is not found.\033[0m"
printf "\033[31m\n Try to reinstall Releem Agent.\033[0m"
exit 3;
fi
if [ "$FLAG_CONFIGURE" -eq 1 ]; then
printf "\033[37m\n * Performance schema and SlowLog are enabled for metrics collection.\033[0m\n"
exit 0
fi
printf "\033[37m To apply changes to the mysql configuration, you need to restart the service\n\033[0m\n"
FLAG_RESTART_SERVICE=1
if [ -z "$RELEEM_RESTART_SERVICE" ]; then
read -p "Please confirm restart MySQL service? (Y/N) " -n 1 -r
echo # move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
printf "\033[31m A confirmation to restart the service has not been received. \033[0m\n"
FLAG_RESTART_SERVICE=0
fi
elif [ "$RELEEM_RESTART_SERVICE" -eq 0 ]; then
FLAG_RESTART_SERVICE=0
fi
if [ "$FLAG_RESTART_SERVICE" -eq 0 ]; then
printf "\033[31m\n * For appling change in configuration mysql need restart service.\n\033[0m"
printf "\033[31m Run the command \`bash /opt/releem/mysqlconfigurer.sh -p\` when it is possible to restart the service.\033[0m\n"
exit 0
fi
#echo "-------Test config-------"
printf "\033[37m Restarting service with command '$RELEEM_MYSQL_RESTART_SERVICE'...\033[0m\n"
eval "$RELEEM_MYSQL_RESTART_SERVICE" &
wait_restart $!
RESTART_CODE=$?
#if [[ $($mysqladmincmd ${connection_string} --user=${MYSQL_LOGIN} --password=${MYSQL_PASSWORD} ping 2>/dev/null || true) == "mysqld is alive" ]];
if [ $RESTART_CODE -eq 0 ];
then
printf "\033[32m\n The MySQL service restarted successfully!\033[0m\n"
printf "\033[32m\n Performance schema and Slow Log are enabled.\033[0m\n"
elif [ $RESTART_CODE -eq 6 ];
then
printf "\033[31m\n The MySQL service failed to restart in 1200 seconds! Check the MySQL error log!\033[0m\n"
elif [ $RESTART_CODE -eq 7 ];
then
printf "\n`date +%Y%m%d-%H:%M:%S`\033[31m The MySQL service failed to restart with error! Check the MySQL error log! \033[0m\n"
fi
$sudo_cmd /opt/releem/releem-agent -f
exit "${RESTART_CODE}"
}
function releem_apply_config() {
if [ "$1" == "auto" ];
then
releem_apply_auto
elif [ "$1" == "automatic" ];
then
releem_apply_automatic
else
releem_apply_manual
fi
}
function releem_apply_auto() {
/opt/releem/releem-agent --task=apply_config > /dev/null
printf "\n`date +%Y%m%d-%H:%M:%S`\033[32m Request to create a task to apply the configuration was sended!\033[0m\n"
exit 0
}
function releem_apply_manual() {
printf "\n`date +%Y%m%d-%H:%M:%S`\033[37m Applying the recommended MySQL configuration...\033[0m\n"
printf "\n`date +%Y%m%d-%H:%M:%S`\033[37m Getting the latest up-to-date configuration...\033[0m\n"
/opt/releem/releem-agent -c >/dev/null 2>&1 || true
if [ ! -f $MYSQLCONFIGURER_CONFIGFILE ]; then
printf "\033[37m\n * Recommended MySQL configuration is not found.\033[0m"
printf "\033[37m\n * Please apply recommended configuration later or run Releem Agent manually:\033[0m"
printf "\033[32m\n /opt/releem/releem-agent -f \033[0m\n\n"
exit 1;
fi
if ! check_mysql_version; then
printf "\033[31m\n * MySQL version is lower than 5.6.7. Check the documentation https://github.com/Releem/mysqlconfigurer#how-to-apply-the-recommended-configuration for applying the configuration. \033[0m\n"
exit 2
fi
if [ -z "$RELEEM_MYSQL_CONFIG_DIR" -o ! -d "$RELEEM_MYSQL_CONFIG_DIR" ]; then
printf "\033[37m\n * MySQL configuration directory is not found.\033[0m"
printf "\033[37m\n * Try to reinstall Releem Agent, and please set the my.cnf location.\033[0m"
exit 3;
fi
diff_cmd=$(which diff || true)
if [ -n "$diff_cmd" ];then
diff "${RELEEM_MYSQL_CONFIG_DIR}/${MYSQLCONFIGURER_FILE_NAME}" "$MYSQLCONFIGURER_CONFIGFILE" > /dev/null 2>&1
retVal=$?
if [ $retVal -eq 0 ];
then
printf "\n`date +%Y%m%d-%H:%M:%S`\033[32m The new configuration does not differ from the current one applied. No restart is required.!\033[0m\n"
exit 0
fi
fi
FLAG_RESTART_SERVICE=1
if [ -z "$RELEEM_RESTART_SERVICE" ]; then
read -p "Please confirm the MySQL service restart? (Y/N) " -n 1 -r
echo # move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
printf "\n`date +%Y%m%d-%H:%M:%S`\033[37m A confirmation to restart the service has not been received. Releem recommended configuration has not been applied.\033[0m\n"
FLAG_RESTART_SERVICE=0
fi
elif [ "$RELEEM_RESTART_SERVICE" -eq 0 ]; then
FLAG_RESTART_SERVICE=0
fi
if [ "$FLAG_RESTART_SERVICE" -eq 0 ]; then
exit 5
fi
printf "\n`date +%Y%m%d-%H:%M:%S`\033[37m Copying file $MYSQLCONFIGURER_CONFIGFILE to directory $RELEEM_MYSQL_CONFIG_DIR/...\033[0m\n"
if [ ! -f "${MYSQLCONFIGURER_PATH}${MYSQLCONFIGURER_FILE_NAME}.bkp" ]; then
yes | cp -f "${RELEEM_MYSQL_CONFIG_DIR}/${MYSQLCONFIGURER_FILE_NAME}" "${MYSQLCONFIGURER_PATH}${MYSQLCONFIGURER_FILE_NAME}.bkp"
fi
yes | cp -fr $MYSQLCONFIGURER_CONFIGFILE $RELEEM_MYSQL_CONFIG_DIR/
chmod 644 $RELEEM_MYSQL_CONFIG_DIR/*
if [ -z "$RELEEM_MYSQL_RESTART_SERVICE" ]; then
printf "\033[37m\n * The command to restart the MySQL service was not found. Try to reinstall Releem Agent.\033[0m"
exit 4;
fi
#echo "-------Test config-------"
printf "\n`date +%Y%m%d-%H:%M:%S`\033[37m Restarting MySQL with the command '$RELEEM_MYSQL_RESTART_SERVICE'...\033[0m\n"
eval "$RELEEM_MYSQL_RESTART_SERVICE" &
wait_restart $!
RESTART_CODE=$?
if [ $RESTART_CODE -eq 0 ];
then
printf "\n`date +%Y%m%d-%H:%M:%S`\033[32m The MySQL service restarted successfully!\033[0m\n"
printf "\n`date +%Y%m%d-%H:%M:%S`\033[32m Recommended configuration applied successfully!\033[0m\n"
printf "\n`date +%Y%m%d-%H:%M:%S` Releem Score and Unapplied recommendations in the Releem Dashboard will be updated in a few minutes.\n"
rm -f "${MYSQLCONFIGURER_PATH}${MYSQLCONFIGURER_FILE_NAME}.bkp"
elif [ $RESTART_CODE -eq 6 ];
then
printf "\n`date +%Y%m%d-%H:%M:%S`\033[31m MySQL service failed to restart in 1200 seconds! \033[0m\n"
printf "\n`date +%Y%m%d-%H:%M:%S`\033[31m Wait for the MySQL service to start and Check the MySQL error log!\033[0m\n"
printf "\n`date +%Y%m%d-%H:%M:%S`\033[31m Try to roll back the configuration application using the command: \033[0m\n"
printf "\n`date +%Y%m%d-%H:%M:%S`\033[32m bash /opt/releem/mysqlconfigurer.sh -r\033[0m\n\n"
elif [ $RESTART_CODE -eq 7 ];
then
printf "\n`date +%Y%m%d-%H:%M:%S`\033[31m MySQL service failed to restart! Check the MySQL error log! \033[0m\n"
printf "\n`date +%Y%m%d-%H:%M:%S`\033[31m Try to roll back the configuration application using the command: \033[0m\n"
printf "\n`date +%Y%m%d-%H:%M:%S`\033[32m bash /opt/releem/mysqlconfigurer.sh -r\033[0m\n\n"
fi
/opt/releem/releem-agent --event=config_applied > /dev/null
printf "\n`date +%Y%m%d-%H:%M:%S`\033[32m Notification to Releem Platform was sent successfully!\033[0m\n"
exit "${RESTART_CODE}"
}
function releem_apply_automatic() {
printf "\n`date +%Y%m%d-%H:%M:%S`\033[37m Applying the recommended MySQL configuration...\033[0m\n"
printf "\n`date +%Y%m%d-%H:%M:%S`\033[37m Getting the latest up-to-date configuration...\033[0m\n"
/opt/releem/releem-agent -c >/dev/null 2>&1 || true
if [ ! -f $MYSQLCONFIGURER_CONFIGFILE ]; then
printf "\033[37m\n * Recommended MySQL configuration is not found.\033[0m"
printf "\033[37m\n * Please apply recommended configuration later or run Releem Agent manually:\033[0m"
printf "\033[32m\n /opt/releem/releem-agent -f \033[0m\n\n"
exit 1;
fi
if ! check_mysql_version; then
printf "\033[31m\n * MySQL version is lower than 5.6.7. Check the documentation https://github.com/Releem/mysqlconfigurer#how-to-apply-the-recommended-configuration for applying the configuration. \033[0m\n"
exit 2
fi
if [ -z "$RELEEM_MYSQL_CONFIG_DIR" -o ! -d "$RELEEM_MYSQL_CONFIG_DIR" ]; then
printf "\033[37m\n * MySQL configuration directory is not found.\033[0m"
printf "\033[37m\n * Try to reinstall Releem Agent, and please set the my.cnf location.\033[0m"
exit 3;
fi
FLAG_RESTART_SERVICE=1
if [ -z "$RELEEM_RESTART_SERVICE" ]; then
read -p "Please confirm the MySQL service restart? (Y/N) " -n 1 -r
echo # move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
printf "\n`date +%Y%m%d-%H:%M:%S`\033[37m A confirmation to restart the service has not been received. Releem recommended configuration has not been applied.\033[0m\n"
FLAG_RESTART_SERVICE=0
fi
elif [ "$RELEEM_RESTART_SERVICE" -eq 0 ]; then
FLAG_RESTART_SERVICE=0
fi
printf "\n`date +%Y%m%d-%H:%M:%S`\033[37m Copying file $MYSQLCONFIGURER_CONFIGFILE to directory $RELEEM_MYSQL_CONFIG_DIR/...\033[0m\n"
if [ ! -f "${MYSQLCONFIGURER_PATH}${MYSQLCONFIGURER_FILE_NAME}.bkp" ]; then
yes | cp -f "${RELEEM_MYSQL_CONFIG_DIR}/${MYSQLCONFIGURER_FILE_NAME}" "${MYSQLCONFIGURER_PATH}${MYSQLCONFIGURER_FILE_NAME}.bkp"
fi
yes | cp -fr $MYSQLCONFIGURER_CONFIGFILE $RELEEM_MYSQL_CONFIG_DIR/
chmod 644 $RELEEM_MYSQL_CONFIG_DIR/*
if [ "$FLAG_RESTART_SERVICE" -ne 0 ]; then
if [ -z "$RELEEM_MYSQL_RESTART_SERVICE" ]; then
printf "\033[37m\n * The command to restart the MySQL service was not found. Try to reinstall Releem Agent.\033[0m"
exit 4;
fi
#echo "-------Test config-------"
printf "\n`date +%Y%m%d-%H:%M:%S`\033[37m Restarting MySQL with the command '$RELEEM_MYSQL_RESTART_SERVICE'...\033[0m\n"
eval "$RELEEM_MYSQL_RESTART_SERVICE" &
wait_restart $!
RESTART_CODE=$?
if [ $RESTART_CODE -eq 0 ];
then
printf "\n`date +%Y%m%d-%H:%M:%S`\033[32m The MySQL service restarted successfully!\033[0m\n"
printf "\n`date +%Y%m%d-%H:%M:%S`\033[32m Recommended configuration applied successfully!\033[0m\n"
printf "\n`date +%Y%m%d-%H:%M:%S` Releem Score and Unapplied recommendations in the Releem Dashboard will be updated in a few minutes.\n"
rm -f "${MYSQLCONFIGURER_PATH}${MYSQLCONFIGURER_FILE_NAME}.bkp"
elif [ $RESTART_CODE -eq 6 ];
then
printf "\n`date +%Y%m%d-%H:%M:%S`\033[31m MySQL service failed to restart in 1200 seconds! \033[0m\n"
printf "\n`date +%Y%m%d-%H:%M:%S`\033[31m Wait for the MySQL service to start and Check the MySQL error log!\033[0m\n"
printf "\n`date +%Y%m%d-%H:%M:%S`\033[31m Try to roll back the configuration application using the command: \033[0m\n"
printf "\n`date +%Y%m%d-%H:%M:%S`\033[32m bash /opt/releem/mysqlconfigurer.sh -r\033[0m\n\n"
elif [ $RESTART_CODE -eq 7 ];
then
printf "\n`date +%Y%m%d-%H:%M:%S`\033[31m MySQL service failed to restart! Check the MySQL error log! \033[0m\n"
printf "\n`date +%Y%m%d-%H:%M:%S`\033[31m Try to roll back the configuration application using the command: \033[0m\n"
printf "\n`date +%Y%m%d-%H:%M:%S`\033[32m bash /opt/releem/mysqlconfigurer.sh -r\033[0m\n\n"
fi
else
RESTART_CODE=0
fi
/opt/releem/releem-agent --event=config_applied > /dev/null
printf "\n`date +%Y%m%d-%H:%M:%S`\033[32m Notification to Releem Platform was sent successfully!\033[0m\n"
exit "${RESTART_CODE}"
}
function releem_runnig_cron() {
HOUR=$(date +%I)
MINUTE=$(date +%M)
send_metrics
if [ "${HOUR}" == "12" ] && [ "${MINUTE}" == "10" ];
then
get_config
update_agent
fi
exit 0
}
function send_metrics() {
#echo -e "\033[37m\n * Checking the environment...\033[0m"
check_env
##### PARAMETERS #####
CACHE_TTL="55"
CACHE_FILE_STATUS="/tmp/releem.mysql.status.`echo $MYSQLCONFIGURER_CONFIGFILE | md5sum | cut -d" " -f1`.cache"
CACHE_FILE_VARIABLES="/tmp/releem.mysql.variables.`echo $MYSQLCONFIGURER_CONFIGFILE | md5sum | cut -d" " -f1`.cache"
EXEC_TIMEOUT="1"
NOW_TIME=`date '+%s'`
##### RUN #####
# Collect MySQL metrics
#echo -e "\033[37m\n * Collecting metrics...\033[0m"
if [ -s "${CACHE_FILE_STATUS}" ]; then
CACHE_TIME=`stat -c"%Y" "${CACHE_FILE_STATUS}"`
else
CACHE_TIME=0
fi
DELTA_TIME=$((${NOW_TIME} - ${CACHE_TIME}))
#
if [ ${DELTA_TIME} -lt ${EXEC_TIMEOUT} ]; then
sleep $((${EXEC_TIMEOUT} - ${DELTA_TIME}))
elif [ ${DELTA_TIME} -gt ${CACHE_TTL} ]; then
echo "" >> "${CACHE_FILE_STATUS}" # !!!
DATACACHE=`mysql -sNe "show global status;"`
echo "${DATACACHE}" > "${CACHE_FILE_STATUS}" # !!!
chmod 640 "${CACHE_FILE_STATUS}"
fi
if [ -s "${CACHE_FILE_VARIABLES}" ]; then
CACHE_TIME=`stat -c"%Y" "${CACHE_FILE_VARIABLES}"`
else
CACHE_TIME=0
fi
DELTA_TIME=$((${NOW_TIME} - ${CACHE_TIME}))
#
if [ ${DELTA_TIME} -lt ${EXEC_TIMEOUT} ]; then
sleep $((${EXEC_TIMEOUT} - ${DELTA_TIME}))
elif [ ${DELTA_TIME} -gt ${CACHE_TTL} ]; then
echo "" >> "${CACHE_FILE_VARIABLES}" # !!!
DATACACHE=`mysql -sNe "show global variables;"`
echo "${DATACACHE}" > "${CACHE_FILE_VARIABLES}" # !!!
chmod 640 "${CACHE_FILE_VARIABLES}"
fi
QUESTIONS=`cat ${CACHE_FILE_STATUS} | grep -w 'Questions' | awk '{print $2}'`
TIMESTAMP=`stat -c"%Y" "${CACHE_FILE_STATUS}"`
HOSTNAME=`cat ${CACHE_FILE_VARIABLES} | grep -w 'hostname' | awk '{print $2}'`
JSON_STRING='{"Hostname": "'${HOSTNAME}'", "Timestamp":"'${TIMESTAMP}'", "ReleemMetrics": {"Questions": "'${QUESTIONS}'"}}'
#echo -e "\033[37m\n * Sending metrics to Releem Cloud Platform...\033[0m"
# Send metrics to Releem Platform. The answer is the configuration file for MySQL
curl -s -d "$JSON_STRING" -H "x-releem-api-key: $RELEEM_API_KEY" -H "Content-Type: application/json" -X POST https://api.releem.com/v1/mysql
}
function check_env() {
# Check RELEEM_API_KEY is not empty
if [ -z "$RELEEM_API_KEY" ]; then
echo >&2 "RELEEM_API_KEY is empty please sign up at https://releem.com/appsignup to get your Releem API key. Aborting."
exit 1;
fi
command -v curl >/dev/null 2>&1 || { echo >&2 "Curl is not installed. Please install Curl. Aborting."; exit 1; }
}
function get_config() {
echo -e "\033[37m\n * Checking the environment...\033[0m"
check_env
command -v perl >/dev/null 2>&1 || { echo >&2 "Perl is not installed. Please install Perl. Aborting."; exit 1; }
perl -e "use JSON;" >/dev/null 2>&1 || { echo >&2 "Perl module JSON is not installed. Please install Perl module JSON. Aborting."; exit 1; }
# Check if the tmp folder exists
if [ -d "$MYSQLCONFIGURER_PATH" ]; then
# Clear tmp directory
rm $MYSQLCONFIGURER_PATH/*
else
# Create tmp directory
mkdir $MYSQLCONFIGURER_PATH
fi
# Check if MySQLTuner already downloaded and download if it doesn't exist
if [ ! -f "$MYSQLTUNER_FILENAME" ]; then
# Download latest version of the MySQLTuner
curl -s -o $MYSQLTUNER_FILENAME -L https://raw.githubusercontent.com/major/MySQLTuner-perl/fdd42e76857532002b8037cafddec3e38983dde8/mysqltuner.pl
chmod +x $MYSQLTUNER_FILENAME
fi
echo -e "\033[37m\n * Collecting metrics to recommend a config...\033[0m"
# Collect MySQL metrics
if perl $MYSQLTUNER_FILENAME --json --verbose --notbstat --nocolstat --noidxstat --nopfstat --forcemem=$MYSQL_MEMORY_LIMIT --outputfile="$MYSQLTUNER_REPORT" --user=${MYSQL_LOGIN} --pass=${MYSQL_PASSWORD} ${connection_string} > /dev/null; then
echo -e "\033[37m\n * Sending metrics to Releem Cloud Platform...\033[0m"
# Send metrics to Releem Platform. The answer is the configuration file for MySQL
curl -s -d @$MYSQLTUNER_REPORT -H "x-releem-api-key: $RELEEM_API_KEY" -H "Content-Type: application/json" -X POST https://api.releem.com/v1/mysql -o "$MYSQLCONFIGURER_CONFIGFILE"
echo -e "\033[37m\n * Downloading recommended MySQL configuration from Releem Cloud Platform...\033[0m"
# Show recommended configuration and exit
msg="\n\n#---------------Releem Agent Report-------------\n\n"
printf "${msg}"
echo -e "1. Recommended MySQL configuration downloaded to ${MYSQLCONFIGURER_CONFIGFILE}"
echo
echo -e "2. To check MySQL Performance Score please visit https://app.releem.com/dashboard?menu=metrics"
echo
echo -e "3. To apply the recommended configuration please read documentation https://app.releem.com/dashboard"
else
# If error then show report and exit
errormsg=" \
\n\n\n\n--------Releem Agent completed with error--------\n \
\nCheck $MYSQLTUNER_REPORT for details \n \
\n--------Please fix the error and run Releem Agent again--------\n"
printf "${errormsg}" >&2
fi
}
mysqladmincmd=$(which mariadb-admin 2>/dev/null || true)
if [ -z $mysqladmincmd ];
then
mysqladmincmd=$(which mysqladmin 2>/dev/null || true)
fi
if [ -z $mysqladmincmd ];
then
printf "\033[31m Couldn't find mysqladmin/mariadb-admin in your \$PATH. Is MySQL installed? \033[0m\n"
exit 1
fi
mysqlcmd=$(which mariadb 2>/dev/null || true)
if [ -z $mysqlcmd ];
then
mysqlcmd=$(which mysql 2>/dev/null || true)
fi
if [ -z $mysqlcmd ];
then
printf "\033[31m Couldn't find mysql/mariadb in your \$PATH. Is MySQL installed? \033[0m\n"
exit 1
fi
connection_string=""
if test -f $RELEEM_CONF_FILE ; then
. $RELEEM_CONF_FILE
if [ ! -z $apikey ]; then
RELEEM_API_KEY=$apikey
fi
if [ ! -z $memory_limit ]; then
MYSQL_MEMORY_LIMIT=$memory_limit
fi
if [ ! -z $mysql_cnf_dir ]; then
RELEEM_MYSQL_CONFIG_DIR=$mysql_cnf_dir
fi
if [ ! -z "$mysql_restart_service" ]; then
RELEEM_MYSQL_RESTART_SERVICE=$mysql_restart_service
fi
if [ ! -z "$mysql_user" ]; then
MYSQL_LOGIN=$mysql_user
fi
if [ ! -z "$mysql_password" ]; then
MYSQL_PASSWORD=$mysql_password
fi
if [ ! -z "$mysql_host" ]; then
if [ -S "$mysql_host" ]; then
connection_string="${connection_string} --socket=$mysql_host"
else
connection_string="${connection_string} --host=$mysql_host"
fi
else
connection_string="${connection_string} --host=127.0.0.1"
fi
if [ ! -z "$mysql_port" ]; then
connection_string="${connection_string} --port=$mysql_port"
else
connection_string="${connection_string} --port=3306"
fi
if [ ! -z "$query_optimization" ]; then
RELEEM_QUERY_OPTIMIZATION=$query_optimization
fi
fi
# Parse parameters
while getopts "k:m:s:arcpu" option
do
case "${option}" in
k) RELEEM_API_KEY=${OPTARG};;
m) MYSQL_MEMORY_LIMIT=${OPTARG};;
a) releem_apply_manual;;
s) releem_apply_config ${OPTARG};;
r) releem_rollback_config;;
c) get_config;;
p) releem_ps_mysql;;
u) update_agent; exit 0;;
esac
done
printf "\033[37m\n\033[0m"
printf "\033[37m * To run Releem Agent manually please use the following command:\033[0m\n"
printf "\033[32m /opt/releem/releem-agent -f\033[0m\n\n"