-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlspg.sh
275 lines (244 loc) · 8.96 KB
/
lspg.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
#!/bin/bash
##############################################################################
# lspg.sh (github.com/davidkulak/lspg)
##############################################################################
# Liste tous les cluster et bases PostgreSQL d'une machine
##############################################################################
# Parametre(s) :
# -l
# -d
# -t
# -s
# -c
# -v
# -f
# -i
# -r
# -p " < entier > "
# -h
##############################################################################
# Exemple(s) : lspg.sh -d -p 5432
##############################################################################
#Configuration
SEP="------------------------------------------------------------------------"
BINPSQL=$(which psql)
BINVACUUM=$(which vacuumdb)
BINREINDEX=$(which reindexdb)
bold=$(tput bold)
normal=$(tput sgr0)
#Requetes
DBNAME="SELECT datname FROM pg_stat_database WHERE datname != 'postgres' AND datname NOT LIKE 'template%';"
CLNAME="SHOW data_directory;"
CLVERSION="SHOW server_version;"
CLENCODING="SHOW server_encoding;"
CLCONFDIR="SHOW hba_file;"
CLMASTER="select pg_is_in_recovery();"
CLSTANDALONE="select count(1) from pg_stat_replication;"
CLSTANDBY="select client_addr,sync_state from pg_stat_replication;"
CLREPMGR="select count(*) from pg_stat_activity where datname='repmgr_db' AND query LIKE '%INSERT%' AND query NOT LIKE '%pg_stat_activity%';"
CLLASTVACUUM="select last_vacuum from pg_stat_user_tables limit 1;"
CLSTARTUPTIME="SELECT pg_postmaster_start_time();"
REQ="$BINPSQL -h $SOCKDIR -p $PORT -U $PGUSER -d postgres --pset tuples_only 2>/dev/null";
#Fonctions
function fHelp
{
echo " USAGE: ./lspg.sh [l | d | t | s | c | v | f | i | r | p <port>]"
echo " -l | Affiche plus d'infos sur les clusters"
echo " -d | Affiche les bases"
echo " -t | Affiche les tablespaces (si il y en a)"
echo " -s | Affiche encodage et taille des bases si option -d utilisee"
echo " -c | Permet de se connecter ensuite a l'un des clusters proposes"
echo " -v | Permet de faire un vaccum des clusters"
echo " -f | Permet de faire un vaccum full des clusters si option -v est utilisee"
echo " -i | Permet de faire un reindex des clusters"
echo " -r | Permet de faire un reload des clusters"
echo " -p <port> | Ne montre que les bases du cluster <port> "
exit 0;
}
function fVacuum ()
{
SOCKDIR=$1;
PORT=$2;
PGUSER=$3;
if [[ $VACUUMFULL == 1 ]]; then
su - $PGUSER -c "$BINVACUUM -avzf -h $SOCKDIR -p $PORT";
else
su - $PGUSER -c "$BINVACUUM -avz -h $SOCKDIR -p $PORT";
fi
}
function fReindex ()
{
SOCKDIR=$1;
PORT=$2;
PGUSER=$3;
su - $PGUSER -c "$BINREINDEX -a -h $SOCKDIR -p $PORT";
}
function fReload ()
{
SOCKDIR=$1;
PORT=$2;
PGUSER=$3;
DATADIR=$4
BINPGCTL=$(find /usr -type f -name pg_ctl | head -n 1)
su - $PGUSER -c "$BINPGCTL -D $DATADIR reload";
}
function fListTbs ()
{
PG_CL_NAME=$1;
PG_TBS_LISTE=$(ls $PG_CL_NAME/pg_tblspc/);
if [ "$PG_TBS_LISTE" != "" ]; then
echo -e "${bold}[TABLESPACES]${normal}";
for tbs in $PG_TBS_LISTE; do
location=$(readlink -f $PG_CL_NAME/pg_tblspc/$tbs);
echo " * $location";
done;
fi
}
function fListDbs ()
{
SOCKDIR=$1;
PORT=$2;
PGUSER=$3;
echo -e "${bold}[BASES]${normal}";
PG_DB_LISTE=$(su - $PGUSER -c "echo \"$DBNAME\" | $REQ ");
for base in $PG_DB_LISTE; do
if [[ $SHOWDBSIZE == 1 ]]; then
BASEENCODING="SELECT pg_encoding_to_char(encoding) FROM pg_database WHERE datname = '$base';";
BASESIZE="SELECT pg_size_pretty(pg_database_size('$base'));";
encodage=$(su - $PGUSER -c "echo \"$BASEENCODING\" | $REQ ");
size=$(su - $PGUSER -c "echo \"$BASESIZE\" | $REQ ");
lastvacuum=$(su - $PGUSER -c "echo \"$CLLASTVACUUM\" | $BINPSQL -h $SOCKDIR -p $PORT -U $PGUSER -d $base --pset tuples_only 2>/dev/null ");
lastvacuumpretty=$(echo $lastvacuum | cut -d ' ' -f 1);
printf " * \e[1;32m%-30s \e[0m%-10s %-10s %-30s \n" $base $encodage ${size//[[:blank:]]/} LastVacuum:$lastvacuumpretty;
else
echo -e " * \e[1;32m$base\e[0m";
fi
done;
}
function fCluster ()
{
PORT=$1;
COUNT=$2;
PID=$(echo $i |awk -F '/' '{print $1}' |awk -F '_' '{print $2}');
SOCKDIR=$(lsof -U -a -Fn +p $PID | grep PGSQL | cut -c2- |awk -F '.' '{print $1}' |head -n 1)
PGUSER=$(ps aux |grep $PID |grep postgres | awk '{print $1}' | uniq);
[[ $PGUSER =~ ^-?[0-9]+$ ]] && PGUSER=$(cat /etc/passwd | grep $PGUSER | awk -F ':' '{print $1}');
REQ="$BINPSQL -h $SOCKDIR -p $PORT -U $PGUSER -d postgres --pset tuples_only 2>/dev/null";
PG_CL_VERSION=$(su - $PGUSER -c "echo \"$CLVERSION\" | $REQ " | sed 's/ //g');
PG_CL_ENCODING=$(su - $PGUSER -c "echo \"$CLENCODING\" | $REQ " | sed 's/ //g');
PG_CL_NAME=$(su - $PGUSER -c "echo \"$CLNAME\" | $REQ " | sed 's/ //g');
PG_CL_STARTUPTIME=$(su - $PGUSER -c "echo \"$CLSTARTUPTIME\" | $REQ " | sed 's/^ //g' | cut -d '.' -f 1);
PG_CL_MASTER=$(su - $PGUSER -c "echo \"$CLMASTER\" | $REQ " | sed 's/ //g');
eval USER_$COUNT=${PGUSER};
eval SOCK_$COUNT=${SOCKDIR};
eval PORT_$COUNT=${PORT};
# Si Postgresql superieur a 9.0 alors replication possible sinon forcement en standalone
if [[ $PG_CL_VERSION =~ 9.* ]]; then
# Si pg_is_in_recovery() est false on est sur le master ou sur un standalone, si true on est sur le standby
if [ $PG_CL_MASTER == "f" ]; then
# Si la vue pg_stat_replication ne renvoie rien on est sur un santalone, sinon sur un master
PG_CL_STANDALONE=$(su - $PGUSER -c "echo \"$CLSTANDALONE\" | $REQ " | sed 's/ //g');
if [ $PG_CL_STANDALONE == 0 ]; then
PGTYPE="\033[34mStandalone\033[0m";
MASTER=0;
else
MASTER=1;
PG_CL_STANDBY=$(su - $PGUSER -c "echo \"$CLSTANDBY\" | $REQ " | sed 's/ //g' | sed 's/|/:/g' | sed ':a;N;$!ba;s/\n/ /g');
PG_CL_REPMGR=$(su - $PGUSER -c "echo \"$CLREPMGR\" | $REQ " | sed 's/ //g');
if [ $PG_CL_REPMGR == 0 ]; then
REPMGR="\033[31m(REPMGR=KO)\033[0m"
else
REPMGR="\033[32m(REPMGR=OK)\033[0m"
fi
PGTYPE="\033[32mMaster ( $PG_CL_STANDBY) $REPMGR\033[0m";
fi
else
PGTYPE="\033[33mStandby\033[0m";
fi
else
PGTYPE="\033[34mStandalone\033[0m";
fi
echo $SEP;
if [[ $CONNECT == 1 ]]; then
echo -e "${bold}[\033[35m$COUNT\033[0m]${bold}[CLUSTER]${normal} (${PG_CL_NAME##*/}) $PGTYPE";
else
echo -e "${bold}[CLUSTER]${normal} (${PG_CL_NAME##*/}) $PGTYPE";
fi
echo -e " * Port : \e[90m$PORT\e[0m";
echo -e " * Version : \e[90m$PG_CL_VERSION\e[0m";
if [[ $DETAIL == 1 ]]; then
echo -e " * Encodage : \e[90m$PG_CL_ENCODING\e[0m";
echo -e " * SockDir : \e[90m$SOCKDIR\e[0m";
fi
echo -e " * DataDir : \e[90m$PG_CL_NAME\e[0m";
if [[ $DETAIL == 1 ]]; then
echo -e " * StartTime : \e[90m$PG_CL_STARTUPTIME\e[0m";
fi
# On liste les Tablespaces existants (si on en trouve)
if [[ $SHOWTBS ]]; then
fListTbs $PG_CL_NAME
fi
# On liste les Bases existantes
if [[ $SHOWDB ]]; then
fListDbs $SOCKDIR $PORT $PGUSER
fi
# On Vacuum
if [[ $VACUUM ]]; then
fVacuum $SOCKDIR $PORT $PGUSER
fi
# On Reindex
if [[ $REINDEX ]]; then
fReindex $SOCKDIR $PORT $PGUSER
fi
# On recharge la configuration
if [[ $RELOAD ]]; then
fReload $SOCKDIR $PORT $PGUSER $PG_CL_NAME
fi
}
#Init
COUNTER=1
#Parametres
while getopts dtlcsvfirp:h option
do
case $option in
l) DETAIL=1 ;;
d) SHOWDB=1 ;;
s) SHOWDBSIZE=1 ;;
t) SHOWTBS=1 ;;
c) CONNECT=1 ;;
v) VACUUM=1 ;;
f) VACUUMFULL=1 ;;
i) REINDEX=1 ;;
r) RELOAD=1 ;;
p) PORTOPTS=${OPTARG} ;;
h) fHelp ;;
*) fHelp ;;
esac
done
#Main
if [ "$(id -u)" != "0" ]; then
echo "Script must be run as root" 1>&2
exit 1
fi
if [[ $PORTOPTS ]]; then
SERVER=$(netstat -laputen | grep -i -e postgres -e postmaster | grep LISTEN | grep 0.0.0.0 | grep $PORTOPTS | awk '{print $4"_"$9}')
else
SERVER=$(netstat -laputen | grep -i -e postgres -e postmaster | grep LISTEN | grep 0.0.0.0 | awk '{print $4"_"$9}')
fi
if [[ $SERVER != "" ]]; then
for i in $SERVER; do
PORT=$(echo $i |awk -F ':' '{print $2}' |awk -F '_' '{print $1}');
fCluster $PORT $COUNTER
COUNTER=$[$COUNTER +1]
done
else
echo -e "${bold}\033[31mNo running PostgreSQL cluster detected !\033[0m${normal}"
fi
if [[ $CONNECT == 1 ]]; then
echo " "
read -p "A quel numero de cluster voulez vous vous connecter ? : " num
CONNECT_USER=$(eval 'echo ${'USER_${num}'}')
CONNECT_SOCK=$(eval 'echo ${'SOCK_${num}'}')
CONNECT_PORT=$(eval 'echo ${'PORT_${num}'}')
su - $CONNECT_USER -c "$BINPSQL -h $CONNECT_SOCK -p $CONNECT_PORT -U $CONNECT_USER -d postgres"
fi