#!/bin/bash

if [ $# -lt 1 ]; then
	echo "Please provide centos version (7, 8)"
	exit 1
fi

version=$1

if [ ! -d centos-$version ]; then
	echo "This version does not exist yet"
	exit 1
fi

echo "Copying binaries"
cp -rp centos-$version/nagios /usr/local/
echo "Copying extra plugins"
cp -rp ../extra/* /usr/local/nagios/libexec/

echo "Creating user and group"
groupadd -r nagios
useradd -r -g nagios -d /usr/local/nagios/ nagios

echo "Settings system and rights"
echo >> /etc/services
echo '# Nagios services' >> /etc/services
echo 'nrpe    5666/tcp' >> /etc/services
mkdir /usr/local/nagios/var && chown -R nagios:nagios /usr/local/nagios/var
mkdir /usr/local/nagios/etc
cp ../cfg_examples/armatis-abc.cfg /usr/local/nagios/etc/nrpe.cfg
chown -R nagios:nagios /usr/local/nagios/libexec
chown -R nagios:nagios /usr/local/nagios/etc

firewall-cmd --add-port=5666/tcp --permanent
firewall-cmd --reload

cp sudoers.d/nrpe /etc/sudoers.d/nrpe
cp centos-$version/nrpe.service /lib/systemd/system/nrpe.service
systemctl daemon-reload
systemctl enable nrpe
systemctl start nrpe
systemctl status nrpe

echo "*****************************************************************************"
echo "              Setup finished"
echo "   Please create a config file in /usr/local/nagios/etc/nrpe.cfg"
echo "   You can start nrpe using /etc/init.d/nrpe start or systemctl start nrpe"
echo "*****************************************************************************"
