|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -eo pipefail |
| 4 | + |
| 5 | +[[ $DEBUG ]] && set -x |
| 6 | + |
| 7 | +# configure etcd |
| 8 | +export ETCD_PORT="${ETCD_PORT:-4001}" |
| 9 | +export ETCD="$HOST:$ETCD_PORT" |
| 10 | +export ETCD_PATH="${ETCD_PATH:-/deis/monitor}" |
| 11 | +export ETCD_TTL="${ETCD_TTL:-20}" |
| 12 | + |
| 13 | +until etcdctl --no-sync -C "$ETCD" ls >/dev/null 2>&1; do |
| 14 | + echo "monitor: waiting for etcd at ${ETCD}..." |
| 15 | + sleep $((ETCD_TTL/2)) # sleep for half the TTL |
| 16 | +done |
| 17 | + |
| 18 | +until confd -onetime -node "$ETCD" --confdir /etc/confd --log-level error; do |
| 19 | + echo "monitor: waiting for confd to write initial templates..." |
| 20 | + sleep $((ETCD_TTL/2)) # sleep for half the TTL |
| 21 | +done |
| 22 | + |
| 23 | +set +e |
| 24 | + |
| 25 | +OPTS="" |
| 26 | + |
| 27 | +if ALERT_MANAGER="$(etcdctl -C "$ETCD" get /deis/monitor/alertManagerUri)"; then |
| 28 | + OPTS="-alertmanager.url=$ALERT_MANAGER" |
| 29 | +fi |
| 30 | + |
| 31 | +set -e |
| 32 | + |
| 33 | +confd -node "$ETCD" --confdir /etc/confd --log-level info --interval 5 & |
| 34 | + |
| 35 | +echo "monitor: starting prometheus" |
| 36 | +/bin/prometheus -config.file=/etc/prometheus/prometheus.yml $OPTS & |
| 37 | +SERVICE_PID=$! |
| 38 | +echo $SERVICE_PID > /var/spool/prometheus.pid |
| 39 | +echo "monitor: monitor has been started in background with pid: ${SERVICE_PID}" |
| 40 | + |
| 41 | +# @fixme: hax. how else can we accomplish this?. |
| 42 | +echo "monitor: updating etcd node list in... etcd" |
| 43 | +while true; do |
| 44 | + for uri in $(etcdctl -C "$ETCD" member list | awk '{print $4}' | awk -F= '{print $2}' | awk -F, '{print $1}'); do |
| 45 | + host=$(echo $uri | awk -F: '{print $2}') |
| 46 | + port=$(echo $uri | awk -F: '{print $3}') |
| 47 | + if ! etcdctl -C "$ETCD" get /deis/monitor/endpoints/etcd/$host >/dev/null 2>&1; then |
| 48 | + etcdctl -C "$ETCD" set /deis/monitor/endpoints/etcd/$host $port >/dev/null 2>&1; |
| 49 | + fi |
| 50 | + done |
| 51 | + sleep 60 |
| 52 | +done |
0 commit comments