-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathips-proxy
executable file
·276 lines (217 loc) · 5.4 KB
/
ips-proxy
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
#!/bin/bash
show_help() {
cat <<EOF
$ExecName version $Version
Usage:
$ExecName [options]
This program acts like the iCommand ips -v, but it resolves the client IP
addresses on the other side of an HAProxy server include their ports.
Options:
-h, --help show help and exit
-v, --version show version and exit
Requirements:
1) iCommands need to be installed.
2) Need the iRODS environment set up and authenticated for the iCommands, i.e.,
iinit needs to have been called.
3) Need passwordless root access to the IES, e.g., shared public key without
passphrase.
4) Need passwordless access to [email protected].
EOF
}
set -o errexit -o nounset -o pipefail
readonly ExecName=$(basename "$0")
readonly Version=3
main() {
local opts
if ! opts=$(getopt --name "$ExecName" --options hv --longoptions help,version -- "$@")
then
show_help >&2
return 1
fi
eval set -- "$opts"
while true
do
case "$1" in
-h|--help)
show_help
return 0
;;
-v|--version)
show_version
return 0
;;
--)
shift
break
;;
*)
show_help >&2
return 1
;;
esac
done
generate_report
}
# Format the output in a way similar to the ips does.
#
# parameter:
# ies the FQDN of the IES
#
# stdout:
# The report formatted like ips -v
format_report() {
local ies="$1"
printf 'Server: %s\n' "$ies"
local pid
local proxyUser
local clientUser
local connTime
local app
local clientHost
sort --numeric | while read -r pid proxyUser clientUser connTime app clientHost
do
printf '%9d %s %s %s %s %s\n' \
"$pid" "$proxyUser" "$clientUser" "$connTime" "$app" "$clientHost"
done
}
# Generate the report
generate_report() {
local irodsHost
irodsHost=$(ienv | sed --quiet 's/^.*irods_host - //p')
if [ -z "$irodsHost" ]
then
printf 'iRODS Host is not configured\n' >&2
exit 1
fi
local proxyHost
proxyHost=$(resolve_name "$irodsHost")
local proxyIp
proxyIp=$(lookup_address "$proxyHost")
local ipsReport
ipsReport=$(ips -v)
local iesHost
iesHost=$(sed 's/Server: //;q' <<<"$ipsReport")
local iesIp
iesIp=$(lookup_address "$iesHost")
if [ "$iesIp" = "$proxyIp" ]
then
echo "$ipsReport"
else
local iesProcReport
iesProcReport=$(ssh -q -p 1657 "$iesHost" sudo lsof -n -P)
local proxyReport
proxyReport=$( \
ssh -q -p 1657 "$proxyHost" sudo socat /var/run/haproxy.sock stdio <<<'show sess all')
local proxyPortClientMap
proxyPortClientMap=$(map_proxy_port_client "$proxyIp" <<<"$proxyReport")
local iesPidProxyPortMap
iesPidProxyPortMap=$( \
sed --quiet "s/^[^ ]* *\\([^ ]*\\) .* TCP $iesIp:1247->$proxyIp:\\(.*\\) .*/\1 \2/p" \
<<<"$iesProcReport")
local iesPidClientMap
iesPidClientMap=$( \
join -1 2 -o 1.1,2.2 \
<(sort --key 2 <<<"$iesPidProxyPortMap") \
<(sort <<<"$proxyPortClientMap"))
local ipsProxy
# sed's last line indicator, $, as a shell expansion by shellcheck
# shellcheck disable=SC2016
ipsProxy=$(sed --quiet '2,${s/^ *//;s/ */ /g;p}' <<<"$ipsReport")
join -a 1 -o 0,1.2,1.3,1.4,1.5,2.2 <(sort <<<"$ipsProxy") <(sort <<<"$iesPidClientMap") \
| awk 'NF == 6 { print $0 }' \
| format_report "$iesHost"
fi
}
# Lookup the IP address of the provided
#
# parameter:
# name the host name to resolve
#
# stdout:
# the corresponding IP address
lookup_address() {
local name="$1"
host "$name" | sed 's/.* has address //'
}
# From the output of the HAProxy stats command 'show sess all'. this function
# generates a map of proxy TCP ports to the IP address of the correspong client
# being proxied.
#
# parameter:
# proxy the IP address used by HAProxy to connect to the IES
#
# stdin:
# It expects the output of the 'show sess all' command as input.
#
# stdout:
# It generates a map of TCP ports to IP addresses, one per line.
#
# <port> <address?
#
# The address is the address of the client being proxied. The port is the one
# used to connect to the IES.
map_proxy_port_client() {
local proxy="$1"
awk --file - <(cat) \
<<EOS
function print_session(svrPort, source) {
if (svrPort != "" && source != "") {
printf "%s %s\n", svrPort, source;
}
}
BEGIN {
svrPort = "";
source = "";
}
/^[^ ]/ {
print_session(svrPort, source);
source = "";
svrPort = "";
patsplit(\$5, terms, "[^=]+");
if (terms[2] != "unix") {
source = terms[2];
}
}
/^ backend=irods_direct/ {
if (source != "") {
patsplit(\$4, terms, "[^=:]+");
if (terms[2] == "$proxy") {
svrPort = terms[3];
}
}
}
END {
print_session(svrPort, source);
}
EOS
}
# This function looks up the domain name a canonical domain name aliases. It
# does this recursively, so that if a canonical name is an alias for another
# canonical name, the latter will be resolved too. This will repeat until a
# name that isn't an alias is reached.
#
# parameter:
# cname the canonical name to resolve
#
# stdout:
# the resolved domain. This will be cname if cname isn't an alias.
resolve_name() {
local cname="$1"
local name=
while [ -z "$name" ]
do
name=$(host "$cname" | sed --quiet "s/$cname is an alias for \(.*\)\./\1/p")
if [ -n "$name" ]
then
cname="$name"
name=
else
name="$cname"
fi
done
printf '%s' "$name"
}
show_version() {
printf '%s\n' "$Version"
}
main "$@"