-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrca.sh
More file actions
371 lines (330 loc) · 12.4 KB
/
rca.sh
File metadata and controls
371 lines (330 loc) · 12.4 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/bin/bash
# Configuration
RCA_ANALYSIS_DIR="rca_analysis"
MESSAGES_FILE="messages.txt"
BOOT_FILE="boot.txt"
NTP_FILE="ntp.txt"
# Color codes
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Create directory if it doesn't exist
mkdir -p "$RCA_ANALYSIS_DIR"
# --- Helper function to convert various date strings to epoch ---
_get_epoch() {
local date_str="$1"
LC_ALL=C date -d "$date_str" +%s 2>/dev/null
}
# --- Function to show progress ---
show_progress() {
local message="$1"
printf "%s" "$message"
for ((i=0; i<3; i++)); do
sleep 0.5
printf "."
done
}
# --- Function to filter /var/log/messages ---
filter_messages() {
local output_file="$RCA_ANALYSIS_DIR/log-messages.out"
local start_time="$1"
local end_time="$2"
if [[ ! -f "$MESSAGES_FILE" ]]; then
echo -e "${RED}Error: $MESSAGES_FILE not found!${NC}"
return 1
fi
show_progress "Filtering /var/log/messages..."
if [[ -n "$start_time" && -n "$end_time" ]]; then
start_time_escaped="${start_time//\//\\/}"
end_time_escaped="${end_time//\//\\/}"
sed -n "/${start_time_escaped}/,/${end_time_escaped}/{/^#==/q;p}" "$MESSAGES_FILE" > "$output_file"
elif [[ -n "$start_time" && -z "$end_time" ]]; then
start_time_escaped="${start_time//\//\\/}"
sed -n "/${start_time_escaped}/,/#==/{/^#==/q;p}" "$MESSAGES_FILE" > "$output_file"
else
sed -n '/\/var\/log\/messages/,/#==/{/^#==/q;p}' "$MESSAGES_FILE" > "$output_file"
fi
if [[ -s "$output_file" ]]; then
echo -e " ${GREEN}Done${NC}"
else
echo -e " ${RED}Fail${NC}"
fi
}
# --- Function to filter specific boot log sections ---
_filter_single_boot_log() {
local boot_file="$1"
local search_pattern="$2"
local output_file="$3"
local description="$4"
show_progress "Filtering $description from $boot_file..."
search_pattern_escaped=$(echo "$search_pattern" | sed 's/\//\\\//g')
sed -n "/# ${search_pattern_escaped}/,/#==/ p" "$boot_file" > "$output_file.tmp"
if [[ -s "$output_file.tmp" ]]; then
cp "$output_file.tmp" "$output_file"
rm "$output_file.tmp"
echo -e " ${GREEN}Done${NC}"
else
echo -e " ${RED}Fail${NC}"
rm "$output_file.tmp"
fi
}
filter_boot_logs() {
local filter_type="$1"
if [[ ! -f "$BOOT_FILE" ]]; then
echo -e "${RED}Error: $BOOT_FILE not found!${NC}"
return 1
fi
local run_journal=false
local run_log=false
local run_dmesg=false
case "$filter_type" in
journal) run_journal=true ;;
log) run_log=true ;;
dmesg) run_dmesg=true ;;
all)
run_journal=true
run_log=true
run_dmesg=true
;;
*)
echo -e "${RED}Error: Invalid filter type '$filter_type' for -boot.${NC}"
echo "Valid types are: journal, log, dmesg, all."
return 1
;;
esac
if $run_journal; then
_filter_single_boot_log "$BOOT_FILE" "/usr/bin/journalctl --no-pager --boot 0" "$RCA_ANALYSIS_DIR/journalctl_no_pager.out" "journalctl --no-pager --boot 0"
fi
if $run_log; then
_filter_single_boot_log "$BOOT_FILE" "/var/log/boot.log" "$RCA_ANALYSIS_DIR/var_log_boot.out" "/var/log/boot.log"
fi
if $run_dmesg; then
_filter_single_boot_log "$BOOT_FILE" "/bin/dmesg -T" "$RCA_ANALYSIS_DIR/dmesg.out" "dmesg -T"
fi
}
# --- Function to show server time and timezone ---
show_server_time() {
local output_file="$RCA_ANALYSIS_DIR/server_time_info.txt"
if [[ ! -f "$NTP_FILE" ]]; then
echo "Error: $NTP_FILE not found!"
return 1
fi
echo "Server time and timezone information (from timedatectl output):"
# Extract only the relevant lines from timedatectl output
sed -n '/^# \/usr\/bin\/timedatectl/,/^#==/{/Local time:/p; /Universal time:/p; /RTC time:/p; /Time zone:/p; /System clock synchronized:/p; /NTP service:/p; /RTC in local TZ:/p}' "$NTP_FILE" | sed 's/^# \/usr\/bin\/timedatectl//; s/^#==//' > "$output_file.tmp"
if [[ -s "$output_file.tmp" ]]; then
mv "$output_file.tmp" "$output_file"
cat "$output_file"
else
echo "Could not find 'timedatectl' output in $NTP_FILE."
rm "$output_file.tmp" 2>/dev/null
fi
}
# --- Function to find last reboot time ---
find_last_reboot() {
local output_file="$RCA_ANALYSIS_DIR/last_reboot.out"
if [[ ! -f "$BOOT_FILE" ]]; then
echo -e "${RED}Error: $BOOT_FILE not found!${NC}" > "$output_file"
return 1
fi
show_progress "Scanning for last reboot information..."
local max_epoch=0
local max_line=""
local max_source=""
local last_output_section
last_output_section=$(sed -n '/^# \/usr\/bin\/last -wxF | egrep "reboot|shutdown|runlevel|system"/,/#==/p' "$BOOT_FILE")
if [[ -n "$last_output_section" ]]; then
echo "$last_output_section" | while IFS= read -r line; do
if [[ "$line" =~ reboot[[:space:]]+system[[:space:]]+boot ]]; then
local date_str_match
date_str_match=$(echo "$line" | grep -oE '[A-Za-z]{3}[[:space:]]+[A-Za-z]{3}[[:space:]]+[0-9]{1,2}[[:space:]]+[0-9]{2}:[0-9]{2}:[0-9]{2}[[:space:]]+[0-9]{4}')
if [[ -n "$date_str_match" ]]; then
local current_epoch
current_epoch=$(_get_epoch "$date_str_match")
if [[ -n "$current_epoch" && "$current_epoch" -gt "$max_epoch" ]]; then
max_epoch=$current_epoch
max_line="$line"
max_source="last -wxF output"
fi
fi
fi
done
fi
local who_b_content
who_b_content=$(sed -n -e '/^# \(\/usr\)\?\/bin\/who -b/{n;p;q}' "$BOOT_FILE")
if [[ -n "$who_b_content" ]]; then
local date_str_match
date_str_match=$(echo "$who_b_content" | grep -oE '[0-9]{4}-[0-9]{2}-[0-9]{2}[[:space:]]+[0-9]{2}:[0-9]{2}(:[0-9]{2})?')
if [[ -n "$date_str_match" ]]; then
local current_epoch
current_epoch=$(_get_epoch "$date_str_match")
if [[ -n "$current_epoch" && "$current_epoch" -gt "$max_epoch" ]]; then
max_epoch=$current_epoch
max_line="$who_b_content"
max_source="who -b output"
fi
fi
fi
local dmesg_first_log_line
dmesg_first_log_line=$(sed -n '/^# \/bin\/dmesg -T/,/#==/{p;}' "$BOOT_FILE" | sed '1d;$d' | head -n 1)
if [[ "$dmesg_first_log_line" =~ ^\[[[:space:]]*(.*)[[:space:]]*\] ]]; then
local date_str_match="${BASH_REMATCH[1]}"
date_str_match=$(echo "$date_str_match" | sed 's/^[ \t]*//;s/[ \t]*$//')
local current_epoch
current_epoch=$(_get_epoch "$date_str_match")
if [[ -n "$current_epoch" && "$current_epoch" -gt "$max_epoch" ]]; then
max_epoch=$current_epoch
max_line="$dmesg_first_log_line"
max_source="dmesg -T (first log line)"
fi
fi
local journal_first_log_line
journal_first_log_line=$(sed -n '/^# \/usr\/bin\/journalctl --no-pager --boot 0/,/#==/{p;}' "$BOOT_FILE" | sed '1d;$d' | head -n 1)
if [[ -n "$journal_first_log_line" ]]; then
local date_str_match
date_str_match=$(echo "$journal_first_log_line" | grep -oE '[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?([+-][0-9]{2}:?[0-9]{2})?')
if [[ -z "$date_str_match" ]]; then
date_str_match=$(echo "$journal_first_log_line" | grep -oE '^[A-Za-z]{3}[[:space:]]+[0-9]{1,2}[[:space:]]+[0-9]{2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?([[:space:]]+[0-9]{4})?' | head -n 1)
if [[ -z "$date_str_match" ]]; then
date_str_match=$(echo "$journal_first_log_line" | grep -oE '^[A-Za-z]{3}[[:space:]]+[0-9]{1,2}[[:space:]]+[0-9]{2}:[0-9]{2}:[0-9]{2}' | head -n 1)
fi
fi
if [[ -n "$date_str_match" ]]; then
local current_epoch
current_epoch=$(_get_epoch "$date_str_match")
if [[ -n "$current_epoch" && "$current_epoch" -gt "$max_epoch" ]]; then
max_epoch=$current_epoch
max_line="$journal_first_log_line"
max_source="journalctl --boot 0 (first log line)"
fi
fi
fi
if [[ "$max_epoch" -gt 0 ]]; then
echo "Last reboot identified by '$max_source':" > "$output_file"
echo "$max_line" >> "$output_file"
echo "Timestamp (Epoch): $max_epoch" >> "$output_file"
echo "" >> "$output_file"
echo "Last reboot determined to be (from '$max_source'):"
echo "$max_line"
echo "(Epoch: $max_epoch, Date: $(LC_ALL=C date -d "@$max_epoch" +"%a %b %d %T %Y %Z"))"
else
echo "Could not reliably determine last reboot time from $BOOT_FILE." > "$output_file"
echo -e "${RED}No reboot information found or parsed successfully.${NC}"
fi
}
# --- Main script logic ---
RUN_ALL=true
RUN_MESSAGES=false
BOOT_FILTER_TYPE=""
RUN_TIME_INFO=false
RUN_REBOOT=false
START_DATE_TIME=""
END_DATE_TIME=""
if [[ $# -eq 0 ]]; then
RUN_MESSAGES=true
BOOT_FILTER_TYPE="all"
RUN_TIME_INFO=true
RUN_REBOOT=true
else
RUN_ALL=false
fi
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-messages)
RUN_MESSAGES=true
RUN_ALL=false
shift
;;
-boot)
RUN_ALL=false
shift
if [[ $# -gt 0 && ! "$1" =~ ^- ]]; then
case "$1" in
log|dmesg|all|journal)
BOOT_FILTER_TYPE="$1"
shift
;;
*)
BOOT_FILTER_TYPE="journal"
;;
esac
else
BOOT_FILTER_TYPE="journal"
fi
;;
-timeinfo)
RUN_TIME_INFO=true
RUN_ALL=false
shift
;;
-reboot)
RUN_REBOOT=true
RUN_ALL=false
shift
;;
-from)
if [[ -n "$2" ]]; then
START_DATE_TIME="$2"
shift
shift
else
echo -e "${RED}Error: -from requires a value.${NC}" >&2; exit 1
fi
;;
-to)
if [[ -n "$2" ]]; then
END_DATE_TIME="$2"
shift
shift
else
echo -e "${RED}Error: -to requires a value.${NC}" >&2; exit 1
fi
;;
-h|--help)
echo "Usage: $0 [options]"
echo "Options:"
echo " -messages Filter /var/log/messages from messages.txt"
echo " -boot [type] Filter boot related logs from boot.txt."
echo " [type] can be: journal (default), log, dmesg, all."
echo " -timeinfo Show server time and timezone from ntp.txt"
echo " -reboot Scan boot.txt for the most recent reboot time."
echo " -from YYYY-MM-DDTHH:MM Specify start date and time for messages filter"
echo " -to YYYY-MM-DDTHH:MM Specify end date and time for messages filter"
echo " -h, --help Show this help message"
echo "If no options are specified, all filters (-messages, -boot all, -timeinfo, -reboot) will be run."
exit 0
;;
*)
echo -e "${RED}Unknown option: $1${NC}"
echo "Run '$0 --help' for usage."
exit 1
;;
esac
done
if $RUN_ALL || $RUN_MESSAGES; then
if [[ -n "$START_DATE_TIME" && -z "$END_DATE_TIME" && ($RUN_MESSAGES || $RUN_ALL) ]]; then
echo -e "${YELLOW}Warning: -from specified without -to for messages. Filtering from $START_DATE_TIME onwards.${NC}"
elif [[ -z "$START_DATE_TIME" && -n "$END_DATE_TIME" && ($RUN_MESSAGES || $RUN_ALL) ]]; then
echo -e "${RED}Error: -to specified without -from for messages. Please specify both or neither.${NC}"
if ! $RUN_ALL ; then exit 1; fi
fi
filter_messages "$START_DATE_TIME" "$END_DATE_TIME"
fi
if [[ -n "$BOOT_FILTER_TYPE" ]] || $RUN_ALL ; then
actual_boot_filter_type="$BOOT_FILTER_TYPE"
if $RUN_ALL && [[ -z "$BOOT_FILTER_TYPE" ]]; then
actual_boot_filter_type="all"
fi
if [[ -n "$actual_boot_filter_type" ]]; then
filter_boot_logs "$actual_boot_filter_type"
fi
fi
if $RUN_ALL || $RUN_TIME_INFO; then
show_server_time
fi
if $RUN_ALL || $RUN_REBOOT; then
find_last_reboot
fi
echo -e "\nFull details saved in ${YELLOW}./$RCA_ANALYSIS_DIR${NC}"