diff --git a/README.md b/README.md index fa2e815..1ca94b9 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,10 @@ # Princeton +## Files that need to be copied to /usr/local/nagios/libexec: +* pu-notify-all-emails + +## Files that need contents imported into XI: +* new_commands.cfg + + +## Also See failover/README.md diff --git a/failover/failover.sh b/failover/failover.sh index 5bd31f8..8d24628 100755 --- a/failover/failover.sh +++ b/failover/failover.sh @@ -30,7 +30,7 @@ done do_nothing() { echo "We are not the primary or secondary. Doing nothing." - exit + exit 1 } do_primary() { @@ -44,8 +44,11 @@ do_secondary() { syncfile=`ls -tr1 /store/backups/nagiosxi/ | tail -1` [ -z "$syncfile" ] && echo "No sync file ($syncfile) found." && exit 2 ./restore_xi.sh /store/backups/nagiosxi/$syncfile - ./nagios_startstop.sh stop - cp /usr/local/nagiosxi/html/includes/components/custom-includes/css/header-gradient.css.secondary /usr/local/nagiosxi/html/includes/components/custom-includes/css/header-gradient.css + if [ $? -eq 0 ]; then + ./nagios_startstop.sh stop + cp /usr/local/nagiosxi/html/includes/components/custom-includes/css/header-gradient.css.secondary /usr/local/nagiosxi/html/includes/components/custom-includes/css/header-gradient.css + rm -f ${syncfile} + fi } case "$hostname" in @@ -53,3 +56,5 @@ case "$hostname" in $secondary) do_secondary;; *) do_nothing esac + +exit 0 diff --git a/failover/restore_xi.sh b/failover/restore_xi.sh index 14239eb..bd2408b 100755 --- a/failover/restore_xi.sh +++ b/failover/restore_xi.sh @@ -46,9 +46,7 @@ fi ############################## # MAKE TEMP RESTORE DIRECTORY ############################## -#ts=`echo $backupfile | cut -d . -f 1` ts=`date +%s` -echo "TS=$ts" mydir=${rootdir}/${ts}-restore mkdir -p $mydir if [ ! -d $mydir ]; then @@ -60,22 +58,10 @@ fi ############################## # UNZIP BACKUP ############################## -echo "Extracting backup to $mydir..." cd $mydir tar xzfps $backupfile - -# Change to subdirectory cd `ls` - -# Make sure we have some directories here... backupdir=`pwd` -echo "In $backupdir..." -#if [ ! -f nagiosxi.tar.gz ]; then -# echo "Unable to find files to restore in $backupdir" -# exit 1 -#fi - -echo "Backup files look okay. Preparing to restore..." ############################## @@ -92,7 +78,7 @@ rootdir=/ . $BASEDIR/../var/xi-sys.cfg php $BASEDIR/import_xiconfig.php > $BASEDIR/config.dat . $BASEDIR/config.dat -rm -rf $BASEDIR/config.dat +rm -f $BASEDIR/config.dat # Overwrite the mysqlpass with the hardcoded one at the top mysqlpass="$themysqlpass" @@ -113,7 +99,7 @@ mysql -h "$ndoutils_dbserver" --port="$ndoutils_dbport" -u root --password=$mysq res=$? if [ $res != 0 ]; then echo "Error restoring MySQL database 'nagios' - check the password in this script!" - exit; + exit $res; fi if [[ "$cfg__db_info__nagiosql__dbserver" == *":"* ]]; then nagiosql_dbport=`echo "$cfg__db_info__nagiosql__dbserver" | cut -f2 -d":"` @@ -126,7 +112,7 @@ mysql -h "$nagiosql_dbserver" --port="$nagiosql_dbport" -u root --password=$mysq res=$? if [ $res != 0 ]; then echo "Error restoring MySQL database 'nagiosql' - check the password in this script!" - exit; + exit $res; fi # Only restore PostgresQL if we are still using it if [ "$cfg__db_info__nagiosxi__dbtype" == "pgsql" ]; then @@ -154,7 +140,7 @@ host all all ::1/128 trust" > $pghba res=$? if [ $res != 0 ]; then echo "Error restoring PostgresQL database 'nagiosxi' !" - exit; + exit $res; fi $BASEDIR/manage_services.sh restart postgresql if [ "$dist" == "el7" ] || [ "$dist" == "el8" ]; then @@ -190,7 +176,7 @@ else res=$? if [ $res != 0 ]; then echo "Error restoring MySQL database 'nagiosxi' !" - exit; + exit $res; fi fi } diff --git a/failover/rsync_xi.sh b/failover/rsync_xi.sh index 3ad87ee..571afeb 100755 --- a/failover/rsync_xi.sh +++ b/failover/rsync_xi.sh @@ -38,7 +38,7 @@ while [ -n "$1" ]; do ;; -h | --help) usage - exit 0 + exit 1 ;; -n | --name) fullname=$2 @@ -149,7 +149,7 @@ mysqldump -h "$ndoutils_dbserver" --port="$ndoutils_dbport" -u $cfg__db_info__nd res=$? if [ $res != 0 ]; then echo "Error backing up MySQL database 'nagios' - check the password in this script!" | tee -a $SBLOG - rm -r $mydir + rm -rf $mydir exit $res; fi if [[ "$cfg__db_info__nagiosql__dbserver" == *":"* ]]; then @@ -163,7 +163,7 @@ mysqldump -h "$nagiosql_dbserver" --port="$nagiosql_dbport" -u $cfg__db_info__na res=$? if [ $res != 0 ]; then echo "Error backing up MySQL database 'nagiosql' - check the password in this script!" | tee -a $SBLOG - rm -r $mydir + rm -rf $mydir exit $res; fi # Only backup PostgresQL if we are still using it @@ -177,7 +177,7 @@ if [ $cfg__db_info__nagiosxi__dbtype == "pgsql" ]; then res=$? if [ $res != 0 ]; then echo "Error backing up PostgresQL database 'nagiosxi' !" | tee -a $SBLOG - rm -r $mydir + rm -rf $mydir exit $res; fi else @@ -192,7 +192,7 @@ else res=$? if [ $res != 0 ]; then echo "Error backing up MySQL database 'nagiosxi' - check the password in this script!" | tee -a $SBLOG - rm -r $mydir + rm -rf $mydir exit $res; fi fi @@ -202,7 +202,7 @@ fi ############################## echo "Compressing backup..." tar czfp $name.tar.gz $name -rm -rf $name +rm -f $name # Change ownership chown $nagiosuser:$nagiosgroup $name.tar.gz @@ -219,8 +219,9 @@ else echo "BACKUP FAILED" echo "===============" echo "File was not created at $rootdir/$name.tar.gz" - rm -r $mydir + rm -rf $mydir exit 1; fi do_rsync $rootdir/$name.tar.gz +exit 0 diff --git a/new_commands.cfg b/new_commands.cfg new file mode 100644 index 0000000..d24fa80 --- /dev/null +++ b/new_commands.cfg @@ -0,0 +1,19 @@ +define command { + command_name notify-host-by-email-address-1 + command_line /usr/bin/printf "%b" "***** Nagios Monitor XI Alert *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTADDRESS1$ +} + +define command { + command_name notify-service-by-email-address-1 + command_line /usr/bin/printf "%b" "***** Nagios Monitor XI Alert *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTADDRESS1$ +} + +define command { + command_name pu-notify-service-by-email-all + command_line /usr/bin/printf "%b" "***** Nagios Monitor XI Alert *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | $USER1$/pu-notify-all-emails -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" -t $CONTACTEMAIL$:$_HOSTALT_CONTACT$,$CONTACTADDRESS1$ +} + +define command { + command_name pu-notify-host-by-email-all + command_line /usr/bin/printf "%b" "***** Nagios Monitor XI Alert *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$:$_HOSTALT_CONTACT$,$CONTACTADDRESS1$ +} diff --git a/pu-notify-all-emails b/pu-notify-all-emails new file mode 100755 index 0000000..fcf60cc --- /dev/null +++ b/pu-notify-all-emails @@ -0,0 +1,44 @@ +#!/bin/sh +# +# Command to send to multiple email addreses simultaneously, separated by commas. +# However, if any email address has a colon in it, then the second email address overrides the first +# +# Ex: +# user1@example.com,user2@example.com:user3@example.com,user4@example.com +# +# This will send to user1, user3 (overriding user2), and user4. +# +# Command line definition from Nagios: +# /usr/bin/printf "%b" "***** Nagios Monitor XI Alert *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | \ +# $USER1$/pu-notify-all-emails -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" \ +# -t $CONTACTEMAIL$:$_HOSTALT_CONTACT$,$CONTACTADDRESS1$ + +PATH=/usr/bin:/bin + +subject="" +sendto="" + +# Get params for subject and receiver +while [ -n "$1" ]; do + case "$1" in + -s) subject="$2"; shift 2;; + -t) sendto="$2"; shift 2;; + *) shift 1;; + esac +done + +# If we have a blank subject or a blank receiver, then exit +[ -z "$subject" -o -z "$sendto" ] && exit + +output="" +output=`cat` + +OLD_FS=$IFS +IFS=, +for receiver in $sendto; do + [ -z "$receiver" ] && continue + override=`echo $receiver | cut -d ":" -f 2` + [ -n "$override" ] && receiver=$override + echo "$output" | /bin/mail -s "$subject" $receiver +done +IFS=$OLD_FS