core_logs.sh 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/bin/bash
  2. # LinuxGSM core_logs.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: https://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Acts as a log rotator, removing old logs.
  7. moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
  8. # Check if logfile variable and file exist, create logfile if it doesn't exist.
  9. if [ "${consolelog}" ]; then
  10. if [ ! -f "${consolelog}" ]; then
  11. touch "${consolelog}"
  12. fi
  13. fi
  14. # For games not displaying a console, and having logs into their game directory.
  15. check_status.sh
  16. if [ "${status}" != "0" ] && [ "${commandname}" == "START" ] && [ -n "${gamelogfile}" ]; then
  17. if [ "$(find "${systemdir}" -name "gamelog*.log")" ]; then
  18. fn_print_info "Moving game logs to ${gamelogdir}"
  19. fn_script_log_info "Moving game logs to ${gamelogdir}"
  20. echo -en "\n"
  21. fn_sleep_time
  22. mv "${systemdir}"/gamelog*.log "${gamelogdir}"
  23. fi
  24. fi
  25. # Log manager will start the cleanup if it finds logs older than "${logdays}".
  26. if [ "$(find "${lgsmlogdir}"/ -type f -mtime +"${logdays}" | wc -l)" -ne "0" ]; then
  27. fn_print_dots "Starting"
  28. # Set common logs directories
  29. commonlogs="${systemdir}/logs"
  30. commonsourcelogs="${systemdir}/*/logs"
  31. # Set addon logs directories
  32. sourcemodlogdir="${systemdir}/addons/sourcemod/logs"
  33. ulxlogdir="${systemdir}/data/ulx_logs"
  34. darkrplogdir="${systemdir}/data/darkrp_logs"
  35. legacyserverlogdir="${logdir}/server"
  36. # Setting up counting variables
  37. scriptcount="0"
  38. consolecount="0"
  39. gamecount="0"
  40. srcdscount="0"
  41. smcount="0"
  42. ulxcount="0"
  43. darkrpcount="0"
  44. legacycount="0"
  45. fn_print_info "Removing logs older than ${logdays} days"
  46. fn_script_log_info "Removing logs older than ${logdays} days"
  47. # Logging logfiles to be removed according to "${logdays}", counting and removing them.
  48. # Script logfiles.
  49. find "${lgsmlogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  50. scriptcount=$(find "${lgsmlogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  51. find "${lgsmlogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  52. # SRCDS and unreal logfiles.
  53. if [ "${engine}" == "unreal2" ] || [ "${engine}" == "source" ]; then
  54. find "${gamelogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  55. gamecount=$(find "${gamelogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  56. find "${gamelogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  57. # Rotated game logfiles.
  58. elif [ -n "${gamelogdate}" ] && [ -d "${gamelogdir}" ]; then
  59. find "${gamelogdir}"/ -maxdepth 1 -type f -name "${selfname}-game-*.log" -mtime +"${logdays}" | tee >> "${lgsmlog}"
  60. gamecount=$(find "${gamelogdir}"/ -maxdepth 1 -type f -name "${selfname}-game-*.log" -mtime +"${logdays}" | wc -l)
  61. find "${gamelogdir}"/ -maxdepth 1 -type f -name "${selfname}-game-*.log" -mtime +"${logdays}" -exec rm -f {} \;
  62. fi
  63. # Console logfiles.
  64. if [ "${consolelog}" ]; then
  65. find "${consolelogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  66. consolecount=$(find "${consolelogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  67. find "${consolelogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  68. fi
  69. # Common logfiles.
  70. if [ -d "${commonlogs}" ]; then
  71. find "${commonlogs}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  72. smcount=$(find "${commonlogs}"/ -type f -mtime +"${logdays}" | wc -l)
  73. find "${commonlogs}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  74. fi
  75. if [ -d "${commonsourcelogs}" ]; then
  76. find "${commonsourcelogs}"/* -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  77. smcount=$(find "${commonsourcelogs}"/* -type f -mtime +"${logdays}" | wc -l)
  78. find "${commonsourcelogs}"/* -mtime +"${logdays}" -type f -exec rm -f {} \;
  79. fi
  80. # Source addons logfiles.
  81. if [ "${engine}" == "source" ]; then
  82. # SourceMod logfiles.
  83. if [ -d "${sourcemodlogdir}" ]; then
  84. find "${sourcemodlogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  85. smcount=$(find "${sourcemodlogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  86. find "${sourcemodlogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  87. fi
  88. # Garry's Mod logfiles.
  89. if [ "${shortname}" == "gmod" ]; then
  90. # ULX logfiles.
  91. if [ -d "${ulxlogdir}" ]; then
  92. find "${ulxlogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  93. ulxcount=$(find "${ulxlogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  94. find "${ulxlogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  95. fi
  96. # DarkRP logfiles.
  97. if [ -d "${darkrplogdir}" ]; then
  98. find "${darkrplogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  99. darkrpcount=$(find "${darkrplogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  100. find "${darkrplogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  101. fi
  102. fi
  103. fi
  104. # Count total amount of files removed.
  105. countlogs=$((scriptcount + consolecount + gamecount + srcdscount + smcount + ulxcount + darkrpcount))
  106. # Job done.
  107. fn_print_ok "Removed ${countlogs} log files"
  108. fn_script_log "Removed ${countlogs} log files"
  109. fi