MartineauPASTEBIN

Asus Router - Move Syslog to USB

Jan 26th, 2017
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.36 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. #===============================================================================
  4. #
  5. # Moves syslog between internal flash memory and USB drive or scans a log for BOOT errors and custom action messages.
  6. #
  7. #          Called as the last line of init-start preferably after 180sec sleep delay!!!!
  8. #                 and as last line of services-stop
  9. #
  10. #   Jan 2017 http://pastebin.com/embed_js/M0qUATyj
  11. #
  12. #
  13. #   e.g.   syslog-move.sh   [help|-h|status| file_name | reset ]
  14. #
  15. #
  16. #          syslog-move.sh
  17. #                            If '/tmp/BOOTINPROGRESS' exists, moves syslog to USB '/tmp/mnt/$MOUNTED/Syslog/syslog.log'
  18. #          syslog-move.sh    reset
  19. #                            Moves syslog back to '/tmp/syslog.log' (flash memory)
  20. #          syslog-move.sh    /tmp/mnt/$MOUNTED/Syslog/syslog.log-20170125-153049-BOOT.txt
  21. #                            Scans file for 'abnormal messages and custom messages and creates 'BOOT_Errors.txt' & 'MyCustomActions.txt'
  22. #          syslog-move.sh    help
  23. #                            This help information
  24.  
  25. #   e.g. init-start
  26. #
  27. #       #!/bin/sh
  28. #       /usr/bin/logger -s -t "($(basename $0))" $$ "Martineau $MYROUTER BOOT in progress... [$@]"
  29. #       # NOTE: Can't use Flash drive /tmp/mnt/$MYROUTER/ 'cos it hasn't been mounted yet :-(
  30. #       # 'flock' is probably a better solution rather than the 'echo' ;-)
  31. #       HARDWARE_MODEL=$(nvram get productid)
  32. #       MYROUTER=$(nvram get computer_name)
  33. #       BUILDNO=$(nvram get buildno)
  34. #       EXTENDNO=$(nvram get extendno)
  35. #       echo $$"-"`date` > /tmp/BOOTINPROGRESS 
  36. #       rm /tmp/SHUTDOWNINPROGRESS
  37. #       # Should be sufficient to cover physical BOOT process?
  38. #       logger -st "($(basename $0))" $$ "Paused for 3 mins....."
  39. #       sleep 180
  40. #       # Call custom scripts/commands here e.g. cifs.sh / cru etc. here
  41. #       /usr/bin/logger -st "($(basename $0))" $$ "Martineau" $MYROUTER "BOOT Completed Firmware build" $BUILDNO $EXTENDNO "[$@]"
  42. #       # Move Syslog to USB Flash drive
  43. #       /jffs/scripts/syslog-move.sh
  44. #       rm /tmp/BOOTINPROGRESS
  45. #
  46. #   e.g. services-stop
  47. #
  48. #       #!/bin/sh
  49. #       /usr/bin/logger -st "($(basename $0))" $$ "Martineau Services cleanup in progress... [$@]"
  50. #       MYROUTER=$(nvram get computer_name)
  51. #       # NOTE: Could use Flash drive /tmp/mnt/$MYROUTER/
  52. #       # 'flock' is probably a better solution rather than the 'echo'
  53. #       echo $$"-"`date` > /tmp/SHUTDOWNINPROGRESS                          # Will be deleted by init-start
  54. #       /usr/bin/logger -st "($(basename $0))" $$ "Stopping rstats....."
  55. #       service stop_rstats
  56. #       /usr/bin/logger -st "($(basename $0))" $$ "Stopping cstats....."
  57. #       service stop_cstats
  58. #       # Call custom scripts/commands here e.g. Entware '/rc.unslung' etc. here
  59. #       # At this point no further logger statements are recorded in Syslog?
  60. #       #    so spoof a logger message!
  61. #       echo `date "+%b   %d %T"` $MYROUTER "Spoof.logger (services-stop): Martineau Services shutdown cleanup complete."  >> /tmp/syslog.log
  62. #       echo `date "+%b   %d %T"` $MYROUTER "Spoof.logger (services-stop): Saving syslog before Reboot..... "  >> /tmp/syslog.log
  63. #       /usr/bin/logger -st "($(basename $0))" $$ "Saving Syslog before Reboot..... "
  64. #       /jffs/scripts/syslog-move.sh
  65. #       exit 0
  66. #
  67. GetLocationSTATUS () {
  68.     # Identify media for current disposition of dataset i.e. is /tmp/syslog.log a link to USB?
  69.     local ORG="?"
  70.     if [[ -L "$SOURCE" ]];then
  71.         #DEST="RAM"
  72.         local WHERE="USB"
  73.         local TEXT="is on"
  74.     else
  75.         #DEST="USB"
  76.         local WHERE="RAM"
  77.         local TEXT="is in"
  78.     fi
  79.     if [ -z $1 ];then
  80.         logger -st "($(basename $0))" $$ $MYROUTER "Syslog '"$SOURCE"'" $TEXT $WHERE
  81.     fi
  82.     echo $WHERE
  83. }
  84. # Print between line beginning with'#==' to first blank line inclusive
  85. ShowHelp() {
  86.     awk '/^#==/{f=1} f{print; if (!NF) exit}' $0
  87. }
  88.  
  89. # Help required?
  90. if [ "$1" = "-h" ] || [ "$1" = "help" ]; then
  91.    ShowHelp                                 # Show help
  92.    exit 0
  93. fi
  94.  
  95. SDx="sda1"                                          # Default - Change for sdb1 etc.
  96.  
  97. DEV_MOUNT=`df | grep -F "$SDx" | awk '{print $6}'`
  98. if [ -z $DEV_MOUNT ];then
  99.     logger -st "($(basename $0))" $$ "***ERROR '/dev/"$SDx"' mount point not found!"
  100.     echo -e "\a"
  101.     exit 99
  102. fi
  103.  
  104. SOURCE="/tmp/syslog.log"                            # Original source of the syslog in Router flash memory
  105. SYSLOG=$DEV_MOUNT"/Syslog/syslog.log"               # Destination of the syslog on USB disk
  106. ORIGINAL=$(GetLocationSTATUS)                       # Current physical location of syslog.log
  107. DEST="USB"                                          # Default target destination
  108.  
  109. ERRORFILE=$DEV_MOUNT"/Syslog/BOOT_Errors.txt"               # Results of scanning log for 'abnormal' messages
  110. MYCUSTOMFILE=$DEV_MOUNT"/Syslog/MyCustomActions.txt"        # Results of scanning log for 'custom action' events
  111.  
  112. # Here because we need the global variables set!
  113. if [ "$1" == "status" ];then
  114.     echo " "
  115.     ORIGINAL=$(GetLocationSTATUS "?")
  116.     # Confirm
  117.     ls -l $SOURCE
  118.     exit 0
  119. fi
  120.  
  121. logger -st "($(basename $0))" $$ "Syslog Housekeeping starting....." [$@]
  122.  
  123. NOW=$(date +"%Y%m%d-%H%M%S")    # current date and time
  124.  
  125. # Explicit request to revert back to flash memory (as used in services-stop @ REBOOT shutdown)
  126. if [ "$1" == "reset" ];then
  127.     DEST="RAM"
  128. fi
  129.  
  130. if [ -z $1 ] || [ "$1" == "reset" ] ; then
  131.    
  132.     # True REBOOT in progress? see init-start
  133.     if [ -e /tmp/BOOTINPROGRESS  ]; then
  134.         logger -st "($(basename $0))" $$ "Boot-in-Progress '/tmp/BOOTINPROGRESS' detected."
  135.         if [ -f $SYSLOG ];then
  136.             logger -st "($(basename $0))" $$ "Renaming previous USB '"$SYSLOG"' to '"$SYSLOG-$NOW"'"
  137.             mv $SYSLOG $SYSLOG-${NOW}_shutdown.txt          # Rename previous USB syslog
  138.         fi
  139.         logger -st "($(basename $0))" $$ "Creating '"$SYSLOG-$NOW-BOOT.raw"'"
  140.         killall syslogd
  141.         sleep 1
  142.         cp $SOURCE $SYSLOG-$NOW-BOOT.raw        # copy current physical /tmp syslog to the new location
  143.         rm $SOURCE
  144.         # Remove all duplicated 'shutdown' lines - they are always incomplete anyway?
  145.         logger -st "($(basename $0))" $$ "Editing '"$SYSLOG-$NOW-BOOT.raw"' -> '"$SYSLOG-$NOW-BOOT.txt"'"
  146.         RC=$(sed -n '/^Aug/,$p' $SYSLOG-$NOW"-BOOT.raw" > $SYSLOG-$NOW"-BOOT.txt")  # i.e. upto 'Aug  1 01:00:15 syslogd started: BusyBox v1.25.1'
  147.         rm $SYSLOG-$NOW-BOOT.raw
  148.         SCANTHIS=$SYSLOG-$NOW-BOOT.txt
  149.     else
  150.         logger -st "($(basename $0))" $$ "Creating '"$SYSLOG-$NOW.txt"'"
  151.         #killall syslogd
  152.         sleep 1
  153.         cp $SOURCE $SYSLOG-$NOW.txt             # copy current /tmp/syslog.log to the new location
  154.         rm $SOURCE
  155.         SCANTHIS=$SYSLOG-$NOW.txt
  156.     fi
  157.  
  158.     killall syslogd
  159.  
  160.     # Start SYSLOG on the USB disk with infinite size i.e. no GDG creation if no ARG supplied
  161.     if [ "$DEST" == "USB" ];then
  162.         if [ "$ORIGINAL" == "RAM" ];then
  163.             ACTION="moved to"
  164.         else
  165.             ACTION="retained on"
  166.         fi
  167.         logger -st "($(basename $0))" $$ "Syslog" $ACTION "USB drive '"$SYSLOG"'"
  168.         CMD="syslogd -O $SYSLOG -s 0"
  169.         $CMD
  170.     else
  171.         logger -st "($(basename $0))" $$ "Syslog reset to internal flash memory '"$SOURCE"'"   # display OK message
  172.         CMD="syslogd -O $SOURCE"
  173.         $CMD                    # Put it back where ASUS expect its to be ? /tmp/syslog.log in internal memory
  174.     fi
  175.  
  176.     if [ "$?" -ne 0 ]; then    # check for error
  177.         logger -st "($(basename $0))" $$ "***ERROR rc=" $? "'"$CMD"'"
  178.         exit $?
  179.     fi
  180.  
  181.     if [ "$DEST" == "USB" ];then
  182.         # Allow Router GUI to see the Syslog!!!!! otherwise it shows it empty!!!
  183.         rm $SOURCE 2> /dev/null
  184.         CMD="ln -s $SYSLOG $SOURCE"
  185.         $CMD          # create a symbolic link from the original syslog to the USB one
  186.  
  187.         if [ "$?" -ne 0 ]; then
  188.             logger -st "($(basename $0))" $$ "**ERROR rc=" $? "'"$CMD"'"
  189.             echo -e "\a"
  190.         fi
  191.     fi
  192.    
  193.     # Confirm????
  194.     #ls -l $SOURCE
  195.  
  196. else
  197.     SCANTHIS=$DEV_MOUNT"/Syslog/$1"                         # Scan filename provided
  198. fi
  199.  
  200.  
  201. #if [ -e /tmp/BOOTINPROGRESS  ]; then
  202.     # Scan the BOOT log for errors i.e. literal 'ERROR' or 'FAILED' or 'ABORT' case insensitive
  203.     logger -st "($(basename $0))" $$ "SYSLOG 'abnormal' message scanning: '" $SCANTHIS"'"
  204.     echo "Scanning '"$SCANTHIS"'" > $ERRORFILE
  205.     ERROR_LINE_CNT=`grep -c -E -i "ERROR|FAILED|ABORT" $SCANTHIS`
  206.     #logger -st "($(basename $0))" $$ "**DEBUG ERROR_LINE_CNT="$ERROR_LINE_CNT
  207.     if [ "$ERROR_LINE_CNT" -gt 0 ]; then
  208.         echo -e "\a"
  209.         logger -st "($(basename $0))" $$ "Scan of '"$SCANTHIS"' found" $ERROR_LINE_CNT "errors"
  210.         RC=`grep -iE "ERROR|FAILED|ABORT" $SCANTHIS >> $ERRORFILE`
  211.         echo
  212.     else
  213.         echo "Nothing 'abnormal' - ERROR or FAILED or ABORT found." >> $ERRORFILE
  214.     fi
  215.     # Scan the log for my custom script actions...
  216.     logger -st "($(basename $0))" $$ "SYSLOG 'custom action' message scanning: '"$SCANTHIS"'"
  217.     CUSTOM_LINE_CNT=`grep -E "\):" $SCANTHIS | grep -vE "]:|kernel:"  > $MYCUSTOMFILE`
  218. #fi
  219.  
  220. logger -st "($(basename $0))" $$ "Syslog Housekeeping complete for '"$SCANTHIS"'"
  221.    
  222. exit 0
Advertisement
Add Comment
Please, Sign In to add comment