-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlog-viewer.sh
More file actions
executable file
路298 lines (254 loc) 路 7.4 KB
/
Copy pathlog-viewer.sh
File metadata and controls
executable file
路298 lines (254 loc) 路 7.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
#!/bin/bash
#############################################################
# Linux System Update Script - Log Viewer
# Zeigt Update-Logs in verschiedenen Formaten an
# MIT License
#############################################################
# Farbcodes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
# Script-Verzeichnis
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG_FILE="${SCRIPT_DIR}/config.conf"
# Standard-Log-Verzeichnis
LOG_DIR="/var/log/system-updates"
LANGUAGE=auto
# Konfiguration laden, falls vorhanden
if [ -f "$CONFIG_FILE" ]; then
# shellcheck source=/dev/null
source "$CONFIG_FILE"
LOG_DIR="${LOG_DIR:-/var/log/system-updates}"
LANGUAGE="${LANGUAGE:-auto}"
fi
# Sprache laden
load_language() {
local lang="${LANGUAGE:-auto}"
# Automatische Sprach-Erkennung
if [ "$lang" = "auto" ]; then
lang="${LANG%%.*}"
lang="${lang%%_*}"
lang="${lang:-en}"
fi
# Sprachdatei f眉r Log-Viewer laden
local viewer_lang_file="${SCRIPT_DIR}/lang/viewer-${lang}.lang"
if [ -f "$viewer_lang_file" ]; then
# shellcheck source=/dev/null
source "$viewer_lang_file"
else
# Fallback zu Englisch
if [ -f "${SCRIPT_DIR}/lang/viewer-en.lang" ]; then
# shellcheck source=/dev/null
source "${SCRIPT_DIR}/lang/viewer-en.lang"
else
echo "ERROR: No language files found!"
exit 1
fi
fi
}
# Sprache initialisieren
load_language
#############################################################
# Funktionen
#############################################################
print_header() {
clear
echo -e "${BLUE}=================================================${NC}"
echo -e "${BLUE} ${VIEWER_HEADER}${NC}"
echo -e "${BLUE}=================================================${NC}"
echo
}
print_info() {
echo -e "${GREEN}[${VIEWER_LABEL_INFO}]${NC} $1"
}
print_error() {
echo -e "${RED}[${VIEWER_LABEL_ERROR}]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[${VIEWER_LABEL_WARNING}]${NC} $1"
}
# Neueste Logdatei finden
get_latest_log() {
if [ ! -d "$LOG_DIR" ]; then
return 1
fi
local latest
latest=$(find "$LOG_DIR" -maxdepth 1 -name "update_*.log" -type f -printf "%T@ %p\n" 2>/dev/null | sort -rn | head -n 1 | cut -d' ' -f2-)
if [ -z "$latest" ]; then
return 1
fi
echo "$latest"
return 0
}
# Neueste Logdatei komplett anzeigen
show_full_log() {
print_header
echo -e "${GREEN}${VIEWER_NEWEST_LOG}${NC}\n"
local logfile
if ! logfile=$(get_latest_log) || [ -z "$logfile" ]; then
print_error "$VIEWER_NO_LOGS: $LOG_DIR"
echo
echo "$VIEWER_CAUSES"
echo " - $VIEWER_CAUSE_NO_UPDATES"
echo " - $VIEWER_CAUSE_WRONG_DIR"
echo " - $VIEWER_CAUSE_NO_ACCESS"
return 1
fi
print_info "$VIEWER_LOGFILE: $logfile"
print_info "$VIEWER_SIZE: $(du -h "$logfile" | cut -f1)"
print_info "$VIEWER_CREATED: $(stat -c %y "$logfile" | cut -d'.' -f1)"
echo
echo -e "${CYAN}${VIEWER_LOG_CONTENT}${NC}"
echo
if [ -r "$logfile" ]; then
cat "$logfile"
else
print_error "$VIEWER_NO_PERMISSION: $logfile"
echo "$VIEWER_TRY_SUDO $0"
return 1
fi
}
# Letzte 50 Zeilen anzeigen
show_tail_log() {
print_header
echo -e "${GREEN}${VIEWER_LAST_50}${NC}\n"
local logfile
if ! logfile=$(get_latest_log) || [ -z "$logfile" ]; then
print_error "$VIEWER_NO_LOGS: $LOG_DIR"
echo
echo "$VIEWER_CAUSES"
echo " - $VIEWER_CAUSE_NO_UPDATES"
echo " - $VIEWER_CAUSE_WRONG_DIR"
echo " - $VIEWER_CAUSE_NO_ACCESS"
return 1
fi
print_info "$VIEWER_LOGFILE: $logfile"
print_info "$VIEWER_SIZE: $(du -h "$logfile" | cut -f1)"
print_info "$VIEWER_CREATED: $(stat -c %y "$logfile" | cut -d'.' -f1)"
echo
echo -e "${CYAN}${VIEWER_LAST_LINES}${NC}"
echo
if [ -r "$logfile" ]; then
tail -n 50 "$logfile"
else
print_error "$VIEWER_NO_PERMISSION: $logfile"
echo "$VIEWER_TRY_SUDO $0"
return 1
fi
}
# Alle Logdateien auflisten
list_all_logs() {
print_header
echo -e "${GREEN}${VIEWER_ALL_LOGS}${NC}\n"
if [ ! -d "$LOG_DIR" ]; then
print_error "$VIEWER_NO_DIR: $LOG_DIR"
return 1
fi
local logs
# Use array to avoid SC2045
logs=("$LOG_DIR"/update_*.log)
if [ ! -e "${logs[0]}" ]; then
print_warning "$VIEWER_NO_LOGS: $LOG_DIR"
return 1
fi
print_info "$VIEWER_LOG_DIR: $LOG_DIR"
echo
echo -e "${CYAN}${VIEWER_TABLE_DATETIME}${NC} ${CYAN}${VIEWER_TABLE_SIZE}${NC} ${CYAN}${VIEWER_TABLE_FILENAME}${NC}"
echo "-----------------------------------------------------------"
# Sortiere Logs nach 脛nderungszeit und zeige sie an
for log in "${logs[@]}"; do
if [ -f "$log" ]; then
stat -c "%y %s %n" "$log" 2>/dev/null
fi
done | sort -r | awk '{
# Datum und Zeit (YYYY-MM-DD HH:MM:SS)
datetime = $1 " " substr($2, 1, 8)
# Gr枚脽e
size = $3
# Dateiname (nur Basename)
split($4, arr, "/")
filename = arr[length(arr)]
printf "%-20s %-8s %s\n", datetime, size, filename
}'
echo
local count
count=$(find "$LOG_DIR" -name "update_*.log" -type f 2>/dev/null | wc -l)
print_info "$VIEWER_TOTAL: $count $VIEWER_LOGFILES"
}
# Logs nach Fehler durchsuchen
search_errors() {
print_header
echo -e "${GREEN}${VIEWER_SEARCH_ERRORS}${NC}\n"
if [ ! -d "$LOG_DIR" ]; then
print_error "$VIEWER_NO_DIR: $LOG_DIR"
return 1
fi
print_info "$VIEWER_SEARCHING"
echo
local found=0
local logfile
# Use glob instead of ls output
for logfile in "$LOG_DIR"/update_*.log; do
[ -e "$logfile" ] || continue
if grep -i "fehler\|error\|failed\|fehlgeschlagen" "$logfile" > /dev/null 2>&1; then
echo -e "${YELLOW}=== $(basename "$logfile") ===${NC}"
grep -i --color=always "fehler\|error\|failed\|fehlgeschlagen" "$logfile"
echo
found=1
fi
done
if [ $found -eq 0 ]; then
print_info "$VIEWER_NO_ERRORS"
fi
}
# Hauptmen眉
show_menu() {
print_header
echo -e "${CYAN}${VIEWER_MENU_PROMPT}${NC}\n"
echo " 1) $VIEWER_MENU_1"
echo " 2) $VIEWER_MENU_2"
echo " 3) $VIEWER_MENU_3"
echo " 4) $VIEWER_MENU_4"
echo " 5) $VIEWER_MENU_5"
echo
echo -ne "${YELLOW}${VIEWER_MENU_CHOICE} [1-5]:${NC} "
}
#############################################################
# Hauptprogramm
#############################################################
# Endlosschleife f眉r Men眉
while true; do
show_menu
read -r choice
case "$choice" in
1)
show_full_log
;;
2)
show_tail_log
;;
3)
list_all_logs
;;
4)
search_errors
;;
5|q|Q)
print_header
print_info "$VIEWER_GOODBYE"
echo
exit 0
;;
*)
print_header
print_error "$VIEWER_INVALID_CHOICE: $choice"
echo
;;
esac
echo
echo -e "${CYAN}---------------------------------------------------${NC}"
read -r -p "$VIEWER_CONTINUE"
done