forked from cynicastic/scribe
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscribe.sh
More file actions
3373 lines (3080 loc) · 106 KB
/
scribe.sh
File metadata and controls
3373 lines (3080 loc) · 106 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
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh -
##################################################################
# _
# _ ( )
# ___ ___ _ __ (_)| |_ __
# /',__) /'___)( '__)| || '_`\ /'__`\
# \__, \( (___ | | | || |_) )( ___/
# (____/`\____)(_) (_)(_,__/'`\____)
# syslog-ng and logrotate installer for Asuswrt-Merlin
#
# Coded by cmkelley
#
# Original interest in syslog-ng on Asuswrt-Merlin inspired by tomsk & kvic
# Good ideas and code borrowed heavily from Adamm, dave14305, Jack Yaz, thelonelycoder, & Xentrx
#
# Installation command:
# curl --retry 3 "https://raw.githubusercontent.com/AMTM-OSR/scribe/master/scribe.h" -o "/jffs/scripts/scribe" && chmod 0755 /jffs/scripts/scribe && /jffs/scripts/scribe install
#
##################################################################
# Last Modified: 2026-Apr-11
#-----------------------------------------------------------------
################ Shellcheck directives ################
# shellcheck disable=SC1090
# shellcheck disable=SC1091
# shellcheck disable=SC2009
# SC2009 = Consider uing pgrep ~ Note that pgrep doesn't exist in asuswrt (exists in Entware procps-ng)
# shellcheck disable=SC2059
# SC2059 = Don't use variables in the printf format string. Use printf "..%s.." "$foo" ~ I (try to) only embed the ansi color escapes in printf strings
# shellcheck disable=SC2034
# shellcheck disable=SC3043
# shellcheck disable=SC3045
#################################################################
readonly script_name="scribe"
readonly scribe_ver="v3.2.12"
readonly scriptVer_TAG="26041123"
scribe_branch="master"
script_branch="$scribe_branch"
# To support automatic script updates from AMTM #
doScriptUpdateFromAMTM=true
# Workaround for Entware ELF binaries compiled with RUNPATH #
unset LD_LIBRARY_PATH
# Ensure firmware binaries are used, not Entware #
export PATH="/sbin:/bin:/usr/sbin:/usr/bin:$PATH"
# Set TMP if not set #
[ -z "${TMP:+xSETx}" ] && export TMP=/opt/tmp
# Parse parameters #
action="X"
got_zip=false
banner=true
usbUnmountCaller=false
[ "${SCRIBE_LOGO:=xFALSEx}" = "nologo" ] && banner=false
while { [ $# -gt 0 ] && [ -n "$1" ] ; }
do
case "$1" in
gotzip)
got_zip=true ; shift
;;
nologo)
banner=false ; shift
;;
unmount)
usbUnmountCaller=true ; shift
;;
service_event | LogRotate)
banner=false
action="$1"
break
;;
amtmupdate)
action="$1"
if [ $# -gt 1 ] && [ "$2" = "check" ]
then banner=false
fi
shift
break
;;
*)
action="$1" ; shift
;;
esac
done
[ "$action" = "X" ] && action="menu"
# scribe constants #
# Version 'vX.Y_Z' format because I'm stubborn #
script_ver="$( echo "$scribe_ver" | sed 's/\./_/2' )"
readonly script_ver
readonly scriptVer_long="$scribe_ver ($scribe_branch)"
readonly script_author="AMTM-OSR"
readonly raw_git="https://raw.githubusercontent.com"
readonly script_zip_file="${TMP}/${script_name}_TEMP.zip"
readonly script_tmp_file="${TMP}/${script_name}_TEMP.tmp"
readonly script_d="/jffs/scripts"
readonly script_loc="${script_d}/$script_name"
readonly config_d="/jffs/addons/${script_name}.d"
readonly script_conf="${config_d}/config"
readonly optmsg="/opt/var/log/messages"
readonly jffslog="/jffs/syslog.log"
readonly tmplog="/tmp/syslog.log"
syslog_loc=""
export optmsg
export tmplog
export jffslog
export script_conf
##-------------------------------------##
## Added by Martinski W. [2025-Jul-07] ##
##-------------------------------------##
readonly branchxStr_TAG="[Branch: $scribe_branch]"
readonly versionDev_TAG="${scribe_ver}_${scriptVer_TAG}"
readonly scribeVerRegExp="v[0-9]{1,2}([.][0-9]{1,2})([_.][0-9]{1,2})"
if [ "$script_branch" = "master" ]
then SCRIPT_VERS_INFO=""
else SCRIPT_VERS_INFO="[$versionDev_TAG]"
fi
##----------------------------------------##
## Modified by Martinski W. [2025-Jul-07] ##
##----------------------------------------##
# router details #
readonly wrtMerlin="ASUSWRT-Merlin"
readonly fwVerReqd="3004.380.68"
fwName="$( uname -o )"
readonly fwName
fwVerBuild="$(nvram get firmver | sed 's/\.//g').$( nvram get buildno )"
fwVerExtNo="$(nvram get extendno)"
fwVersFull="${fwVerBuild}.${fwVerExtNo:=0}"
readonly fwVerBuild
readonly fwVerExtNo
readonly fwVersFull
model="$( nvram get odmpid )"
[ -z "$model" ] && model="$( nvram get productid )"
readonly model
arch="$( uname -m )"
readonly arch
# miscellaneous constants #
readonly sld="syslogd"
readonly sng="syslog-ng"
readonly sng_reqd="3.19"
readonly lr="logrotate"
readonly init_d="/opt/etc/init.d"
readonly S01sng_init="$init_d/S01$sng"
readonly rcfunc_sng="rc.func.$sng"
readonly rcfunc_loc="$init_d/$rcfunc_sng"
readonly sng_loc="/opt/sbin/$sng"
readonly sngctl_loc="${sng_loc}-ctl"
readonly lr_loc="/opt/sbin/$lr"
readonly sng_conf="/opt/etc/${sng}.conf"
readonly debug_sep="=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*="
readonly script_debug_name="${script_name}_debug.log"
readonly script_debug="${TMP}/$script_debug_name"
readonly sngconf_merged="${TMP}/${sng}-complete.conf"
readonly sngconf_error="${TMP}/${sng}-error.conf"
readonly lr_conf="/opt/etc/${lr}.conf"
readonly lr_daily="/opt/tmp/logrotate.daily"
readonly lr_temp="/opt/tmp/logrotate.temp"
readonly sngd_d="/opt/etc/${sng}.d"
readonly lrd_d="/opt/etc/${lr}.d"
readonly etc_d="/opt/etc/*.d"
readonly sng_share="/opt/share/$sng"
readonly lr_share="/opt/share/$lr"
readonly share_ex="/opt/share/*/examples"
readonly script_bakname="${TMP}/${script_name}-backup.tar.gz"
readonly fire_start="$script_d/firewall-start"
readonly srvcEvent="$script_d/service-event"
readonly postMount="$script_d/post-mount"
readonly unMount="$script_d/unmount"
readonly skynet="$script_d/firewall"
readonly sky_req="6.9.2"
readonly divers="/opt/bin/diversion"
readonly div_req="4.1"
##-------------------------------------##
## Added by Martinski W. [2025-Dec-05] ##
##-------------------------------------##
readonly HOMEdir="/home/root"
readonly TEMPdir="/tmp/var/tmp"
readonly optTempDir="/opt/tmp"
readonly optVarLogDir="/opt/var/log"
readonly syslogNgStr="syslog-ng"
readonly logRotateStr="logrotate"
readonly syslogNgCmd="/opt/sbin/$syslogNgStr"
readonly logRotateCmd="/opt/sbin/$logRotateStr"
readonly logRotateConfDir="/opt/etc/${logRotateStr}.d"
readonly logRotateShareDir="/opt/share/$logRotateStr"
readonly logRotateExamplesDir="${logRotateShareDir}/examples"
readonly logRotateTopConfig="/opt/etc/${logRotateStr}.conf"
readonly logRotateGlobalName="A01global"
readonly logRotateGlobalConf="${logRotateConfDir}/$logRotateGlobalName"
readonly LR_FLock_FD=513
readonly LR_FLock_FName="/tmp/scribeLogRotate.flock"
readonly logFilesRegExp="${optVarLogDir}/.*([.]log)?"
readonly filteredLogList="${config_d}/.filteredlogs"
readonly noConfigLogList="${config_d}/.noconfiglogs"
readonly syslogNg_ConfDir="/opt/etc/${syslogNgStr}.d"
readonly syslogNg_ShareDir="/opt/share/$syslogNgStr"
readonly syslogNg_ExamplesDir="${syslogNg_ShareDir}/examples"
readonly syslogNg_ConfName=${syslogNgStr}.conf
readonly syslogNg_TopConfig="/opt/etc/$syslogNg_ConfName"
readonly syslogNg_WaitnSEM_FPath="${TEMPdir}/scribe_SysLogNg.WAITN.SEM"
readonly syslogNg_StartSEM_FPath="${TEMPdir}/scribe_SysLogNg.START.SEM"
readonly syslogD_InitRebootLogFPath="${optVarLogDir}/syslogd.ScribeInitReboot.LOG"
readonly sysLogLinesMAX=20480
readonly sysLogMsgeSizeMAX=2048
sysLogFiFoSizeMIN=1600
readonly debugMsgDiscardFilterName="A0DebugMsgsDiscard"
readonly debugMsgDiscardFilterConf="${syslogNg_ConfDir}/$debugMsgDiscardFilterName"
readonly debugMsgDiscardFilterSrce="${syslogNg_ExamplesDir}/$debugMsgDiscardFilterName"
readonly debugMsgCaptureFilterName="A0DebugMsgsCapture"
readonly debugMsgCaptureFilterConf="${syslogNg_ConfDir}/$debugMsgCaptureFilterName"
readonly debugMsgCaptureFilterSrce="${syslogNg_ExamplesDir}/$debugMsgCaptureFilterName"
# color constants #
readonly red="\033[1;31m"
readonly green="\033[1;32m"
readonly yellow="\033[1;33m"
readonly blue="\033[1;34m"
readonly magenta="\033[1;35m"
readonly cyan="\033[1;36m"
readonly white="\033[1;37m"
readonly std="\e[0m"
readonly BOLD="\e[1m"
readonly CLRct="\e[0m"
readonly GRNct="\e[1;32m"
##-------------------------------------##
## Added by Martinski W. [2025-Nov-29] ##
##-------------------------------------##
readonly oneMByte=1048576
readonly twoMByte=2097152
readonly LR_CronJobMins=5
readonly LR_CronJobHour=0
readonly LR_CronTagStr="scribeLogRotate"
readonly validHourRegExp="(2|3|4|6|8|12|24)"
readonly validHourLstStr="2, 3, 4, 6, 8, 12, and 24."
##----------------------------------------##
## Modified by Martinski W. [2025-Jul-07] ##
##----------------------------------------##
# uiScribe add-on constants #
readonly uiscribeName="uiScribe"
readonly uiscribeAuthor="AMTM-OSR"
readonly uiscribeBranch="master"
readonly uiscribeRepo="$raw_git/$uiscribeAuthor/$uiscribeName/$uiscribeBranch/${uiscribeName}.sh"
readonly uiscribePath="$script_d/$uiscribeName"
readonly uiscribeVerRegExp="v[0-9]{1,2}([.][0-9]{1,2})([.][0-9]{1,2})"
readonly menuSepStr="${white} =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=${CLRct}\n\n"
isInteractive=false
[ -t 0 ] && ! tty | grep -qwi "NOT" && isInteractive=true
if ! "$isInteractive" ; then banner=false ; fi
# Check if Scribe is already installed by looking for link in /opt/bin #
[ -e "/opt/bin/$script_name" ] && scribeInstalled=true || scribeInstalled=false
# Check if uiScribe is installed #
[ -e "$uiscribePath" ] && uiScribeInstalled=true || uiScribeInstalled=false
# Check if Skynet is installed
if [ -e "$fire_start" ] && grep -q "skynetloc" "$fire_start"
then
skynet_inst=true
else
skynet_inst=false
fi
#### functions ####
##-------------------------------------##
## Added by Martinski W. [2025-Jul-07] ##
##-------------------------------------##
SetUpRepoBranchVars()
{
script_repoFile="$raw_git/$script_author/$script_name/$script_branch/${script_name}.sh"
script_repo_ZIP="https://github.com/$script_author/$script_name/archive/${script_branch}.zip"
unzip_dirPath="$TMP/${script_name}-$script_branch"
}
present(){ printf "$green present. $std\n"; }
updated(){ printf "$yellow updated. $std\n"; }
finished(){ printf "$green done. $std\n"; }
not_installed(){ printf "\n ${blue}%s ${red}NOT${white} installed!${std}\n" "$1"; }
PressEnterTo()
{ printf "$white Press <Enter> key to %s $std" "$1"; read -rs inputKey; echo; }
VersionStrToNum()
{ echo "$1" | sed 's/v//; s/_/./' | awk -F. '{ printf("%d%03d%02d\n", $1, $2, $3); }'; }
MD5_Hash(){ md5sum "$1" | awk -F' ' '{print $1}' ; }
strip_path(){ basename "$1"; }
delfr(){ rm -fr "$1"; }
Same_MD5_Hash(){ if [ "$(MD5_Hash "$1")" = "$(MD5_Hash "$2")" ]; then true; else false; fi; }
AppendDateTimeStamp()
{ [ -e "$1" ] && mv -f "$1" "${1}_$(date +'%Y-%m-%d_T%H%M%S')" ; }
SyslogNg_Running(){ if [ -n "$(pidof "$sng")" ]; then true; else false; fi; }
SyslogD_Running(){ if [ -n "$(pidof "$sld")" ]; then true; else false; fi; }
##-------------------------------------##
## Added by Martinski W. [2025-Nov-30] ##
##-------------------------------------##
_GetFileSize_()
{
if [ $# -eq 0 ] || [ -z "$1" ] || [ ! -s "$1" ]
then echo 0 ; return 1
fi
ls -1l "$1" | awk -F ' ' '{print $3}'
return 0
}
##----------------------------------------##
## Modified by Martinski W. [2026-Jan-10] ##
##----------------------------------------##
Clear_Syslog_Links()
{
if [ -L "$tmplog" ]
then delfr "$tmplog"
fi
if [ -L "${tmplog}-1" ]
then delfr "${tmplog}-1"
fi
if [ -L "$jffslog" ] || [ -d "$jffslog" ]
then delfr "$jffslog"
fi
if [ -L "${jffslog}-1" ] || [ -d "${jffslog}-1" ]
then delfr "${jffslog}-1"
fi
}
##----------------------------------------##
## Modified by Martinski W. [2026-Jan-10] ##
##----------------------------------------##
Start_SyslogD()
{
service start_logger
"$usbUnmountCaller" && count=5 || count=30
while ! SyslogD_Running && [ "$count" -gt 0 ]
do
sleep 1 #Give syslogd time to start up#
count="$(( count - 1 ))"
done
if [ "$count" -eq 0 ] && ! "$usbUnmountCaller"
then
printf "\n ${red}UNABLE to start syslogd. ABORTING!!${std}\n"
exit 1
fi
}
##-------------------------------------##
## Added by Martinski W. [2025-Jun-22] ##
##-------------------------------------##
_ServiceEventTime_()
{
[ ! -d "$config_d" ] && mkdir "$config_d"
[ ! -e "$script_conf" ] && touch "$script_conf"
if [ $# -eq 0 ] || [ -z "$1" ]
then return 1
fi
local timeNotFound lastEventTime
if ! grep -q "^SRVC_EVENT_TIME=" "$script_conf"
then timeNotFound=true
else timeNotFound=false
fi
case "$1" in
update)
if "$timeNotFound"
then
echo "SRVC_EVENT_TIME=$2" >> "$script_conf"
else
sed -i 's/^SRVC_EVENT_TIME=.*$/SRVC_EVENT_TIME='"$2"'/' "$script_conf"
fi
;;
check)
if "$timeNotFound"
then
lastEventTime=0
echo "SRVC_EVENT_TIME=0" >> "$script_conf"
else
lastEventTime="$(grep "^SRVC_EVENT_TIME=" "$script_conf" | cut -d'=' -f2)"
if ! echo "$lastEventTime" | grep -qE "^[0-9]+$"
then lastEventTime=0
fi
fi
echo "$lastEventTime"
;;
esac
}
##-------------------------------------##
## Added by Martinski W. [2025-Nov-29] ##
##-------------------------------------##
_Config_Option_Update_()
{
[ ! -d "$config_d" ] && mkdir "$config_d"
[ ! -e "$script_conf" ] && touch "$script_conf"
if [ $# -lt 2 ] || [ -z "$1" ] || [ -z "$2" ]
then return 1
fi
if ! grep -qE "^${1}=" "$script_conf"
then
echo "${1}=${2}" >> "$script_conf"
else
sed -i "s~${1}=.*~${1}=${2}~" "$script_conf"
fi
}
##-------------------------------------##
## Added by Martinski W. [2025-Nov-29] ##
##-------------------------------------##
_Config_Option_Get_()
{
[ ! -d "$config_d" ] && mkdir "$config_d"
[ ! -e "$script_conf" ] && touch "$script_conf"
if [ ! -s "$script_conf" ] || \
[ $# -eq 0 ] || [ -z "$1" ] || \
! grep -qE "^${1}=" "$script_conf"
then
echo ; return 1
fi
grep "^${1}=" "$script_conf" | cut -d'=' -f2
}
##-------------------------------------##
## Added by Martinski W. [2025-Nov-29] ##
##-------------------------------------##
_Config_Option_Check_()
{
if [ $# -eq 0 ] || [ -z "$1" ]
then return 1
fi
if [ -n "$(_Config_Option_Get_ "$1")" ]
then return 0
else return 1
fi
}
#-----------------------------------------------------------
# random routers point syslogd at /jffs instead of /tmp
# figure out where default syslog.log location is
# function assumes syslogd is running!
#-----------------------------------------------------------
##----------------------------------------##
## Modified by Martinski W. [2025-Nov-29] ##
##----------------------------------------##
Where_SyslogD()
{
local findStr
if [ -n "$(pidof syslogd)" ]
then
findStr="$(ps ww | grep '/syslogd' | grep -oE '\-O .*/syslog.log')"
if [ -n "$findStr" ]
then
syslog_loc="$(echo "$findStr" | awk -F' ' '{print $2}')"
fi
fi
[ -n "$syslog_loc" ] && _Config_Option_Update_ SYSLOG_LOC "$syslog_loc"
}
##----------------------------------------##
## Modified by Martinski W. [2025-Nov-30] ##
##----------------------------------------##
Create_Config()
{
printf "\n$white Detecting default syslog location... "
if SyslogNg_Running
then
slg_was_rng=true
printf "\n Briefly shutting down %s" "$sng"
killall -q "$sng" 2>/dev/null
count=10
while SyslogNg_Running && [ "$count" -gt 0 ]
do
sleep 1
count="$(( count - 1 ))"
done
Clear_Syslog_Links
else
slg_was_rng=false
fi
if ! SyslogD_Running
then Start_SyslogD
fi
Where_SyslogD
if "$slg_was_rng"
then
# If syslog-ng was running, kill syslogd and restart #
$S01sng_init start
elif [ -x "$sng_loc" ] && [ -x "$lr_loc" ] && \
[ -d "$lrd_d" ] && [ -n "$syslog_loc" ] && \
[ -s "$syslog_loc" ] && [ ! -L "$syslog_loc" ]
then
# Prepend /opt/var/messages to syslog & create link #
cat "$syslog_loc" >> "$optmsg"
mv -f "$optmsg" "$syslog_loc"
ln -s "$syslog_loc" "$optmsg"
fi
# Assume uiScribe is still running if it was before stopping syslog-ng #
}
##----------------------------------------##
## Modified by Martinski W. [2026-Jan-30] ##
##----------------------------------------##
Read_Config()
{
if [ -s "$script_conf" ] && grep -q "^SYSLOG_LOC=" "$script_conf"
then
syslog_loc="$(_Config_Option_Get_ SYSLOG_LOC)"
else
Create_Config
fi
export syslog_loc
if ! _Config_Option_Check_ LR_CRONJOB_HOUR
then
_Config_Option_Update_ LR_CRONJOB_HOUR 24
fi
if ! _Config_Option_Check_ DEBUG_LOG_LEVEL_MSG
then
_Config_Option_Update_ DEBUG_LOG_LEVEL_MSG Allowed
fi
if ! _Config_Option_Check_ FILTER_INIT_REBOOT_LOG
then
_Config_Option_Update_ FILTER_INIT_REBOOT_LOG true
fi
# Set correct permissions to avoid "world-readable" status #
if [ "$action" != "debug" ] && \
[ -f /var/lib/logrotate.status ]
then chmod 600 /var/lib/logrotate.status
fi
}
##----------------------------------------##
## Modified by Martinski W. [2025-Aug-23] ##
##----------------------------------------##
Update_File()
{
if [ $# -gt 2 ] && [ "$3" = "BACKUP" ]
then AppendDateTimeStamp "$2"
fi
cp -fp "$1" "$2"
}
Yes_Or_No()
{
read -r resp
case "$resp" in
[yY][eE][sS]|[yY])
true
;;
*)
false
;;
esac
}
##-------------------------------------##
## Added by Martinski W. [2025-Nov-23] ##
##-------------------------------------##
_CenterTextStr_()
{
if [ $# -lt 2 ] || [ -z "$1" ] || [ -z "$2" ] || \
! echo "$2" | grep -qE "^[1-9][0-9]+$"
then return 1
fi
local stringLen="${#1}"
local space1Len="$((($2 - stringLen)/2))"
local space2Len="$space1Len"
local totalLen="$((space1Len + stringLen + space2Len))"
if [ "$totalLen" -lt "$2" ]
then space2Len="$((space2Len + 1))"
elif [ "$totalLen" -gt "$2" ]
then space1Len="$((space1Len - 1))"
fi
if [ "$space1Len" -gt 0 ]
then spaceLenX="$space1Len"
fi
}
##----------------------------------------##
## Modified by Martinski W. [2025-Jun-09] ##
##----------------------------------------##
ScriptLogo()
{
if ! "$banner"
then return 0
fi
local spaceLenT=45 spaceLenX=5 colorCT
_CenterTextStr_ "$scribe_ver $branchxStr_TAG" "$spaceLenT"
[ "$script_branch" = "master" ] && colorCT="$green" || colorCT="$magenta"
clear
printf "$white _\n"
printf " _ ( ) \n"
printf " ___ ___ _ __ (_)| |_ __ \n"
printf " /',__) /'___)( '__)| || '_\`\\ /'__\`\\ \n"
printf " \\__, \\( (___ | | | || |_) )( ___/ \n"
printf " (____/\`\\____)(_) (_)(_,__/'\`\\____) \n"
printf " %s and %s installation $std\n" "$sng" "$lr"
printf "%*s${green}%s${std} ${colorCT}%s${std}\n" "$spaceLenX" '' "$scribe_ver" "$branchxStr_TAG"
printf " ${blue}https://github.com/AMTM-OSR/scribe${std}\n"
printf " ${blue}Original author: cmkelley${std}\n\n"
}
warning_sign()
{
printf "\n\n$white"
printf " *********************\n"
printf " ***$red W*A*R*N*I*N*G$white ***\n"
printf " *********************\n\n"
}
##----------------------------------------##
## Modified by Martinski W. [2025-Jun-09] ##
##----------------------------------------##
Get_ZIP_File()
{
if ! $got_zip
then
delfr "$unzip_dirPath"
delfr "$script_zip_file"
printf "\n$white Fetching %s from GitHub %s branch ...$std\n" "$script_name" "$script_branch"
if curl -fL --retry 4 --retry-delay 5 --retry-connrefused "$script_repo_ZIP" -o "$script_zip_file"
then
printf "\n$white unzipping %s ...$std\n" "$script_name"
unzip "$script_zip_file" -d "$TMP"
/opt/bin/opkg update
got_zip=true
else
printf "\n$white %s GitHub repository$red is unavailable! $std -- Aborting.\n" "$script_name"
exit 1
fi
fi
}
##----------------------------------------##
## Modified by Martinski W. [2026-Mar-02] ##
##----------------------------------------##
Restart_uiScribe()
{
local from=""
if "$uiScribeInstalled"
then
printf "\n$white Restarting ${uiscribeName}...\n"
if [ $# -gt 0 ] && [ "$1" = "true" ]
then from="Scribe"
fi
$uiscribePath startup $from
fi
}
Reload_SysLogNg_Config()
{
printf "$white reloading %s ... $cyan" "$( strip_path $sng_conf )"
$sngctl_loc reload
printf "\n$std"
Restart_uiScribe "$@"
}
Copy_SysLogNg_RcFunc()
{
printf " ${white}copying %s to %s ...$std" "$rcfunc_sng" "$init_d"
cp -fp "${unzip_dirPath}/init.d/$rcfunc_sng" "$init_d/"
chmod 644 "$rcfunc_loc"
finished
}
##-------------------------------------##
## Added by Martinski W. [2026-Feb-27] ##
##-------------------------------------##
_TopLevelConfigLinesCheck_()
{
if [ $# -eq 0 ] || [ -z "$1" ] || [ ! -s "$1" ]
then return 1
fi
local retCode=0 lineNum=1
local lineStr1 lineStr2 lineStr3 lineStr4 lineStr5
lineStr1='wildcard_file(base_dir("/opt/var/log")'
lineStr2='filename_pattern("syslogd.ScribeInitReboot.LOG")'
lineStr3='recursive(no) max-files(1) follow_freq(1)'
lineStr4='log_iw_size(1200) log_fetch_limit(1000) flags(syslog-protocol))'
lineStr5='filter \{ level\((info|debug)..emerg\); \}'
while read -r theLINE && [ -n "$theLINE" ]
do
if ! echo "$theLINE" | grep -qF "$(eval echo '$'"lineStr$lineNum")"
then retCode=1 ; break
fi
lineNum="$((lineNum + 1))"
done <<EOT
$(grep -F -A3 "$lineStr1" "$1")
EOT
if ! grep -qE "$lineStr5" "$1"
then retCode=1
fi
return "$retCode"
}
##-------------------------------------##
## Added by Martinski W. [2026-Jan-15] ##
##-------------------------------------##
Copy_SysLogNg_Top_Config()
{
local forceUpdate=false
local diffFile="/opt/tmp/syslogNG_diffs.TEMP.txt"
local srceFile="${unzip_dirPath}/${syslogNgStr}.share/${syslogNg_ConfName}-scribe"
[ ! -s "$srceFile" ] && return 1
[ ! -d "$syslogNg_ExamplesDir" ] && mkdir -p "$syslogNg_ExamplesDir"
if [ $# -gt 0 ] && [ "$1" = "force" ]
then forceUpdate=true
fi
for destFile in "$syslogNg_TopConfig" "${syslogNg_ExamplesDir}/${syslogNg_ConfName}-scribe"
do
if [ ! -s "$destFile" ] || [ "$destFile" != "$syslogNg_TopConfig" ]
then
cp -fp "$srceFile" "$destFile"
elif "$forceUpdate" || ! Same_MD5_Hash "$srceFile" "$destFile"
then
diff -U0 "$srceFile" "$destFile" 2>/dev/null | \
grep -Ev "^(\-\-\-|\+\+\+)" | grep -E "^(\-|\+)" | \
grep -Ev "^(\-|\+)[[:blank:]]+log_fifo_size\(" > "$diffFile"
if [ -s "$diffFile" ] && \
grep -qE "^(\-|\+)" "$diffFile" && \
[ "$(wc -l < "$diffFile")" -gt 0 ] && \
! _TopLevelConfigLinesCheck_ "$destFile"
then
printf " ${yellow}updating $destFile ..."
Update_File "$srceFile" "$destFile" "BACKUP"
finished
printf " ${red}------------\n ***NOTICE***\n ------------${std}\n"
printf " ${yellow}The file ${green}/opt/etc/syslog-ng.conf${yellow} has been replaced with"
printf " a newer version.\n This means that any custom configuration options that you may"
printf " have added\n or modified in the previously installed file are now removed.\n A backup"
printf " of the previous file was created in the '${green}/opt/etc${yellow}' directory.${std}\n\n"
PressEnterTo "continue..."
fi
rm -f "$diffFile"
fi
chmod 644 "$destFile"
done
}
##-------------------------------------##
## Added by Martinski W. [2026-Jan-12] ##
##-------------------------------------##
Copy_LogRotate_Global_Options()
{
local forceUpdate=false
local srceFile="${unzip_dirPath}/${logRotateStr}.d/$logRotateGlobalName"
[ ! -s "$srceFile" ] && return 1
[ ! -d "$logRotateExamplesDir" ] && mkdir -p "$logRotateExamplesDir"
if [ $# -gt 0 ] && [ "$1" = "force" ]
then forceUpdate=true
fi
for destFile in "$logRotateGlobalConf" "${logRotateExamplesDir}/$logRotateGlobalName"
do
if [ ! -s "$destFile" ] || "$forceUpdate"
then
cp -fp "$srceFile" "$destFile"
chmod 600 "$destFile"
fi
done
}
##-------------------------------------##
## Added by Martinski W. [2026-Jan-11] ##
##-------------------------------------##
_ShowSysLogNg_WaitStart_Msge_()
{
local waitSecs=180
if [ -s "$syslogNg_WaitnSEM_FPath" ]
then waitSecs="$(head -n1 "$syslogNg_WaitnSEM_FPath")"
fi
printf " ${magenta}NOTICE:\n -------${CLRct}\n"
printf " ${yellow}%s will start in about ${GRNct}%d${yellow} seconds...\n" "$sng" "$waitSecs"
printf " Please wait until %s has been started.${CLRct}\n\n" "$sng"
}
##----------------------------------------##
## Modified by Martinski W. [2026-Jan-11] ##
##----------------------------------------##
Check_SysLogNg()
{
printf "\n$white %34s" "checking $sng daemon ..."
if SyslogNg_Running
then
printf " ${green}alive.${std}\n"
else
printf " ${red}dead.${std}\n"
if [ -f "$syslogNg_WaitnSEM_FPath" ]
then
echo ; _ShowSysLogNg_WaitStart_Msge_
fi
printf "$white %34s" "the system logger (syslogd) ..."
if SyslogD_Running
then
printf " ${green}is running.${std}\n\n"
if [ ! -f "$syslogNg_WaitnSEM_FPath" ]
then
printf " ${yellow}Type ${green}%s restart${yellow} at shell prompt or select ${green}rs${std}\n" "$script_name"
printf " ${yellow}from %s main menu to start %s.${std}\n\n" "$script_name" "$sng"
fi
else
printf " ${red}is NOT running!${std}\n\n"
if [ ! -f "$syslogNg_WaitnSEM_FPath" ]
then
printf " ${yellow}Type ${green}%s -Fevd${yellow} at shell prompt or select '${green}sd${std}'\n" "$sng"
printf " ${yellow}from %s utilities menu ('${green}su${yellow}' option) to view %s\n" "$script_name" "$sng"
printf " debugging information.${std}\n\n"
fi
fi
fi
}
sed_SysLogNg_Init()
{
printf "$white %34s" "checking $( strip_path "$S01sng_init" ) ..."
if ! grep -q "$rcfunc_sng" "$S01sng_init"
then
sed -i "\~/opt/etc/init.d/rc.func$~i . $rcfunc_loc #${script_name}#\n" "$S01sng_init"
updated
else
present
fi
}
rd_warn(){
printf "$yellow Use utility menu (su) option 'rd' to re-detect! $std\n"
}
##----------------------------------------##
## Modified by Martinski W. [2025-Nov-29] ##
##----------------------------------------##
SysLogd_Check()
{
local checksys_loc
printf "$white %34s" "syslog.log default location ..."
if [ "$syslog_loc" != "$jffslog" ] && [ "$syslog_loc" != "$tmplog" ]
then
printf "$red NOT SET!\n"
rd_warn
return 1
else
printf "$green %s $std\n" "$syslog_loc"
fi
printf "$white %34s" "... & agrees with config file ..."
checksys_loc="$(_Config_Option_Get_ SYSLOG_LOC)"
if [ -z "$checksys_loc" ]
then
printf "$red NO CONFIG FILE!\n"
rd_warn
elif [ "$syslog_loc" = "$checksys_loc" ]
then
printf "$green okay! $std\n"
else
printf "$red DOES NOT MATCH!\n"
rd_warn
return 1
fi
}
sed_srvcEvent()
{
printf "$white %34s" "checking $( strip_path "$srvcEvent" ) ..."
if [ -f "$srvcEvent" ]
then
[ "$( grep -c "#!/bin/sh" "$srvcEvent" )" -ne 1 ] && sed -i "1s~^~#!/bin/sh -\n\n~" "$srvcEvent"
if grep -q "$script_name kill-logger" "$srvcEvent"
then sed -i "/$script_name kill-logger/d" "$srvcEvent"
fi
if grep -q "$script_name kill_logger" "$srvcEvent"
then sed -i "/$script_name kill_logger/d" "$srvcEvent"
fi
if ! grep -q "^$script_loc service_event" "$srvcEvent"
then
echo "$script_loc service_event \"\$@\" & #${script_name}#" >> "$srvcEvent"
updated
else
present
fi
else
{
echo "#!/bin/sh -" ; echo
echo "$script_loc service_event \"\$@\" & #${script_name}#"
} > "$srvcEvent"
printf "$green created. $std\n"
fi
[ ! -x "$srvcEvent" ] && chmod 0755 "$srvcEvent"
}
##----------------------------------------##
## Modified by Martinski W. [2025-Dec-05] ##
##----------------------------------------##
LogRotate_CronJob_PostMount_Create()
{
local foundLineCount cronHourTmp
local cronMinsStr="$LR_CronJobMins"
local cronHourStr="$(_Get_LogRotate_CronHour_)"
[ ! -x "$postMount" ] && chmod 0755 "$postMount"
cronHourTmp="$(echo "$cronHourStr" | sed 's/\*/[\*]/')"
foundLineCount="$(grep -cE "cru a $logRotateStr .* [*] [*] [*] $lr_loc $lr_conf" "$postMount")"
if [ "$foundLineCount" -gt 0 ]
then
sed -i "/cru a ${logRotateStr}/d" "$postMount"
fi
foundLineCount="$(grep -cE "cru a $LR_CronTagStr .* [*] [*] [*] $script_loc LogRotate" "$postMount")"
if ! grep -qE "cru a $LR_CronTagStr \"$cronMinsStr $cronHourTmp [*] [*] [*] $script_loc LogRotate" "$postMount"
then
if [ "$foundLineCount" -gt 0 ]
then
sed -i "/cru a ${LR_CronTagStr}/d" "$postMount"
fi
{
echo '[ -x "${1}/entware/bin/opkg" ] && cru a '"$LR_CronTagStr"' "'"$cronMinsStr $cronHourStr"' * * * '"$script_loc"' LogRotate" #'"$script_name"'#'
} >> "$postMount"
return 0
else
return 1
fi
}
##----------------------------------------##
## Modified by Martinski W. [2025-Nov-29] ##
##----------------------------------------##
LogRotate_CronJob_PostMount_Check()
{
printf "$white %34s" "checking $( strip_path "$postMount" ) ..."
if [ ! -s "$postMount" ]
then
printf "$red MISSING! \n"
printf " Entware is not properly set up!\n"
printf " Correct Entware installation before continuing! ${std}\n\n"
exit 1
fi
if LogRotate_CronJob_PostMount_Create
then
updated
else
present
fi
# Set correct permissions to avoid "world-readable" status #
[ -f /var/lib/logrotate.status ] && chmod 600 /var/lib/logrotate.status
}
##----------------------------------------##
## Modified by Martinski W. [2026-Jan-10] ##
##----------------------------------------##
sed_unMount()
{
printf "$white %34s" "checking $( strip_path "$unMount" ) ..."
if [ -f "$unMount" ]
then
[ "$( grep -c "#!/bin/sh" "$unMount" )" -ne 1 ] && sed -i "1s~^~#!/bin/sh -\n\n~" "$unMount"
if grep -q " && $script_name stop nologo" "$unMount"
then