Skip to main content

nagios initial setup

I recall first installing Nagios many years ago and having a lot if issues, happily things have improved a lot including some excellent documentation. Below outlines the steps to get my initial installation up and running, I will add a follow up post later when I've got everything I want to monitor added, and have the final structure configured.

VM setup.

  • Installed a Ubuntu VM using current LTS Server iso (ubuntu-20.04.3-live-server-amd64), initial configuration of 1vCPU, 2GB RAM, 25GB Disk
  • Copy over SSH key and disable both root and password authentication in sshd_config
  • Run updates and reboot

For installation of Nagios I'm using the Ubuntu Easy Setup steps available. I'm just listing the commands below that worked for my specific install, including any modifications I had to make.

Install Nagios.

sudo apt-get install -y autoconf gcc libc6 make wget unzip apache2 php libapache2-mod-php7.4 libgd-dev
mkdir nagios-dl && cd nagios-dl
wget -O nagioscore.tar.gz https://github.com/NagiosEnterprises/nagioscore/archive/nagios-4.4.6.tar.gz
tar xzf nagioscore.tar.gz

cd nagioscore-nagios-4.4.6
sudo ./configure --with-httpd-conf=/etc/apache2/sites-enabled
sudo make all
sudo make install-groups-users
sudo usermod -a -G nagios www-data

sudo make install
sudo make install-daemoninit

sudo make install-commandmode
sudo make install-config

sudo make install-webconf
sudo a2enmod rewrite
sudo a2enmod cgi
sudo ufw allow Apache
sudo ufw reload
sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagios-user
Edit username in /usr/local/nagios/etc/cgi.cfg to match user created above ## if different from default
sudo systemctl start nagios.service

Access url at http://192.168.50.183/nagios/ to make sure it's running.

Install Nagios Plugins.

cd ~/nagios-dl
sudo apt-get install -y autoconf gcc libc6 libmcrypt-dev make libssl-dev wget bc gawk dc build-essential snmp libnet-snmp-perl gettext
tar zxf nagios-plugins.tar.gz
cd nagios-plugins-release-2.3.3

sudo ./tools/setup
sudo ./configure
sudo make
sudo make install

Nagios should now be installed and running and you can view the default monitoring in place for localhost.

Setup email settings for sending out alerts.

Updated admin contact in /usr/local/nagios/etc/objects/contacts.cfg with my email address so alerts will be received. I setup an App password on a gmail account to use as the sender.

sudo apt install ssmtp mailutils 
sudo vi /etc/ssmtp/ssmtp.conf
sudo chown root:nagios /etc/ssmtp/ssmtp.conf
sudo chmod 640 /etc/ssmtp/ssmtp.conf

Next I needed to change the path to mail in sudo vi /usr/local/nagios/etc/objects/commands.cfg there were two instances for me. Restart nagios service once the changes are made and don't forget to check SPAM for the alerts!

Add a device to monitor.

Initially I want to monitor a handful of internal devices and my website so I'm going to create a few config files.

To monitor my www I created /usr/local/nagios/etc/objects/remotehosts.cfg and entered the following (I can add additional remote hosts to this config file if required):

define host {
    use         generic-host
    host_name   gerryr.com
    alias       gerryr.com
    address     5.79.70.215
    check_command       check_http
    max_check_attempts  5
}

To monitor http on my server I create a config file /usr/local/nagios/etc/objects/svc-http.cfgfor that service (I can add additional hosts to the service config file later if I want to monitor http on them)

define service {
    use                 generic-service
    host_name           gerryr.com
    service_description HTTP
    check_command       check_http
}

I won't have a lot of devices or services to monitor so I will just store them all in the objects directory so I commented out the direct references and added the line below that to the /usr/local/nagios/etc/nagios.cfg config file.

Now, because we've added some config files we need to verify the configs and assuming there are no errors, restart the service. You need to do that every time you add/remove/edit a .cfg file.

sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
sudo systemctl restart nagios

So thats the initial setup and alerting configured, I will follow up with another post once I get all my hosts added and decide on the grouping and services I want added to the config files.