diff --git a/failover/Makefile b/failover/Makefile index f226a60..71bf5b3 100644 --- a/failover/Makefile +++ b/failover/Makefile @@ -1,8 +1,8 @@ primary: sync - ./failover.sh -P 192.168.56.113 -S 192.168.56.114 -H 192.168.56.113 + ./failover.sh -P nagiosxi-p.princeton.edu -S nagiosxi-f.princeton.edu -H nagiosxi-p.princeton.edu secondary: - ./failover.sh -P 192.168.56.113 -S 192.168.56.114 -H 192.168.56.114 + ./failover.sh -P nagiosxi-p.princeton.edu -S nagiosxi-f.princeton.edu -H nagiosxi-f.princeton.edu sync: - rsync -av --stats ./ secondary:/root/princeton/failover + rsync -av /home/nagios/bin/ nagiosxi-f.princeton.edu:/home/nagios/bin/ diff --git a/failover/colors.sh b/failover/colors.sh new file mode 100755 index 0000000..e282b5f --- /dev/null +++ b/failover/colors.sh @@ -0,0 +1,20 @@ +verbose () { + echo "${color_green}$1${color_normal}" +} +error () { + echo "${color_red}$1${color_normal}" + } +warning () { + echo "${color_yellow}$1${color_normal}" +} +# check for color support +if test -t 1; then + # see if it supports colors... + ncolors=$(tput colors) + if test -n "$ncolors" && test $ncolors -ge 8; then + color_normal="$(tput sgr0)" + color_red="$(tput setaf 1)" + color_green="$(tput setaf 2)" + color_yellow="$(tput setaf 3)" + fi +fi diff --git a/failover/failover.sh b/failover/failover.sh index 8d24628..f44163f 100755 --- a/failover/failover.sh +++ b/failover/failover.sh @@ -15,6 +15,8 @@ syncname="sync" hostname=`/bin/hostname` +. ./colors.sh + while [ -n "$1" ]; do case "$1" in -H | --hostname) hostname="$2"; shift 2;; @@ -26,25 +28,27 @@ while [ -n "$1" ]; do esac done -[ -z "$hostname" ] && echo "Invalid hostname." && exit 2 +[ -z "$hostname" ] && warning "Invalid hostname." && exit 2 do_nothing() { - echo "We are not the primary or secondary. Doing nothing." + warning "We are not the primary or secondary. Doing nothing." exit 1 } do_primary() { - echo "We are the primary. Creating package to send to secondary." + verbose "We are the primary. Creating package to send to secondary." ./rsync_xi.sh --prepend ${syncname} --secondary "${secondary}" cp /usr/local/nagiosxi/html/includes/components/custom-includes/css/header-gradient.css.primary /usr/local/nagiosxi/html/includes/components/custom-includes/css/header-gradient.css } do_secondary() { - echo "We are the secondary. Expanding package from the primary." + verbose "We are the secondary. Expanding package from the primary." syncfile=`ls -tr1 /store/backups/nagiosxi/ | tail -1` - [ -z "$syncfile" ] && echo "No sync file ($syncfile) found." && exit 2 + [ -z "$syncfile" ] && error "No sync file ($syncfile) found." && exit 2 ./restore_xi.sh /store/backups/nagiosxi/$syncfile if [ $? -eq 0 ]; then + verbose "Stopping Nagios services" + sleep 2 ./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} diff --git a/failover/local/usr/local/nagiosxi/html/includes/components/custom-includes/css/header-gradient.css.primary b/failover/local/usr/local/nagiosxi/html/includes/components/custom-includes/css/header-gradient.css.primary index ace83bc..c8274ac 100644 --- a/failover/local/usr/local/nagiosxi/html/includes/components/custom-includes/css/header-gradient.css.primary +++ b/failover/local/usr/local/nagiosxi/html/includes/components/custom-includes/css/header-gradient.css.primary @@ -1,7 +1,7 @@ #header { - background: rgb(59,234,2); - background: -moz-linear-gradient(90deg, rgba(59,234,2,1) 0%, rgba(8,124,8,1) 35%); - background: -webkit-linear-gradient(90deg, rgba(59,234,2,1) 0%, rgba(8,124,8,1) 35%); - background: linear-gradient(90deg, rgba(59,234,2,1) 0%, rgba(8,124,8,1) 35%); - filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#3bea02",endColorstr="#087c08",GradientType=1); + background: rgb(0,0,255); + background: -moz-linear-gradient(90deg, rgba(0,0,255,1) 0%, rgba(0,0,0,1) 50%); + background: -webkit-linear-gradient(90deg, rgba(0,0,255,1) 0%, rgba(0,0,0,1) 50%); + background: linear-gradient(90deg, rgba(0,0,255,1) 0%, rgba(0,0,0,1) 50%); + filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#0000ff",endColorstr="#000000",GradientType=1); } diff --git a/failover/local/usr/local/nagiosxi/html/includes/components/custom-includes/css/header-gradient.css.secondary b/failover/local/usr/local/nagiosxi/html/includes/components/custom-includes/css/header-gradient.css.secondary index dd7f429..489f176 100644 --- a/failover/local/usr/local/nagiosxi/html/includes/components/custom-includes/css/header-gradient.css.secondary +++ b/failover/local/usr/local/nagiosxi/html/includes/components/custom-includes/css/header-gradient.css.secondary @@ -1,7 +1,7 @@ #header { - background: rgb(234,59,2); - background: -moz-linear-gradient(90deg, rgba(234,59,2,1) 0%, rgba(121,51,11,1) 35%); - background: -webkit-linear-gradient(90deg, rgba(234,59,2,1) 0%, rgba(121,51,11,1) 35%); - background: linear-gradient(90deg, rgba(234,59,2,1) 0%, rgba(121,51,11,1) 35%); - filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#ea3b02",endColorstr="#79330b",GradientType=1); + background: rgb(0,255,0); + background: -moz-linear-gradient(90deg, rgba(0,255,0,1) 0%, rgba(0,0,0,1) 50%); + background: -webkit-linear-gradient(90deg, rgba(0,255,0,1) 0%, rgba(0,0,0,1) 50%); + background: linear-gradient(90deg, rgba(0,255,0,1) 0%, rgba(0,0,0,1) 50%); + filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#00ff00",endColorstr="#000000",GradientType=1); } diff --git a/failover/restore_xi.sh b/failover/restore_xi.sh index bd2408b..85ca429 100755 --- a/failover/restore_xi.sh +++ b/failover/restore_xi.sh @@ -7,14 +7,18 @@ # # Restore just needs to restore the MySQL stuff. Everything else was shipped over via rsync +PATH=/home/nagios/bin:/usr/bin:/bin + +. /home/nagios/bin/colors.sh + # Change this password if your root password is different than "nagiosxi" # MySQL root password themysqlpass="nagiosxi" # Make sure we have the backup file if [ $# != 1 ]; then - echo "Usage: $0 " - echo "This script restores your XI system using a previously made Nagios XI backup file." + error "Usage: $0 " + warning "This script restores your XI system using a previously made Nagios XI backup file." exit 1 fi backupfile=$1 @@ -26,12 +30,12 @@ SPECIAL_BACKUP=0 . $BASEDIR/../var/xi-sys.cfg eval $(php $BASEDIR/import_xiconfig.php) -# Must be root -me=`whoami` -if [ $me != "root" ]; then - echo "You must be root to run this script." - exit 1 -fi +## Must be root +#me=`whoami` +#if [ $me != "root" ]; then +# error "You must be root to run this script." +# exit 1 +#fi rootdir=/store/backups/nagiosxi @@ -39,7 +43,7 @@ rootdir=/store/backups/nagiosxi # MAKE SURE BACKUP FILE EXIST ############################## if [ ! -f $backupfile ]; then - echo "Unable to find backup file $backupfile!" + error "Unable to find backup file $backupfile!" exit 1 fi @@ -50,7 +54,7 @@ ts=`date +%s` mydir=${rootdir}/${ts}-restore mkdir -p $mydir if [ ! -d $mydir ]; then - echo "Unable to create restore directory $mydir!" + error "Unable to create restore directory $mydir!" exit 1 fi @@ -67,18 +71,16 @@ backupdir=`pwd` ############################## # SHUTDOWN SERVICES ############################## -echo "Shutting down services..." -$BASEDIR/manage_services.sh stop nagios -$BASEDIR/manage_services.sh stop ndo2db -$BASEDIR/manage_services.sh stop npcd +verbose "Shutting down services..." +sudo $BASEDIR/manage_services.sh stop nagios +sudo $BASEDIR/manage_services.sh stop ndo2db +sudo $BASEDIR/manage_services.sh stop npcd rootdir=/ # RE-IMPORT ALL XI CFG VARS . $BASEDIR/../var/xi-sys.cfg -php $BASEDIR/import_xiconfig.php > $BASEDIR/config.dat -. $BASEDIR/config.dat -rm -f $BASEDIR/config.dat +eval `php $BASEDIR/import_xiconfig.php` # Overwrite the mysqlpass with the hardcoded one at the top mysqlpass="$themysqlpass" @@ -87,7 +89,7 @@ mysqlpass="$themysqlpass" # RESTORE DATABASES ############################## restore_databases() { -echo "Restoring MySQL databases..." +verbose "Restoring MySQL databases..." if [[ "$cfg__db_info__ndoutils__dbserver" == *":"* ]]; then ndoutils_dbport=`echo "$cfg__db_info__ndoutils__dbserver" | cut -f2 -d":"` ndoutils_dbserver=`echo "$cfg__db_info__ndoutils__dbserver" | cut -f1 -d":"` @@ -98,7 +100,7 @@ fi mysql -h "$ndoutils_dbserver" --port="$ndoutils_dbport" -u root --password=$mysqlpass < $backupdir/mysql/nagios.sql res=$? if [ $res != 0 ]; then - echo "Error restoring MySQL database 'nagios' - check the password in this script!" + error "Error restoring MySQL database 'nagios' - check the password in this script!" exit $res; fi if [[ "$cfg__db_info__nagiosql__dbserver" == *":"* ]]; then @@ -111,13 +113,13 @@ fi mysql -h "$nagiosql_dbserver" --port="$nagiosql_dbport" -u root --password=$mysqlpass < $backupdir/mysql/nagiosql.sql res=$? if [ $res != 0 ]; then - echo "Error restoring MySQL database 'nagiosql' - check the password in this script!" + error "Error restoring MySQL database 'nagiosql' - check the password in this script!" exit $res; fi # Only restore PostgresQL if we are still using it if [ "$cfg__db_info__nagiosxi__dbtype" == "pgsql" ]; then service postgresql initdb &>/dev/null || true - echo "Restoring Nagios XI PostgresQL database..." + verbose "Restoring Nagios XI PostgresQL database..." if [ -f /var/lib/pgsql/data/pg_hba.conf ]; then pghba="/var/lib/pgsql/data/pg_hba.conf" cp -pr $pghba $pghba.old @@ -129,20 +131,20 @@ if [ "$cfg__db_info__nagiosxi__dbtype" == "pgsql" ]; then echo "local all all trust host all all 127.0.0.1/32 trust host all all ::1/128 trust" > $pghba - $BASEDIR/manage_services.sh start postgresql + sudo $BASEDIR/manage_services.sh start postgresql sudo -u postgres psql -c "create user nagiosxi with password 'n@gweb';" sudo -u postgres psql -c "create database nagiosxi owner nagiosxi;" - $BASEDIR/manage_services.sh restart postgresql + sudo $BASEDIR/manage_services.sh restart postgresql # Sleep a bit (required so Postgres finishes startup before we connect again) - echo "Sleeping for a few seconds (please wait)..." + verbose "Sleeping for a few seconds (please wait)..." sleep 7 psql -U nagiosxi nagiosxi < $backupdir/pgsql/nagiosxi.sql res=$? if [ $res != 0 ]; then - echo "Error restoring PostgresQL database 'nagiosxi' !" + error "Error restoring PostgresQL database 'nagiosxi' !" exit $res; fi - $BASEDIR/manage_services.sh restart postgresql + sudo $BASEDIR/manage_services.sh restart postgresql if [ "$dist" == "el7" ] || [ "$dist" == "el8" ]; then systemctl enable postgresql.service elif [[ "$distro" == "Ubuntu" ]] || [[ "$distro" == "Debian" ]]; then @@ -160,7 +162,7 @@ host all all ::1/128 trust" > $pghba fi mysql -h "$nagiosql_dbserver" --port="$nagiosql_dbport" -u root --password=$mysqlpass < "DROP TABLE IF EXISTS nagiosxi;" else - echo "Restoring Nagios XI MySQL database..." + verbose "Restoring Nagios XI MySQL database..." if [[ "$cfg__db_info__nagiosxi__dbserver" == *":"* ]]; then nagiosxi_dbport=`echo "$cfg__db_info__nagiosxi__dbserver" | cut -f2 -d":"` nagiosxi_dbserver=`echo "$cfg__db_info__nagiosxi__dbserver" | cut -f1 -d":"` @@ -175,19 +177,19 @@ else mysql -h "$nagiosxi_dbserver" --port="$nagiosxi_dbport" -u root --password=$mysqlpass < $backupdir/mysql/nagiosxi.sql res=$? if [ $res != 0 ]; then - echo "Error restoring MySQL database 'nagiosxi' !" + error "Error restoring MySQL database 'nagiosxi' !" exit $res; fi fi } restore_databases -echo "Restarting services..." -$BASEDIR/manage_services.sh restart mysqld -$BASEDIR/manage_services.sh restart httpd -$BASEDIR/manage_services.sh start npcd -$BASEDIR/manage_services.sh start ndo2db -$BASEDIR/manage_services.sh start nagios +verbose "Restarting services..." +sudo $BASEDIR/manage_services.sh restart mysqld +sudo $BASEDIR/manage_services.sh restart httpd +sudo $BASEDIR/manage_services.sh start npcd +sudo $BASEDIR/manage_services.sh start ndo2db +sudo $BASEDIR/manage_services.sh start nagios rm -rf $mydir rm -f /tmp/nagiosxi.forceinstall @@ -197,7 +199,7 @@ echo "===============" echo "RESTORE COMPLETE" echo "===============" -echo "Restarting Nagios XI" +verbose "Restarting Nagios XI" cd /usr/local/nagiosxi/scripts ./reconfigure_nagios.sh diff --git a/failover/rsync_xi.sh b/failover/rsync_xi.sh index 571afeb..51043c4 100755 --- a/failover/rsync_xi.sh +++ b/failover/rsync_xi.sh @@ -4,6 +4,9 @@ # Copyright (c) 2011-2018 Nagios Enterprises, LLC. All rights reserved. # +PATH=/home/nagios/bin:/usr/bin:/bin +. /home/nagios/bin/colors.sh + BASEDIR=/usr/local/nagiosxi/scripts # FIXME SBLOG="/usr/local/nagiosxi/var/components/scheduledbackups.log" @@ -60,7 +63,7 @@ done # Restart nagios to forcibly update retention.dat echo "Restart nagios to forcibly update retention.dat" -$BASEDIR/manage_services.sh restart nagios +sudo $BASEDIR/manage_services.sh restart nagios sleep 10 if [ -z $rootdir ]; then @@ -93,9 +96,10 @@ mkdir -p $mydir ############################## # BACKUP DIRS ############################## +# sudo rsync -av --rsync-path="sudo /bin/rsync" /usr/local/test/ nagios@secondary:/usr/local/test/ do_rsync() { - echo "Syncing $*..." - rsync -av --delete --delete-after "$1" "${secondary}:${1}" + verbose "Syncing $*..." + sudo rsync -av --delete --delete-after --rsync-path="sudo /bin/rsync" "$1" "nagios@${secondary}:${1}" } # Only backup NagiosQL if it exists @@ -202,7 +206,7 @@ fi ############################## echo "Compressing backup..." tar czfp $name.tar.gz $name -rm -f $name +rm -rf $name # Change ownership chown $nagiosuser:$nagiosgroup $name.tar.gz