-
Notifications
You must be signed in to change notification settings - Fork 0
/
salomon.sh
executable file
·666 lines (617 loc) · 21.3 KB
/
salomon.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
#!/usr/bin/env bash
#
# Salomon - Simple log file monitor and analyzer
# Main script
# Copyright (c) 2024 by Ralf Kilian
# Distributed under the MIT License (https://opensource.org/licenses/MIT)
#
# GitHub: https://github.com/urbanware-org/salomon
# GitLab: https://gitlab.com/urbanware-org/salomon
#
script_dir=$(dirname $(readlink -f $0))
. ${script_dir}/core/shell.sh # Use POSIX standard instead of 'source' here
shell_precheck
source ${script_dir}/core/compat.sh
compatibility_precheck
script_file=$(basename "$0")
source ${script_dir}/core/analyze.sh
source ${script_dir}/core/colors.sh
source ${script_dir}/core/common.sh
source ${script_dir}/core/dialogs.sh
source ${script_dir}/core/global.sh
source ${script_dir}/core/interactive.sh
source ${script_dir}/core/monitor.sh
source ${script_dir}/core/output.sh
read_config_file; check_config
set_global_variables
set_line_characters
# Script files stored inside the 'debug' sub-directory (if existing) will be
# loaded after the included ones listed above. This allows to overwrite
# existing functions from scripts inside the 'core' sub-directory without
# manipulating them
if [ -d "${script_dir}/debug" ]; then
ls "${script_dir}/debug" | grep "\.sh$" &>/dev/null
if [ $? -eq 0 ]; then
if [[ ! $* == *"--debug"* ]]; then
debug_notification
fi
source ${script_dir}/debug/*.sh &>/dev/null
fi
fi
# Check command-line arguments
if [ $# -eq 0 ]; then
usage "At least one required argument is missing"
else
while [ $# -gt 0 ]; do
arg="$1"
case "$arg" in
# Required arguments
-a|--action)
if [ -n "$action" ]; then
usage "The action argument can only be given once"
fi
shift
action="$1"
shift
;;
-i|--input-file)
shift
prepare_path "$1"
input_file="$input_file $path_prepared"
check_argument "-i/--input-file" "$input_file" "file"
shift
;;
# Optional arguments
-c|--color-file)
if [ -n "$color_file" ]; then
usage "The color file argument can only be given once"
fi
shift
color_file="$1"
check_argument "-c/--color-file" "$color_file" "file"
shift
;;
--cut-off)
highlight_cut_off=1
shift
;;
-d|--delay)
shift
delay="$1"
shift
;;
--dialogs)
interactive=1
shift
;;
-e|--exclude)
if [ -n "$exclude_pattern" ]; then
usage "The exclude argument can only be given once" \
"Use a pattern instead"
fi
shift
exclude_pattern="$1"
shift
;;
--export-file)
if [ -n "$export_file" ]; then
usage "The export argument can only be given once"
fi
shift
export_file="$1"
export_log=1
shift
;;
-f|--filter)
if [ -n "$filter_pattern" ]; then
usage "The filter argument can only be given once" \
"Use a pattern instead"
fi
shift
filter_pattern="$1"
shift
;;
--force-dark)
if [ -n "$color_force" ]; then
usage "The force dark argument can only be given once"
fi
color_force="dark"
shift
;;
--force-light)
if [ -n "$color_force" ]; then
usage "The force light argument can only be given once"
fi
color_force="light"
shift
;;
-h|--head)
shift
head_lines="$1"
shift
;;
-ha|--highlight-all)
highlight_all=1
shift
;;
-hm|--highlight-matches)
if [ $is_openbsd -eq 1 ]; then
highlight_upper=1
else
highlight_matches=1
fi
shift
;;
-hu|--highlight-upper)
highlight_upper=1
shift
;;
-ic|--ignore-case)
arg_case="-i"
shift
;;
--interactive)
interactive=1
shift
;;
--less)
analyze_less=1
shift
;;
-m|--merge)
merge=1
shift
;;
--no-info)
header=0
shift
;;
-p|--prompt)
prompt=1
shift
;;
--pause)
shift
pause_lines="$1"
pause=1
shift
;;
-r|--remove)
if [ -n "$remove_pattern" ]; then
usage "The remove argument can only be given once" \
"Use a pattern instead"
fi
shift
remove_pattern="$1"
shift
;;
-s|--slow)
slow=1
shift
;;
-t|--tail)
shift
tail_lines="$1"
shift
;;
--timestamp)
timestamp=1
shift
;;
-w|--wait)
shift
wait_match="$1"
shift
;;
# General arguments
--color-table)
color_table=1
shift
;;
--format-list)
format_list=1
shift
;;
--version)
echo "$version"
exit
;;
--version-update)
check_update
;;
-\?|--help)
usage
;;
# Alternatives to the required arguments
--analyze)
if [ -n "$action" ]; then
usage "An action argument can only be given once"
fi
action="analyze"
shift
;;
--monitor)
if [ -n "$action" ]; then
usage "An action argument can only be given once"
fi
action="monitor"
shift
;;
# Inofficial arguments
--debug)
debug=1
shift
;;
# Unrecognized arguments
*)
usage "The given argument '$1' is unrecognized"
;;
esac
done
# Print color table (if requested)
if [ $color_table -eq 1 ]; then
print_color_table
exit
fi
# Print color table (if requested)
if [ $format_list -eq 1 ]; then
print_format_list
exit
fi
if [ $interactive -eq 1 ]; then
dialog_program_dialog="$(command -v dialog)"
dialog_program_whiptail="$(command -v whiptail)"
if [ -z "$dialog_program_dialog" ] && \
[ -z "$dialog_program_whiptail" ]; then
dialog_program=""
interactive=0
usage "No supported dialog program found"
fi
if [ "$dialog_program" = "auto" ]; then
if [ -n "$dialog_program_dialog" ]; then
dialog_program="dialog"
elif [ -n "$dialog_program_whiptail" ]; then
dialog_program="whiptail"
fi
elif [ "$dialog_program" = "dialog" ]; then
if [ -z "$dialog_program_dialog" ]; then
dialog_program=""
fi
elif [ "$dialog_program" = "whiptail" ]; then
if [ -z "$dialog_program_whiptail" ]; then
dialog_program=""
fi
else
dialog_program=""
fi
if [ -z "$dialog_program" ]; then
interactive=0
usage "The dialog program given in the config file was not found"
fi
init_dialogs
interactive_mode
else
# Input file
input_file=$(sed -e "s/^ *//g;s/ *$//g" <<< "$input_file")
if [ -z "$input_file" ]; then
usage "No input file(s) given"
fi
for item in $input_file; do
file=$(sed -e "s/^ *//g;s/ *$//g;s/\/\//\ /g" <<< $item)
if [ -e "$file" ]; then
filepath="$file"
elif [ -e "/var/log/$file" ]; then
filepath="/var/log/$file"
else
usage "The given input file '$file' does not exist"
fi
if [ ! -f "$filepath" ]; then
usage "The given input file path '$file' is not a file"
fi
tail "$filepath" &>/dev/null
if [ $? -ne 0 ]; then
usage "No read permission on the given input file '$filepath'"
fi
filelist="$filelist $(sed -e "s/\ /\/\//g" <<< "$filepath")"
done
input_file="$filelist"
# Action to perform
if [ -n "$action" ]; then
if [ "$action" = "analyze" ]; then
follow=0
elif [ "$action" = "monitor" ]; then
if [ $follow -eq 0 ]; then
usage "Multiple action arguments given (only one allowed)"
fi
else
usage "The action '$action' does not exist"
fi
else
usage \
"No action given (must either be '--analyze' or '--monitor')"
fi
# Color file
if [ -n "$color_file" ]; then
msg="The given color config file"
color_file_found=1
color_file_path="$color_file"
if [ ! -e "$color_file_path" ]; then
color_file_path="${user_colors}/${color_file}"
if [ ! -e "$color_file_path" ]; then
color_file_path="${color_dir}/${color_file}"
if [ ! -e "$color_file_path" ]; then
color_file_found=0
fi
fi
fi
if [ $color_file_found -eq 0 ]; then
usage "$msg path '$color_file' does not exist"
else
color_file="$color_file_path"
fi
if [ ! -f "$color_file" ]; then
usage "$msg path '$color_file' is not a file"
else
msg="No read permission"
tail "$color_file" &>/dev/null
if [ $? -ne 0 ]; then
usage "$msg on the given color file '$color_file'"
else
read_color_file "$color_file"
fi
fi
fi
# Filter pattern
if [ -z "$filter_pattern" ]; then
msg="can only be used with"
if [ "$arg_case" = "-i" ]; then
usage "The '--ignore-case' argument $msg a filter"
fi
if [ $highlight_matches -eq 1 ] || \
[ $highlight_upper -eq 1 ]; then
usage "This highlighting argument $msg a filter"
fi
else
msg="The given filter file"
filter_file_found=1
filter_file="$filter_pattern"
filter_file_path="$filter_file"
if [ ! -e "$filter_file_path" ]; then
filter_file_path="${user_filters}/${filter_file}"
if [ ! -e "$filter_file_path" ]; then
filter_file_path="${filter_dir}/${filter_file}"
if [ ! -e "$filter_file_path" ]; then
filter_file_found=0
fi
fi
fi
if [ $filter_file_found -eq 1 ]; then
if [ ! -f "$filter_file_path" ]; then
usage "$msg path '$filter_file' is not a file"
else
msg="No read permission"
tail "$filter_file_path" &>/dev/null
if [ $? -ne 0 ]; then
usage "$msg on the given color file '$filter_file'"
else
filter_file="$filter_file_path"
read_filter "$filter_file"
fi
fi
else
filter_file=""
if [[ $filter_pattern == *"§"* ]]; then
usage \
"The filter pattern must not contain any section signs"
fi
fi
filter_pattern=$((tr -s ";;" ";" | \
sed -e "s/^;//" \
-e "s/;$//") <<< "$filter_pattern")
if [ $is_bsd -eq 1 ]; then
filter_list=$(sed -e "s/^;*//g" \
-e "s/;*$//g" \
-e "s/\ /§/g" \
-e $'s/;/\\\n/g' <<< "$filter_pattern")
else # Linux
filter_list=$(sed -e "s/^;*//g" \
-e "s/;*$//g" \
-e "s/\ /§/g" \
-e "s/;/\n/g" <<< "$filter_pattern")
fi
filter_pattern=$(sed -e "s/§/\ /g" <<< "$filter_pattern")
filter=1
fi
# Highlighting
highlight_params=$((
highlight_all + highlight_matches + highlight_upper ))
if [ $highlight_params -gt 1 ]; then
usage "Multiple highlighting arguments given (only one allowed)"
fi
if [ $highlight_all -eq 0 ] && [ $highlight_cut_off -eq 1 ]; then
msg="can only be used with"
usage "The '--cut-off' argument $msg '--highlight-all'"
fi
if [ "$highlight_forecolor" = "1" ] || \
[ "$highlight_forecolor" = "black" ]; then
hl_fgcolor="$cl_bk"
elif [ "$highlight_forecolor" = "2" ] || \
[ "$highlight_forecolor" = "white" ]; then
hl_fgcolor="$cl_wh"
else
hl_fgcolor="$cl_n"
fi
# Exclude pattern
if [ -n "$exclude_pattern" ]; then
if [[ $exclude_pattern == *"§"* ]]; then
usage "The exclude pattern must not contain any section signs"
fi
exclude_list=$((tr -s ";;" ";" | \
sed -e "s/^;*//" \
-e "s/;*$//" \
-e "s/\ /§/g" \
-e "s/;/\n/g") <<< "$exclude_pattern")
exclude=1
fi
# Remove pattern
if [ -n "$remove_pattern" ]; then
if [[ $remove_pattern == *"§"* ]]; then
usage "The remove pattern must not contain any section signs"
fi
remove_list=$((tr -s ";;" ";" | \
sed -e "s/^;*//" \
-e "s/;*$//" \
-e "s/\ /§/g" \
-e "s/;/\n/g") <<< "$remove_pattern")
remove=1
fi
# Head and tail
input_count=$(wc -w <<< "$input_file")
if [ -z "$head_lines" ]; then
head_lines=0
else
re='^[0-9]+$'
if [[ ! $head_lines =~ $re ]]; then
usage \
"The argument '--head' expects a number greater than zero"
fi
if [[ ! $tail_lines =~ $re ]]; then
usage \
"The argument '--tail' expects a number greater than zero"
fi
if [ $head_lines -gt 0 ] && [ $follow = 1 ]; then
usage \
"The '--head' argument cannot be used with monitoring mode"
fi
if [ $head_lines -gt 0 ] && [ $tail_lines -gt 0 ]; then
usage \
"Use either '--head' or '--tail', not both at the same time"
elif [ $head_lines -gt 0 ] || [ $tail_lines -gt 0 ]; then
if [ $input_count -gt 1 ]; then
msg_args="'--head' or '--tail'"
usage \
"When using $msg_args only one input file can be given"
fi
fi
fi
# Merge input files
if [ $merge -eq 1 ] && [ $input_count -lt 2 ]; then
usage "The '--merge' argument requires at least two input files"
fi
# Use 'less' command to analyze input files
if [ $analyze_less -eq 1 ]; then
less_msg="The '--less' argument cannot be used"
if [ "$action" = "monitor" ]; then
usage "$less_msg with monitoring mode"
elif [ $pause -eq 1 ]; then
usage "$less_msg together with '--pause'"
elif [ $prompt -eq 1 ]; then
usage "$less_msg together with '-p' (or '--prompt')"
elif [ $slow -eq 1 ]; then
usage "$less_msg together with '-s' (or '--slow')"
elif [ ! "$wait_match" = "0" ]; then
usage "$less_msg together with '-w' (or '--wait')"
fi
fi
# Pause output
if [ $pause -eq 1 ] && [ $follow -eq 1 ]; then
usage "The '--pause' argument cannot be used with monitoring mode"
fi
if [ -z "$pause_lines" ] && [ $pause -eq 1 ]; then
expects="expects a number greater than zero or 'auto'"
usage "The '--pause' argument $expects"
elif [ "$pause_lines" = "auto" ]; then
pause=1
else
if [ $pause -eq 1 ]; then
re='^[0-9]+$'
if [ $pause_lines -lt 1 ] || [[ ! $pause_lines =~ $re ]]; then
expects="expects a number greater than zero or 'auto'"
usage "The '--pause' argument $expects"
fi
fi
fi
# Slow down (delay)
grep -E "^[0-9]*$" <<< "$delay" &>/dev/null
if [ $? -eq 0 ]; then
if [ $delay -lt 100 ]; then
delay=100
elif [ $delay -gt 900 ]; then
delay=900
fi
else
usage "The delay must be a number between 100 and 900"
fi
# Wait on match
if [ -z "$wait_match" ]; then
usage "The wait value must not be empty"
else
grep -E "^[0-9]*$" <<< "$wait_match" &>/dev/null
if [ $? -eq 0 ]; then
if [ $wait_match -le 0 ]; then
wait=0
fi
else
usage "The wait value must be a number greater than zero"
fi
fi
# Export file
if [ $export_log -eq 1 ]; then
msg_export="The given export file path '$export_file'"
if [ -z "$export_file" ]; then
export_log=0
else
if [ -e "$export_file" ]; then
if [ -d "$export_file" ]; then
usage "$msg_export is not a file"
elif [ -f "$export_file" ]; then
usage "$msg_export already exists"
fi
fi
touch $export_file &>/dev/null
if [ $? -ne 0 ]; then
usage "$msg_export seems to be read-only"
fi
fi
fi
fi
# Force dark/light text colors
if [ "$color_force" = "dark" ]; then
color_lightblue=$cl_db ; cl_lb=$color_lightblue
color_lightcyan=$cl_dc ; cl_lc=$color_lightcyan
color_lightgray=$cl_dy ; cl_ly=$color_lightgray
color_lightgreen=$cl_dg ; cl_lg=$color_lightgreen
color_lightpurple=$cl_dp ; cl_lp=$color_lightpurple
color_lightred=$cl_dr ; cl_lr=$color_lightred
color_white=$cl_bk ; cl_wh=$color_white
color_yellow=$cl_br ; cl_yl=$color_yellow
elif [ "$color_force" = "light" ]; then
color_black=$cl_wh ; cl_bk=$color_black
color_brown=$cl_yl ; cl_br=$color_brown
color_darkblue=$cl_lb ; cl_db=$color_darkblue
color_darkcyan=$cl_dc ; cl_dc=$color_darkcyan
color_darkgray=$cl_ly ; cl_dy=$color_darkgray
color_darkgreen=$cl_lg ; cl_dg=$color_darkgreen
color_darkpurple=$cl_lp ; cl_dp=$color_darkpurple
color_darkred=$cl_lr ; cl_dr=$color_darkred
fi
fi
# Check requirements
check_command grep grep
check_command sed sed
check_command tail coreutils
if [ $warn_suffix -ne 0 ]; then
notice "Global config file extensions other than '*.cfg' are deprecated"
fi
if [ $header -eq 1 ]; then
print_output_header
fi
# Finally, process the given input file
if [ $analyze_less -eq 0 ]; then
trap "cancel_process" 2
fi
if [ $follow -eq 1 ]; then
monitor_input_file
else
analyze_input_file
fi