Failover seems to work without root

2021
Eric Loyd 6 years ago
parent c5d35bacde
commit e6fcff099e

@ -1,8 +1,8 @@
primary: sync 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: 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: sync:
rsync -av --stats ./ secondary:/root/princeton/failover rsync -av /home/nagios/bin/ nagiosxi-f.princeton.edu:/home/nagios/bin/

@ -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

@ -15,6 +15,8 @@ syncname="sync"
hostname=`/bin/hostname` hostname=`/bin/hostname`
. ./colors.sh
while [ -n "$1" ]; do while [ -n "$1" ]; do
case "$1" in case "$1" in
-H | --hostname) hostname="$2"; shift 2;; -H | --hostname) hostname="$2"; shift 2;;
@ -26,25 +28,27 @@ while [ -n "$1" ]; do
esac esac
done done
[ -z "$hostname" ] && echo "Invalid hostname." && exit 2 [ -z "$hostname" ] && warning "Invalid hostname." && exit 2
do_nothing() { do_nothing() {
echo "We are not the primary or secondary. Doing nothing." warning "We are not the primary or secondary. Doing nothing."
exit 1 exit 1
} }
do_primary() { 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}" ./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 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() { 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` 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 ./restore_xi.sh /store/backups/nagiosxi/$syncfile
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
verbose "Stopping Nagios services"
sleep 2
./nagios_startstop.sh stop ./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 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} rm -f ${syncfile}

@ -1,7 +1,7 @@
#header { #header {
background: rgb(59,234,2); background: rgb(0,0,255);
background: -moz-linear-gradient(90deg, rgba(59,234,2,1) 0%, rgba(8,124,8,1) 35%); background: -moz-linear-gradient(90deg, rgba(0,0,255,1) 0%, rgba(0,0,0,1) 50%);
background: -webkit-linear-gradient(90deg, rgba(59,234,2,1) 0%, rgba(8,124,8,1) 35%); background: -webkit-linear-gradient(90deg, rgba(0,0,255,1) 0%, rgba(0,0,0,1) 50%);
background: linear-gradient(90deg, rgba(59,234,2,1) 0%, rgba(8,124,8,1) 35%); background: linear-gradient(90deg, rgba(0,0,255,1) 0%, rgba(0,0,0,1) 50%);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#3bea02",endColorstr="#087c08",GradientType=1); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#0000ff",endColorstr="#000000",GradientType=1);
} }

@ -1,7 +1,7 @@
#header { #header {
background: rgb(234,59,2); background: rgb(0,255,0);
background: -moz-linear-gradient(90deg, rgba(234,59,2,1) 0%, rgba(121,51,11,1) 35%); background: -moz-linear-gradient(90deg, rgba(0,255,0,1) 0%, rgba(0,0,0,1) 50%);
background: -webkit-linear-gradient(90deg, rgba(234,59,2,1) 0%, rgba(121,51,11,1) 35%); background: -webkit-linear-gradient(90deg, rgba(0,255,0,1) 0%, rgba(0,0,0,1) 50%);
background: linear-gradient(90deg, rgba(234,59,2,1) 0%, rgba(121,51,11,1) 35%); background: linear-gradient(90deg, rgba(0,255,0,1) 0%, rgba(0,0,0,1) 50%);
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#ea3b02",endColorstr="#79330b",GradientType=1); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#00ff00",endColorstr="#000000",GradientType=1);
} }

