#!/bin/sh
# Nico Schottelius, 2018-02-22
# Copyright ungleich glarus ag

if [ $# -ne 1 ]; then
    echo "$0: to monitor"
    echo "f.i. osd.17 or mon.server4"
    exit 1
fi

to_monitor=$1

set -e

depends="cephrundir"
conf="/etc/monit/conf.d/$to_monitor"

daemon=$(echo $to_monitor | awk -F . '{ print $1 }')
id=$(echo $to_monitor | awk -F . '{ print $2 }')


case "$daemon" in
    osd)
        depends="${depends}, ${to_monitor}-whoami"
    cat > "$conf" <<EOF
check file ${to_monitor}-whoami with path /var/lib/ceph/osd/ceph-${id}/whoami
 if content != "${id}" then alert
EOF

        ;;
esac

cat >> "$conf" <<EOF
# Generated by $0
check process ${to_monitor} with pidfile /var/run/ceph/${to_monitor}.pid
   start program = "/usr/bin/ceph-${daemon} -i ${id} --pid-file /var/run/ceph/${daemon}.${id}.pid -c /etc/ceph/ceph.conf --cluster ceph --setuser ceph --setgroup ceph" with timeout 3600 seconds
   stop program = "/usr/bin/pkill -f '/usr/bin/ceph-${daemon} -i ${id}'"

   depends on $depends
EOF

/etc/init.d/monit restart
# monit reload
sleep 1
monit start "${to_monitor}"
