-
Notifications
You must be signed in to change notification settings - Fork 1
/
connectPMM.sh
351 lines (271 loc) · 9.06 KB
/
connectPMM.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
#!/usr/bin/env bash
#
# This script will perform all needed steps to install pmm's latest client, connect
# to a PMM server, create a monitoring DB user and start monitoring
#
#
set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT
# Set defaults
default_db_username="pmm_monitor"
default_db_password="S0meWh@tSafe?"
#######################################
# Show Script Usage Information
#######################################
#######################################
# Defines colours for output messages.
#######################################
setup_colors() {
if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then
NOFORMAT='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m'
BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' YELLOW='\033[1;33m'
else
NOFORMAT='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW=''
fi
}
#######################################
# Prints message to stderr with new line at the end.
#######################################
msg() {
echo >&2 -e "${1-}"
}
#######################################
# Prints message and exit with code.
# Arguments:
# message string;
# exit code.
# Outputs:
# writes message to stderr.
#######################################
die() {
local msg=$1
local code=${2-1} # default exit status 1
msg "$msg"
exit "$code"
}
#######################################
# Clean up setup if interrupt.
#######################################
cleanup() {
trap - SIGINT SIGTERM ERR EXIT
}
######################################
# Check if command exists
######################################
check_command() {
command -v "$@" 1>/dev/null
}
#######################################
# Runs command as root.
#######################################
run_root() {
sh='sh -c'
if [ "$(id -un)" != 'root' ]; then
if check_command sudo; then
sh='sudo -E sh -c'
elif check_command su; then
sh='su -c'
else
die "${RED}ERROR: root rights needed to run "$*" command${NOFORMAT}"
fi
fi
${sh} "$@"
}
#######################################
# What is the OS (to know what packages to get)
#######################################
check_os() {
case "$(uname -s)" in
*darwin* | *Darwin* ) OS=mac ;;
*Linux* | *linux* )
case "$(awk -F= '/^ID_LIKE/{print $2}' /etc/os-release)" in
*rhel* ) OS=redhat ;;
*debian* ) OS=debian ;;
esac
;;
esac
}
##############################################
# Check if pmm client installed and the latest version
##############################################
if ! check_command pmm-admin; then
echo "PMM Client not installed, downloading and installing"
check_os
echo "OS is $OS"
if [ $OS == "mac" ]; then
echo "Mac is not supported for this installer yet"
exit
elif [ $OS == "debian" ]; then
run_root 'apt update'
run_root 'apt install -y wget gnupg2 lsb-release'
wget https://repo.percona.com/apt/percona-release_latest.generic_all.deb
run_root 'dpkg -i percona-release_latest.generic_all.deb'
run_root 'percona-release enable-only pmm2-client'
run_root 'apt update'
run_root 'apt install -y pmm2-client'
rm -f percona-release_latest.generic_all.deb
elif [ $OS == "redhat" ]; then
echo "installing for redhat"
run_root 'yum -y install https://repo.percona.com/yum/percona-release-1.0-27.noarch.rpm'
run_root 'percona-release enable-only pmm2-client'
run_root 'yum -y install pmm2-client'
else
echo "could not detect os properly"
fi
else
echo "PMM Client installed, moving to configuration"
fi
# test connectivity to PMM Server
# Connect to PMM server
############################################
# Gather Data to setup monitoring user
############################################
collect_params() {
echo -n "Please provide a monitoring username [$default_db_username]:"; read db_username
: ${db_username:="$default_db_username"}
proceed="false"
until [ $proceed == "true" ]
do
echo -n "Please provide a password for the monitoring user [$default_db_password]" ; read -s db_password
: ${db_password:="$default_db_password"}
echo
if [ $db_password != $default_db_password ]; then
echo -n "Please re-enter password to confirm:" ; read -s db_password2
if [ $db_password == $db_password2 ] ; then
proceed="true"
else
echo
echo -n "Passwords do not match, try again"
fi
else
proceed="true"
fi
done
}
###############################################
# set up database user
###############################################
setup_mongo() {
if ! check_command mongo; then
echo "mongo command not detected...is mongodb installed on this server?"
# exit
fi
# mongo <<EOF
cat <<EOF
db.getSiblingDB("admin").createRole({
role: "explainRole",
privileges: [{
resource: {
db: "",
collection: ""
},
actions: [
"listIndexes",
"listCollections",
"dbStats",
"dbHash",
"collStats",
"find"
]
}],
roles:[]
})
db.getSiblingDB("admin").createUser({
user: "$db_username",
pwd: "$db_password",
roles: [
{ role: "explainRole", db: "admin" },
{ role: "clusterMonitor", db: "admin" },
{ role: "read", db: "local" }
]
})
EOF
}
setup_postgres() {
if ! check_command psql; then
echo "psql command not detected...is Postgres installed on this server?"
# exit
fi
psql <<EOF
CREATE USER $db_username WITH SUPERUSER ENCRYPTED PASSWORD '$db_password'
ALTER USER $db_username CONNECTION LIMIT 10;
EOF
#need to find the location of the pg_hba.conf file to add access for the PMM user
hba_location=run_root "psql -t -P format=unaligned -c 'show hba_file';"
echo "local all pmm md5" >> $hba_location
}
setup_mysql() {
if ! check_command mysql; then
echo "mysql command not detected...is MySQL client installed on this server?"
exit
fi
echo -n "Please provide a username that has the CREATE USER privilege: "; read monitor_username
echo -n "Please provide the password for the user $monitor_username: "; read -s monitor_password
echo
echo -n "Please provide the hostname/IP of your MySQL server: "; read monitor_hostname
echo -n "Please provide the port that your mysql server is running on (default 3306): " ; read monitor_port
: ${monitor_port:="3306"}
mysql -u $monitor_username -p $monitor_password -h $monitor_hostname -P $monitor_port << MYSQL_SCRIPT
CREATE USER '$db_username'@'localhost' IDENTIFIED BY '$db_password' WITH MAX_USER_CONNECTIONS 10;
GRANT SELECT, PROCESS, REPLICATION CLIENT, RELOAD ON *.* TO '$db_username'@'localhost';
MYSQL_SCRIPT
if [ $? -eq 0 ] ; then
echo "Successfully added a monitoring user"
else
echo "There was a problem creating a monitoring user, check your logs, fix and try again"
fi
}
###############################################
# register with PMM
################################################
register_pmm_node() {
#Register server to PMM
echo -n "Please provide an admin account to register with your PMM server [admin]: "; read pmm_username
: ${pmm_username:="admin"}
echo -n "Please provide the password for the "$pmm_username" account: "; read -s pmm_password
echo
echo -n "Please provide the hostname or IP address of your PMM server: "; read pmm_hostname
#logic needed so can't be blank...probably password too
echo -n "Please provide a secure port for your pmm server [443]: "; read pmm_port
: ${pmm_port:="443"}
echo -n "Please provide a friendly name for this system [`hostname -s`]: "; read pmm_node_name
: ${pmm_node_name:="`hostname -s`"}
pmm-admin config --server-insecure-tls --server-url=https://$pmm_username:$pmm_password@$pmm_hostname:$pmm_port $pmm_hostname generic $pmm_node_name
#register_command="pmm-admin config --server-insecure-tls --server-url=https://$pmm_username:$pmm_password@$pmm_hostname:$pmm_port" $pmm_hostname generic $pmm_node_nam
#echo $register_command
#run_root '$register_command'
#connect client with sane defaults
#mysql
echo -n "Please choose either 'perfschema' or 'slowlog' for detailed Query Analytics [perfschema]: "; read pmm_qan
: ${pmm_qan:="perfschema"}
pmm-admin add $database --username=$db_username --password=$db_password --query-source=$pmm_qan
#monitor_command="pmm-admin add $database --username=$db_username --password=$db_password --query-source=$pmm_qan"
#echo $monitor_command
#run_root '$register_command'
}
################################################
# manually specify DB options
# FUTURE: auto-detect before failing to prompt
###############################################
select database in mysql postgresql mongodb exit; do
case $database in
mysql ) echo "Gathering info for MySQL"
collect_params
setup_mysql
register_pmm_node
break ;;
postgresql ) echo "Gathering info for PostgreSQL"
collect_params
setup_postgres
break ;;
mongodb ) echo "Gathering info for MongoDB"
collect_params
setup_mongo
break ;;
exit) echo "exiting"
break ;;
esac
done
# enable services (pg_stat_monitor, other)
# gather inputs
#locally execute or remote execute