summaryrefslogtreecommitdiff
path: root/ambari-metrics/ambari-metrics-host-monitoring/conf/unix/ambari-metrics-monitor
blob: 7464c559035535be694c8f4b9ca014d04c15d06e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/bin/bash

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific


MONITOR_CONF_DIR=/etc/ambari-metrics-monitor/conf/
METRIC_MONITOR=ambari-metrics-monitor

RESOURCE_MONITORING_DIR=/usr/lib/python2.6/site-packages/resource_monitoring
METRIC_MONITOR_PY_SCRIPT=${RESOURCE_MONITORING_DIR}/main.py

PIDFILE=/var/run/ambari-metrics-monitor/ambari-metrics-monitor.pid
OUTFILE=/var/log/ambari-metrics-monitor/ambari-metrics-monitor.out

STOP_TIMEOUT=5

OK=0
NOTOK=1

if [ -a /usr/bin/python2.7 ] && [ -z "${PYTHON}" ]; then
  PYTHON=/usr/bin/python2.7
fi

if [ -a /usr/bin/python2.6 ] && [ -z "${PYTHON}" ]; then
  PYTHON=/usr/bin/python2.6
fi

if [ "x$PYTHON" == "x" ]; then
  PYTHON=/usr/bin/python
fi

export PYTHON=${PYTHON}

check_python_version ()
{
  echo "Verifying Python version compatibility..."
  majversion=`${PYTHON} -V 2>&1 | awk '{print $2}' | cut -d'.' -f1`
  minversion=`${PYTHON} -V 2>&1 | awk '{print $2}' | cut -d'.' -f2`
  numversion=$(( 10 * $majversion + $minversion))
  if (( $numversion < 26 )); then
    echo "ERROR: Found Python version $majversion.$minversion. Ambari Metric Monitor requires Python version > 2.6"
    return ${NOTOK}
  fi
  echo "Using python " ${PYTHON}
  return ${OK}
}

function write_pidfile
{
    local pidfile="$1"
    echo $! > "${pidfile}" 2>/dev/null
    if [[ $? -gt 0 ]]; then
      echo "ERROR:  Cannot write pid ${pidfile}."
      exit 1;
    fi
}

#locate config dir
while [[ -z "${_ams_configs_done}" ]]; do
  case $1 in
    --config)
      shift
      confdir=$1
      shift
      if [[ -d "${confdir}" ]]; then
        MONITOR_CONF_DIR="${confdir}"
      elif [[ -z "${confdir}" ]]; then
        echo "ERROR: No parameter provided for --config "
        exit 1
      else
        echo "ERROR: Cannot find configuration directory \"${confdir}\""
        exit 1
      fi
    ;;
    *)
      _ams_configs_done=true
    ;;
  esac
done

#execute ams-env.sh
if [[ -f "${MONITOR_CONF_DIR}/ams-env.sh" ]]; then
  . "${MONITOR_CONF_DIR}/ams-env.sh"
else
  echo "ERROR: Cannot execute ${MONITOR_CONF_DIR}/ams-env.sh." 2>&1
  exit 1
fi

#TODO decide if rebuild on each start (pretty quickly) to tolerate major node changes (like kernel update)
#build psutil
if [ ! "$(ls -A ${RESOURCE_MONITORING_DIR}/psutil/build)" ]; then
  echo "Building psutil..."
  dir=$(pwd)
  cd "${RESOURCE_MONITORING_DIR}/psutil"
  ${PYTHON} "setup.py" "build"
  cd "${dir}"
else
  echo "psutil build directory is not empty, continuing..."
fi

# Set log directory path
if [[ -n "${AMS_MONITOR_LOG_DIR}" ]]; then
  OUTFILE=${AMS_MONITOR_LOG_DIR}/ambari-metrics-monitor.out
fi

# Set pid directory path
if [[ -n "${AMS_MONITOR_PID_DIR}" ]]; then
  PIDFILE=${AMS_MONITOR_PID_DIR}/ambari-metrics-monitor.pid
fi

case "$1" in

  start)
    check_python_version
    if [ "$?" -eq "${NOTOK}" ]; then
          exit -1
    fi

    echo "Checking for previously running Metric Monitor..."
    if [ -f ${PIDFILE} ]; then
      PID=`cat ${PIDFILE}`
      if [ -z "`ps ax | grep -w ${PID} | grep resource_monitoring`" ]; then
        echo "${PIDFILE} found with no process. Removing ${PID}..."
        rm -f ${PIDFILE}
      else
        tput bold
        echo "WARN: ${METRIC_MONITOR} already running with PID: ${PID}"
        tput sgr0
        echo "Exiting."
        exit 0
      fi
    fi

    echo "Starting ${METRIC_MONITOR}"

    nohup ${PYTHON} ${METRIC_MONITOR_PY_SCRIPT} "$@" > ${OUTFILE} 2>&1 &
    PID=$!
    write_pidfile ${PIDFILE}

    sleep 2

    echo "Verifying ${METRIC_MONITOR} process status..."
    if [ -z "`ps ax | grep -w ${PID} | grep resource_monitoring`" ]; then
      if [ -s ${OUTFILE} ]; then
        echo "ERROR: ${METRIC_MONITOR} start failed. For more details, see ${OUTFILE}:"
        echo "===================="
        tail -n 10 ${OUTFILE}
        echo "===================="
      else
        echo "ERROR: ${METRIC_MONITOR} start failed"
        rm -f ${PIDFILE}
      fi
      echo "Monitor out at: ${OUTFILE}"
      exit -1
    fi

    echo "Metric Monitor successfully started"
    echo "Server log at: ${OUTFILE}"
  ;;
  status)
    if [ -f ${PIDFILE} ]; then
      PID=`cat ${PIDFILE}`
      echo "Found ${METRIC_MONITOR} PID: $PID"
      if [ -z "`ps ax | grep -w ${PID} | grep resource_monitoring`" ]; then
        echo "${METRIC_MONITOR} not running. Stale PID File at: $PIDFILE"
        retcode=2
      else
        tput bold
        echo "${METRIC_MONITOR} running."
        tput sgr0
        echo "Monitor PID at: ${PIDFILE}"
        echo "Monitor out at: ${OUTFILE}"
      fi
    else
      tput bold
      echo "${METRIC_MONITOR} currently not running"
      tput sgr0
      echo "Usage: /usr/sbin/${METRIC_MONITOR} {start|stop|restart|status}"
      retcode=3
    fi
  ;;
  stop)
    pidfile=${PIDFILE}

    if [[ -f "${pidfile}" ]]; then
        pid=$(cat "$pidfile")

        kill "${pid}" >/dev/null 2>&1
        sleep "${STOP_TIMEOUT}"

        if kill -0 "${pid}" > /dev/null 2>&1; then
          echo "WARNING: ${METRIC_MONITOR} did not stop gracefully after ${STOP_TIMEOUT} seconds: Trying to kill with kill -9"
          kill -9 "${pid}" >/dev/null 2>&1
        fi

        if ps -p "${pid}" > /dev/null 2>&1; then
          echo "ERROR: Unable to kill ${pid}"
        else
          rm -f "${pidfile}" >/dev/null 2>&1
        fi
    fi

  ;;
  restart)
    echo -e "Restarting ${METRIC_MONITOR}"
    $0 stop
    $0 start "$@"
    retcode=$?
  ;;
esac