Last active
June 20, 2017 18:45
-
-
Save davecozz/fb94e2e68eb8709c7ff0d654b754e677 to your computer and use it in GitHub Desktop.
Install Nagios4 on Ubuntu 16.x and probably other Debian Jessie -based OS's. Works on Raspberry Pi too!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Downloads and installs Nagios4, Plugins, and NRPE | |
# https://gist.github.com/davecozz/fb94e2e68eb8709c7ff0d654b754e677 | |
set -e | |
## check latest version here: https://sourceforge.net/projects/nagios/files/nagios-4.x/ | |
NAGIOS_SOURCE_URL=http://downloads.sourceforge.net/project/nagios/nagios-4.x/nagios-4.2.4/nagios-4.2.4.tar.gz | |
## check latest version here: https://www.nagios.org/downloads/nagios-plugins/ | |
NAGIOS_PLUGINS_URL=https://nagios-plugins.org/download/nagios-plugins-2.1.4.tar.gz | |
## check latest version here: https://github.com/NagiosEnterprises/nrpe/releases/ | |
NAGIOS_NRPE_URL=https://github.com/NagiosEnterprises/nrpe/releases/download/3.0.1/nrpe-3.0.1.tar.gz | |
NAGIOS_SOURCE_DIR=/usr/local/src/nagios4 | |
NAGIOS_TMP_DIR=/tmp/nagios4 | |
NAGIOS_PREREQ_PKGS="build-essential libgd2-xpm-dev openssl libssl-dev xinetd apache2 apache2-utils libapache2-mod-php curl unzip" | |
NAGIOS_USER=nagios | |
NAGIOS_GROUP=nagcmd | |
APACHE_DIR=/etc/apache2 | |
APACHE_SITES_DIR=${APACHE_DIR}/sites-available | |
APACHE_ENABLED_DIR=${APACHE_DIR}/sites-enabled | |
CPU_CORES=`nproc` | |
apt-get update && apt-get install --yes $NAGIOS_PREREQ_PKGS | |
useradd $NAGIOS_USER || true | |
groupadd $NAGIOS_GROUP || true | |
usermod --append --groups $NAGIOS_GROUP $NAGIOS_USER | |
usermod --groups $NAGIOS_GROUP www-data | |
a2enmod rewrite | |
a2enmod cgi | |
curl --location --create-dirs --output ${NAGIOS_TMP_DIR}/nagios4.tar.gz $NAGIOS_SOURCE_URL | |
mkdir --parents $NAGIOS_SOURCE_DIR | |
tar xvzf ${NAGIOS_TMP_DIR}/nagios4.tar.gz --directory $NAGIOS_SOURCE_DIR | |
cd ${NAGIOS_SOURCE_DIR}/nagios-4* | |
./configure --with-nagios-group=$NAGIOS_USER --with-command-group=$NAGIOS_GROUP --with-httpd-conf=$APACHE_SITES_DIR | |
make -j${CPU_CORES} all | |
make install | |
make install-commandmode | |
make install-init | |
make install-config | |
make install-webconf | |
ln -s ${APACHE_SITES_DIR}/nagios.conf ${APACHE_ENABLED_DIR}/ | |
cd - | |
NAGIOS_HTPASSWD_FILE=`grep AuthUserFile ${APACHE_SITES_DIR}/nagios.conf | tail -1 | awk '{print $NF}'` | |
tee /etc/systemd/system/nagios.service <<-'EOF' | |
[Unit] | |
Description=Nagios | |
BindTo=network.target | |
[Install] | |
WantedBy=multi-user.target | |
[Service] | |
User=nagios | |
Group=nagios | |
Type=simple | |
ExecStart=/usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.cfg | |
EOF | |
echo "-----------------" | |
read -p "Install Nagios Plugins? (Y/n) " yn | |
if [ -z "$yn" ] || [ "$yn" = "y" ] || [ "$yn" = "Y" ]; then | |
curl --location --create-dirs --output ${NAGIOS_TMP_DIR}/nagios_plugins.tar.gz $NAGIOS_PLUGINS_URL | |
tar xvzf ${NAGIOS_TMP_DIR}/nagios_plugins.tar.gz --directory $NAGIOS_SOURCE_DIR | |
cd ${NAGIOS_SOURCE_DIR}/nagios-plugins-* | |
./configure --with-nagios-user=$NAGIOS_USER --with-nagios-group=$NAGIOS_GROUP --with-openssl | |
make -j${CPU_CORES} all | |
make install | |
cd - | |
else | |
echo "Not installing plugins." | |
fi | |
echo "-----------------" | |
read -p "Install NRPE? (Y/n) " yn | |
if [ -z "$yn" ] || [ "$yn" = "y" ] || [ "$yn" = "Y" ]; then | |
curl --location --create-dirs --output ${NAGIOS_TMP_DIR}/nagios_nrpe.tar.gz $NAGIOS_NRPE_URL | |
tar xvzf ${NAGIOS_TMP_DIR}/nagios_nrpe.tar.gz --directory $NAGIOS_SOURCE_DIR | |
ARCH=`arch` | |
if [ `echo $ARCH | grep x86_64` ]; then | |
SSL_LIB=/usr/lib/x86_64-linux-gnu | |
elif [ `echo $ARCH | grep arm` ]; then | |
SSL_LIB=/usr/lib/arm-linux-gnueabihf | |
else | |
echo "UNABLE TO DETERMINE ARCHITECTURE, NEXT STEP WILL PROBABLY FAIL!" | |
fi | |
cd ${NAGIOS_SOURCE_DIR}/nrpe-* | |
./configure --enable-command-args --with-nagios-user=$NAGIOS_USER --with-nagios-group=$NAGIOS_GROUP --with-ssl=/usr/bin/openssl --with-ssl-lib=$SSL_LIB | |
make -j${CPU_CORES} all | |
make install | |
make install-config | |
make install-init | |
cd - | |
systemctl enable nrpe | |
systemctl restart nrpe | |
else | |
echo "Not installing NRPE." | |
fi | |
systemctl enable apache2 | |
systemctl enable nagios | |
systemctl restart apache2 | |
systemctl restart nagios | |
echo "" | |
echo "" | |
NAGIOS_HTPASSWD_DIR=`dirname $NAGIOS_HTPASSWD_FILE || true` | |
if [ -d $NAGIOS_HTPASSWD_DIR ]; then | |
echo "Please type a password for the 'nagiosadmin' user." | |
htpasswd -c $NAGIOS_HTPASSWD_FILE nagiosadmin | |
else | |
echo "Could not determine the dir to contain the htpasswd file to use for 'nagiosadmin' auth (I got this: ${NAGIOS_HTPASSWD_DIR})." | |
echo "Please set the password for 'nagiosadmin' manually!" | |
fi | |
echo "" | |
echo "DONE!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Install NRPE should be from https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-3.1.1/nrpe-3.1.1.tar.gz