forked from percona/proxysql-admin-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxysql_galera_checker
executable file
·2590 lines (2271 loc) · 92.9 KB
/
proxysql_galera_checker
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
## inspired by Percona clustercheck.sh
#-------------------------------------------------------------------------------
#
# Step 1 : Bash internal configuration
#
set -o nounset # no undefined variables
#-------------------------------------------------------------------------------
#
# Step 2 : Global variables
#
#
# Script parameters/constants
#
declare -i DEBUG=0
readonly PROXYSQL_ADMIN_VERSION="1.4.14"
#Timeout exists for instances where mysqld may be hung
declare -i TIMEOUT=10
declare RED=""
declare NRED=""
#
# Global variables used by the script
#
declare ERR_FILE="/dev/stderr"
declare NODE_MONITOR_LOG_FILE=""
declare CONFIG_FILE="/etc/proxysql-admin.cnf"
declare HOST_PRIORITY_FILE=""
declare CHECKER_PIDFILE=""
# Set to send output here when DEBUG is set
declare DEBUG_ERR_FILE="/dev/null"
declare -i HOSTGROUP_WRITER_ID=1
declare -i HOSTGROUP_READER_ID=-1
declare -i HOSTGROUP_SLAVEREADER_ID=-1
declare -i NUMBER_WRITERS=0
declare WRITER_IS_READER="ondemand"
declare SLAVE_IS_WRITER="yes"
declare P_MODE=""
declare P_PRIORITY=""
declare MYSQL_USERNAME
declare MYSQL_PASSWORD
declare PROXYSQL_USERNAME
declare PROXYSQL_PASSWORD
declare PROXYSQL_HOSTNAME
declare PROXYSQL_PORT
declare PROXYSQL_DATADIR='/var/lib/proxysql'
# Set to 1 if slave readers are being used
declare -i HAVE_SLAVEREADERS=0
declare -i HAVE_SLAVEWRITERS=0
# How far behind can a slave be before its put into OFFLINE_SOFT state
declare SLAVE_SECONDS_BEHIND=3600
# Some extra text that will be logged
# (useful for debugging)
declare LOG_TEXT=""
# Default value for max_connections in mysql_servers
declare MAX_CONNECTIONS=1000
#-------------------------------------------------------------------------------
#
# Step 3 : Helper functions
#
function log() {
local lineno=$1
shift
if [[ -n $ERR_FILE ]]; then
if [[ -n $lineno && $DEBUG -ne 0 ]]; then
echo -e "[$(date +%Y-%m-%d\ %H:%M:%S)] $$ (line $lineno) $*" >> $ERR_FILE
else
echo -e "[$(date +%Y-%m-%d\ %H:%M:%S)] $$ $*" >> $ERR_FILE
fi
fi
}
# Checks the return value of the most recent command
#
# Globals:
# None
#
# Arguments:
# 1: the lineno where the error occurred
# 2: the error code of the most recent command
# 3: the error message if the error code is non-zero
#
function check_cmd() {
local lineno=$1
local retcode=$2
local errmsg=$3
shift 3
if [[ ${retcode} -ne 0 ]]; then
error "$lineno" $errmsg
if [[ -n "$*" ]]; then
log "$lineno" $*
fi
fi
return $retcode
}
# Checks the return value of the most recent command
# Exits the program if the command fails (non-zero return codes)
#
# This should not be used with commands that can fail for legitimate
# reasons.
#
# Globals:
# None
#
# Arguments:
# 1: the lineno where the error occurred
# 2: the error code of the most recent command
# 3: the error message if the error code is non-zero
#
function check_cmd_and_exit() {
check_cmd "$@"
local retcode=$?
if [[ $retcode -ne 0 ]]; then
exit 1
fi
return $retcode
}
function log_if_success() {
local lineno=$1
local rc=$2
shift 2
if [[ $rc -eq 0 ]]; then
log "$lineno" "$*"
fi
return $rc
}
function error() {
local lineno=$1
shift
log "$lineno" "proxysql_galera_checker : Error ($lineno): $*"
}
function warning() {
local lineno=$1
shift
log "$lineno" "Warning: $*"
}
function debug() {
if [[ $DEBUG -eq 0 ]]; then
return
fi
local lineno=$1
shift
log "$lineno" "${RED}debug: $*${NRED}"
}
function usage() {
local path=$0
cat << EOF
Usage: ${path##*/} --write-hg=10 --read-hg=11 --config-file=/etc/proxysql-admin.cnf --log=/var/lib/proxysql/pxc_test_proxysql_galera_check.log
Options:
-w, --write-hg=<NUMBER> Specify ProxySQL write hostgroup.
-r, --read-hg=<NUMBER> Specify ProxySQL read hostgroup.
-c, --config-file=PATH Specify ProxySQL-admin configuration file.
-l, --log=PATH Specify proxysql_galera_checker log file.
--log-text=TEXT This is text that will be written to the log file
whenever this script is run (useful for debugging).
--node-monitor-log=PATH Specify proxysql_node_monitor log file.
-n, --writer-count=<NUMBER> Maximum number of write hostgroup_id nodes
that can be marked ONLINE
When 0 (default), all nodes can be marked ONLINE
-p, --priority=<HOST_LIST> Can accept comma delimited list of write nodes priority
-m, --mode=[loadbal|singlewrite] ProxySQL read/write configuration mode,
currently supporting: 'loadbal' and 'singlewrite'
--writer-is-reader=<value> Defines if the writer node also accepts writes.
Possible values are 'always', 'never', and 'ondemand'.
'ondemand' means that the writer node only accepts reads
if there are no other readers.
(default: 'never')
--use-slave-as-writer=<yes/no> If this is 'yes' then slave nodes may
be added to the write hostgroup if all other
cluster nodes are down.
(default: 'yes')
--max-connections=<NUMBER> Value for max_connections in the mysql_servers table.
This is the maximum number of connections that
ProxySQL will open to the backend servers.
(default: 1000)
--debug Enables additional debug logging.
-h, --help Display script usage information
-v, --version Print version info
Notes about the mysql_servers in ProxySQL:
- NODE STATUS * Nodes that are in status OFFLINE_HARD will not be checked
nor will their status be changed
* SHUNNED nodes are not to be used with Galera based systems,
they will be checked and their status will be changed
to either ONLINE or OFFLINE_SOFT.
When no nodes were found to be in wsrep_local_state=4 (SYNCED) for either
read or write nodes, then the script will try 5 times for each node to try
to find nodes wsrep_local_state=4 (SYNCED) or wsrep_local_state=2 (DONOR/DESYNC)
EOF
}
# Check the permissions for a file or directory
#
# Globals:
# None
#
# Arguments:
# 1: the bash test to be applied to the file
# 2: the lineno where this call is invoked (used for errors)
# 3: the path to the file
# 4: (optional) description of the path (mostly used for existence checks)
#
# Exits the script if the permissions test fails.
#
function check_permission() {
local permission=$1
local lineno=$2
local path_to_check=$3
local description=""
if [[ $# -gt 3 ]]; then
description="$4"
fi
if [ ! $permission "$path_to_check" ] ; then
if [[ $permission == "-r" ]]; then
error $lineno "You do not have READ permission for: $path_to_check"
elif [[ $permission == "-w" ]]; then
error $lineno "You do not have WRITE permission for: $path_to_check"
elif [[ $permission == "-x" ]]; then
error $lineno "You do not have EXECUTE permission for: $path_to_check"
elif [[ $permission == "-e" ]]; then
if [[ -n $description ]]; then
error $lineno "Could not find the $description: $path_to_check"
else
error $lineno "Could not find: $path_to_check"
fi
elif [[ $permission == "-d" ]]; then
if [[ -n $description ]]; then
error $lineno "Could not find the $description: $path_to_check"
else
error $lineno "Could not find the directory: $path_to_check"
fi
elif [[ $permission == "-f" ]]; then
if [[ -n $description ]]; then
error $lineno "Could not find the $description: $path_to_check"
else
error $lineno "Could not find the file: $path_to_check"
fi
else
error $lineno "You do not have the correct permissions for: $path_to_check"
fi
exit 1
fi
}
# Executes a SQL query with the (fully) specified server
#
# Globals:
# None
#
# Arguments:
# 1: lineno
# 2: the name of the user
# 3: the user's password
# 4: the hostname of the server
# 5: the port used to connect to the server
# 6: the query to be run
# 7: arguments to the mysql client
# 8: timeout in secs
# 9: additional options, space separated
# Available options:
# "hide_output"
# This will not show the output of the query when DEBUG is set.
# Used to stop the display of sensitve information (such as passwords)
# from being displayed when debugging.
#
function exec_sql() {
local lineno=$1
local user=$2
local password=$3
local hostname=$4
local port=$5
local query=$6
local args=$7
local timeout_secs=$8
local more_options=$9
local retvalue
local retoutput
debug "$lineno" "exec_sql : $user@$hostname:$port ($args) ==> $query"
retoutput=$(printf "[client]\nuser=${user}\npassword=\"${password}\"\nhost=${hostname}\nport=${port}" \
| timeout ${timeout_secs} mysql --defaults-file=/dev/stdin --protocol=tcp \
${args} -e "$query")
retvalue=$?
if [[ $DEBUG -eq 1 ]]; then
local number_of_newlines=0
local dbgoutput=$retoutput
if [[ " $more_options " =~ [[:space:]]hide_output[[:space:]] ]]; then
dbgoutput="**** data hidden ****"
fi
if [[ -n $dbgoutput ]]; then
number_of_newlines=$(printf "%s" "${dbgoutput}" | wc -l)
fi
if [[ $retvalue -ne 0 ]]; then
debug "" "--> query failed $retvalue"
elif [[ -z $dbgoutput ]]; then
debug "" "--> query returned $retvalue : <query returned no data>"
elif [[ ${number_of_newlines} -eq 0 ]]; then
debug "" "--> query returned $retvalue : ${dbgoutput}"
else
debug "" "--> query returned $retvalue : <data follows>"
printf "${dbgoutput//%/%%}\n" | while IFS= read -r line; do
debug "" "----> $line"
done
fi
fi
printf "${retoutput//%/%%}"
return $retvalue
}
# Executes a SQL query on proxysql (with a timeout of $TIMEOUT seconds)
#
# Globals:
# PROXYSQL_USERNAME
# PROXYSQL_PASSWORD
# PROXYSQL_HOSTNAME
# PROXYSQL_PORT
#
# Arguments:
# 1: lineno (used for debugging/output, may be blank)
# 2: Additional arguments to the mysql client for the query
# 3: The SQL query
# 4: (optional) see the additional options for exec_sql
#
function proxysql_exec() {
local lineno=$1
local args=$2
local query="$3"
local more_options=""
local retoutput
if [[ $# -ge 4 ]]; then
more_options=$4
fi
exec_sql "$lineno" "$PROXYSQL_USERNAME" "$PROXYSQL_PASSWORD" \
"$PROXYSQL_HOSTNAME" "$PROXYSQL_PORT" \
"$query" "$args" "$TIMEOUT" "$more_options"
retoutput=$?
if [[ $retoutput -eq 124 ]]; then
error $lineno "TIMEOUT: SQL query ($PROXYSQL_HOSTNAME:$PROXYSQL_PORT) : $query"
fi
return $retoutput
}
# Executes a SQL query on mysql (with a timeout of $TIMEOUT secs)
#
# Globals:
# MYSQL_USERNAME
# MYSQL_PASSWORD
#
# Arguments:
# 1: lineno (used for debugging/output, may be blank)
# 2: the hostname of the server
# 3: the port used to connect to the server
# 4: arguments to the mysql client
# 5: the query to be run
# 6: (optional) more options see exec_sql
#
function mysql_exec() {
local lineno=$1
local hostname=$2
local port=$3
local args=$4
local query=$5
local more_options=""
local retoutput
if [[ $# -ge 6 ]]; then
more_options=$6
fi
exec_sql "$lineno" "$MYSQL_USERNAME" "$MYSQL_PASSWORD" \
"$hostname" "$port" \
"$query" "$args" "$TIMEOUT" "$more_options"
retoutput=$?
if [[ $retoutput -eq 124 ]]; then
error $lineno "TIMEOUT: SQL query ($hostname:$$port) : $query"
fi
return $retoutput
}
# Separates the IP address from the port in a network address
# Works for IPv4 and IPv6
#
# Globals:
# None
#
# Params:
# 1. The network address to be parsed
#
# Outputs:
# A string with a space separating the IP address from the port
#
function separate_ip_port_from_address()
{
#
# Break address string into host:port/path parts
#
local address=$1
# Has to have at least one ':' to separate the port from the ip address
if [[ $address =~ : ]]; then
ip_addr=${address%:*}
port=${address##*:}
else
ip_addr=$address
port=""
fi
# Remove any braces that surround the ip address portion
ip_addr=${ip_addr#\[}
ip_addr=${ip_addr%\]}
echo "${ip_addr} ${port}"
}
# Combines the IP address and port into a network address
# Works for IPv4 and IPv6
# (If the IP address is IPv6, the IP portion will have brackets)
#
# Globals:
# None
#
# Params:
# 1: The IP address portion
# 2: The port
#
# Outputs:
# A string containing the full network address
#
function combine_ip_port_into_address()
{
local ip_addr=$1
local port=$2
local addr
if [[ ! $ip_addr =~ \[.*\] && $ip_addr =~ .*:.* ]] ; then
# If there are no brackets and it does have a ':', then add the brackets
# because this is an unbracketed IPv6 address
addr="[${ip_addr}]:${port}"
else
addr="${ip_addr}:${port}"
fi
echo $addr
}
# upgrade scheduler from old layout to new layout
#
# Globals:
# PROXYSQL_DATADIR
# TIMEOUT
# HOST_PRIORITY_FILE
#
# Arguments:
# None
function upgrade_scheduler(){
log $LINENO "**** Scheduler upgrade started ****"
if [[ -f /etc/proxysql-admin.cnf ]]; then
source /etc/proxysql-admin.cnf
else
error $LINENO "Assert! proxysql-admin configuration file : /etc/proxysql-admin.cnf does not exist, Terminating!"
exit 1
fi
# For this function, use a shorter timeout than normal
TIMEOUT=2
local scheduler_rows
local -i rows_found=0
local -i rows_modified=0
scheduler_rows=$(proxysql_exec $LINENO "-Ns" "SELECT * FROM scheduler")
check_cmd_and_exit $LINENO $? "Could not retreive rows from scheduler (query failed). Exiting"
while read i; do
if [[ -z $i ]]; then continue; fi
rows_found+=1
# Extract fields from the line
local id=$(echo "$i" | awk '{print $1}')
local s_write_hg=$(echo "$i" | awk '{print $5}')
local s_read_hg=$(echo "$i" | awk '{print $6}')
local s_number_of_writes=$(echo "$i" | awk '{print $7}')
local s_log=$(echo "$i" | awk '{print $9}')
local s_cluster_name=$(echo "$i" | awk '{print $10}')
local s_mode=""
log $LINENO "Modifying the scheduler for write hostgroup: $s_write_hg"
# Get the mode for this cluster
local proxysql_mode_file
if [[ -z $s_cluster_name ]]; then
proxysql_mode_file="${PROXYSQL_DATADIR}/mode"
else
proxysql_mode_file="${PROXYSQL_DATADIR}/${s_cluster_name}_mode"
fi
if [[ -f ${proxysql_mode_file} && -r ${proxysql_mode_file} ]] ; then
s_mode=$(cat ${proxysql_mode_file})
else
log $LINENO ".. Cannot find the ${proxysql_mode_file} file"
if [[ $s_read_hg == "-1" ]]; then
log $LINENO ".. Assuming mode='loadbal'"
s_mode="loadbal"
else
log $LINENO ".. Assuming mode='singlewrite'"
s_mode="singlewrite"
fi
fi
# TODO: kennt
# This will fail in the multi-cluster case, but may be a non-issue
# since those nodes should appear in one cluster (may cause extra work
# for the othre clusters though).
# Get the host priority file
local s_host_priority=''
if [[ -n $HOST_PRIORITY_FILE && -f $HOST_PRIORITY_FILE ]]; then
debug $LINENO "Found a host priority file: $HOST_PRIORITY_FILE"
local p_priority_hosts=""
# Get the list of hosts from the host_priority file ignoring blanks
# and any lines that start with '#'
p_priority_hosts=$(cat $HOST_PRIORITY_FILE | grep '^[^#]' | sed ':a;N;$!ba;s/\n/,/g')
if [[ ! -z $p_priority_hosts ]] ; then
s_host_priority="--priority=$p_priority_hosts"
fi
fi
# Make the changes
if [[ ! -z $s_write_hg ]] && [[ ! -z $s_read_hg ]] && [[ ! -z $s_number_of_writes ]] && [[ ! -z $s_log ]]; then
proxysql_exec $LINENO -Ns "UPDATE scheduler SET arg1='--config-file=/etc/proxysql-admin.cnf --writer-is-reader=ondemand --write-hg=$s_write_hg --read-hg=$s_read_hg --writer-count=$s_number_of_writes $s_host_priority --mode=$s_mode --log=$s_log', arg2=NULL, arg3=NULL, arg4=NULL, arg5=NULL WHERE id=$id"
check_cmd_and_exit $LINENO $? "Could not update the scheduler (query failed). Exiting."
rows_modified+=1
fi
done< <(printf "${scheduler_rows}\n")
if [[ $rows_modified -gt 0 ]]; then
proxysql_exec $LINENO -Ns "LOAD SCHEDULER TO RUNTIME; SAVE SCHEDULER TO DISK;"
check_cmd_and_exit $LINENO $? "Could not save scheduler changes to runtime or disk (query failed). Exiting."
log_if_success $LINENO $? "Scheduler changes saved to runtime and disk"
fi
log $LINENO "$rows_modified row(s) modified / $rows_found row(s) found"
log $LINENO "**** Scheduler upgrade finished ****"
}
# Upgrade the status of a node
# This may also perform an INSERT (depending on the reader_status)
#
# Globals:
# TIMEOUT
#
# Arguments:
# 1: lineno
# 2: hostgroup
# 3: server address
# 4: port
# 5: new status
# 6: reader_status
# 7: comment
#
function change_server_status() {
local lineno=$1
local hostgroup=$2
local server=$3
local port=$4
local status=$5
local reader_status=$6
local comment=$7
local address
address=$(combine_ip_port_into_address "$server" "$port")
# If we have a PRIORITY_NODE, then we don't have a WRITER entry
# to upgrade, but we do have a READER entry, so upgrade that
if [[ $reader_status == "PRIORITY_NODE" ]]; then
proxysql_exec $lineno -Ns "INSERT INTO mysql_servers
(hostname,hostgroup_id,port,weight,status,comment,max_connections)
VALUES ('$server',$hostgroup,$port,1000000,'$status','WRITE',$MAX_CONNECTIONS);"
check_cmd_and_exit $LINENO $? "Could not create new mysql_servers row (query failed). Exiting."
log "$lineno" "Adding server $hostgroup:$address with status $status. Reason: $comment"
else
proxysql_exec $lineno -Ns "UPDATE mysql_servers
set status = '$status' WHERE hostgroup_id = $hostgroup AND hostname = '$server' AND port = $port;" 2>>${ERR_FILE}
check_cmd_and_exit $LINENO $? "Could not update new mysql_servers row (query failed). Exiting."
log "$lineno" "Changing server $hostgroup:$address to status $status. Reason: $comment"
fi
}
# Arguments:
# The arguments to the script.
#
function parse_args() {
local go_out=""
# TODO: kennt, what happens if we don't have a functional getopt()?
# Check if we have a functional getopt(1)
if ! getopt --test; then
go_out="$(getopt --options=w:r:c:l:n:m:p:vh --longoptions=write-hg:,read-hg:,config-file:,log:,node-monitor-log:,writer-count:,mode:,priority:,writer-is-reader:,use-slave-as-writer:,log-text:,max-connections:,version,debug,help \
--name="$(basename "$0")" -- "$@")"
if [[ $? -ne 0 ]]; then
# no place to send output
echo "proxysql_galera_checker : Script error: getopt() failed" >&2
exit 1
fi
eval set -- "$go_out"
fi
if [[ $go_out == " --" ]];then
usage
exit 1
fi
#
# We iterate through the command-line options twice
# (1) to handle options that don't need permissions (such as --help)
# (2) to handle options that need to be done before other
# options, such as loading the config file
#
for arg
do
case "$arg" in
-- ) shift; break;;
--config-file )
CONFIG_FILE="$2"
check_permission -e $LINENO "$CONFIG_FILE" "proxysql-admin configuration file"
debug $LINENO "--config-file specified, using : $CONFIG_FILE"
shift 2
;;
--help)
usage
exit 0
;;
-v | --version)
echo "proxysql_galera_checker version $PROXYSQL_ADMIN_VERSION"
exit 0
;;
--debug)
DEBUG=1
shift
;;
*)
shift
;;
esac
done
#
# Load the config file before reading in the command-line options
#
readonly CONFIG_FILE
if [ ! -e "$CONFIG_FILE" ]; then
warning "" "Could not locate the configuration file: $CONFIG_FILE"
else
check_permission -r $LINENO "$CONFIG_FILE"
debug $LINENO "Loading $CONFIG_FILE"
source "$CONFIG_FILE"
fi
if [[ $DEBUG -ne 0 ]]; then
# For now
if [[ -t 1 ]]; then
ERR_FILE=/dev/stderr
fi
fi
# Reset the command line for the next invocation
eval set -- "$go_out"
for arg
do
case "$arg" in
-- ) shift; break;;
-w | --write-hg )
HOSTGROUP_WRITER_ID=$2
shift 2
;;
-r | --read-hg )
HOSTGROUP_READER_ID=$2
shift 2
;;
--config-file )
# Do no processing of config-file here, it is processed
# before this loop (see above)
shift 2
;;
-l | --log )
ERR_FILE="$2"
shift 2
# Test if stderr is open to a terminal
# We cannot use stdout as the log output, since it is used
# to return values.
if [[ $ERR_FILE == "/dev/stderr" ]]; then
RED=$(tput setaf 1)
NRED=$(tput sgr0)
else
RED=""
NRED=""
fi
;;
--node-monitor-log )
NODE_MONITOR_LOG_FILE="$2"
shift 2
;;
-n | --writer-count )
NUMBER_WRITERS="$2"
shift 2
;;
-p | --priority )
P_PRIORITY="$2"
shift 2
;;
-m | --mode )
P_MODE="$2"
shift 2
if [ "$P_MODE" != "loadbal" ] && [ "$P_MODE" != "singlewrite" ]; then
echo "ERROR: Invalid --mode passed:"
echo " Please choose one of these modes: loadbal, singlewrite"
exit 1
fi
;;
--writer-is-reader )
WRITER_IS_READER="$2"
shift 2
;;
--use-slave-as-writer )
SLAVE_IS_WRITER="$2"
shift 2
;;
--max-connections )
MAX_CONNECTIONS="$2"
shift 2
;;
--debug )
# Not handled here, see above
shift
;;
--log-text )
LOG_TEXT="$2"
shift 2
;;
-v | --version )
# Not handled here, see above
shift
;;
-h | --help )
# Not handled here, see above
shift
;;
esac
done
if [[ $DEBUG -eq 1 ]]; then
DEBUG_ERR_FILE=$ERR_FILE
fi
#
# Argument validation
#
test $HOSTGROUP_WRITER_ID -ge 0 &>/dev/null
if [[ $? -ne 0 ]]; then
echo "ERROR: writer hostgroup_id is not an integer"
usage
exit 1
fi
test $HOSTGROUP_READER_ID -ge -1 &>/dev/null
if [[ $? -ne 0 ]]; then
echo "ERROR: reader hostgroup_id is not an integer"
usage
exit 1
fi
HOSTGROUP_SLAVEREADER_ID=$HOSTGROUP_READER_ID
if [ $HOSTGROUP_SLAVEREADER_ID -eq $HOSTGROUP_WRITER_ID ];then
let HOSTGROUP_SLAVEREADER_ID+=1
fi
if [[ $NUMBER_WRITERS -lt 0 ]]; then
echo "ERROR: The number of writers should either be 0 to enable all possible nodes ONLINE"
echo " or be larger than 0 to limit the number of writers"
usage
exit 1
fi
if [[ ! $WRITER_IS_READER =~ ^(always|never|ondemand)$ ]]; then
error "" "Invalid --writer-is-reader option: '$WRITER_IS_READER'"
echo "Please choose one of these values: always, never, or ondemand"
exit 1
fi
if [[ ! $SLAVE_IS_WRITER =~ ^(yes|YES|no|NO)$ ]]; then
error "" "Invalid --use-slave-as-writer option: '$SLAVE_IS_WRITER'"
echo "Please choose either yes or no"
exit 1
fi
if [[ $SLAVE_IS_WRITER =~ ^(yes|YES)$ ]]; then
SLAVE_IS_WRITER="yes"
else
SLAVE_IS_WRITER="no"
fi
# These may get set in the config file, but they are not used
# by this script. So to avoid confusion and problems, remove
# these variables explicitly.
unset WRITE_HOSTGROUP_ID
unset READ_HOSTGROUP_ID
unset SLAVEREAD_HOSTGROUP_ID
# Verify that we have an integer
if [[ -n $MAX_CONNECTIONS ]]; then
if ! [ "$MAX_CONNECTIONS" -eq "$MAX_CONNECTIONS" ] 2>/dev/null
then
error "" "option '--max-connections' parameter (must be a number) : $MAX_CONNECTIONS"
exit 1
fi
fi
readonly ERR_FILE
readonly CONFIG_FILE
readonly DEBUG_ERR_FILE
readonly HOSTGROUP_WRITER_ID
readonly HOSTGROUP_READER_ID
readonly HOSTGROUP_SLAVEREADER_ID
readonly NUMBER_WRITERS
readonly WRITER_IS_READER
readonly SLAVE_IS_WRITER
readonly P_PRIORITY
readonly P_MODE
readonly MAX_CONNECTIONS
}
# Checks to see if another instance of the script is running.
# We want only one instance of this script to be running at a time.
#
# Globals:
# PROXYSQL_DATADIR
# ERR_FILE
#
# Arguments:
# 1: the cluster name
#
# This function will exit if another instance of the script is running.
#
# With thanks, http://bencane.com/2015/09/22/preventing-duplicate-cron-job-executions/
#
function check_is_galera_checker_running() {
local cluster_name=$1
if [[ -z $cluster_name ]]; then
CHECKER_PIDFILE=${PROXYSQL_DATADIR}/galera_checker.pid
else
CHECKER_PIDFILE=${PROXYSQL_DATADIR}/${cluster_name}_galera_checker.pid
fi
if [[ -f $CHECKER_PIDFILE && -r $CHECKER_PIDFILE ]] ; then
local GPID
GPID=$(cat "$CHECKER_PIDFILE")
if ps -p $GPID -o args=ARGS | grep $ERR_FILE | grep -o proxysql_galera_check >/dev/null 2>&1 ; then
ps -p $GPID > /dev/null 2>&1
if [[ $? -eq 0 ]]; then
log "$LINENO" "ProxySQL galera checker process already running. (pid:$GPID this pid:$$)"
# We don't want to remove this file on cleanup
CHECKER_PIDFILE=""
exit 1
else
echo $$ > $CHECKER_PIDFILE
if [[ $? -ne 0 ]] ; then
warning "$LINENO" "Could not create galera checker PID file"
exit 1
fi
debug "$LINENO" "Created PID file at $CHECKER_PIDFILE"
fi
else
warning "$LINENO" "Existing PID($GPID) belongs to some other process. Creating new PID file."
echo $$ > $CHECKER_PIDFILE
if [[ $? -ne 0 ]] ; then
warning "$LINENO" "Could not create galera checker PID file"
exit 1
fi
debug "$LINENO" "Created PID file at $CHECKER_PIDFILE"
fi
else
echo "$$" > "$CHECKER_PIDFILE"
if [[ $? -ne 0 ]]; then
warning "$LINENO" "Could not create galera checker PID file"
exit 1
fi
debug "$LINENO" "Created PID file at $CHECKER_PIDFILE"
fi
}
function cleanup_handler() {
if [[ -n $CHECKER_PIDFILE ]]; then
rm -f $CHECKER_PIDFILE
fi
}
# Checks to see that the READ hostgroup entries have been created/deleted
# This function has no meaning if mode="loadbal".
#
# Globals:
# HOSTGROUP_READER_ID
# HOSTGROUP_WRITER_ID
# WRITER_IS_READER
# MODE
#
# Arguments:
# 1: the name of the reload_check_file
# 2: the number of online-readers
#
function writer_is_reader_check() {
local reload_check_file=$1
local number_readers_online=$2
local servers
if [[ $MODE == "loadbal" ]]; then
return
fi
servers=$(proxysql_exec $LINENO -Ns "SELECT hostgroup_id, hostname, port, status
FROM mysql_servers
WHERE hostgroup_id IN ($HOSTGROUP_READER_ID,$HOSTGROUP_WRITER_ID) AND
status <> 'OFFLINE_HARD'
AND comment <> 'SLAVEREAD'
ORDER BY hostname, port, hostgroup_id")
check_cmd_and_exit $LINENO $? "Could not retreive data from mysql_servers (query failed). Exiting."
debug $LINENO "writer_is_reader_check : number_readers_online:$number_readers_online"
# The extra test at the end is to ensure that the very last line read in
# is also handled (the read may be returning EOF)
printf "${servers}" | while read hostgroup server port stat || [ -n "$stat" ]
do
debug $LINENO "Examining $hostgroup:$server:$port"
# Only look at writer nodes, so skip non-writers
if [[ $hostgroup -ne $HOSTGROUP_WRITER_ID ]]; then
continue