hue_download_watcher.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #!/bin/bash
  2. # Licensed to Cloudera, Inc. under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. Cloudera, Inc. licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #parse command line arguments
  18. parse_arguments()
  19. {
  20. # Test that we're using compatible getopt version.
  21. getopt -T > /dev/null
  22. if [[ $? -ne 4 ]]; then
  23. echo "Incompatible getopt version."
  24. exit 1
  25. fi
  26. # Parse short and long option parameters.
  27. ROTATE_SIZE=10
  28. SPAWN_WATCHER=
  29. GETOPT=`getopt -n $0 -o d:,s:,w,h \
  30. -l hue-log-dir:,max-log-size:,watcher,help \
  31. -- "$@"`
  32. eval set -- "$GETOPT"
  33. while true;
  34. do
  35. case "$1" in
  36. -d|--hue-log-dir)
  37. DESKTOP_LOG_DIR=$2
  38. shift 2
  39. ;;
  40. -s|--max-log-size)
  41. ROTATE_SIZE=$2
  42. shift 2
  43. ;;
  44. -w|--watcher)
  45. SPAWN_WATCHER=$1
  46. shift 1
  47. ;;
  48. --)
  49. shift
  50. break
  51. ;;
  52. *)
  53. usage
  54. exit 1
  55. ;;
  56. esac
  57. done
  58. }
  59. usage() {
  60. cat << EOF
  61. usage: $0
  62. Watches Hue log for downloads and queries:
  63. OPTIONS
  64. -d|--hue-log-dir </log/path Path to Hue logs if not in /var/log/hue.
  65. -s|--max-log-size <MB> Max log size before rotating.
  66. -w|--watcher Spawns the watcher daemon. DO NOT RUN FLAG YOURSELF
  67. -h|--help Show this message.
  68. EOF
  69. }
  70. main() {
  71. INITIAL_ARGS=("$@")
  72. parse_arguments "$@"
  73. if [[ -z ${DESKTOP_LOG_DIR} ]]
  74. then
  75. DESKTOP_LOG_DIR=/var/log/hue
  76. fi
  77. ENDFILE=${DESKTOP_LOG_DIR}/$(basename "$0" | awk -F\. '{print $1}').finish
  78. RUNCPSERVER=${DESKTOP_LOG_DIR}/runcpserver.log
  79. LOG_FILE=${DESKTOP_LOG_DIR}/$(basename "$0" | awk -F\. '{print $1}').log
  80. if [[ ! ${USER} =~ .*root* ]]
  81. then
  82. echo "Script must be run as root: exiting"
  83. exit 1
  84. fi
  85. if [[ -f ${ENDFILE} ]] && [[ -z ${SPAWN_WATCHER} ]]
  86. then
  87. echo "$0 already running. Please run:"
  88. echo "rm -f ${ENDFILE}"
  89. echo "to exit the previous running version."
  90. exit 1
  91. fi
  92. if [[ ! -z ${SPAWN_WATCHER} ]] && [[ ! -f ${ENDFILE} ]]
  93. then
  94. echo "$0 must not be run manually with -w or --watcher flags"
  95. exit 1
  96. fi
  97. if [[ ! -f ${RUNCPSERVER} ]]
  98. then
  99. echo "${RUNCPSERVER} does not exist"
  100. echo "Please run command with correct log directory for Hue"
  101. usage
  102. exit 1
  103. fi
  104. if [[ ! "${ROTATE_SIZE}" =~ ^[0-9]+$ ]]
  105. then
  106. echo "--max-log-size or -s must be a number"
  107. exit 1
  108. fi
  109. OLDTIME=1800
  110. # Get current and file times
  111. CURTIME=$(date +%s)
  112. FILETIME=$(stat ${RUNCPSERVER} -c %Y)
  113. TIMEDIFF=$(expr ${CURTIME} - ${FILETIME})
  114. # Check if file older
  115. if [ ${TIMEDIFF} -gt ${OLDTIME} ]; then
  116. echo "${RUNCPSERVER} has not been written to in 30 mins"
  117. echo "Be sure that ${DEKSTOP_LOG_DIR} is the correct log path"
  118. echo "If it is then run:"
  119. echo "touch ${RUNCPSERVER}"
  120. echo "And run this command again"
  121. echo "Otherwise run command with correct log directory for Hue"
  122. echo "$0 --hue-log-dir /path/to/hue/logs"
  123. echo "For example:"
  124. echo "$0 --hue-log-dir /var/log/hue"
  125. exit 1
  126. fi
  127. if [[ ! -z ${SPAWN_WATCHER} ]]
  128. then
  129. tail -F ${RUNCPSERVER} | egrep --line-buffered "\/download|downloaded|TExecuteStatementResp\(|[[:digit:]]{5,9}ms" >> ${LOG_FILE} &
  130. PID=$!
  131. while [[ -f ${ENDFILE} ]]
  132. do
  133. sleep 10
  134. if [[ -f ${LOG_FILE} ]]
  135. then
  136. LOG_SIZE=$(du -sm ${LOG_FILE} | awk '{print $1}')
  137. if [[ ${LOG_SIZE} -gt ${ROTATE_SIZE} ]]
  138. then
  139. cat ${LOG_FILE} > ${LOG_FILE}.1
  140. cat /dev/null > ${LOG_FILE}
  141. fi
  142. fi
  143. done
  144. kill -9 ${PID}
  145. rm ${ENDFILE}
  146. else
  147. echo "Launching watcher daemon. To end process run:"
  148. echo "rm ${ENDFILE}"
  149. touch ${ENDFILE}
  150. exec nohup "${BASH_SOURCE[0]}" --watcher "${INITIAL_ARGS[@]}" 0<&- &> /dev/null &
  151. fi
  152. exit
  153. }
  154. main "$@"