Advertisement
kmfdm

Scribe

Aug 11th, 2022
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 49.05 KB | None | 0 0
  1. #!/bin/sh
  2. #tof
  3. # _
  4. # _ ( )
  5. # ___ ___ _ __ (_)| |_ __
  6. # /',__) /'___)( '__)| || '_`\ /'__`\
  7. # \__, \( (___ | | | || |_) )( ___/
  8. # (____/`\____)(_) (_)(_,__/'`\____)
  9. # syslog-ng and logrotate installer for Asuswrt-Merlin
  10. #
  11. # Coded by cynicastic
  12. #
  13. # Original interest in syslog-ng on Asuswrt-Merlin inspired by tomsk & kvic
  14. # Good ideas and code borrowed heavily from Adamm, Jack Yaz, thelonelycoder, & Xentrx
  15. #
  16. # Bad ideas and code are entirely mine
  17. #
  18. # install command:
  19. # curl --retry 3 "https://raw.githubusercontent.com/cynicastic/scribe/master/scribe" -o "/jffs/scripts/scribe" && chmod 0755 /jffs/scripts/scribe && /jffs/scripts/scribe install
  20. #
  21. # shellcheck disable=SC2009
  22. # SC2009 = Consider uing pgrep ~ Note that pgrep doesn't exist in asuswrt (exists in Entware procps-ng)
  23. # shellcheck disable=SC2059
  24. # 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
  25.  
  26.  
  27. # parse parameters
  28. action="$1"
  29. [ "X$action" = "X" ] && action="menu"
  30. got_zip=false
  31. [ -n "$2" ] && [ "$2" = "gotzip" ] && got_zip=true && shift
  32. banner=true
  33. [ "$SCRIBE_LOGO" = "nologo" ] && banner=false
  34. [ -n "$2" ] && [ "$2" = "nologo" ] && banner=false && shift
  35. [ -z "$TMP" ] && TMP=/opt/tmp
  36.  
  37. # scribe constants
  38. readonly scribe_name="scribe"
  39. readonly scribe_branch="master"
  40. scribe_ver="v2.4.3" # version number for amtm compatibility, but keep vX.Y_Z otherwise because I'm stubborn
  41. readonly scribe_ver="$( echo $scribe_ver | sed 's/\./_/2' )"
  42. readonly scribe_long="$scribe_ver ($scribe_branch)"
  43. readonly scribe_author="cynicastic"
  44. readonly raw_git="https://raw.githubusercontent.com"
  45. readonly scribe_repo="$raw_git/$scribe_author/$scribe_name/$scribe_branch/$scribe_name"
  46. readonly unzip_dir="$TMP/$scribe_name-$scribe_branch"
  47. readonly scribe_tmp="$TMP/scribe.tmp"
  48. readonly script_d="/jffs/scripts"
  49. readonly scribe_script="$script_d/$scribe_name"
  50.  
  51. # router details
  52. readonly merlin="ASUSWRT-Merlin"
  53. readonly fwreqd="380.68"
  54. readonly fwname="$( uname -o )"
  55. readonly fwvers="$( nvram get buildno )"
  56. model="$( nvram get odmpid )"
  57. [ -z "$model" ] && model="$( nvram get productid )"
  58. readonly model
  59. readonly dcd_crash="AC86"
  60. readonly arch="$( uname -m )"
  61.  
  62. # miscellaneous constants
  63. readonly header="=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=$std\n\n"
  64. readonly sng="syslog-ng"
  65. readonly sng_reqd="3.19"
  66. readonly lr="logrotate"
  67. readonly init_d="/opt/etc/init.d"
  68. readonly S01sng_init="$init_d/S01$sng"
  69. readonly rcfunc_sng="rc.func.$sng"
  70. readonly rcfunc_loc="$init_d/$rcfunc_sng"
  71. readonly syslog_log="/tmp/syslog.log"
  72. readonly sng_loc="/opt/sbin/$sng"
  73. readonly sngctl_loc="$sng_loc-ctl"
  74. readonly lr_loc="/opt/sbin/$lr"
  75. readonly sng_conf="/opt/etc/$sng.conf"
  76. readonly debug_sep="=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*="
  77. readonly scribe_debug_name=$scribe_name"_debug.log"
  78. readonly scribe_debug=$TMP/$scribe_debug_name
  79. readonly sngconf_merged="$TMP/$sng-complete.conf"
  80. readonly sngconf_error="$TMP/$sng-error.conf"
  81. readonly lr_conf="/opt/etc/$lr.conf"
  82. readonly lr_daily="/opt/tmp/logrotate.daily"
  83. readonly lr_temp="/opt/tmp/logrotate.temp"
  84. readonly sngd_d="/opt/etc/$sng.d"
  85. readonly lrd_d="/opt/etc/$lr.d"
  86. readonly etc_d="/opt/etc/*.d"
  87. readonly sng_share="/opt/share/$sng"
  88. readonly lr_share="/opt/share/$lr"
  89. readonly share_ex="/opt/share/*/examples"
  90. readonly scribe_bakname="$TMP/$scribe_name-backup.tar.gz"
  91. readonly fire_start="$script_d/firewall-start"
  92. readonly srvc_event="$script_d/service-event"
  93. readonly postmount="$script_d/post-mount"
  94. readonly unmount="$script_d/unmount"
  95. readonly skynet="$script_d/firewall"
  96. readonly sky_req="6.9.2"
  97. readonly divers="/opt/bin/diversion"
  98. readonly div_req="4.1"
  99.  
  100. readonly uisc_name="uiScribe"
  101. readonly uisc_author="jackyaz"
  102. readonly uisc_branch="master"
  103. readonly uisc_repo="$raw_git/$uisc_author/$uisc_name/$uisc_branch/$uisc_name.sh"
  104. readonly uiscribe="$script_d/$uisc_name"
  105.  
  106. # color constants
  107. readonly red="\033[1;31m"
  108. readonly green="\033[1;32m"
  109. readonly yellow="\033[1;33m"
  110. readonly blue="\033[1;34m"
  111. readonly magenta="\033[1;35m"
  112. readonly cyan="\033[1;36m"
  113. readonly white="\033[1;37m"
  114. readonly std="\033[m"
  115.  
  116. # check if scribe is already installed by looking for link in /opt/bin
  117. [ -e "/opt/bin/$scribe_name" ] && is_inst=true || is_inst=false
  118.  
  119. # test if uiScribe is installed
  120. [ -e "$uiscribe" ] && uisc_inst=true || uisc_inst=false
  121.  
  122. # check if Skynet is installed
  123. if [ -e "$fire_start" ] && grep -q "skynetloc" "$fire_start"
  124. then
  125. skynet_inst=true
  126. else
  127. skynet_inst=false
  128. fi
  129.  
  130. # check if syslog-ng is running
  131. [ -n "$( pidof $sng )" ] && sng_rung=true || sng_rung=false
  132.  
  133. #### functions ####
  134.  
  135. present(){ printf "$green present. $std\n"; }
  136.  
  137. updated(){ printf "$yellow updated. $std\n"; }
  138.  
  139. finis (){ printf "$green done. $std\n"; }
  140.  
  141. not_installed(){ printf "\n$blue %s$red NOT$white installed! $std\n" "$1"; }
  142.  
  143. enter_to(){ printf "$white Press [Enter] to %s: $std" "$1"; read -r; echo; }
  144.  
  145. ver_num(){ echo "$1" | sed 's/v//; s/_/./' | awk -F. '{ printf("%d%02d%02d\n", $1, $2, $3); }'; }
  146.  
  147. md5_file(){ md5sum "$1" | awk '{ printf( $1 ); }'; } # get md5sum of file
  148.  
  149. dlt(){ rm -rf "$1"; }
  150.  
  151. same_same(){ if [ "$( md5_file "$1" )" = "$( md5_file "$2" )" ]; then true; else false; fi; }
  152.  
  153. date_stamp(){ [ -e "$1" ] && mv "$1" "$1-$( date -Iseconds | cut -c 1-19 )"; }
  154.  
  155. update_file(){
  156. [ -n "$3" ] && [ "$3" = "backup" ] && date_stamp "$2"
  157. cp -pf "$1" "$2"
  158. }
  159.  
  160. hup_uisc(){
  161. if $uisc_inst
  162. then
  163. printf "$white Restarting uiScribe ..."
  164. $uiscribe startup
  165. printf " $green done. $std\n"
  166. fi
  167. }
  168.  
  169. # Check yes or no
  170. yes_no(){
  171. read -r resp
  172. case "$resp" in
  173. [yY][eE][sS]|[yY])
  174. true
  175. ;;
  176. *)
  177. false
  178. ;;
  179. esac
  180. }
  181.  
  182. logo(){
  183. if ! $banner; then return; fi
  184. clear
  185. printf "$white _\n"
  186. printf " _ ( ) \n"
  187. printf " ___ ___ _ __ (_)| |_ __ \n"
  188. printf " /',__) /'___)( '__)| || '_\`\\ /'__\`\\ \n"
  189. printf " \\__, \\( (___ | | | || |_) )( ___/ \n"
  190. printf " (____/\`\\____)(_) (_)(_,__/'\`\\____) \n"
  191. printf " %s and %s installation $std\n" "$sng" "$lr"
  192. printf " $green %-15s $blue Coded by cynicastic $std\n\n" "$scribe_long"
  193. }
  194.  
  195. warning_sign(){
  196. printf "\n\n$white"
  197. printf " *********************\n"
  198. printf " ***$red W*A*R*N*I*N*G$white ***\n"
  199. printf " *********************\n\n"
  200. }
  201.  
  202. get_zip(){
  203. if ! $got_zip
  204. then
  205. dlt $unzip_dir
  206. dlt $TMP/$scribe_name.zip
  207. printf "\n$white fetching %s from GitHub %s branch ...$std\n" "$scribe_name" "$scribe_branch"
  208. if curl -fL https://github.com/$scribe_author/$scribe_name/archive/$scribe_branch.zip -o $TMP/$scribe_name.zip
  209. then
  210. printf "\n$white unzipping %s ...$std\n" "$scribe_name"
  211. unzip $TMP/$scribe_name.zip -d $TMP
  212. opkg update
  213. got_zip=true
  214. else
  215. printf "\n$white %s GitHub repository$red is unavailable! $std -- Aborting.\n" "$scribe_name"
  216. exit 1
  217. fi
  218. fi
  219. }
  220.  
  221. rld_conf(){
  222. printf "$white reloading %s ... $cyan" "$( basename $sng_conf )"
  223. $sngctl_loc reload
  224. printf "\n$std"
  225. hup_uisc
  226. }
  227.  
  228. copy_rcfunc(){
  229. printf "$white copying %s to %s ...$std" "$rcfunc_sng" "$init_d"
  230. cp -pf $unzip_dir/init.d/$rcfunc_sng $init_d/
  231. chmod 644 $rcfunc_loc
  232. finis
  233. }
  234.  
  235. check_sng(){
  236. printf "\n$white %34s" "checking $sng daemon ..."
  237. if $sng_rung
  238. then
  239. printf "$green alive. $std\n"
  240. else
  241. printf "$red dead. $std\n"
  242. printf "$white %34s" "the system logger (syslogd) ..."
  243. if [ -n "$( pidof syslogd )" ]
  244. then
  245. printf "$green is running. $std\n\n"
  246. printf "$yellow Type$red %s restart$yellow at shell prompt or select$red rs$yellow\n" "$scribe_name"
  247. printf " from %s main menu to start %s.\n" "$scribe_name" "$sng"
  248. else
  249. printf "$red is not running! $std\n\n"
  250. printf "$white Type$red %s -Fevd$white at shell prompt or select$red sd$white\n" "$sng"
  251. printf " from %s utilities menu ($red%s$white) to view %s\n" "$scribe_name" "su" "$sng"
  252. printf " debugging data.\n"
  253. fi
  254. fi
  255. }
  256.  
  257. sed_sng(){
  258. printf "$white %34s" "checking $( basename $S01sng_init ) ..."
  259. if ! grep -q $rcfunc_sng $S01sng_init
  260. then
  261. sed -i "\~/opt/etc/init.d/rc.func~i . $rcfunc_loc # added by scribe\n" $S01sng_init
  262. updated
  263. else
  264. present
  265. fi
  266. }
  267.  
  268. sed_srvc(){
  269. printf "$white %34s" "checking $( basename $srvc_event ) ..."
  270. if [ -f $srvc_event ]
  271. then
  272. [ "$( grep -c "#!/bin/sh" $srvc_event )" -ne 1 ] && sed -i "1s~^~#!/bin/sh\n\n~" $srvc_event
  273. if grep -q "kill_logger" $srvc_event; then sed -i "s/$scribe_name kill_logger/$scribe_name kill-logger/g" $srvc_event; fi
  274. if grep -q "nologo \"\$2\"" $srvc_event; then sed -i "s/nologo \"\$2\"/nologo \"\$1\" \"\$2\"/g" $srvc_event; fi
  275. if ! grep -q "$scribe_name kill-logger" $srvc_event
  276. then
  277. echo "$scribe_script kill-logger nologo \"\$1\" \"\$2\" & # added by scribe" >> $srvc_event
  278. updated
  279. else
  280. present
  281. fi
  282. else
  283. {
  284. echo "#!/bin/sh"
  285. echo ""
  286. echo "$scribe_script kill-logger nologo \"\$1\" \"\$2\" & # added by scribe"
  287. } > $srvc_event
  288. printf "$green created. $std\n"
  289. fi
  290. [ ! -x $srvc_event ] && chmod 0755 $srvc_event
  291. }
  292.  
  293. lr_post(){
  294. printf "$white %34s" "checking $( basename $postmount ) ..."
  295. if [ ! -f $postmount ]
  296. then
  297. printf "$red MISSING! \n"
  298. printf " Entware is not properly set up!\n"
  299. printf " Correct Entware installation before continuing! $std\n\n"
  300. exit 1
  301. fi
  302. if ! grep -q $lr $postmount
  303. then
  304. echo "cru a $lr \"5 0 * * * $lr_loc $lr_conf >> $lr_daily 2>&1\" # added by scribe" >> $postmount
  305. updated
  306. else
  307. present
  308. fi
  309. }
  310.  
  311. sed_umnt(){
  312. printf "$white %34s" "checking $( basename $unmount ) ..."
  313. if [ -f $unmount ]
  314. then
  315. [ "$( grep -c "#!/bin/sh" $unmount )" -ne 1 ] && sed -i "1s~^~#!/bin/sh\n\n~" $unmount
  316. if ! grep -q "$scribe_name stop" $unmount
  317. then
  318. echo "[ \"\$(/usr/bin/find \$1/entware*/bin/$scribe_name 2> /dev/null)\" ] && $scribe_name stop nologo # added by $scribe_name" >> $unmount
  319. updated
  320. else
  321. present
  322. fi
  323. else
  324. {
  325. echo "#!/bin/sh"
  326. echo ""
  327. echo "[ \"\$(/usr/bin/find \$1/entware*/bin/$scribe_name 2> /dev/null)\" ] && $scribe_name stop nologo # added by $scribe_name"
  328. } > $unmount
  329. printf "$green created. $std\n"
  330. fi
  331. [ ! -x $unmount ] && chmod 0755 $unmount
  332. }
  333.  
  334. lr_cron(){
  335. printf "$white %34s" "checking $lr cron job ..."
  336. if ! cru l | grep -q $lr
  337. then
  338. cru a $lr "5 0 * * * $lr_loc $lr_conf >> $lr_daily 2>&1"
  339. updated
  340. else
  341. present
  342. fi
  343. }
  344.  
  345. dir_links(){
  346. printf "$white %34s" "checking directory links ..."
  347. if [ ! -L "/tmp/syslog.log" ] || [ ! -d "/jffs/syslog.log" ] || [ ! -d "/jffs/syslog.log-1" ]
  348. then
  349. # load kill_logger() function to reset system path links/hacks
  350. # keep shellcheck from barfing on sourcing $rcfunc_loc
  351. # shellcheck disable=SC1091
  352. # shellcheck source=/opt/etc/init.d/rc.func.syslog-ng
  353. . $rcfunc_loc
  354. kill_logger
  355. updated
  356. else
  357. present
  358. fi
  359. }
  360.  
  361. sync_conf(){
  362. printf "$white %34s" "$( basename $sng_conf ) version check ..."
  363. sng_vers="$( $sng --version | grep -m1 $sng | grep -oE '[0-9]{1,2}([_.][0-9]{1,2})' )"
  364. sng_conf_vers="$( grep -m1 '@version:' $sng_conf | grep -oE '[0-9]{1,2}([_.][0-9]{1,2})' )"
  365. man_vers="$( grep -oE 'on\/[0-9]{1,2}([_.][0-9]{1,2})' $sng_conf )"
  366. if [ "$sng_vers" != "$sng_conf_vers" ]
  367. then
  368. printf "$red out of sync! (%s) $std\n" "$sng_conf_vers"
  369. printf "$cyan *** Updating %s and restarting %s *** $std\n" "$( basename $sng_conf )" "$sng"
  370. $S01sng_init stop
  371. sng_rung=false
  372. sed -i "s~$man_vers~on/$sng_vers~" $sng_conf
  373. sed -i "s/$sng_conf_vers.*/$sng_vers/" $sng_conf
  374. $S01sng_init start
  375. hup_uisc
  376. [ -n "$( pidof $sng )" ] && sng_rung=true || sng_rung=false
  377. printf "$white %34s" "$( basename $sng_conf ) version ..."
  378. printf "$yellow updated! (%s) $std\n" "$sng_vers"
  379. logger -t "$scribe_name" "$( basename $sng_conf ) version number updated ($sng_vers)!"
  380. else
  381. printf "$green in sync. (%s) $std\n" "$sng_vers"
  382. fi
  383. }
  384.  
  385. sng_syntax(){
  386. printf "$white %34s" "$( basename $sng_conf ) syntax check ..."
  387. if $sng_loc -s >> /dev/null 2>&1; then printf "$green okay! $std\n"; else printf "$red FAILED! $std\n\n"; fi
  388. }
  389.  
  390. get_vers(){
  391. # only get scribe from github once
  392. scribe_md5="$( md5_file "$scribe_script")"
  393. dlt $scribe_tmp
  394. curl -fsL --retry 3 "$scribe_repo" -o "$scribe_tmp"
  395. [ ! -e "$scribe_tmp" ] && printf "\n\n$white %s GitHub repository is unavailable! -- $red ABORTING! $std\n\n" "$scribe_name" && exit 1
  396. github_ver="$( grep -m1 "scribe_ver=" "$scribe_tmp" | grep -oE 'v?[0-9]{1,2}([.][0-9]{1,2})([_.][0-9]{1,2})' | sed 's/\./_/2' )"
  397. github_branch="$( grep -m1 "scribe_branch=" "$scribe_tmp" | awk -F\" '{ printf ( $2 ); }'; )"
  398. github_long="$github_ver ($github_branch)"
  399. github_md5="$( md5_file "$scribe_tmp")"
  400. new_vers="none"
  401. if [ "$( ver_num "$github_ver" )" -lt "$( ver_num "$scribe_ver" )" ]; then new_vers="older"
  402. elif [ "$( ver_num "$github_ver" )" -gt "$( ver_num "$scribe_ver" )" ]; then new_vers="major"
  403. elif [ "$scribe_md5" != "$github_md5" ]; then new_vers="minor"
  404. fi
  405. dlt $scribe_tmp
  406. }
  407.  
  408. prt_vers(){
  409. printf "\n$white %34s$green %s \n" "$scribe_name installed version:" "$scribe_long"
  410. printf "$white %34s$green %s $std\n" "$scribe_name GitHub version:" "$github_long"
  411. case "$new_vers" in
  412. older)
  413. printf "$red Local %s version greater than GitHub version!" "$scribe_name"
  414. ;;
  415. major)
  416. printf "$yellow %45s" "New $scribe_name version available"
  417. ;;
  418. minor)
  419. printf "$blue %45s" "Minor $scribe_name update available"
  420. ;;
  421. none)
  422. printf "$green %40s" "$scribe_name is up to date!"
  423. ;;
  424. esac
  425. printf "$std\n\n"
  426. }
  427.  
  428. # install default file in /usr/etc/$1.d
  429. setup_ddir(){
  430. [ "$1" = "$sng" ] && d_dir="$sngd_d"
  431. [ "$1" = "$lr" ] && d_dir="$lrd_d"
  432.  
  433. for dfile in "$unzip_dir/$1.d"/*
  434. do
  435. dfbase="$( basename "$dfile" )"
  436. ddfile="$d_dir/$dfbase"
  437. { [ ! -e "$ddfile" ] || [ "$2" = "ALL" ]; } && cp -p "$dfile" "$ddfile"
  438. done
  439. chmod 600 $d_dir/*
  440. }
  441.  
  442. # install example files in /usr/share/$1/examples
  443. setup_exmpls(){
  444. [ "$1" = "$sng" ] && share="$sng_share" && conf="$sng_conf"
  445. [ "$1" = "$lr" ] && share="$lr_share" && conf="$lr_conf"
  446. opkg="$1.conf-opkg"
  447. conf_opkg="$conf-opkg"
  448.  
  449. [ "$2" != "ALL" ] && printf "\n$white"
  450. [ ! -d $share ] && mkdir $share
  451. [ ! -d $share/examples ] && mkdir $share/examples
  452.  
  453. for exmpl in "$unzip_dir/$1.share"/*
  454. do
  455. shrfile="$share/examples/$( basename "$exmpl" )"
  456. if [ ! -e "$shrfile" ] || [ "$2" = "ALL" ]
  457. then
  458. update_file "$exmpl" "$shrfile"
  459. elif ! same_same "$exmpl" "$shrfile"
  460. then
  461. printf " updating %s\n" "$shrfile"
  462. update_file "$exmpl" "$shrfile"
  463. fi
  464. done
  465.  
  466. if [ -e $conf_opkg ]
  467. then
  468. update_file "$conf_opkg" "$share/examples/$opkg" "backup"
  469. dlt $conf_opkg
  470. elif [ ! -e "$share/examples/$opkg" ]
  471. then
  472. cp -pf $conf "$share/examples/$opkg"
  473. if [ "$1" = "$sng" ]
  474. then
  475. printf "\n$white NOTE: The %s file provided by the Entware %s package sources a very\n" "$( basename $conf )" "$sng"
  476. printf " complex set of logging functions most users don't need.$magenta A replacement %s has been\n" "$( basename $conf )"
  477. printf " installed to %s$white that corrects this issue. The %s file provided\n" "$conf" "$( basename $conf )"
  478. printf " by the Entware package has been moved to $cyan%s$white.\n" "$share/examples/$opkg"
  479. fi
  480. fi
  481. chmod 600 $share/examples/*
  482. printf "$std"
  483. }
  484.  
  485. force_install(){
  486. printf "\n$blue %s$white already installed!\n" "$1"
  487. [ "$1" != "$scribe_name" ] && printf "$yellow Forcing installation$red WILL OVERWRITE$yellow any modified configuration files!\n"
  488. printf "$white Do you want to force re-installation of %s [y|n]? $std" "$1"
  489. yes_no
  490. return $?
  491. }
  492.  
  493. show_config(){
  494. if [ -e $sng_loc ]
  495. then
  496. dlt $sngconf_merged
  497. dlt $sngconf_error
  498. if $sng_loc --preprocess-into=$sngconf_merged 2> $sngconf_error
  499. then
  500. less $sngconf_merged
  501. else
  502. less $sngconf_error
  503. fi
  504. true
  505. else
  506. not_installed $sng
  507. false
  508. fi
  509. }
  510.  
  511. show_loaded(){
  512. dlt $sngconf_merged
  513. $sngctl_loc config --preprocessed > $sngconf_merged
  514. less $sngconf_merged
  515. }
  516.  
  517. run_logrotate(){
  518. dlt $lr_daily
  519. printf "\n$white %34s" "running $lr ..."
  520. $lr_loc $lr_conf >> $lr_daily 2>&1
  521. finis
  522. printf "\n$magenta checking %s log for errors $cyan\n\n" "$lr"
  523. tail -v $lr_daily
  524. }
  525.  
  526. menu_status(){
  527. check_sng
  528. printf "\n$magenta checking system for necessary %s hooks ...\n\n" "$scribe_name"
  529. sed_sng
  530. if $sng_rung; then sed_srvc; fi
  531. lr_post
  532. sed_umnt
  533. if $sng_rung; then lr_cron; dir_links;fi
  534. printf "\n$magenta checking %s configuration ...\n\n" "$sng"
  535. sync_conf
  536. sng_syntax
  537. get_vers
  538. prt_vers
  539. }
  540.  
  541. sng_ver_chk(){
  542. sng_vers="$( $sng --version | grep -m1 $sng | grep -oE '[0-9]{1,2}([_.][0-9]{1,2})' )"
  543. if [ "$( ver_num "$sng_vers" )" -lt "$( ver_num "$sng_reqd" )" ]
  544. then
  545. printf "\n$red %s version %s or higher required!\n" "$sng" "$sng_reqd"
  546. printf "Please update your Entware packages and run %s install again.$cyan\n\n" "$scribe_name"
  547. opkg remove "$sng"
  548. printf "$std\n\n"
  549. exit 1
  550. fi
  551. }
  552.  
  553. setup_sng(){
  554. printf "\n$magenta setting up %s ...\n$std" "$sng"
  555. copy_rcfunc
  556. sed_sng
  557. sed_srvc
  558. sed_umnt
  559. if [ "$( md5_file "$sng_share/examples/$sng.conf-scribe" )" != "$( md5_file "$sng_conf" )" ]
  560. then
  561. printf "$white %34s" "updating $( basename $sng_conf ) ..."
  562. update_file $sng_share/examples/$sng.conf-scribe $sng_conf "backup"
  563. finis
  564. fi
  565. sync_conf
  566. }
  567.  
  568. setup_lr(){
  569. # assumes since entware is required / installed, post-mount exists and is properly executable
  570. printf "\n$magenta setting up %s ...\n" "$lr"
  571. lr_post
  572. lr_cron
  573. }
  574.  
  575. install(){
  576. force=""
  577. [ "X$2" = "XFORCE" ] && force="--force-reinstall"
  578. printf "\n$cyan"
  579. opkg install $force "$1"
  580. [ "$1" = "$sng" ] && sng_ver_chk
  581. setup_ddir "$1" "ALL"
  582. setup_exmpls "$1" "ALL"
  583. [ "$1" = "$sng" ] && setup_sng
  584. [ "$1" = "$lr" ] && setup_lr
  585. }
  586.  
  587. setup_scribe(){
  588. printf "\n$white setting up %s ..." "$scribe_name"
  589. cp -pf $unzip_dir/$scribe_name $scribe_script
  590. chmod 0755 $scribe_script
  591. [ ! -e "/opt/bin/$scribe_name" ] && ln -s $scribe_script /opt/bin
  592. # install correct firewall or skynet file, these are mutually exclusive
  593. if $skynet_inst
  594. then
  595. dlt $sngd_d/firewall
  596. dlt $lrd_d/firewall
  597. if [ ! -e "$sngd_d/skynet" ] || [ "$1" = "ALL" ]
  598. then
  599. printf "$white installing %s Skynet filter ..." "$sng"
  600. cp -p "$sng_share/examples/skynet" "$sngd_d"
  601. fi
  602. printf "$blue setting Skynet log file location$white ..."
  603. skynetlog="$( grep -m1 'file("' $sngd_d/skynet | awk -F\" '{ printf ( $2 ); }'; )"
  604. sh $skynet settings syslog "$skynetlog" > /dev/null 2>&1
  605. else
  606. dlt $sngd_d/skynet
  607. dlt $lrd_d/skynet
  608. if [ ! -e "$sngd_d/firewall" ] || [ "$1" = "ALL" ]
  609. then
  610. printf "$white installing %s firewall filter ..." "$sng"
  611. cp -p "$sng_share/examples/firewall" "$sngd_d"
  612. printf "$white installing firewall log rotation ..."
  613. cp -p "$lr_share/examples/firewall" "$lrd_d"
  614. fi
  615. fi
  616. finis
  617. # install script unique to AC86U (dcd_tainted)
  618. if [ "$( echo "$model" | grep -oE 'AC[0-9]{2}' )" = "$dcd_crash" ]
  619. then
  620. printf "$white %s detected," "$model"
  621. if [ ! -e "$sngd_d/crash" ] || [ "$1" = "ALL" ]
  622. then
  623. printf " installing %s \"dcd Tainted\" filter ..." "$sng"
  624. cp -p $sng_share/examples/crash $sngd_d
  625. else
  626. printf " %s \"dcd Tainted\" filter already installed, skipping ..." "$sng"
  627. fi
  628. if [ ! -e "$lrd_d/crash" ] || [ "$1" = "ALL" ]
  629. then
  630. printf " installing \"dcd Tainted\" log rotation ..."
  631. cp -p $lr_share/examples/crash $lrd_d
  632. else
  633. printf " \"dcd Tainted\" log rotation already installed, skipping ..."
  634. fi
  635. finis
  636. else
  637. dlt $sngd_d/crash
  638. dlt $lrd_d/crash
  639. fi
  640. }
  641.  
  642. setup_uisc(){
  643. uisc_ver=$(/usr/sbin/curl -fsL --retry 3 "$uisc_repo" | grep "SCRIPT_VERSION=" | grep -m1 -oE 'v[0-9]{1,2}([.][0-9]{1,2})([.][0-9]{1,2})')
  644. printf "\n$white Would you like to install$cyan %s %s$white, a script by Jack Yaz\n" "$uisc_name" "$uisc_ver"
  645. printf " that modifies the webui$yellow System Log$white page to show the various logs\n"
  646. printf " generated by %s in individual drop-down windows [y|n]? " "$sng"
  647. if yes_no
  648. then
  649. printf "\n"
  650. /usr/sbin/curl --retry 3 "$uisc_repo" -o "$uiscribe" && chmod 0755 $uiscribe && $uiscribe install
  651. fi
  652. }
  653.  
  654. pre_install(){
  655. # check for required components
  656. okay=true
  657.  
  658. # check if Entware & ASUSWRT-Merlin are installed and Merlin version number
  659. if [ ! -x "/opt/bin/opkg" ] || [ "$fwname" != "$merlin" ] || [ "$( ver_num "$fwvers" )" -lt "$( ver_num "$fwreqd" )" ]
  660. then
  661. printf "\n\n$red %s version %s or later with Entware is required! $std\n" "$merlin" "$fwreqd"
  662. okay=false
  663. fi
  664.  
  665. # check if diversion is installed and version number
  666. if [ -x "$divers" ]
  667. then
  668. printf "\n\n$white Diversion detected, checking version ..."
  669. div_ver="$( grep -m1 "VERSION" $divers | grep -oE '[0-9]{1,2}([.][0-9]{1,2})' )"
  670. printf " version %s detected ..." "$div_ver"
  671. if [ "$( ver_num "$div_ver" )" -lt "$( ver_num "$div_req" )" ]
  672. then
  673. printf "$red update required!\n"
  674. printf " Diversion %s or later is required! $std\n" "$div_req"
  675. okay=false
  676. else
  677. printf "$green okay! $std\n"
  678. fi
  679. fi
  680.  
  681. # check if Skynet is installed and version number
  682. if $skynet_inst
  683. then
  684. printf "\n\n$white Skynet detected, checking version ..."
  685. sky_ver="$( grep -m1 -oE 'v[0-9]{1,2}([.][0-9]{1,2})([.][0-9]{1,2})' "$skynet" )"
  686. printf " version %s detected ..." "$sky_ver"
  687. if [ "$( ver_num "$sky_ver" )" -lt "$( ver_num "$sky_req" )" ]
  688. then
  689. printf "$red update required!\n"
  690. printf " Skynet %s or later is required! $std\n" "$sky_req"
  691. okay=false
  692. else
  693. printf "$green okay! $std\n"
  694. fi
  695. else
  696. printf "$white\n\n Skynet is$red NOT$white installed on this system!\n\n"
  697. printf " If you plan to install Skynet, it is recommended\n"
  698. printf " to stop %s installation now and install Skynet\n" "$scribe_name"
  699. printf " using amtm (https://github.com/decoderman/amtm).\n\n"
  700. printf " If Skynet is installed after %s, run \"%s install\"\n" "$scribe_name" "$scribe_name"
  701. printf " and force installation to configure %s and Skynet\n" "$scribe_name"
  702. printf " to work together.\n\n"
  703. if $okay
  704. then
  705. printf " Do you want to continue installation of %s [y|n]? $std" "$scribe_name"
  706. if ! yes_no
  707. then
  708. okay=false
  709. fi
  710. fi
  711. fi
  712.  
  713. # exit if requiements not met
  714. if ! $okay
  715. then
  716. printf "\n\n$magenta exiting %s installation. $std\n\n" "$scribe_name"
  717. dlt $scribe_script
  718. exit 1
  719. fi
  720. }
  721.  
  722. menu_install(){
  723. if [ ! -e $sng_loc ]
  724. then
  725. install "$sng"
  726. elif force_install "$sng"
  727. then
  728. $S01sng_init stop
  729. install "$sng" "FORCE"
  730. fi
  731. echo
  732. $S01sng_init start
  733.  
  734. if [ ! -e $lr_loc ]
  735. then
  736. install "$lr"
  737. elif force_install "$lr"
  738. then
  739. install "$lr" "FORCE"
  740. fi
  741. run_logrotate
  742.  
  743. if ! $is_inst
  744. then
  745. setup_scribe "ALL"
  746. elif force_install "$scribe_name script"
  747. then
  748. setup_scribe "ALL"
  749. fi
  750.  
  751. rld_conf
  752. printf "\n$white %s setup complete! " "$scribe_name"
  753. enter_to "continue"
  754. if ! $uisc_inst; then setup_uisc; fi
  755. }
  756.  
  757. menu_restart(){
  758. if $sng_rung
  759. then
  760. printf "\n$yellow Restarting %s... $std\n" "$sng"
  761. $S01sng_init restart
  762. else
  763. printf "\n$white %s$red NOT$white running! $yellow Starting %s ... $std\n" "$sng"
  764. $S01sng_init start
  765. fi
  766. hup_uisc
  767. [ -n "$( pidof $sng )" ] && sng_rung=true || sng_rung=false
  768. }
  769.  
  770. stop_sng(){
  771. printf "$white stopping %s ..." "$sng"
  772. $S01sng_init stop
  773. sng_rung=false
  774. dlt $syslog_log
  775. dlt $syslog_log-1
  776. dlt /jffs/syslog.log
  777. dlt /jffs/syslog.log-1
  778. mv /opt/var/log/messages $syslog_log
  779. ln -s $syslog_log /opt/var/log/messages
  780. printf "$white starting system klogd and syslogd ..."
  781. service start_logger
  782. if ! $banner; then return; fi
  783. printf "\n$white %s will be started at next reboot; you\n" "$sng"
  784. printf " may type '%s restart' at shell prompt, or\n" "$scribe_name"
  785. printf " select rs from %s menu to restart %s $std\n\n" "$scribe_name" "$sng"
  786. }
  787.  
  788. stop_lr(){ if cru l | grep -q $lr; then cru d $lr; fi; }
  789.  
  790. menu_stop(){
  791. stop_sng
  792. stop_lr
  793. }
  794.  
  795. uninst_uisc(){
  796. printf "\n"
  797. if $uisc_inst
  798. then
  799. printf "$white uiScribe detected, uninstalling ...\n\n"
  800. $uiscribe uninstall
  801. fi
  802. }
  803.  
  804. uninstall(){
  805. printf "\n\n"
  806. banner=false # suppress certain messages
  807. if [ -e $sng_loc ]
  808. then
  809. if $sng_rung; then stop_sng; fi
  810. sed -i "/$scribe_name kill-logger/d" $srvc_event
  811. sed -i "/$scribe_name stop/d" $unmount
  812. dlt $S01sng_init
  813. dlt $rcfunc_loc
  814. printf "\n$cyan"
  815. opkg remove "$sng"
  816. dlt $sng_conf
  817. dlt $sngd_d
  818. dlt $sng_share
  819.  
  820. if $skynet_inst && ! $reinst
  821. then
  822. printf "$white restoring Skynet logging to %s ..." "$syslog_log"
  823. sh $skynet settings syslog "$syslog_log" > /dev/null 2>&1
  824. fi
  825. else
  826. not_installed "$sng"
  827. fi
  828.  
  829. if [ -e $lr_loc ]
  830. then
  831. stop_lr
  832. sed -i "/cru a $lr/d" $postmount
  833. printf "\n$cyan"
  834. opkg remove "$lr"
  835. dlt $lr_conf
  836. dlt $lrd_d
  837. dlt $lr_share
  838. dlt $lr_daily
  839. else
  840. not_installed "$lr"
  841. fi
  842.  
  843. dlt $TMP/$scribe_name.zip
  844. dlt $TMP/$scribe_name-$scribe_branch
  845. dlt /opt/bin/$scribe_name
  846. dlt $scribe_script
  847. is_inst=false
  848. if ! $reinst
  849. then
  850. printf "\n$white %s, %s, and %s have been removed from the system.\n" "$sng" "$lr" "$scribe_name"
  851. printf " It is recommended to reboot the router at this time. If you do not\n"
  852. printf " wish to reboot the router, press ctrl-c now to exit.\n\n\n"
  853. enter_to "reboot"
  854. service reboot; exit 0
  855. fi
  856. }
  857.  
  858. menu_uninstall(){
  859. andre="remove"
  860. uni="UN"
  861. if $reinst
  862. then
  863. andre="remove and reinstall"
  864. uni="RE"
  865. fi
  866. warning_sign
  867. printf " This will completely$magenta %s$yellow %s$white and$yellow %s$white.\n" "$andre" "$sng" "$lr"
  868. printf " Ensure you have backed up any configuration files you wish to keep.\n"
  869. printf " All configuration files in$yellow %s$white,$yellow %s$white,\n" "$sngd_d" "$lrd_d"
  870. printf " $yellow %s$white, and$yellow %s$white will be deleted!\n" "$sng_share" "$lr_share"
  871. warning_sign
  872. printf " Type YES to$magenta %s$yellow %s$white: $std" "$andre" "$scribe_name"
  873. read -r wipeit
  874. case "$wipeit" in
  875. YES)
  876. if ! $reinst; then uninst_uisc; fi
  877. uninstall
  878. ;;
  879. *)
  880. do_inst=false
  881. printf "\n\n$white *** %sINSTALL ABORTED! ***$std\n\n" "$uni"
  882. ;;
  883. esac
  884. }
  885.  
  886. menu_filters(){
  887. printf "\n$white Do you want to update$yellow %s$white and$yellow %s$white filter files?\n" "$sng" "$lr"
  888. printf "$cyan 1) Adds any new files to$yellow %s$cyan directories\n" "$share_ex"
  889. printf " and updates any example files that have changed.\n"
  890. printf " 2) Adds any new files to$yellow %s$cyan directories.\n" "$etc_d"
  891. printf " 3) Asks to update existing files in$yellow %s$cyan directories\n" "$etc_d"
  892. printf "$magenta _IF_$cyan a corresponding file exists in$yellow %s$cyan,\n" "$share_ex"
  893. printf "$magenta _AND_$cyan it is different from the file in$yellow %s$cyan.\n" "$etc_d"
  894. printf "$white NOTE:$cyan You will be provided an opportunity to review\n"
  895. printf " the differences between the existing file and the\n"
  896. printf " proposed update.\n\n"
  897. printf "$yellow If you are unsure, you should answer 'y' here; any changes to\n"
  898. printf " the running configuration will require confirmation.\n\n"
  899. printf "$white Update filter files? [y|n] $std"
  900. if yes_no
  901. then
  902. get_zip
  903. for pckg in $sng $lr
  904. do
  905. setup_ddir "$pckg" "NEW"
  906. setup_exmpls "$pckg" "NEWER"
  907. check_dir="$( echo "$etc_d" | sed "s/\*/$pckg/" )"
  908. comp_dir="$( echo "$share_ex" | sed "s/\*/$pckg/" )"
  909. for upd_file in "$check_dir"/*
  910. do
  911. comp_file="$comp_dir/$( basename "$upd_file" )"
  912. if [ -e "$comp_file" ] && ! same_same "$upd_file" "$comp_file"
  913. then
  914. processed=false
  915. printf "\n$white Update available for$yellow %s$white.\n" "$upd_file"
  916. while ! $processed
  917. do
  918. printf " (a)ccept, (r)eject, or (v)iew diff for this file? "
  919. read -r dispo
  920. case "$dispo" in
  921. a)
  922. update_file "$comp_file" "$upd_file"
  923. printf "\n$green %s updated!$std\n" "$upd_file"
  924. processed=true
  925. ;;
  926. r)
  927. printf "\n$magenta %s not updated!$std\n" "$upd_file"
  928. processed=true
  929. ;;
  930. v)
  931. echo
  932. diff "$upd_file" "$comp_file" | more
  933. echo
  934. ;;
  935. *)
  936. echo
  937. ;;
  938. esac
  939. done
  940. fi
  941. done
  942. done
  943. printf "\n$white %s and %s example files updated!$std\n" "$sng" "$lr"
  944. rld_conf
  945. else
  946. printf "\n$white %s and %s example files$red not$white updated!$std\n" "$sng" "$lr"
  947. fi
  948. }
  949.  
  950. menu_update(){
  951. if [ "$new_vers" = "major" ] || [ "$new_vers" = "minor" ]
  952. then
  953. [ "$new_vers" = "major" ] && printf "\n$green New version" || printf "$cyan Minor update"
  954. printf "$white available!\n"
  955. printf " Do you wish to upgrade? [y|n] $std"
  956. else
  957. printf "\n$white No new version available. (GitHub version"
  958. [ "$new_vers" = "none" ] && printf " equal to " || printf "$red LESS THAN $white"
  959. printf "local version)\n"
  960. printf " Do you wish to force re-installation of %s script? [y|n] $std" "$scribe_name"
  961. fi
  962. if yes_no
  963. then
  964. get_zip
  965. setup_scribe "NEWER"
  966. copy_rcfunc
  967. printf "\n$white %s updated!$std\n" "$scribe_name"
  968. sh "$scribe_script" filters gotzip nologo
  969. sh "$scribe_script" status nologo
  970. run_scribe=true
  971. else
  972. printf "\n$white *** %s$red not$white updated! *** $std\n\n" "$scribe_name"
  973. fi
  974. }
  975.  
  976. menu_forgrnd(){
  977. restrt=false
  978. if $sng_rung
  979. then
  980. warning_sign
  981. printf " %s is currently running; starting the debugging\n" "$sng"
  982. printf " mode is usually not necessary if %s is running.\n" "$sng"
  983. printf " Debugging mode is intended for troubleshooting when\n"
  984. printf " %s will not start.\n\n" "$sng"
  985. printf " Are you certain you wish to start debugging mode [y|n]? $std"
  986. if ! yes_no; then return; fi
  987. restrt=true
  988. fi
  989. printf "\n$yellow NOTE: If there are no errors, debugging mode will\n"
  990. printf " continue indefinitely. If this happens, type\n"
  991. printf " ctrl-c to halt debugging mode output\n\n"
  992. enter_to "start"
  993. if $restrt; then $S01sng_init stop; printf "\n"; fi
  994. trap '' 2
  995. $sng_loc -Fevd
  996. trap - 2
  997. if $restrt; then printf "\n"; $S01sng_init start; fi
  998. printf "\n"
  999. }
  1000.  
  1001. gather_debug(){
  1002. dlt $scribe_debug
  1003. printf "\n$white gathering debugging information ..."
  1004. get_vers
  1005.  
  1006. # everything between { } goes to $scribe_debug
  1007. {
  1008. printf "%s\n" "$debug_sep"
  1009. printf "### %s Version: %s\n" "$scribe_name" "$scribe_long"
  1010. printf "### Local %s md5: %s\n" "$scribe_name" "$scribe_md5"
  1011. printf "### GitHub Version: %s\n" "$github_long"
  1012. printf "### GitHub %s md5: %s\n" "$scribe_name" "$github_md5"
  1013. printf "### Router: %s (%s)\n" "$model" "$arch"
  1014. printf "### Firmware Version: %s %s\n" "$fwname" "$fwvers"
  1015. printf "\n%s\n### check running log processes:\n" "$debug_sep"
  1016. ps | grep "log"
  1017. printf "\n%s\n### check crontab:\n" "$debug_sep"
  1018. cru l | grep $lr
  1019. printf "\n%s\n### directory check:\n" "$debug_sep"
  1020. ls -ld /tmp/syslog*
  1021. ls -ld /jffs/syslog*
  1022. printf "\n%s\n### top output:\n" "$debug_sep"
  1023. top -b -n1 | head
  1024. printf "\n%s\n### *log references in top:\n" "$debug_sep"
  1025. top -b -n1 | grep log
  1026. printf "\n%s\n### init.d directory:\n" "$debug_sep"
  1027. ls -l /opt/etc/init.d
  1028. printf "\n%s\n### contents of S01syslog-ng\n" "$debug_sep"
  1029. cat /opt/etc/init.d/S01syslog-ng
  1030. printf "\n%s\n### /opt/var/log directory:\n" "$debug_sep"
  1031. ls -l /opt/var/log
  1032. printf "\n%s\n### installed packages:\n" "$debug_sep"
  1033. opkg list-installed
  1034. printf "\n%s\n### %s running configuration:\n" "$debug_sep" "$sng"
  1035. } >> $scribe_debug
  1036. if $sng_rung
  1037. then
  1038. $sngctl_loc config --preprocessed >> $scribe_debug
  1039. else
  1040. printf "#### %s not running! ####\n%s\n" "$sng" "$debug_sep"
  1041. fi
  1042. printf "\n%s\n### %s on-disk syntax check:\n" "$debug_sep" "$sng" >> $scribe_debug
  1043. dlt $sngconf_merged
  1044. dlt $sngconf_error
  1045. $sng_loc --preprocess-into=$sngconf_merged 2> $sngconf_error
  1046. cat $sngconf_merged >> $scribe_debug
  1047. if [ -s $sngconf_error ]
  1048. then
  1049. {
  1050. printf "#### SYSLOG-NG SYNTAX ON-DISK CHECK FAILED! SEE BELOW ####\n"
  1051. cat $sngconf_error
  1052. printf "###### END SYSLOG-NG ON-DISK SYNTAX FAILURE OUTPUT ######\n"
  1053. } >> $scribe_debug
  1054. else
  1055. printf "#### syslog-ng on-disk syntax check okay! ####\n" >> $scribe_debug
  1056. fi
  1057. printf "\n%s\n### logrotate debug output:\n" "$debug_sep" >> $scribe_debug
  1058. $lr_loc -d $lr_conf >> $scribe_debug 2>&1
  1059. printf "\n%s\n### Skynet log locations:\n" "$debug_sep"
  1060. if $skynet_inst
  1061. then
  1062. skynetloc="$( grep -ow "skynetloc=.* # Skynet" $fire_start 2>/dev/null | grep -vE "^#" | awk '{print $1}' | cut -c 11- )"
  1063. skynetcfg="${skynetloc}/skynet.cfg"
  1064. grep "syslog" "$skynetcfg" >> $scribe_debug
  1065. else
  1066. printf "#### Skynet not installed! ####\n%s\n" "$debug_sep" >> $scribe_debug
  1067. fi
  1068. printf "\n%s\n### end of output ###\n" "$debug_sep" >> $scribe_debug
  1069.  
  1070. printf " redacting username and USB drive names ..."
  1071. redact="$( echo "$USER" | awk '{ print substr($0, 1, 8); }' )"
  1072. sed -i "s/$redact/redacted/g" "$scribe_debug"
  1073. mntnum=0
  1074. for usbmnt in /tmp/mnt/*
  1075. do
  1076. usbdrv="$( echo "$usbmnt" | awk -F/ '{ printf( $4 ); }' )"
  1077. # note that if the usb drive name has a comma in it, the sed will fail
  1078. if [ "X$( echo "$usbmnt" | grep ',' )" = "X" ]
  1079. then
  1080. sed -i "s,$usbdrv,usb#$mntnum,g" "$scribe_debug"
  1081. else
  1082. printf "\n\n USB drive $cyan%s$white has a comma in the drive name,$red unable to redact!$white\n\n" "$usbdrv"
  1083. fi
  1084. mntnum=$(( mntnum + 1 ))
  1085. done
  1086.  
  1087. printf " taring the output ..."
  1088. tar -zcvf $scribe_debug.tar.gz -C $TMP $scribe_debug_name > /dev/null 2>&1
  1089. finis
  1090. printf "\n$std Debug output stored in $cyan%s$std, please review this file\n" "$scribe_debug"
  1091. printf " to ensure you understand what information is being disclosed.\n\n"
  1092. printf " Tarball of debug output is $cyan%s.tar.gz $std\n" "$scribe_debug"
  1093. }
  1094.  
  1095. menu_backup(){
  1096. printf "\n$white Backing up %s and %s Configurations ... \n" "$sng" "$lr"
  1097. date_stamp "$scribe_bakname"
  1098. tar -zcvf $scribe_bakname $sng_conf $sngd_d $lr_conf $lrd_d
  1099. printf "\n$std Backup data is stored in $cyan%s$std.\n\n" "$scribe_bakname"
  1100. }
  1101.  
  1102. menu_restore(){
  1103. warning_sign
  1104. printf " This will overwrite $yellow%s$white and $yellow%s$white,\n" "$sng_conf" "$lr_conf"
  1105. printf " and replace all files in $yellow%s$white and $yellow%s$white!!\n" "$sngd_d" "$lrd_d"
  1106. printf " The file must be named $cyan%s$white.\n\n" "$scribe_bakname"
  1107. if [ ! -e $scribe_bakname ]
  1108. then
  1109. printf " Backup file $magenta%s$white missing!!\n\n" "$scribe_bakname"
  1110. else
  1111. printf " Are you SURE you want to restore from $cyan%s$white (type YES to restore)? $std" $scribe_bakname
  1112. read -r rstit
  1113. case "$rstit" in
  1114. YES)
  1115. printf "\n$white Restoring %s and %s Configurations ... \n" "$sng" "$lr"
  1116. dlt $sngd_d
  1117. dlt $lrd_d
  1118. tar -zxvf $scribe_bakname -C /
  1119. chmod 600 $sngd_d/*
  1120. chmod 600 $lrd_d/*
  1121. printf "\n$std Backup data has been restored from $cyan%s$std.\n" "$scribe_bakname"
  1122. menu_restart
  1123. menu_status
  1124. ;;
  1125. *)
  1126. printf "\n\n$white *** RESTORE ABORTED! ***$std\n\n"
  1127. ;;
  1128. esac
  1129. fi
  1130. }
  1131.  
  1132. ut_menu(){
  1133. printf "$magenta %s Utilities $white\n\n" "$scribe_name"
  1134. printf " bu. Backup configuration files\n"
  1135. printf " rt. Restore configuration files\n\n"
  1136. printf " d. Generate debug file\n"
  1137. printf " c. Check on-disk %s config\n" "$sng"
  1138. if $sng_rung
  1139. then
  1140. printf " lc. Show loaded %s config\n" "$sng"
  1141. fi
  1142. printf " sd. Run %s debugging mode\n" "$sng"
  1143. printf " ld. Show %s debug info\n\n" "$lr"
  1144. printf " ui. "
  1145. if $uisc_inst; then printf "Run"; else printf "Install"; fi
  1146. printf " %s\n" "$uisc_name"
  1147. printf " e. Exit to Main Menu\n"
  1148. }
  1149.  
  1150. main_menu(){
  1151. and_lr=" & $lr cron\n"
  1152. if $sng_rung
  1153. then
  1154. res="Res"
  1155. ins="Rei"
  1156. else
  1157. res="S"
  1158. ins="I"
  1159. fi
  1160. if $is_inst
  1161. then
  1162. printf " s. Show %s status\n" "$scribe_name"
  1163. if $sng_rung
  1164. then
  1165. printf " rl. Reload %s.conf\n" "$sng"
  1166. fi
  1167. printf " lr. Run logrotate now\n"
  1168. printf " rs. %start %s" "$res" "$sng"
  1169. if ! $sng_rung
  1170. then
  1171. printf "$and_lr"
  1172. else
  1173. printf "\n st. Stop %s$and_lr" "$sng"
  1174. fi
  1175. if $sng_rung
  1176. then
  1177. printf "\n u. Update scribe\n"
  1178. printf " uf. Update filters\n"
  1179. fi
  1180. printf " su. %s utilities\n" "$scribe_name"
  1181. fi
  1182. printf " e. Exit %s\n\n" "$scribe_name"
  1183. printf " is. %snstall %s\n" "$ins" "$scribe_name"
  1184. printf " zs. Remove %s\n" "$scribe_name"
  1185. }
  1186.  
  1187. scribe_menu(){
  1188. while true
  1189. do
  1190. pause=true
  1191. not_recog=false
  1192. run_scribe=false
  1193. logo
  1194. printf "$white $header"
  1195. case "$menu_type" in
  1196. utils)
  1197. ut_menu
  1198. ;;
  1199. *)
  1200. main_menu
  1201. ;;
  1202. esac
  1203. printf "\n$white $header"
  1204. printf "$magenta Please select an option: $std"
  1205. read -r choice
  1206. if $is_inst || [ "$choice" = "e" ] || [ "$choice" = "is" ] || [ "$choice" = "zs" ]
  1207. then
  1208. case "$choice" in
  1209. s)
  1210. menu_status
  1211. ;;
  1212. rl)
  1213. if $sng_rung
  1214. then
  1215. rld_conf
  1216. else
  1217. not_recog=true
  1218. fi
  1219. ;;
  1220. lr)
  1221. run_logrotate
  1222. ;;
  1223. rs)
  1224. menu_restart
  1225. menu_status
  1226. ;;
  1227. st)
  1228. if $sng_rung
  1229. then
  1230. menu_stop
  1231. else
  1232. not_recog=true
  1233. fi
  1234. ;;
  1235. u)
  1236. if $sng_rung
  1237. then
  1238. get_vers
  1239. prt_vers
  1240. menu_update
  1241. else
  1242. not_recog=true
  1243. fi
  1244. ;;
  1245. uf)
  1246. if $sng_rung
  1247. then
  1248. menu_filters
  1249. else
  1250. not_recog=true
  1251. fi
  1252. ;;
  1253. su)
  1254. menu_type="utils"
  1255. pause=false
  1256. ;;
  1257. bu)
  1258. menu_backup
  1259. ;;
  1260. rt)
  1261. menu_restore
  1262. ;;
  1263. d)
  1264. gather_debug
  1265. printf "\n$white Would you like to review the debug data (opens in less)? [y|n] $std"
  1266. if yes_no; then pause=false; less "$scribe_debug"; fi
  1267. ;;
  1268. c)
  1269. show_config
  1270. pause=false
  1271. ;;
  1272. lc)
  1273. if $sng_rung
  1274. then
  1275. show_loaded
  1276. pause=false
  1277. else
  1278. not_recog=true
  1279. fi
  1280. ;;
  1281. sd)
  1282. menu_forgrnd
  1283. ;;
  1284. ld)
  1285. dlt $lr_temp
  1286. $lr_loc -d $lr_conf >> $lr_temp 2>&1
  1287. less $lr_temp
  1288. dlt $lr_temp
  1289. pause=false
  1290. ;;
  1291. ui)
  1292. if $uisc_inst
  1293. then
  1294. $uiscribe
  1295. pause=false
  1296. else
  1297. setup_uisc
  1298. fi
  1299. ;;
  1300. e)
  1301. if [ "$menu_type" = "main" ]
  1302. then
  1303. printf "\n$white Thanks for using scribe! $std\n\n\n"
  1304. exit 0
  1305. else
  1306. menu_type="main"
  1307. pause=false
  1308. fi
  1309. ;;
  1310. is)
  1311. do_inst=true
  1312. reinst=false
  1313. if $is_inst
  1314. then
  1315. reinst=true
  1316. menu_uninstall
  1317. fi
  1318. if $do_inst
  1319. then
  1320. pre_install
  1321. get_zip
  1322. menu_install
  1323. sh "$scribe_script" status nologo
  1324. run_scribe=true
  1325. fi
  1326. ;;
  1327. zs)
  1328. reinst=false
  1329. menu_uninstall
  1330. ;;
  1331. *)
  1332. not_recog=true
  1333. ;;
  1334. esac
  1335. else
  1336. not_recog=true
  1337. fi
  1338. if $not_recog; then printf "\n$red Unrecognized command \"%s\". " "$choice"; fi
  1339. if $pause; then enter_to "continue"; fi
  1340. if $run_scribe; then sh "$scribe_script"; exit 0; fi
  1341. [ -n "$( pidof $sng )" ] && sng_rung=true || sng_rung=false
  1342. done
  1343. }
  1344.  
  1345.  
  1346. ##############
  1347. #### MAIN ####
  1348. ##############
  1349.  
  1350. if [ "$action" = "menu" ]
  1351. then
  1352. menu_type="main"
  1353. scribe_menu
  1354. else
  1355. logo
  1356. fi
  1357.  
  1358. case "$action" in
  1359.  
  1360. # install syslog-ng, logrotate, & scribe script
  1361. install)
  1362. if $is_inst
  1363. then
  1364. printf "\n$white *** %s already installed! *** \n\n" "$scribe_name"
  1365. printf " Please use menu command 'is' to reinstall. $std\n\n"
  1366. exit 1
  1367. fi
  1368. pre_install
  1369. get_zip
  1370. menu_install
  1371. sh "$scribe_script" status nologo
  1372. exit 0
  1373. ;;
  1374.  
  1375. # uninstall scribe
  1376. uninstall | remove)
  1377. reinst=false
  1378. menu_uninstall
  1379. ;;
  1380.  
  1381. # update scribe
  1382. update)
  1383. if $sng_rung
  1384. then
  1385. get_vers
  1386. prt_vers
  1387. menu_update
  1388. fi
  1389. ;;
  1390.  
  1391. # show total combined config
  1392. show-config | config)
  1393. if $is_inst
  1394. then
  1395. if show_config; then sng_syntax; fi
  1396. fi
  1397. ;;
  1398.  
  1399. # verify syslog-ng is running and logrotate is listed in 'cru l'
  1400. status)
  1401. if $is_inst; then menu_status; fi
  1402. ;;
  1403.  
  1404. # reload syslog-ng configuration
  1405. reload)
  1406. if $sng_rung; then rld_conf; fi
  1407. ;;
  1408.  
  1409. # restart (or start if not running) syslog-ng
  1410. restart | start)
  1411. if $is_inst
  1412. then
  1413. menu_restart
  1414. menu_status
  1415. fi
  1416. ;;
  1417.  
  1418. # stop syslog-ng & logrotate cron job
  1419. stop)
  1420. if $sng_rung; then menu_stop; fi
  1421. ;;
  1422.  
  1423. # generate debug tarball
  1424. debug)
  1425. if $is_inst; then gather_debug; fi
  1426. ;;
  1427.  
  1428. # update syslog-ng and logrotate filters - only used in update process
  1429. filters)
  1430. if $sng_rung; then menu_filters; fi
  1431. ;;
  1432.  
  1433.  
  1434. # kill syslogd & klogd - only available via cli
  1435. kill-logger)
  1436. [ "$sng_rung" = "false" ] || [ "$2" = "stop" ] || { [ "$3" != "logger" ] && [ "$3" != "time" ]; } && exit 0
  1437. # load kill_logger() function
  1438. # keep shellcheck from barfing on sourcing $rcfunc_loc
  1439. # shellcheck disable=SC1091
  1440. # shellcheck source=/opt/etc/init.d/rc.func.syslog-ng
  1441. . $rcfunc_loc
  1442. count=120
  1443. foundk=false
  1444. founds=false
  1445. while [ $count -gt 0 ]
  1446. do
  1447. sleep 1 # give them a moment to start up
  1448. if [ -n "$( pidof klogd )" ] || [ -n "$( pidof syslogd )" ]
  1449. then
  1450. kill_logger
  1451. [ -z "$( pidof klogd )" ] && foundk=true
  1452. [ -z "$( pidof syslogd )" ] && founds=true
  1453. if $foundk && $founds; then count=-1; fi
  1454. fi
  1455. count=$(( count - 1 ))
  1456. done
  1457. if [ $count -eq 0 ]; then logger -t "$scribe_name" "kill-logger FAILED! check if klogd or syslogd running."; fi
  1458. sync_conf
  1459. ;;
  1460.  
  1461. # unrecognized command
  1462. *)
  1463. printf "\n$white Usage: $0 ( uninstall | update | [show-]config | status | reload | [re]start | debug )$std\n\n"
  1464. exit 1
  1465. ;;
  1466. esac
  1467.  
  1468. if ! $is_inst
  1469. then
  1470. printf "\n$yellow %s $white not installed, command \"%s\" not valid!$std\n\n" "$scribe_name" "$action"
  1471. elif ! $sng_rung && [ $action != "stop" ]
  1472. then
  1473. printf "\n$yellow %s $white not running, command \"%s\" not valid!$std\n\n" "$sng" "$action"
  1474. else
  1475. printf "\n"
  1476. fi
  1477.  
  1478. #eof
  1479.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement