Skip to content

Instantly share code, notes, and snippets.

@WildGenie
Last active February 10, 2019 23:03
Show Gist options
  • Save WildGenie/91a8554349fb533b62b779d491286e3e to your computer and use it in GitHub Desktop.
Save WildGenie/91a8554349fb533b62b779d491286e3e to your computer and use it in GitHub Desktop.
Raspbian Nagios Install
#!/bin/bash
#
# Script Name: raspbian-nagios-install.sh
# Arguments: -r = restart, -c = clean
#
# Author: Ben Staker <[email protected]>
# Date: 08/Jan/2017
#
# Description: Installs Nagios.
# Notes: Ensure that apt-get update, upgrade, and dist-upgrade are executed
# first, then reboot before executing this script. See:
# https://gist.github.com/benstaker/a1637cc2b3a8e22ab96be99e398cc677
# Variables
n_admin_name=nagiosadmin
n_admin_password=test
n_filename_core=nagios-4.4.3
n_filename_nrpe=nrpe-3.2.1
n_filename_plugins=nagios-plugins-2.2.1
n_temp_folder=raspbian-nagios-install
n_user_name=nagios
n_user_group=nagcmd
n_user_password=test
n_user_passwordhash=$(echo $n_user_password | openssl passwd -1 -stdin)
# Configure the options
while getopts ":cr" opt; do
case $opt in
c)
echo "Cleaning previous installation..." >&2
systemctl stop nagios
systemctl disable /etc/systemd/system/nagios.service
userdel -r -f $n_user_name
groupdel $n_user_group
rm -rf /tmp/$n_temp_folder
rm -rf /usr/local/nagios
rm /etc/rcS.d/S99nagios
rm /etc/apache2/sites-enabled/nagios.conf
rm /etc/apache2/sites-available/nagios.conf
rm /etc/systemd/system/nagios.service
echo "Cleaned previous installation." >&2
;;
r)
system_restart=true
echo "System will restart upon completion." >&2
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
# Install required packages
apt-get install -y build-essential autoconf gcc libc6 make wget unzip apache2 apache2-utils php libgd-dev autoconf gcc libc6 libmcrypt-dev make libssl-dev wget bc gawk dc build-essential snmp libnet-snmp-perl gettext
# Download and extract Nagios Core + Plugins + NRPE
mkdir -p /tmp/$n_temp_folder
cd /tmp/$n_temp_folder
wget https://assets.nagios.com/downloads/nagioscore/releases/$n_filename_core.tar.gz
wget http://downloads.sourceforge.net/project/nagios/nrpe-3.x/$n_filename_nrpe.tar.gz
wget https://nagios-plugins.org/download/$n_filename_plugins.tar.gz
tar zxvf $n_filename_core.tar.gz
tar zxvf $n_filename_nrpe.tar.gz
tar zxvf $n_filename_plugins.tar.gz
# Create "nagios" user
useradd -m -s /bin/bash $n_user_name
usermod -p $n_user_passwordhash $n_user_name
groupadd $n_user_group
usermod -a -G $n_user_group $n_user_name
usermod -a -G $n_user_group www-data
# Install Nagios Core
cd /tmp/$n_temp_folder/$n_filename_core
./configure --with-command-group=$n_user_group
make all
make install
make install-init
make install-config
make install-commandmode
# Configure Apache with Nagios
install -c -m 644 ./sample-config/httpd.conf /etc/apache2/sites-available/nagios.conf
ln -s /etc/apache2/sites-available/nagios.conf /etc/apache2/sites-enabled/nagios.conf
make install-webconf
htpasswd -b -c /usr/local/nagios/etc/htpasswd.users $n_admin_name $n_admin_password
ln -s /etc/apache2/mods-available/cgi.load /etc/apache2/mods-enabled/cgi.load
ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load
apache2 reload
# Install Nagios NRPE
cd /tmp/$n_temp_folder/$n_filename_nrpe
./configure --enable-command-args --with-nagios-user=$n_user_name --with-nagios-group=$n_user_name --with-ssl=/usr/bin/openssl --with-ssl-lib=/usr/lib/arm-linux-gnueabihf
make all
make install
# TODO: Had issues with below commands; `xinetd` and `daemon-configg`
make install-xinetd
make install-daemon-config
# Install Nagios Plugins
cd /tmp/$n_temp_folder/$n_filename_plugins
./configure --with-nagios-user=$n_user_name --with-nagios-group=$n_user_name
make
make install
# Start Nagios upon startup
ln -s /etc/init.d/nagios /etc/rcS.d/S99nagios
# Create Nagios service
(
echo "[Unit]"
echo "Description=Nagios"
echo "BindTo=network.target"
echo ""
echo "[Install]"
echo "WantedBy=multi-user.target"
echo ""
echo "[Service]"
echo "User=$n_user_name"
echo "Group=$n_user_name"
echo "Type=simple"
echo "ExecStart=/usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.cfg"
)>/etc/systemd/system/nagios.service
systemctl enable /etc/systemd/system/nagios.service
systemctl start nagios
# Cleanup
rm -rf /tmp/$n_temp_folder
# Restart the system
if [ "$system_restart" = true ]; then
sudo reboot
else
echo "Nagios installation nearly complete, please modify `/etc/xinetd.d/nrpe` to include your IP Addresses."
echo "Once added, restart xinetd using `sudo service xinetd restart`"
echo "You can run `systemctl status nagios` and `/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg` for debugging."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment