-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_mysql_galera.sh
executable file
·254 lines (223 loc) · 5.69 KB
/
check_mysql_galera.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
#!/bin/bash
# Default values for variables
HOST=""
USER=""
PASSWORD="fakepass"
COMMAND=""
usage() {
echo -e ""
echo -e "check_mysql_galera.sh: Monitor MySQL/MariaDB Galera cluster"
echo -e -e ""
echo -e "Usage: check_mysql_galera.sh <options>"
echo -e ""
echo -e "Available options:"
echo -e ""
echo -e " --host Hostname to connect to (MANDATORY)"
echo -e " --user User to connect as (MANDATORY)"
echo -e " --password User's password to use to connect (MANDATORY)"
echo -e " --command <command> (MANDATORY)"
echo -e " Available commands:"
echo -e " cluster-status Check state of this host component"
echo -e " mysql-connected Check host is connected to cluster"
echo -e " cluster-size Check number of cluster hosts"
echo -e " Can be used with --warning or --critical"
echo -e " thread-count Total number of wsrep (applier/rollbacker) threads"
echo -e " ready Whether or not the Galera wsrep provider is ready"
echo -e " synchronized Whether or not the local and cluster nodes are in sync"
echo -e " --help Prints this help and exit"
echo -e " --critical Number considered as critical limit for result command"
echo -e " 'cluster-size'"
echo -e " --warning Number considered as a warning limit for result command"
echo -e " 'cluster-size'"
echo -e ""
echo -e "Examples:"
echo -e ""
echo -e " Check Galera cluster is ready:"
echo -e " $0 --host localhost --user mysql_user --password s3cr3T --command ready"
echo -e ""
echo -e " Check Galera cluster size is at least 9 nodes. Report warning is under:"
echo -e " $0 --host localhost --user mysql_user --password s3cr3T --command cluster-size --warning 9"
echo -e ""
echo -e "Authors: Originally written by Sander Petersson (https://github.com/linpopilan)"
echo -e " Updated by Emmanuel Quevillon (https://github.com/horkko)"
echo -e ""
}
# Checks that required mysql options are set
check_connection() {
if [ "${HOST}" = "" ];then
error "--host option must be set"
fi
if [ "${USER}" = "" ];then
error "--user option must be set"
fi
if [ "${PASSWORD}" = "fakepass" ];then
error "--password options must be set!"
fi
mysql -h "${HOST}" -u "${USER}" --password="${PASSWORD}" -e "quit" || \
error "Can't connect to mysql server"
}
error() {
local err
err=$1
echo -e "check_mysql_galera: ERROR: ${err}" 1>&2
echo -e "Try check_mysql_galera --help to get more help" 1>&2
exit 1
}
mysqlconnect () {
local cmd
cmd="${1}"
mysql -h "${HOST}" -u "${USER}" --password="${PASSWORD}" -s -B -N -e "${cmd}" | sed -e 's/[[:space:]]\{1,\}/,/g' | cut -d"," -f2
}
checkquery () {
galera_cluster_status () {
value=$(mysqlconnect "SHOW STATUS LIKE 'wsrep_cluster_status';")
case $value in
Primary )
echo -e "Status: $value"
exit 0
;;
NON_PRIMARY )
echo -e "Status: $value"
exit 2
;;
DISCONNECTED )
echo -e "Status: $value"
exit 2
;;
* )
echo -e "Status: $value"
exit 3
;;
esac
}
galera_connected () {
value=$(mysqlconnect "SHOW STATUS LIKE 'wsrep_connected';")
case $value in
ON )
echo -e "Status: $value"
exit 0
;;
OFF )
echo -e "Status: $value"
exit 2
;;
* )
echo -e "Status: $value"
exit 3
;;
esac
}
galera_cluster_size () {
value=$(mysqlconnect "SHOW STATUS LIKE 'wsrep_cluster_size';")
if [[ $value -le $CRITICAL ]]; then
echo -e "CRITICAL! Number of nodes connected: $value"
exit 2
elif [[ $value -le $WARNING ]]; then
echo -e "WARNING! Number of nodes connected: $value"
exit 1
else
echo -e "OK! Number of nodes connected: $value"
exit 0
fi
}
galera_thread_count () {
value=$(mysqlconnect "SHOW STATUS LIKE 'wsrep_thread_count';")
if [[ $value -lt 0 ]]; then
echo -e "CRITICAL! Galera thread count: $value"
exit 2
else
echo -e "OK! Galera thread count: $value"
exit 0
fi
}
galera_ready () {
value=$(mysqlconnect "SHOW STATUS LIKE 'wsrep_ready';")
case $value in
ON )
echo -e "OK! Galera provider is ready"
exit 0
;;
OFF)
echo -e "CRITICAL! Galera provider is not ready"
exit 2
;;
esac
}
galera_cluster_synced () {
node_uuid=$(mysqlconnect "SHOW STATUS LIKE 'wsrep_local_state_uuid';")
cluster_uuid=$(mysqlconnect "SHOW STATUS LIKE 'wsrep_cluster_state_uuid';")
if [ "${node_uuid}" = "" ] && [ "${cluster_uuid}" = "" ];then
echo "CRITICAL! Could not get UUID from node nor cluster"
exit 1
fi
if [ "${node_uuid}" = "${cluster_uuid}" ];then
echo -e "OK! Node synchronized with cluster"
exit 0
else
echo -e "CRITICAL! Node not synchronized with cluster"
exit 1
fi
}
case $1 in
cluster-status )
galera_cluster_status
;;
mysql-connected )
galera_connected
;;
cluster-size )
galera_cluster_size
;;
thread-count )
galera_thread_count
;;
ready )
galera_ready
;;
synchronized )
galera_cluster_synced
;;
* )
error "Unsupported command '$1'"
esac
}
[[ "${#}" = "0" ]] && echo -e "To get usage, try --help option." && exit 1
while [[ ${#} > 0 ]]; do
key="${1}"
case $key in
--host )
HOST="${2}"
shift
;;
--user )
USER="${2}"
shift
;;
--password )
PASSWORD="${2}"
shift
;;
--command )
COMMAND="${2}"
shift
;;
--warning )
WARNING="${2}"
shift
;;
--critical )
CRITICAL="${2}"
shift
;;
--help )
usage
exit 0
;;
* )
error "Unsupported option $key"
;;
esac
shift
done
{ check_connection && checkquery "${COMMAND}"; } || error "Can't run command ${COMMAND}"
exit 0