ambari-metrics-collector 6.7 KB

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