Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
3294 rexy 1
#!/bin/bash
2
 
3
#########################
4
## ALCASAR replication ##
5
##         list        ##
6
#########################
7
# The script is designed to list replicas of the ALCASAR instance.
8
 
9
# Constants
10
readonly ALCASAR_PWD="/root/ALCASAR-passwords.txt"
11
readonly SERVICES_PATH="/etc/systemd/system"
12
 
13
# Dynamically generated constants
14
DB_ROOT_PWD="$(grep db_root "$ALCASAR_PWD" | cut -d '=' -f 2-)"
15
readonly DB_ROOT_PWD
16
 
17
# Variables
18
query=""
19
attributes=""
20
 
21
 
22
check_args() {
23
	# Parse args
24
	args="$(getopt --longoptions "all,name:,help" --options "a,n:,h" -- "$@")"
25
 
26
	# Reset script args list
27
	eval set -- "$args"
28
 
29
	# Print help
30
	if [ $# -eq 1 ]
31
	then
32
		usage
33
		return 1
34
	fi
35
 
36
	# Loop over all args
37
	while true
38
	do
39
		case "$1" in
40
			--all | -a)
41
				query="SHOW ALL REPLICAS STATUS \\G"
42
				break
43
				;;
44
			--name | -n)
45
				query="SHOW REPLICA '$2' STATUS \\G"
46
				shift
47
				break
48
				;;
49
			--help | -h)
50
				usage
51
				return 2
52
				;;
53
			--)
54
				# End of args
55
				break
56
				;;
57
			*)
58
				echo "error: unknown $1" >&2
59
				return 3
60
				break;;
61
		esac
62
		shift
63
	done
64
}
65
 
66
# Edit informations with more information (like real remote IP)
67
edit_output() {
68
	# Remove useless spaces
69
	attributes="$(echo "$attributes" | sed "s/^ *//")"
70
 
71
	# Loop over connections
72
	for conn_name in $(echo "$attributes" | grep "Connection_name" | cut -d ' ' -f 2-)
73
	do
74
		service_file="$SERVICES_PATH/replication-$conn_name.service"
75
		ip=""
76
 
77
		# Look for service file where remote IP is hardcoded (on secondary only)
78
		if ls "$service_file" &> /dev/null
79
		then
80
			# Get IP from SSH tunnel service file
81
			ip="$(grep "ExecStart" "$service_file" | sed -E "s/.*@(.*) *.*/\1/")"
82
		else
83
			port="$(echo "$attributes" | grep -A5 "Connection_name: $conn_name" | grep "^Master_Port" | cut -d ' ' -f 2-)"
84
 
85
			# Get IP from 'ss' established connections
86
			pid="$(/usr/sbin/ss --tcp --processes | grep "$port.*sshd" | grep -oE "pid=[0-9]+" | cut -d '=' -f 2)"
87
			# Skip if connection is closed
88
			[ -z "$pid" ] && continue
89
 
90
			# Find corresponding IP from PID
91
			ip="$(/usr/sbin/ss --tcp --process | grep "$pid" | grep -v "127.0.0.1" | tr -s ' ' | cut -d ' ' -f 5 | cut -d ':' -f 1)"
92
		fi
93
 
94
		# Replace loopback address with the real remote IP
95
		attributes="$(echo "$attributes" | sed "/Connection_name: $conn_name/,/Master_Host/s/127.0.0.1/$ip/")"
96
	done
97
}
98
 
99
# Print script usage
100
usage() {
101
	echo "usage: $0 OPTIONS"
102
	echo
103
	echo "OPTIONS"
104
	echo "	--all"
105
	echo "		list all primary connections attributes"
106
	echo "	--name=NAME, -n NAME"
107
	echo "		list primary connection attributes"
108
	echo "	--help, -h"
109
	echo "		print this help message"
110
}
111
 
112
 
113
# Main
114
check_args "$@" || exit
115
 
116
# Execute the query
117
attributes="$(/usr/bin/mariadb --host="localhost" --user="root" --password="$DB_ROOT_PWD" --execute="$query")"
118
[ "$?" -ne 0 ] && exit 1
119
 
120
# Edit some information
121
edit_output
122
 
123
# Display data if any (in case there are no remotes)
124
[ -n "$attributes" ] && echo "$attributes"
125
exit 0