-
Notifications
You must be signed in to change notification settings - Fork 3
/
gather-logs
executable file
·155 lines (122 loc) · 2.85 KB
/
gather-logs
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
#!/bin/bash
show_help()
{
# shellcheck disable=SC2006
cat \
<<EOF
$ExecName version $Version
Usage:
$ExecName [options] <irods_server>
Downloads iRODS log entries from <irods_server>
Parameters:
<irods_server> the iRODS server containing the log files
Options:
-E, --extension-pattern A globbing pattern applied to the log files'
extension. Only entries from the matching files will
be downloaded.
-P, --password A password, if any, the user needs to execute sudo on
<irods_server>.
-h, --help show help and exit
-v, --version show version and exit
Summary:
This program downloads all of the log entries from <irods_server>. The set of
log files may be restricted to those with certain file extensions. Each entry is
formatted by `format-log-entries`.
The program first connects to the server as the current user. Once on the server,
it uses sudo to inspect the log file. If sudo will request a password, it should
be provided using the appropriate command line option.
EOF
}
set -o errexit -o nounset -o pipefail
readonly Version=1
readonly ExecAbsPath=$(readlink --canonicalize "$0")
readonly ExecName=$(basename "$ExecAbsPath")
readonly ExecDir=$(dirname "$ExecAbsPath")
readonly LogBase=rodsLog
main()
{
local opts
if ! opts=$(format_opts "$@")
then
show_help >&2
return 1
fi
eval set -- "$opts"
local extPat='*'
local password=
while true
do
case "$1" in
-E|--extension-pattern)
extPat="$2"
shift 2
;;
-h|--help)
show_help
return 0
;;
-P|--password)
password="$2"
shift 2
;;
-v|--version)
printf '%s' "$Version"
return 0
;;
--)
shift
break
;;
*)
show_help >&2
return 1
;;
esac
done
if [[ "$#" -lt 1 ]]
then
show_help >&2
return 1
fi
local host="$1"
gather_logs "$host" "$password" "$extPat"
}
format_opts()
{
getopt \
--longoptions extension-pattern:,help,password:,version \
--options E:hP:v \
--name "$ExecName" \
-- \
"$@"
}
gather_logs()
{
local host="$1"
local password="$2"
local extPat="$3"
local namePat="$LogBase"."$extPat"
local log
for log in $("$ExecDir"/list-rods-logs --name-pattern "$namePat" --password "$password" "$host")
do
local logName
logName=$(basename "$log")
rcat_log "$host" "$password" "$log" | "$ExecDir"/format-log-entries "${logName:8:4}"
printf 'gather_logs: finished processing %s\n' "$logName" >&2
done
}
rcat_log()
{
local host="$1"
local password="$2"
local log="$3"
#shellcheck disable=SC2087
ssh -q -t "$host" 2> /dev/null \
<<EOF
if ! cat "$log" 2> /dev/null
then
printf '%s\n' "$password" | sudo -S cat "$log"
fi
EOF
}
main "$@"