#!/bin/sh
#
# Nagios XI failover for Princeton University
#
# Initial version written by:
# Eric Loyd (eloyd@everwatch.global)
# EverWatch Global, Inc.

PATH=/usr/sbin:/sbin:/usr/bin:/bin

#primary="nagiosxi-p.princeton.edu"
#secondary="nagiosxi-f.princeton.edu"
backupDir="/store/backups/nagiosxi"
force=""
noping="false"
primary=""
secondary=""
synconly="false"
syncname="sync"
extraCmd=""
cssDir="/usr/local/nagiosxi/html/includes/components/custom-includes/css"

hostname=`/bin/hostname`

. ./colors.sh

while [ -n "$1" ]; do
  case "$1" in
    -H | --hostname) hostname="$2"; shift 2;;
    -S | --secondary) secondary="$2"; shift 2;;
    -P | --primary) primary="$2"; shift 2;;
    -n | --sync-name) syncname="$2"; shift 2;;
    -s | --sync-only) synconly="true"; shift 1;;
    --force) force="true"; shift 1;;
    --noping) noping="true"; shift 1;;
    *) extraCmd="$extraCmd $1"; shift 1;;
  esac
done

[ -z "$hostname" ] && warning "Invalid hostname." && exit 2

ping_host() {
  $noping && return 0
  /usr/local/nagios/libexec/check_icmp -H $1 >/dev/null 2>&1
}

stop_nagios() {
    /home/nagios/bin/nagios_startstop.sh stop
}

start_nagios() {
    /home/nagios/bin/nagios_startstop.sh start
}

clean_old_copies() {
  verbose "Cleaning up old sync files..." -n
  find ${backupDir} -maxdepth 1 -name sync.\* -mtime +1 -exec rm {} \;

  verbose "Done"
}

do_nothing() {
  warning "We are not the primary or secondary.  Doing nothing."
  exit 1
}

do_primary() {
  verbose "We are the primary.  Checking to see if secondary is on the network..."
  ping_host ${secondary}
  [ $? -ne 0 ] && error "Cannot ping ${secondary}.  Use --noping to continue wihout ping." && exit
  verbose "Creating package to send to secondary"
  /home/nagios/bin/rsync_xi.sh --prepend ${syncname} --primary "${primary}" --secondary "${secondary}" $extraCmd
  # Update header.css with proper gradient files
  if [ -r "${cssDir}/header.css.primary" -a -w "${cssDir}/header.css" ]; then
    verbose "Copying local gradient files"
    rsync ${cssDir}/header.css.primary ${cssDir}/header.css
  fi
}

do_secondary() {
  verbose "We are the secondary.  Checking to see if primary is on the network..."
  ping_host ${primary}
  if [ $? -eq 0 ]; then
    if [ -z $force ]; then
      warning "The primary (${primary}) is on the network.  This process will be aborted.  Use --force to force it to continue."
      exit 1
    else
      warning "The primary (${primary}) is on the network but --force specified.  Proceeding anyway."
    fi
  fi
  verbose "Expanding package from the primary"
  syncfile=$(basename `ls -tr1 ${backupDir}/${syncname}* | tail -1`)
  if [ -z "$syncfile" ]; then
    error "No sync file ($syncfile) found."
    warning "Continue anyway? (10 second timeout, one letter only please) [y/N] " -n
    read -t 10 -e -n 1 continueAnyway
    if [ "$continueAnyway" = "y" -o "$continueAnyway" = "Y" ]; then
      /home/nagios/bin/rsync_xi.sh --primary "${primary}" --secondary "${secondary}" --file "/store/backups/nagiosxi/$syncfile" --nofile
    else
      verbose "Exiting."
      exit 2
    fi
  else
    /home/nagios/bin/rsync_xi.sh --primary "${primary}" --secondary "${secondary}" --file "/store/backups/nagiosxi/$syncfile"
  fi
  if [ $? -eq 0 ]; then
    # Update header.css with proper gradient files
    if [ -r "${cssDir}/header.css.secondary" -a -w "${cssDir}/header.css" ]; then
      verbose "Copying local gradient files"
      rsync ${cssDir}/header.css.secondary ${cssDir}/header.css
    fi
    verbose "Ensuring that xi.key file has proper permissions"
    chmod 0640 /usr/local/nagiosxi/var/keys/xi.key
    chown nagios:nagios /usr/local/nagiosxi/var/keys/xi.key
    verbose "***"
    warning "***"
    error "***"
    warning "This is intended to be manually executed when needed, so we assume you want to start Nagios..." -n
    start_nagios
    rm -f ${syncfile}
    verbose "Done."
  fi
}

case "$hostname" in
  $primary) do_primary;;
  $secondary) do_secondary;;
  *) do_nothing
esac
clean_old_copies

exit 0

