core_logs.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/bin/bash
  2. # LinuxGSM core_logs.sh module
  3. # Author: Daniel Gibbs
  4. # Contributors: http://linuxgsm.com/contrib
  5. # Website: https://linuxgsm.com
  6. # Description: Acts as a log rotator, removing old logs.
  7. functionselfname="$(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" ; consolecount="0" ; gamecount="0" ; srcdscount="0" ; smcount="0" ; ulxcount="0" ; darkrpcount="0" ; legacycount="0"
  38. fn_sleep_time
  39. fn_print_info "Removing logs older than ${logdays} days"
  40. fn_script_log_info "Removing logs older than ${logdays} days"
  41. # Logging logfiles to be removed according to "${logdays}", counting and removing them.
  42. # Script logfiles.
  43. find "${lgsmlogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  44. scriptcount=$(find "${lgsmlogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  45. find "${lgsmlogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  46. # SRCDS and unreal logfiles.
  47. if [ "${engine}" == "unreal2" ]||[ "${engine}" == "source" ]; then
  48. find "${gamelogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  49. gamecount=$(find "${gamelogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  50. find "${gamelogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  51. fi
  52. # Console logfiles.
  53. if [ "${consolelog}" ]; then
  54. find "${consolelogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  55. consolecount=$(find "${consolelogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  56. find "${consolelogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  57. fi
  58. # Common logfiles.
  59. if [ -d "${commonlogs}" ]; then
  60. find "${commonlogs}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  61. smcount=$(find "${commonlogs}"/ -type f -mtime +"${logdays}" | wc -l)
  62. find "${commonlogs}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  63. fi
  64. if [ -d "${commonsourcelogs}" ]; then
  65. find "${commonsourcelogs}"/* -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  66. smcount=$(find "${commonsourcelogs}"/* -type f -mtime +"${logdays}" | wc -l)
  67. find "${commonsourcelogs}"/* -mtime +"${logdays}" -type f -exec rm -f {} \;
  68. fi
  69. # Source addons logfiles.
  70. if [ "${engine}" == "source" ]; then
  71. # SourceMod logfiles.
  72. if [ -d "${sourcemodlogdir}" ]; then
  73. find "${sourcemodlogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  74. smcount=$(find "${sourcemodlogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  75. find "${sourcemodlogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  76. fi
  77. # Garry's Mod logfiles.
  78. if [ "${shortname}" == "gmod" ]; then
  79. # ULX logfiles.
  80. if [ -d "${ulxlogdir}" ]; then
  81. find "${ulxlogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  82. ulxcount=$(find "${ulxlogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  83. find "${ulxlogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  84. fi
  85. # DarkRP logfiles.
  86. if [ -d "${darkrplogdir}" ]; then
  87. find "${darkrplogdir}"/ -type f -mtime +"${logdays}" | tee >> "${lgsmlog}"
  88. darkrpcount=$(find "${darkrplogdir}"/ -type f -mtime +"${logdays}" | wc -l)
  89. find "${darkrplogdir}"/ -mtime +"${logdays}" -type f -exec rm -f {} \;
  90. fi
  91. fi
  92. fi
  93. # Count total amount of files removed.
  94. countlogs=$((scriptcount + consolecount + gamecount + srcdscount + smcount + ulxcount + darkrpcount))
  95. # Job done.
  96. fn_print_ok "Removed ${countlogs} log files"
  97. fn_script_log "Removed ${countlogs} log files"
  98. fi