You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

logs.sh 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/bin/bash
  2. ###############################
  3. # usage: logs.sh [ -l ARG ] [ -n ARG ]
  4. # -l ARG: name of log file to display.
  5. # valid values are 'all', 'sonar', 'web', 'ce' and 'es' (case insensitive).
  6. # default value is 'all'.
  7. # -n ARG: number of log line to display. Value is passed to n option of tail.
  8. # default value is 25
  9. ###############################
  10. set -euo pipefail
  11. DEFAULT_LOG="all"
  12. DEFAULT_LINES="25"
  13. LOGS="sonar web ce es"
  14. function checkLogArgument() {
  15. local logArg="$1"
  16. if [ "${logArg,,}" == "$DEFAULT_LOG" ]; then
  17. return
  18. fi
  19. for t in $LOGS; do
  20. if [ "${logArg,,}" == "$t" ]; then
  21. return
  22. fi
  23. done
  24. echo "Unsupported -l argument $logArg"
  25. exit 1
  26. }
  27. function buildTailArgs() {
  28. local logArg="$1"
  29. local logLines="$2"
  30. local res=""
  31. for t in $LOGS; do
  32. if [ "${logArg,,}" == "$DEFAULT_LOG" ] || [ "${logArg,,}" == "$t" ]; then
  33. res="$res -Fn $logLines $SQ_HOME/logs/$t.log"
  34. fi
  35. done
  36. echo "$res"
  37. }
  38. function doTail() {
  39. local logArg="$1"
  40. local logLines="${2:-"$DEFAULT_LINES"}"
  41. TAIL_ARG=$(buildTailArgs "$logArg" "$logLines")
  42. tail $TAIL_ARG
  43. }
  44. # check the script was called to avoid execute when script is only sourced
  45. script_name=$(basename "$0")
  46. if [ "$script_name" == "logs.sh" ]; then
  47. LOG="$DEFAULT_LOG"
  48. LINES="$DEFAULT_LINES"
  49. while getopts ":l:n:" opt; do
  50. case "$opt" in
  51. l) LOG=${OPTARG:=$DEFAULT_LOG}
  52. ;;
  53. n) LINES=${OPTARG:=$DEFAULT_LINES}
  54. ;;
  55. \?)
  56. echo "Unsupported option $OPTARG" >&2
  57. exit 1
  58. ;;
  59. esac
  60. done
  61. checkLogArgument "$LOG"
  62. ROOT=$(pwd)
  63. cd sonar-application/target/sonarqube-*
  64. SQ_HOME=$(pwd)
  65. cd "$ROOT"
  66. doTail "$LOG" "$LINES"
  67. fi