ambari-metrics-grafana 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. # chkconfig: 2345 80 05
  16. # description: Grafana web server & backend
  17. # processname: grafana
  18. # config: /etc/grafana/ams-grafana.ini
  19. # pidfile: /var/run/ambari-metrics-grafana.pid
  20. ### BEGIN INIT INFO
  21. # Provides: grafana
  22. # Required-Start: $all
  23. # Required-Stop: $remote_fs $syslog
  24. # Default-Start: 2 3 4 5
  25. # Default-Stop: 0 1 6
  26. # Short-Description: Start grafana at boot time
  27. ### END INIT INFO
  28. # tested on
  29. # 1. New lsb that define start-stop-daemon
  30. # 3. Centos with initscripts package installed
  31. PATH=/bin:/usr/bin:/sbin:/usr/sbin
  32. NAME=grafana-server
  33. DESC="Ambari Metrics Grafana"
  34. CONF_DIR=/etc/ambari-metrics-grafana/conf
  35. # execute ams-grafana-env.sh
  36. if [[ -f "${CONF_DIR}/ams-grafana-env.sh" ]]; then
  37. . "${CONF_DIR}/ams-grafana-env.sh"
  38. else
  39. echo "ERROR: Cannot execute ${CONF_DIR}/ams-grafana-env.sh." 2>&1
  40. exit 1
  41. fi
  42. GRAFANA_HOME=${AMS_GRAFANA_HOME_DIR}
  43. WORK_DIR=$GRAFANA_HOME
  44. DATA_DIR=${AMS_GRAFANA_DATA_DIR}
  45. LOG_DIR=${AMS_GRAFANA_LOG_DIR}
  46. LOG_FILE=$LOG_DIR/grafana.log
  47. CONF_FILE=$CONF_DIR/ams-grafana.ini
  48. MAX_OPEN_FILES=10000
  49. PID_FILE=${AMS_GRAFANA_PID_DIR}/$NAME.pid
  50. DAEMON=$GRAFANA_HOME/bin/$NAME
  51. OUT_FILE=${AMS_GRAFANA_LOG_DIR}/grafana.out
  52. STOP_TIMEOUT=5
  53. #if [ `id -u` -ne 0 ]; then
  54. # echo "You need root privileges to run this script"
  55. # exit 4
  56. #fi
  57. if [ ! -x $DAEMON ]; then
  58. echo "Program not installed or not executable"
  59. exit 5
  60. fi
  61. #
  62. # init.d / servicectl compatibility (openSUSE)
  63. #
  64. # if [ -f /etc/rc.status ]; then
  65. # . /etc/rc.status
  66. # rc_reset
  67. # fi
  68. #
  69. # Source function library.
  70. #
  71. # if [ -f /etc/rc.d/init.d/functions ]; then
  72. # . /etc/rc.d/init.d/functions
  73. # fi
  74. # overwrite settings from default file
  75. # [ -e /etc/sysconfig/$NAME ] && . /etc/sysconfig/$NAME
  76. DAEMON_OPTS="--pidfile=${PID_FILE} --config=${CONF_FILE} cfg:default.paths.data=${DATA_DIR} cfg:default.paths.logs=${LOG_DIR}"
  77. function isRunning() {
  78. status -p $PID_FILE $NAME > /dev/null 2>&1
  79. }
  80. case "$1" in
  81. start)
  82. echo $"Starting $DESC: .... " >> $LOG_FILE
  83. isRunning
  84. if [ $? -eq 0 ]; then
  85. echo "Already running." >> $LOG_FILE
  86. exit 0
  87. fi
  88. # Prepare environment
  89. # mkdir -p "$LOG_DIR" "$DATA_DIR" && chown "$GRAFANA_USER":"$GRAFANA_GROUP" "$LOG_DIR" "$DATA_DIR"
  90. touch "$PID_FILE" && chown "$GRAFANA_USER":"$GRAFANA_GROUP" "$PID_FILE"
  91. # if [ -n "$MAX_OPEN_FILES" ]; then
  92. # ulimit -n $MAX_OPEN_FILES
  93. # fi
  94. # Start Daemon
  95. cd $GRAFANA_HOME
  96. nohup ${DAEMON} ${DAEMON_OPTS} > $OUT_FILE 2>&1 &
  97. return=$?
  98. if [ $return -eq 0 ]
  99. then
  100. sleep 5
  101. # check if pid file has been written two
  102. if ! [[ -s $PID_FILE ]]; then
  103. echo "Start FAILED because daemon did not write pid in pid_file" >> $LOG_FILE
  104. exit 1
  105. fi
  106. i=0
  107. timeout=60
  108. # Wait for the process to be properly started before exiting
  109. until { cat "$PID_FILE" | xargs kill -0; } >/dev/null 2>&1
  110. do
  111. sleep 1
  112. i=$(($i + 1))
  113. if [ $i -gt $timeout ]; then
  114. echo "FAILED"
  115. exit 1
  116. fi
  117. done
  118. fi
  119. echo "OK" >> $LOG_FILE
  120. exit $return
  121. ;;
  122. stop)
  123. echo -n "Stopping $DESC ..." >> $LOG_FILE
  124. if [ -f "$PID_FILE" ]; then
  125. pid=$(cat "$PID_FILE")
  126. kill "${pid}" >/dev/null 2>&1
  127. sleep "${STOP_TIMEOUT}"
  128. if kill -0 "${pid}" > /dev/null 2>&1; then
  129. echo "WARNING: $DESC did not stop gracefully after ${STOP_TIMEOUT} seconds: Trying to kill with kill -9" >> $LOG_FILE
  130. kill -9 "${pid}" >/dev/null 2>&1
  131. fi
  132. if ps -p "${pid}" > /dev/null 2>&1; then
  133. echo "ERROR: Unable to kill ${pid}" >> $LOG_FILE
  134. else
  135. rm -f "$PID_FILE" >/dev/null 2>&1
  136. fi
  137. echo "OK"
  138. else
  139. echo -n "(not running)" >> $LOG_FILE
  140. fi
  141. exit 0
  142. ;;
  143. status)
  144. status -p $PID_FILE $NAME
  145. exit $?
  146. ;;
  147. restart|force-reload)
  148. if [ -f "$PID_FILE" ]; then
  149. $0 stop
  150. sleep 1
  151. fi
  152. $0 start
  153. ;;
  154. *)
  155. echo "Usage: $0 {start|stop|restart|force-reload|status}"
  156. exit 3
  157. ;;
  158. esac