@ -7,14 +7,18 @@
# #
# Restore just needs to restore the MySQL stuff. Everything else was shipped over via rsync # 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" # Change this password if your root password is different than "nagiosxi"
# MySQL root password # MySQL root password
themysqlpass="nagiosxi" themysqlpass="nagiosxi"
# Make sure we have the backup file # Make sure we have the backup file
if [ $# != 1 ]; then if [ $# != 1 ]; then
echo "Usage: $0 <backupfile>" error "Usage: $0 <backupfile>"
echo "This script restores your XI system using a previously made Nagios XI backup file." warning "This script restores your XI system using a previously made Nagios XI backup file."
exit 1 exit 1
fi fi
backupfile=$1 backupfile=$1
@ -26,12 +30,12 @@ SPECIAL_BACKUP=0
. $BASEDIR/../var/xi-sys.cfg . $BASEDIR/../var/xi-sys.cfg
eval $(php $BASEDIR/import_xiconfig.php) eval $(php $BASEDIR/import_xiconfig.php)
# Must be root ## Must be root
me=`whoami` #me=`whoami`
if [ $me != "root" ]; then #if [ $me != "root" ]; then
echo "You must be root to run this script." # error "You must be root to run this script."
exit 1 # exit 1
fi #fi
rootdir=/store/backups/nagiosxi rootdir=/store/backups/nagiosxi
@ -39,7 +43,7 @@ rootdir=/store/backups/nagiosxi
# MAKE SURE BACKUP FILE EXIST # MAKE SURE BACKUP FILE EXIST
############################## ##############################
if [ ! -f $backupfile ]; then if [ ! -f $backupfile ]; then
echo "Unable to find backup file $backupfile!" error "Unable to find backup file $backupfile!"
exit 1 exit 1
fi fi
@ -50,7 +54,7 @@ ts=`date +%s`
mydir=${rootdir}/${ts}-restore mydir=${rootdir}/${ts}-restore
mkdir -p $mydir mkdir -p $mydir
if [ ! -d $mydir ]; then if [ ! -d $mydir ]; then
echo "Unable to create restore directory $mydir!" error "Unable to create restore directory $mydir!"
exit 1 exit 1
fi fi
@ -67,18 +71,16 @@ backupdir=`pwd`
############################## ##############################
# SHUTDOWN SERVICES # SHUTDOWN SERVICES
############################## ##############################
echo "Shutting down services..." verbose "Shutting down services..."
$BASEDIR/manage_services.sh stop nagios sudo $BASEDIR/manage_services.sh stop nagios
$BASEDIR/manage_services.sh stop ndo2db sudo $BASEDIR/manage_services.sh stop ndo2db
$BASEDIR/manage_services.sh stop npcd sudo $BASEDIR/manage_services.sh stop npcd
rootdir=/ rootdir=/
# RE-IMPORT ALL XI CFG VARS # RE-IMPORT ALL XI CFG VARS
. $BASEDIR/../var/xi-sys.cfg . $BASEDIR/../var/xi-sys.cfg
php $BASEDIR/import_xiconfig.php > $BASEDIR/config.dat eval `php $BASEDIR/import_xiconfig.php`
. $BASEDIR/config.dat
rm -f $BASEDIR/config.dat
# Overwrite the mysqlpass with the hardcoded one at the top # Overwrite the mysqlpass with the hardcoded one at the top
mysqlpass="$themysqlpass" mysqlpass="$themysqlpass"
@ -87,7 +89,7 @@ mysqlpass="$themysqlpass"
# RESTORE DATABASES # RESTORE DATABASES
############################## ##############################
restore_databases() { restore_databases() {
echo "Restoring MySQL databases..." verbose "Restoring MySQL databases..."
if [[ "$cfg__db_info__ndoutils__dbserver" == *":"* ]]; then if [[ "$cfg__db_info__ndoutils__dbserver" == *":"* ]]; then
ndoutils_dbport=`echo "$cfg__db_info__ndoutils__dbserver" | cut -f2 -d":"` ndoutils_dbport=`echo "$cfg__db_info__ndoutils__dbserver" | cut -f2 -d":"`
ndoutils_dbserver=`echo "$cfg__db_info__ndoutils__dbserver" | cut -f1 -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 mysql -h "$ndoutils_dbserver" --port="$ndoutils_dbport" -u root --password=$mysqlpass < $backupdir/mysql/nagios.sql
res=$? res=$?
if [ $res != 0 ]; then 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; exit $res;
fi fi
if [[ "$cfg__db_info__nagiosql__dbserver" == *":"* ]]; then 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 mysql -h "$nagiosql_dbserver" --port="$nagiosql_dbport" -u root --password=$mysqlpass < $backupdir/mysql/nagiosql.sql
res=$? res=$?
if [ $res != 0 ]; then 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; exit $res;
fi fi
# Only restore PostgresQL if we are still using it # Only restore PostgresQL if we are still using it
if [ "$cfg__db_info__nagiosxi__dbtype" == "pgsql" ]; then if [ "$cfg__db_info__nagiosxi__dbtype" == "pgsql" ]; then
service postgresql initdb &>/dev/null || true 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 if [ -f /var/lib/pgsql/data/pg_hba.conf ]; then
pghba="/var/lib/pgsql/data/pg_hba.conf" pghba="/var/lib/pgsql/data/pg_hba.conf"
cp -pr $pghba $pghba.old cp -pr $pghba $pghba.old
@ -129,20 +131,20 @@ if [ "$cfg__db_info__nagiosxi__dbtype" == "pgsql" ]; then
echo "local all all trust echo "local all all trust
host all all 127.0.0.1/32 trust host all all 127.0.0.1/32 trust
host all all ::1/128 trust" > $pghba 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 user nagiosxi with password 'n@gweb';"
sudo -u postgres psql -c "create database nagiosxi owner nagiosxi;" 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) # 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 sleep 7
psql -U nagiosxi nagiosxi < $backupdir/pgsql/nagiosxi.sql psql -U nagiosxi nagiosxi < $backupdir/pgsql/nagiosxi.sql
res=$? res=$?
if [ $res != 0 ]; then if [ $res != 0 ]; then
echo "Error restoring PostgresQL database 'nagiosxi' !" error "Error restoring PostgresQL database 'nagiosxi' !"
exit $res; exit $res;
fi fi
$BASEDIR/manage_services.sh restart postgresql sudo $BASEDIR/manage_services.sh restart postgresql
if [ "$dist" == "el7" ] || [ "$dist" == "el8" ]; then if [ "$dist" == "el7" ] || [ "$dist" == "el8" ]; then
systemctl enable postgresql.service systemctl enable postgresql.service
elif [[ "$distro" == "Ubuntu" ]] || [[ "$distro" == "Debian" ]]; then elif [[ "$distro" == "Ubuntu" ]] || [[ "$distro" == "Debian" ]]; then
@ -160,7 +162,7 @@ host all all ::1/128 trust" > $pghba
fi fi
mysql -h "$nagiosql_dbserver" --port="$nagiosql_dbport" -u root --password=$mysqlpass < "DROP TABLE IF EXISTS nagiosxi;" mysql -h "$nagiosql_dbserver" --port="$nagiosql_dbport" -u root --password=$mysqlpass < "DROP TABLE IF EXISTS nagiosxi;"
else else
echo "Restoring Nagios XI MySQL database..." verbose "Restoring Nagios XI MySQL database..."
if [[ "$cfg__db_info__nagiosxi__dbserver" == *":"* ]]; then if [[ "$cfg__db_info__nagiosxi__dbserver" == *":"* ]]; then
nagiosxi_dbport=`echo "$cfg__db_info__nagiosxi__dbserver" | cut -f2 -d":"` nagiosxi_dbport=`echo "$cfg__db_info__nagiosxi__dbserver" | cut -f2 -d":"`
nagiosxi_dbserver=`echo "$cfg__db_info__nagiosxi__dbserver" | cut -f1 -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 mysql -h "$nagiosxi_dbserver" --port="$nagiosxi_dbport" -u root --password=$mysqlpass < $backupdir/mysql/nagiosxi.sql
res=$? res=$?
if [ $res != 0 ]; then if [ $res != 0 ]; then
echo "Error restoring MySQL database 'nagiosxi' !" error "Error restoring MySQL database 'nagiosxi' !"
exit $res; exit $res;
fi fi
fi fi
} }
restore_databases restore_databases
echo "Restarting services..." verbose "Restarting services..."
$BASEDIR/manage_services.sh restart mysqld sudo $BASEDIR/manage_services.sh restart mysqld
$BASEDIR/manage_services.sh restart httpd sudo $BASEDIR/manage_services.sh restart httpd
$BASEDIR/manage_services.sh start npcd sudo $BASEDIR/manage_services.sh start npcd
$BASEDIR/manage_services.sh start ndo2db sudo $BASEDIR/manage_services.sh start ndo2db
$BASEDIR/manage_services.sh start nagios sudo $BASEDIR/manage_services.sh start nagios
rm -rf $mydir rm -rf $mydir
rm -f /tmp/nagiosxi.forceinstall rm -f /tmp/nagiosxi.forceinstall
@ -197,7 +199,7 @@ echo "==============="
echo "RESTORE COMPLETE" echo "RESTORE COMPLETE"
echo "===============" echo "==============="
echo "Restarting Nagios XI" verbose "Restarting Nagios XI"
cd /usr/local/nagiosxi/scripts cd /usr/local/nagiosxi/scripts
./reconfigure_nagios.sh ./reconfigure_nagios.sh

@ -4,6 +4,9 @@
# Copyright (c) 2011-2018 Nagios Enterprises, LLC. All rights reserved. # 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 BASEDIR=/usr/local/nagiosxi/scripts
# FIXME # FIXME
SBLOG="/usr/local/nagiosxi/var/components/scheduledbackups.log" SBLOG="/usr/local/nagiosxi/var/components/scheduledbackups.log"
@ -60,7 +63,7 @@ done
# Restart nagios to forcibly update retention.dat # Restart nagios to forcibly update retention.dat
echo "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 sleep 10
if [ -z $rootdir ]; then if [ -z $rootdir ]; then
@ -93,9 +96,10 @@ mkdir -p $mydir
############################## ##############################
# BACKUP DIRS # BACKUP DIRS
############################## ##############################
# sudo rsync -av --rsync-path="sudo /bin/rsync" /usr/local/test/ nagios@secondary:/usr/local/test/
do_rsync() { do_rsync() {
echo "Syncing $*..." verbose "Syncing $*..."
rsync -av --delete --delete-after "$1" "${secondary}:${1}" sudo rsync -av --delete --delete-after --rsync-path="sudo /bin/rsync" "$1" "nagios@${secondary}:${1}"
} }
# Only backup NagiosQL if it exists # Only backup NagiosQL if it exists
@ -202,7 +206,7 @@ fi
############################## ##############################
echo "Compressing backup..." echo "Compressing backup..."
tar czfp $name.tar.gz $name tar czfp $name.tar.gz $name
rm -f $name rm -rf $name
# Change ownership # Change ownership
chown $nagiosuser:$nagiosgroup $name.tar.gz chown $nagiosuser:$nagiosgroup $name.tar.gz

Loading…
Cancel
Save