core_logs.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. fi
  58. # Console logfiles.
  59. if [ "${consolelog}" ]; then
  60. find "${consolelogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  61. consolecount=$(find "${consolelogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  62. find "${consolelogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  63. fi
  64. # Common logfiles.
  65. if [ -d "${commonlogs}" ]; then
  66. find "${commonlogs}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  67. smcount=$(find "${commonlogs}"/ -type f -mtime +"${logdays}" | wc -l)
  68. find "${commonlogs}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  69. fi
  70. if [ -d "${commonsourcelogs}" ]; then
  71. find "${commonsourcelogs}"/* -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  72. smcount=$(find "${commonsourcelogs}"/* -type f -mtime +"${logdays}" | wc -l)
  73. find "${commonsourcelogs}"/* -mtime +"${logdays}" -type f -exec rm -f {} \;
  74. fi
  75. # Source addons logfiles.
  76. if [ "${engine}" == "source" ]; then
  77. # SourceMod logfiles.
  78. if [ -d "${sourcemodlogdir}" ]; then
  79. find "${sourcemodlogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  80. smcount=$(find "${sourcemodlogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  81. find "${sourcemodlogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  82. fi
  83. # Garry's Mod logfiles.
  84. if [ "${shortname}" == "gmod" ]; then
  85. # ULX logfiles.
  86. if [ -d "${ulxlogdir}" ]; then
  87. find "${ulxlogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  88. ulxcount=$(find "${ulxlogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  89. find "${ulxlogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  90. fi
  91. # DarkRP logfiles.
  92. if [ -d "${darkrplogdir}" ]; then
  93. find "${darkrplogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  94. darkrpcount=$(find "${darkrplogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  95. find "${darkrplogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  96. fi
  97. fi
  98. fi
  99. # Count total amount of files removed.
  100. countlogs=$((scriptcount + consolecount + gamecount + srcdscount + smcount + ulxcount + darkrpcount))
  101. # Job done.
  102. fn_print_ok "Removed ${countlogs} log files"
  103. fn_script_log "Removed ${countlogs} log files"
  104. fi