-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiagnostics.sh
executable file
·190 lines (157 loc) · 3.77 KB
/
diagnostics.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
#!/bin/sh
# set proper gettext domain
export TEXTDOMAIN=turris-diagnostics
export TEXTDOMAINDIR=/usr/share/locale
MAX_LINES_PER_MODULE=${MAX_LINES_PER_MODULE:-100000}
MODULES_PATH=./modules
# enter the script directory
cd "$(dirname $0)"
module_exists() {
[ -x "$1" ]
}
module_name() {
local t="$(basename "${1%.module}")"
echo "${t#*_}" # remove initial index from XX_name
}
module_from_name() {
# There should be only one module of the same name but to be sure we
# potentially select the one with lower number.
find -name "??_$1.module" | head -1
}
list_modules() {
for module in "$MODULES_PATH"/*.module; do
[ -f "$module" ] || continue
echo "$(module_name "$module")"
"$module" help | sed 's/^/ /'
done
}
module_help() {
for module in "$MODULES_PATH"/*.module; do
[ -f "$module" ] || continue
printf " %s\n" "$(module_name "$module")"
"$module" help | sed 's/^/ /'
echo
done
}
print_usage() {
echo "Usage: $(basename $0) [-h | -l | -b] [-o <file> | -O <directory>] [module]..."
}
print_help() {
print_usage
echo
echo "Arguments:"
echo " -h $(gettext "print this help text")"
echo " -l $(gettext "list all available modules in machine readable format")"
echo " -b $(gettext "run in background")"
echo " -o <file> $(gettext "print output to a file")"
echo " -O <file> $(gettext "print output to a directory module per file")"
echo
gettext "modules:"
echo
module_help
}
module_run() {
local module="$1"
if [ -n "$OUTPUT_DIRECTORY" ] ; then
module_wrapper "$module" >> "$OUTPUT_DIRECTORY.preparing/$module.out.preparing"
mv "$OUTPUT_DIRECTORY.preparing/$module".out.preparing "$OUTPUT_DIRECTORY.preparing/$module".out
elif [ -n "$OUTPUT_FILE" ] ; then
{
module_header "$module"
module_wrapper "$module"
module_footer "$module"
} >> "$OUTPUT_FILE".preparing
else
module_header "$module"
module_wrapper "$module"
module_footer "$module"
fi
}
module_header() {
local module="$1"
printf "############## %s\n" "$(module_name "$module")"
}
module_footer() {
local module="$1"
printf "************** %s\n" "$(module_name "$module")"
}
module_wrapper() {
"$1" run 2>&1 < /dev/null | tail -n "$MAX_LINES_PER_MODULE"
}
OUTPUT_FILE=
OUTPUT_DIRECTORY=
list_modules="n"
background="n"
while getopts "hlbBo:O:" opt; do
case "$opt" in
h)
print_help
exit 0
;;
l)
list_modules="y"
;;
b)
background="y"
;;
o)
OUTPUT_FILE="$OPTARG"
;;
O)
OUTPUT_DIRECTORY="$OPTARG"
;;
*)
print_usage
exit 1
;;
esac
done
if [ "$background" = "y" -a "$TURRIS_DIAGNOSTICS_IN_BACKGROUND" != "1" ]; then
TURRIS_DIAGNOSTICS_IN_BACKGROUND=1 "$0" "$@" >/dev/null 2>&1 </dev/null &
exit 0
fi
shift "$((OPTIND - 1))"
if [ "$list_modules" = "y" ]; then
list_modules
exit 0
fi
# Clean the last output files
if [ -n "$OUTPUT_FILE" ]; then
rm -rf "$OUTPUT_FILE"
rm -rf "$OUTPUT_FILE".preparing
elif [ -n "$OUTPUT_DIRECTORY" ]; then
rm -rf "$OUTPUT_DIRECTORY" "$OUTPUT_DIRECTORY.preparing"
mkdir -p "$OUTPUT_DIRECTORY.preparing" || {
echo "Failed to created the log directory" >&2
exit 1
}
fi
module_executed="n"
if [ $# = 0 ] ; then
# no parameters run all modules
for module in "$MODULES_PATH"/*.module; do
[ -f "$module" ] || continue
module_run "$module"
module_executed="y"
done
else
for module_name in "$@"; do
module="$(module_from_name "$module_name")"
if ! module_exists "$module"; then
printf "!!!!!!!!!!!!!! %s not found\n" "$module_name"
else
module_run "$module"
module_executed="y"
fi
done
fi
# rename the output directory when finished
if [ -n "$OUTPUT_DIRECTORY" ] ; then
mv "$OUTPUT_DIRECTORY".preparing "$OUTPUT_DIRECTORY"
else
# rename the output file when finished
if [ -n "$OUTPUT_FILE" ] ; then
mv "$OUTPUT_FILE".preparing "$OUTPUT_FILE"
fi
fi
[ "$module_executed" = "y" ]