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.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 toLower() {
  15. echo "$1" | tr '[:upper:]' '[:lower:]'
  16. }
  17. function checkLogArgument() {
  18. local logArg="$1"
  19. local lowerLogArg=$(toLower $logArg)
  20. if [ "$lowerLogArg" == "$DEFAULT_LOG" ]; then
  21. return
  22. fi
  23. for t in $LOGS; do
  24. if [ "$lowerLogArg" == "$t" ]; then
  25. return
  26. fi
  27. done
  28. echo "Unsupported -l argument $logArg"
  29. exit 1
  30. }
  31. function buildTailArgs() {
  32. local logArg=$(toLower $logArg)
  33. local logLines="$2"
  34. local res=""
  35. for t in $LOGS; do
  36. if [ "$logArg" == "$DEFAULT_LOG" ] || [ "$logArg" == "$t" ]; then
  37. res="$res -Fn $logLines $SQ_HOME/logs/$t.log"
  38. fi
  39. done
  40. echo "$res"
  41. }
  42. function doTail() {
  43. local logArg="$1"
  44. local logLines="${2:-"$DEFAULT_LINES"}"
  45. TAIL_ARG=$(buildTailArgs "$logArg" "$logLines")
  46. tail $TAIL_ARG
  47. }
  48. # check the script was called to avoid execute when script is only sourced
  49. script_name=$(basename "$0")
  50. if [ "$script_name" == "logs.sh" ]; then
  51. LOG="$DEFAULT_LOG"
  52. LINES="$DEFAULT_LINES"
  53. while getopts ":l:n:" opt; do
  54. case "$opt" in
  55. l) LOG=${OPTARG:=$DEFAULT_LOG}
  56. ;;
  57. n) LINES=${OPTARG:=$DEFAULT_LINES}
  58. ;;
  59. \?)
  60. echo "Unsupported option $OPTARG" >&2
  61. exit 1
  62. ;;
  63. esac
  64. done
  65. checkLogArgument "$LOG"
  66. ROOT=$(pwd)
  67. cd sonar-application/build/distributions/sonarqube-*
  68. SQ_HOME=$(pwd)
  69. cd "$ROOT"
  70. doTail "$LOG" "$LINES"
  71. fi