ambari-metrics-collector 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. function hbase_daemon
  29. {
  30. local daemon=$1
  31. local cmd=$2
  32. local pid
  33. case "${daemon}" in
  34. "master")
  35. pid=${HBASE_MASTER_PID}
  36. ;;
  37. "zookeeper")
  38. pid=${HBASE_ZK_PID}
  39. ;;
  40. "regionserver")
  41. pid=${HBASE_RS_PID}
  42. ;;
  43. esac
  44. daemon_status "${pid}"
  45. if [[ $? == 0 ]]; then
  46. echo "${daemon} is running as process $(cat "${pid}"). Continuing"
  47. else
  48. # stale pid file, so just remove it and continue on
  49. rm -f "${pid}" >/dev/null 2>&1
  50. fi
  51. ${HBASE_DIR}/bin/hbase-daemon.sh --config ${HBASE_CONF_DIR} ${cmd} ${daemon}
  52. }
  53. function write_pidfile
  54. {
  55. local pidfile="$1"
  56. echo $! > "${pidfile}" 2>/dev/null
  57. if [[ $? -gt 0 ]]; then
  58. echo "ERROR: Cannot write pid ${pidfile}."
  59. exit 1;
  60. fi
  61. }
  62. function hadoop_java_setup
  63. {
  64. # Bail if we did not detect it
  65. if [[ -z "${JAVA_HOME}" ]]; then
  66. echo "ERROR: JAVA_HOME is not set and could not be found."
  67. exit 1
  68. fi
  69. if [[ ! -d "${JAVA_HOME}" ]]; then
  70. echo "ERROR: JAVA_HOME ${JAVA_HOME} does not exist."
  71. exit 1
  72. fi
  73. JAVA="${JAVA_HOME}/bin/java"
  74. if [[ ! -x "$JAVA" ]]; then
  75. echo "ERROR: $JAVA is not executable."
  76. exit 1
  77. fi
  78. # shellcheck disable=SC2034
  79. JAVA_HEAP_MAX=-Xmx1g
  80. HADOOP_HEAPSIZE=${HADOOP_HEAPSIZE:-1024}
  81. # check envvars which might override default args
  82. if [[ -n "$HADOOP_HEAPSIZE" ]]; then
  83. # shellcheck disable=SC2034
  84. JAVA_HEAP_MAX="-Xmx${HADOOP_HEAPSIZE}m"
  85. fi
  86. }
  87. function daemon_status()
  88. {
  89. #
  90. # LSB 4.1.0 compatible status command (1)
  91. #
  92. # 0 = program is running
  93. # 1 = dead, but still a pid (2)
  94. # 2 = (not used by us)
  95. # 3 = not running
  96. #
  97. # 1 - this is not an endorsement of the LSB
  98. #
  99. # 2 - technically, the specification says /var/run/pid, so
  100. # we should never return this value, but we're giving
  101. # them the benefit of a doubt and returning 1 even if
  102. # our pid is not in in /var/run .
  103. #
  104. local pidfile="$1"
  105. shift
  106. local pid
  107. if [[ -f "${pidfile}" ]]; then
  108. pid=$(cat "${pidfile}")
  109. if ps -p "${pid}" > /dev/null 2>&1; then
  110. return 0
  111. fi
  112. return 1
  113. fi
  114. return 3
  115. }
  116. while [[ -z "${_ams_configs_done}" ]]; do
  117. case $1 in
  118. --config)
  119. shift
  120. confdir=$1
  121. shift
  122. if [[ -d "${confdir}" ]]; then
  123. COLLECTOR_CONF_DIR="${confdir}"
  124. elif [[ -z "${confdir}" ]]; then
  125. echo "ERROR: No parameter provided for --config "
  126. exit 1
  127. else
  128. echo "ERROR: Cannot find configuration directory \"${confdir}\""
  129. exit 1
  130. fi
  131. ;;
  132. *)
  133. _ams_configs_done=true
  134. ;;
  135. esac
  136. done
  137. # execute ams-env.sh
  138. if [[ -f "${COLLECTOR_CONF_DIR}/ams-env.sh" ]]; then
  139. . "${COLLECTOR_CONF_DIR}/ams-env.sh"
  140. else
  141. echo "ERROR: Cannot execute ${COLLECTOR_CONF_DIR}/ams-env.sh." 2>&1
  142. exit 1
  143. fi
  144. # set pid dir path
  145. if [[ -n "${AMS_PID_DIR}" ]]; then
  146. PIDFILE=${AMS_PID_DIR}/ambari-metrics-collector.pid
  147. fi
  148. # set out file path
  149. if [[ -n "${AMS_COLLECTOR_LOG_DIR}" ]]; then
  150. OUTFILE=${AMS_COLLECTOR_LOG_DIR}/ambari-metrics-collector.out
  151. fi
  152. #TODO manage 3 hbase daemons for start/stop/status
  153. case "$1" in
  154. start)
  155. hadoop_java_setup
  156. #hbase_daemon "zookeeper" "start"
  157. hbase_daemon "master" "start"
  158. #hbase_daemon "regionserver" "start"
  159. sleep 30
  160. CLASS='org.apache.hadoop.yarn.server.applicationhistoryservice.ApplicationHistoryServer'
  161. # YARN_OPTS="${YARN_OPTS} ${YARN_TIMELINESERVER_OPTS}"
  162. # if [[ -n "${YARN_TIMELINESERVER_HEAPSIZE}" ]]; then
  163. # JAVA_HEAP_MAX="-Xmx${YARN_TIMELINESERVER_HEAPSIZE}m"
  164. # fi
  165. # check if this is needed?
  166. # export PHOENIX_JAR_PATH=/usr/lib/ambari-metrics/timelineservice/phoenix-client.jar
  167. # export HBASE_CONF_DIR=${HBASE_DIR}/conf
  168. daemon_status "${PIDFILE}"
  169. if [[ $? == 0 ]]; then
  170. echo "AMS is running as process $(cat "${PIDFILE}"). Exiting"
  171. exit 1
  172. else
  173. # stale pid file, so just remove it and continue on
  174. rm -f "${PIDFILE}" >/dev/null 2>&1
  175. fi
  176. 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 &
  177. PID=$!
  178. write_pidfile "${PIDFILE}"
  179. sleep 2
  180. echo "Verifying ${METRIC_COLLECTOR} process status..."
  181. if [ -z "`ps ax -o pid | grep ${PID}`" ]; then
  182. if [ -s ${OUTFILE} ]; then
  183. echo "ERROR: ${METRIC_COLLECTOR} start failed. For more details, see ${OUTFILE}:"
  184. echo "===================="
  185. tail -n 10 ${OUTFILE}
  186. echo "===================="
  187. else
  188. echo "ERROR: ${METRIC_COLLECTOR} start failed"
  189. rm -f ${PIDFILE}
  190. fi
  191. echo "Collector out at: ${OUTFILE}"
  192. exit -1
  193. fi
  194. echo "Collector successfully started."
  195. ;;
  196. stop)
  197. pidfile=${PIDFILE}
  198. if [[ -f "${pidfile}" ]]; then
  199. pid=$(cat "$pidfile")
  200. kill "${pid}" >/dev/null 2>&1
  201. sleep "${STOP_TIMEOUT}"
  202. if kill -0 "${pid}" > /dev/null 2>&1; then
  203. echo "WARNING: ${METRIC_COLLECTOR} did not stop gracefully after ${STOP_TIMEOUT} seconds: Trying to kill with kill -9"
  204. kill -9 "${pid}" >/dev/null 2>&1
  205. fi
  206. if ps -p "${pid}" > /dev/null 2>&1; then
  207. echo "ERROR: Unable to kill ${pid}"
  208. else
  209. rm -f "${pidfile}" >/dev/null 2>&1
  210. fi
  211. fi
  212. #stop hbase daemons
  213. #hbase_daemon "zookeeper" "stop"
  214. hbase_daemon "master" "stop"
  215. #hbase_daemon "regionserver" "stop"
  216. ;;
  217. status)
  218. daemon_status "${PIDFILE}"
  219. if [[ $? == 0 ]]; then
  220. echo "AMS is running as process $(cat "${PIDFILE}")."
  221. else
  222. echo "AMS is not running."
  223. fi
  224. #print embedded hbase daemons statuses?
  225. ;;
  226. restart)
  227. ;;
  228. esac