File switch-nagios-theme of Package nagios-theme-switcher
#!/bin/bash
#
# Copyright (C) 2014, SUSE Linux Products GmbH
# Author: Lars Vogdt
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the Novell nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
THEME_DIR='/usr/share/nagios-themes'
NAGIOS_CGI_CFG='/etc/nagios/cgi.cfg'
APACHE_CONFIG='/etc/apache2/conf.d/nagios.conf'
DEFAULT_THEME='nagios'
BACKUP_DIR="$THEME_DIR/backups"
APACHECTL='/etc/init.d/apache2'
SYSTEMCTL='/usr/bin/systemctl'
DO_BACKUP='yes'
DO_ECHO=''
function usage() {
echo
echo "Usage: $(basename $0) [OPTIONS] <theme>"
echo " <theme> : this is the name of the theme "
echo " (same as the directory name in $THEME_DIR)"
echo " (default: $DEFAULT_THEME)"
echo
echo " OPTIONS:"
echo " -h : help (this message)"
echo
echo " -l : list available themes"
echo " -f : do not create backups"
echo " -d : debug (just create backups - do nothing else)"
echo " -b : directory for backup files (default: $BACKUP_DIR)"
echo " -t : use theme directory (default: $THEME_DIR)"
echo " -c : use Nagios cgi.cfg (default: $NAGIOS_CGI_CFG)"
echo " -a : use Apache config (default: $APACHE_CONFIG)"
echo
echo " Currently available themes:"
list_themes
exit $1
}
function get_current_theme(){
set -- `grep ^physical_html_path "$NAGIOS_CGI_CFG" | tail -n1 | sed 's@=@ @' | tr -d '[:cntrl:]'`
shift # remove first ARG
echo $*
}
function make_backup(){
if [ ! -f "$1" ]; then
echo 'ERROR: make_backup() needs at least a valid file' >&2
exit 1
else
ORGFILE="$(basename $1)"
BACKUP=$(date +"%Y%m%d%H%M%S")
if [ -f "$BACKUP_DIR/$ORGFILE-$BACKUP" ]; then
echo "ERROR: $ORGFILE-$BACKUP already exists in $BACKUP_DIR" >&2
fi
cp -f "$1" "$BACKUP_DIR/$ORGFILE-$BACKUP"
fi
}
function list_themes(){
CURRENT=$(basename $(get_current_theme))
for dir in $(ls -d "$THEME_DIR"/*) $DEFAULT_THEME ; do
dir=$(basename $dir)
case $dir in
backups)
continue;
;;
"$CURRENT")
LIST="=> $dir\n$LIST"
;;
*)
LIST="$dir\n$LIST"
;;
esac
done
echo -e "$LIST"
}
if [ ! "$1" ]; then
echo 'ERROR: Please define at least a theme name' >&2
usage 1
fi
while getopts 'lfha:b:c:dt:' OPTION; do
case $OPTION in
h) usage 0
;;
a) APACHE_CONFIG="$OPTARG"
;;
f) DO_BACKUP='no'
;;
l) list_themes;
exit 0;
;;
b) BACKUP_DIR="$OPTARG"
;;
c) NAGIOS_CGI_CFG="$OPTARG"
;;
d) DO_ECHO="echo "
;;
t) THEME_DIR="$OPTARG"
;;
esac
done
shift $(( OPTIND - 1 ))
THEME="$1"
if [ x"$UID" != x"0" ]; then
echo 'ERROR: Only 'root' can run this script' >&2
ERROR=1
fi
if [ x"$THEME" == x"$DEFAULT_THEME" ]; then
NEW_THEME='/usr/share/nagios'
else
NEW_THEME="$THEME_DIR/$THEME"
fi
if [ ! -d "$NEW_THEME" ]; then
echo "ERROR: $NEW_THEME is not a directory" >&2
ERROR=1
fi
if [ ! -r "$NAGIOS_CGI_CFG" ]; then
echo "ERROR: Could not read $NAGIOS_CGI_CFG" >&2
ERROR=1
fi
if [ ! -r "$APACHE_CONFIG" ]; then
echo "ERROR: Could not read $APACHE_CONFIG" >&2
ERROR=1
fi
if [ "$DO_BACKUP" != "no" ]; then
if [ ! -d "$BACKUP_DIR" ]; then
install -d -m700 "$BACKUP_DIR" || ERROR=1
fi
fi
if [ ! -x "$APACHECTL" ]; then
if [ ! -x "$SYSTEMCTL" ]; then
echo "ERROR: Can not restart apache2 - neither $APACHECTL nor $SYSTEMCTL are available" >&2
ERROR=1
fi
fi
if [ -n "$ERROR" ]; then
echo 'ERROR: There is at least one error - exiting' >&2
usage 1
fi
# Switch the physical html path in cgi.cfg
# physical_html_path=/home/nagios
if [ "$DO_BACKUP" != "no" ]; then
make_backup "$NAGIOS_CGI_CFG"
fi
CURRENT_THEME="$(get_current_theme)"
$DO_ECHO sed -i "s|^physical_html_path.*|physical_html_path=$NEW_THEME|g" "$NAGIOS_CGI_CFG"
# (Re-)configure apache, so access to the new directory is permitted
if [ "$DO_BACKUP" != "no" ]; then
make_backup "$APACHE_CONFIG"
fi
$DO_ECHO sed -i "s|^Alias.*/nagios.*$CURRENT_THEME.*|Alias /nagios \"$NEW_THEME\"|; \
s|<Directory.*$CURRENT_THEME.*|<Directory \"$NEW_THEME\">|" "$APACHE_CONFIG"
if [ -x $APACHECTL ]; then
$DO_ECHO $APACHECTL try-restart
elif [ -x $SYSTEMCTL ]; then
$DO_ECHO $SYSTEMCTL try-restart apache2
fi