ambari-metrics-collector 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #!/usr/bin/env bash
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific
  15. #JAVA_HOME=/usr/jdk64/jdk1.7.0_45
  16. PIDFILE=/var/run/ambari-metrics-collector/ambari-metrics-collector.pid
  17. OUTFILE=/var/log/ambari-metrics-collector/ambari-metrics-collector.out
  18. HBASE_ZK_PID=/var/run/ams-hbase/hbase-hbase-zookeeper.pid
  19. HBASE_MASTER_PID=/var/run/ams-hbase/hbase-hbase-master.pid
  20. HBASE_RS_PID=/var/run/ams-hbase/hbase-hbase-regionserver.pid
  21. HBASE_DIR=/usr/lib/ams-hbase
  22. DAEMON_NAME=timelineserver
  23. COLLECTOR_CONF_DIR=/etc/ambari-metrics-collector/conf
  24. HBASE_CONF_DIR=/etc/ams-hbase/conf
  25. METRIC_COLLECTOR=ambari-metrics-collector
  26. AMS_LOG_DIR=/var/log/ambari-metrics-collector
  27. STOP_TIMEOUT=5
  28. DISTRIBUTED_HBASE=false
  29. function hbase_daemon
  30. {
  31. local daemon=$1
  32. local cmd=$2
  33. local pid
  34. case "${daemon}" in
  35. "master")
  36. pid=${HBASE_MASTER_PID}
  37. ;;
  38. "zookeeper")
  39. pid=${HBASE_ZK_PID}
  40. ;;
  41. "regionserver")
  42. pid=${HBASE_RS_PID}
  43. ;;
  44. esac
  45. daemon_status "${pid}"
  46. if [[ $? == 0 ]]; then
  47. echo "${daemon} is running as process $(cat "${pid}"). Continuing"
  48. else
  49. # stale pid file, so just remove it and continue on
  50. rm -f "${pid}" >/dev/null 2>&1
  51. fi
  52. ${HBASE_DIR}/bin/hbase-daemon.sh --config ${HBASE_CONF_DIR} ${cmd} ${daemon}
  53. }
  54. function write_pidfile
  55. {
  56. local pidfile="$1"
  57. echo $! > "${pidfile}" 2>/dev/null
  58. if [[ $? -gt 0 ]]; then
  59. echo "ERROR: Cannot write pid ${pidfile}."
  60. exit 1;
  61. fi
  62. }
  63. function hadoop_java_setup
  64. {
  65. # Bail if we did not detect it
  66. if [[ -z "${JAVA_HOME}" ]]; then
  67. echo "ERROR: JAVA_HOME is not set and could not be found."
  68. exit 1
  69. fi
  70. if [[ ! -d "${JAVA_HOME}" ]]; then
  71. echo "ERROR: JAVA_HOME ${JAVA_HOME} does not exist."
  72. exit 1
  73. fi
  74. JAVA="${JAVA_HOME}/bin/java"
  75. if [[ ! -x "$JAVA" ]]; then
  76. echo "ERROR: $JAVA is not executable."
  77. exit 1
  78. fi
  79. # shellcheck disable=SC2034
  80. JAVA_HEAP_MAX=-Xmx1g
  81. HADOOP_HEAPSIZE=${HADOOP_HEAPSIZE:-1024}
  82. # check envvars which might override default args
  83. if [[ -n "$HADOOP_HEAPSIZE" ]]; then
  84. # shellcheck disable=SC2034
  85. JAVA_HEAP_MAX="-Xmx${HADOOP_HEAPSIZE}m"
  86. fi
  87. }
  88. function daemon_status()
  89. {
  90. #
  91. # LSB 4.1.0 compatible status command (1)
  92. #
  93. # 0 = program is running
  94. # 1 = dead, but still a pid (2)
  95. # 2 = (not used by us)
  96. # 3 = not running
  97. #
  98. # 1 - this is not an endorsement of the LSB
  99. #
  100. # 2 - technically, the specification says /var/run/pid, so
  101. # we should never return this value, but we're giving
  102. # them the benefit of a doubt and returning 1 even if
  103. # our pid is not in in /var/run .
  104. #
  105. local pidfile="$1"
  106. shift
  107. local pid
  108. if [[ -f "${pidfile}" ]]; then
  109. pid=$(cat "${pidfile}")
  110. if ps -p "${pid}" > /dev/null 2>&1; then
  111. return 0
  112. fi
  113. return 1
  114. fi
  115. return 3
  116. }
  117. while [[ -z "${_ams_configs_done}" ]]; do
  118. case $1 in
  119. --config)
  120. shift
  121. confdir=$1
  122. shift
  123. if [[ -d "${confdir}" ]]; then
  124. COLLECTOR_CONF_DIR="${confdir}"
  125. elif [[ -z "${confdir}" ]]; then
  126. echo "ERROR: No parameter provided for --config "
  127. exit 1
  128. else
  129. echo "ERROR: Cannot find configuration directory \"${confdir}\""
  130. exit 1
  131. fi
  132. ;;
  133. --distributed)
  134. DISTRIBUTED_HBASE=true
  135. shift
  136. ;;
  137. *)
  138. _ams_configs_done=true
  139. ;;
  140. esac
  141. done
  142. # execute ams-env.sh
  143. if [[ -f "${COLLECTOR_CONF_DIR}/ams-env.sh" ]]; then
  144. . "${COLLECTOR_CONF_DIR}/ams-env.sh"
  145. else
  146. echo "ERROR: Cannot execute ${COLLECTOR_CONF_DIR}/ams-env.sh." 2>&1
  147. exit 1
  148. fi
  149. # set pid dir path
  150. if [[ -n "${AMS_PID_DIR}" ]]; then
  151. PIDFILE=${AMS_PID_DIR}/ambari-metrics-collector.pid
  152. fi
  153. # set out file path
  154. if [[ -n "${AMS_COLLECTOR_LOG_DIR}" ]]; then
  155. OUTFILE=${AMS_COLLECTOR_LOG_DIR}/ambari-metrics-collector.out
  156. fi
  157. #TODO manage 3 hbase daemons for start/stop/status
  158. case "$1" in
  159. start)
  160. hadoop_java_setup
  161. # hbase_daemon "zookeeper" "start"
  162. # hbase_daemon "master" "start"
  163. # hbase_daemon "regionserver" "start"
  164. if [ !"${DISTRIBUTED_HBASE}" ]; then
  165. hbase_daemon "master" "start"
  166. else
  167. echo "Launching in distributed mode. Assuming Hbase daemons up and running."
  168. fi
  169. # sleep 30
  170. #TODO check issue fixed
  171. CLASS='org.apache.hadoop.yarn.server.applicationhistoryservice.ApplicationHistoryServer'
  172. # YARN_OPTS="${YARN_OPTS} ${YARN_TIMELINESERVER_OPTS}"
  173. # if [[ -n "${YARN_TIMELINESERVER_HEAPSIZE}" ]]; then
  174. # JAVA_HEAP_MAX="-Xmx${YARN_TIMELINESERVER_HEAPSIZE}m"
  175. # fi
  176. # check if this is needed?
  177. # export PHOENIX_JAR_PATH=/usr/lib/ambari-metrics/timelineservice/phoenix-client.jar
  178. # export HBASE_CONF_DIR=${HBASE_DIR}/conf
  179. daemon_status "${PIDFILE}"
  180. if [[ $? == 0 ]]; then
  181. echo "AMS is running as process $(cat "${PIDFILE}"). Exiting"
  182. exit 1
  183. else
  184. # stale pid file, so just remove it and continue on
  185. rm -f "${PIDFILE}" >/dev/null 2>&1
  186. fi
  187. nohup "${JAVA}" "-cp" "/usr/lib/ambari-metrics-collector/*:${COLLECTOR_CONF_DIR}" "-Djava.net.preferIPv4Stack=true" "-Dams.log.dir=${AMS_COLLECTOR_LOG_DIR}" "-Dproc_${DAEMON_NAME}" "${CLASS}" "$@" > $OUTFILE 2>&1 &
  188. PID=$!
  189. write_pidfile "${PIDFILE}"
  190. sleep 2
  191. echo "Verifying ${METRIC_COLLECTOR} process status..."
  192. if [ -z "`ps ax -o pid | grep ${PID}`" ]; then
  193. if [ -s ${OUTFILE} ]; then
  194. echo "ERROR: ${METRIC_COLLECTOR} start failed. For more details, see ${OUTFILE}:"
  195. echo "===================="
  196. tail -n 10 ${OUTFILE}
  197. echo "===================="
  198. else
  199. echo "ERROR: ${METRIC_COLLECTOR} start failed"
  200. rm -f ${PIDFILE}
  201. fi
  202. echo "Collector out at: ${OUTFILE}"
  203. exit -1
  204. fi
  205. echo "Collector successfully started."
  206. ;;
  207. stop)
  208. pidfile=${PIDFILE}
  209. if [[ -f "${pidfile}" ]]; then
  210. pid=$(cat "$pidfile")
  211. kill "${pid}" >/dev/null 2>&1
  212. sleep "${STOP_TIMEOUT}"
  213. if kill -0 "${pid}" > /dev/null 2>&1; then
  214. echo "WARNING: ${METRIC_COLLECTOR} did not stop gracefully after ${STOP_TIMEOUT} seconds: Trying to kill with kill -9"
  215. kill -9 "${pid}" >/dev/null 2>&1
  216. fi
  217. if ps -p "${pid}" > /dev/null 2>&1; then
  218. echo "ERROR: Unable to kill ${pid}"
  219. else
  220. rm -f "${pidfile}" >/dev/null 2>&1
  221. fi
  222. fi
  223. #stop hbase daemons
  224. if [ ! "${DISTRIBUTED_HBASE}" ]; then
  225. hbase_daemon "master" "stop"
  226. fi
  227. ;;
  228. status)
  229. daemon_status "${PIDFILE}"
  230. if [[ $? == 0 ]]; then
  231. echo "AMS is running as process $(cat "${PIDFILE}")."
  232. else
  233. echo "AMS is not running."
  234. fi
  235. #print embedded hbase daemons statuses?
  236. ;;
  237. restart)
  238. ;;
  239. esac