forked from andersdd/xtuple-utility
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconman.sh
More file actions
executable file
·211 lines (163 loc) · 4.49 KB
/
conman.sh
File metadata and controls
executable file
·211 lines (163 loc) · 4.49 KB
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
#!/bin/bash
WORKDIR=`pwd`
#source common.sh
#source logging.sh
menu_title=Conman
MYIPADDR=`arp $(hostname) | awk -F'[()]' '{print $2}'`
connectSSH()
{
ssh $CONNECTION
RET=$?
if [ $RET -ne 0 ]; then
msgbox "Error Connecting to $CONNECTION"
fi
# selectServer
}
setEC2Data()
{
REMOTEEC2DATA=`ssh $CONNECTION ec2metadata`
}
setPGInfo()
{
setEC2Data
REMOTEIPV4=`ssh $CONNECTION ec2metadata --public-ipv4`
REMOTEPGINFO=`ssh $CONNECTION pg_lsclusters -h | head -1`
REMOTEPGVER=$(echo $REMOTEPGINFO | cut -d' ' -f1)
REMOTEPGCLUSTER=$(echo $REMOTEPGINFO | cut -d' ' -f2)
REMOTEPGPORT=$(echo $REMOTEPGINFO | cut -d' ' -f3)
REMOTEPGSTATE=$(echo $REMOTEPGINFO | cut -d' ' -f4)
REMOTEPGOWNER=$(echo $REMOTEPGINFO | cut -d' ' -f5)
REMOTEPGDATA=$(echo $REMOTEPGINFO | cut -d' ' -f6)
REMOTEPGLOG=$(echo $REMOTEPGINFO | cut -d' ' -f7)
REMOTEPGHOME=/etc/postgresql/${REMOTEPGVER}/${REMOTEPGCLUSTER}
REMOTEPGHBA=${REMOTEPGHOME}/pg_hba.conf
REMOTEPGCONF=${REMOTEPGHOME}/postgresql.conf
}
readHbaConf()
{
setPGInfo
PGHBAINFO=`ssh ${CONNECTION} "sudo cat ${REMOTEPGHBA}"`
msgbox "${PGHBAINFO}"
}
getEC2Info()
{
setEC2Data
msgbox "$REMOTEEC2DATA"
}
getDiskInfo()
{
DISKSTAT=`ssh $CONNECTION df -h`
msgbox "${DISKSTAT}"
}
createPGTunnel()
{
setPGInfo
RANDPORT=`shuf -i 5500-65000 -n 1`
SOCKETNAME=${CONNECTION}_ctrl-socket
ssh -M -S ${SOCKETNAME} -fnNT -L${RANDPORT}:localhost:$REMOTEPGPORT $CONNECTION
sleep 5
}
killPGTunnel()
{
ssh -S ${SOCKETNAME} -O exit $CONNECTION
}
createTunnel()
{
createPGTunnel
msgbox "Tunnel to $CONNECTION Created on ${RANDPORT} \n
Socket: ${WORKDIR}/${SOCKETNAME}\n
Kill the socket manually with: \n
ssh -S ${SOCKETNAME} -O exit ${CONNECTION} \n
You can close this message box."
cat << EOF >> cleansockets.sh
ssh -S ${SOCKETNAME} -O exit ${CONNECTION}
EOF
}
getPGInfo()
{
createPGTunnel
PGCONN="psql -At -U postgres -h localhost -p ${RANDPORT}"
msgbox "Created PG Tunnel to ${CONNECTION} on Port ${RANDPORT}"
NUMPGUSERS=`$PGCONN postgres -c "SELECT count(*) FROM pg_stat_activity;"`
TUNDATABASES=`$PGCONN postgres -c "SELECT datname FROM pg_database WHERE datname NOT IN ('postgres','template1','template0') ORDER BY 1 LIMIT 10;"`
for TUNDATABASE in $TUNDATABASES; do
DBVERS+=`$PGCONN -d $TUNDATABASE -c "SELECT ' ${TUNDATABASE}: Ap: '||fetchmetrictext('Application')||' v'||fetchmetrictext('ServerVersion');"`
done
TUNPGTEST=`$PGCONN postgres -c "SELECT now();"`
RET=$?
if [ $RET -ne 0 ]; then
msgbox "Error connecting to PostgreSQL on $CONNECTION"
else
msgbox "While this window is SHOWING,\nYou can connect with: \n
Server: localhost (or ${MYIPADDR}) \n
Port: ${RANDPORT} \n
User: admin \n
Password: None \n
Databases: \n
${TUNDATABASES}\n
Versions:\n
${DBVERS}\n
Other Info:\n
${REMOTEPGINFO}\n
User Count: ${NUMPGUSERS}\n
Remote Connection Info: \n
Server: $REMOTEIPV4 Port: $REMOTEPGPORT User: admin Pass: None"
fi
killPGTunnel
msgbox "Killed Tunnel to ${CONNECTION}"
}
getClusterInfo()
{
VAL=`ssh $CONNECTION psql -U postgres -l`
RET=$?
if [ $RET -ne 0 ]; then
msgbox "Error Connecting to $CONNECTION"
else
msgbox "PGInfo For $CONNECTION \n $VAL"
fi
}
conman_menu() {
#log "Opened server menu"
while true; do
DBM=$(whiptail --backtitle "Viewing $CONNECTION" --menu "Actions on $CONNECTION" 20 80 10 --cancel-button "Cancel" --ok-button "Select" \
"1" "Connect SSH to $CONNECTION" \
"2" "Inspect And Connect to PG on $CONNECTION" \
"3" "Create Tunnel to $CONNECTION" \
"4" "View pg_hba.conf on $CONNECTION" \
"5" "View Disk Info on $CONNECTION" \
"6" "View EC2 Info for $CONNECTION" \
"9" "Select another Server" \
"10" "Exit Program" \
3>&1 1>&2 2>&3)
RET=$?
if [ $RET -ne 0 ]; then
break
else
case "$DBM" in
"1") connectSSH ;;
"2") getPGInfo ;;
"3") createTunnel ;;
"4") readHbaConf ;;
"5") getDiskInfo ;;
"6") getEC2Info ;;
"9") selectServer ;;
"10") exit 0 ;;
*) msgbox "How did you get here?" && break ;;
esac
fi
done
}
selectServer()
{
CONNECTIONS=()
while read -r line; do
CONNECTIONS+=("$line" "$line")
done < <(grep '^Host ' ~/.ssh/config | grep -v '[?*]' | cut -d ' ' -f 2- | sort)
CONNECTION=$(whiptail --title "XTN Servers" --menu "Reading ${HOME}/.ssh/config . Select server to connect to" 40 80 30 "${CONNECTIONS[@]}" --notags 3>&1 1>&2 2>&3)
RET=$?
if [ $RET -ne 0 ]; then
return 0
fi
conman_menu
}
# selectServer