Nagios 4.4.6 on Ubuntu 20.04
I use Nagios a lot for work to monitor our Linux and Windows servers as well as Ubiquiti equipment. I put together a walkthrough of how I setup Nagios. This is mainly for my documentation, but if you find it useful as well or have any recommendations to make my steps better, please let me know in the comments.
Prerequisites
- A base install of Ubuntu LTS with a static IP address
- When editing a file with Nano, to save your changes to do the following:
- Ctrl+X to Exit
- Y to save the edits currently stored in the buffer
- Enter to write to the current file name
- Download links for Nagios and its plugins (version numbers are at the time of writing)
Software Installation
Update the OS and install prerequisite software
apt update && apt dist-upgrade -y && apt autoremove -y && apt clean
apt install build-essential autoconf openssl libssl-dev libmcrypt-dev dc xinetd python apache2 apache2-utils php7.4 php7.4-gd libapache2-mod-php7.4 libperl-dev libgd-dev perl ssmtp libnet-snmp-perl gettext mailutils snmp unzip
Download and extract the Nagios Core source. We’ll be working in the /tmp directory
curl -L -O https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.6.tar.gz
tar xvzf nagios-4.4.6.tar.gz && cd nagios-4.4.6
Compile and install Nagios Core
./configure --with-httpd-conf=/etc/apache2/sites-enabled
make all
make install-groups-users
make install
make install-daemoninit
make install-commandmode
make install-config
/usr/bin/install -c -m 644 sample-config/httpd.conf /etc/apache2/sites-available/nagios.conf
Download and install Nagios Plugins. We’ll be working in the /tmp directory
curl -L -O http://nagios-plugins.org/download/nagios-plugins-2.3.3.tar.gz
tar xvzf nagios-plugins-2.3.3.tar.gz && cd nagios-plugins-2.3.3
Compile and install Nagios Plugins
./configure
make && make install
Download and move the NCPA plugin
curl -L -O https://assets.nagios.com/downloads/ncpa/check_ncpa.tar.gz
cp /tmp/check_ncpa.py /usr/local/nagios/libexec/.
Configure the Nagios Web Interface
Enable the necessary Apache2 modules
a2enmod cgi rewrite
Give Nagios access to run web commands in Apache2
usermod -a -G nagios www-data
Create the default user password (default user is nagiosadmin). See ### to create additional users
htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
Reload Apache2 for changes to take effect
systemctl reload apache2
Configuration Directories
Configuration directories are listed in the nagios.cfg file located in /usr/local/nagios/etc/. We’ll be modifying the directory structure and moving the directories for our configuration to /opt/nagios/ for easier management. This is optional
Edit the nagios.cfg file and comment the following lines under “You can specify individual objects…” line (comments are made with a # at the beginning of the line)
nano /usr/local/nagios/etc/nagios.cfg
#cfg_file=/usr/local/nagios/etc/objects/contacts.cfg #cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg #cfg_file=/usr/local/nagios/etc/objects/templates.cfg
Add the following under “You can also tell Nagios to process all config files in a particular directory”
cfg_dir=/opt/nagios/servers cfg_dir=/opt/nagios/groups cfg_dir=/opt/nagios/templates cfg_dir=/opt/nagios/commands cfg_dir=/opt/nagios/contacts
Save the changes by pressing Ctrl+X, Y for Yes, and Enter to select the file name to write (by default is the file we have opened)
Create the following directories to house our configuration files
mkdir /opt/nagios/servers mkdir /opt/nagios/groups mkdir /opt/nagios/templates mkdir /opt/nagios/commands mkdir /opt/nagios/contacts
Change the permissions of the directories
chown -R nagios:nagios /usr/local/nagios/etc && chown -R nagios:nagios /opt/nagios
Templates
Templates allow the centralization of settings for each command, host, group, etc… We’ll be adding the following templates:
- Time Periods
- Services
- Servers
Time Period Template
Time Periods allow you to set when a service performs its checks or a contact/group will receive alerts. Create the file and copy the time period info.
nano /opt/nagios/templates/timeperiods.cfg
define timeperiod { name 24x7 timeperiod_name 24x7 alias 24 Hours a Day, 7 Days a Week sunday 00:00-24:00 monday 00:00-24:00 tuesday 00:00-24:00 wednesday 00:00-24:00 thursday 00:00-24:00 friday 00:00-24:00 saturday 00:00-24:00 } define timeperiod { name 7x6 timeperiod_name 7x6 alias 7AM to 6PM monday 07:00-18:00 tuesday 07:00-18:00 wednesday 07:00-18:00 thursday 07:00-18:00 friday 07:00-18:00 }
Save the changes by pressing Ctrl+X, Y for Yes, and Enter to select the file name to write (by default is the file we have opened)
Server Template
nano /opt/nagios/templates/servers-template.cfg
define host { name servers-template check_command check-host-alive check_period 24x7 check_interval 15 ; minutes retry_interval 3 ; minutes max_check_attempts 3 notification_period 7x6 notification_interval 60 ; minutes contact_groups network-admins register 0 }
Save the changes by pressing Ctrl+X, Y for Yes, and Enter to select the file name to write (by default is the file we have opened)
Services Template
nano /opt/nagios/templates/services-template.cfg
define service { name services-template check_period 24x7 check_interval 60 ; minutes retry_interval 3 ; minutes max_check_attempts 3 notification_period 7x6 notification_interval 60 ; minutes notifications_enabled 1 contact_groups network-admins register 0 }
Save the changes by pressing Ctrl+X, Y for Yes, and Enter to select the file name to write (by default is the file we have opened)
The general setup of Nagios is completed. Contacts and Servers will still need to be added and instructions are available in the posts below
- Adding Contacts to Nagios
- Adding Hosts to Nagios
- Nagios NCPA Agent Installation
- Nagios Web Interface AD Authentication
- Backing Up Nagios to Github Automagically
Adding Contacts to Nagios – Inept Tech
February 1, 2022 at 11:12 pm[…] If you don’t already have a Nagios server setup, you can follow my previous post here: Nagios 4.4.6 on Ubuntu 20.04 […]