#!/bin/bash

action="start"
xihost="192.168.123.16"
key="NRDP Key for Nagios Server"
logFile="${HOME}/logs/gearman.log"

start_options="--server=${xihost} --key=${key}"
start_options+=" --min-worker=2"
start_options+=" --max-worker=5"
start_options+=" --spawn-rate=1"
start_options+=" --load_limit1=1"
start_options+=" --load_limit5=2"
start_options+=" --load_limit15=3"
start_options+=" --idle-timeout=600"
start_options+=" --hosts --services --hostgroup=PODhosts --servicegroup=PODservices --logmode=file --logfile=${logFile} --debug=1"

while [ -n "$1" ]; do
  case "$1" in
    --start) action="start"; shift 1;;
    --stop) action="stop"; shift 1;;
    --restart) action="restart"; shift 1;;
    *) shift 1;;
  esac
done


do_stop() {
  echo "Stopping nagios-mod-gearman-worker..."
  killall /usr/local/bin/nagios-mod-gearman-worker
  echo "Done."
}

do_start() {
  echo "Starting nagios-mod-gearman-worker..."
  /usr/local/bin/nagios-mod-gearman-worker -d $start_options
  echo "Done."
}

do_restart() {
  do_stop
  sleep 5
  do_start
}

do_${action}
