Command Palette

Search for a command to run...

GitHubGitHub

Nagios Installation Guide

Install Nagios Core 4.5.14 and the Nagios Plugins on Linux, configure hosts and services, and verify the checks that keep your infrastructure honest.

Nagios Core is the original open-source monitoring system. It runs checks on a schedule, shows the current state of every host and service, and alerts you when something breaks — and again when it is fixed.

System Requirements

  • 2 CPU cores (4+ recommended for larger deployments)
  • 2 GB RAM minimum (4 GB+ recommended)
  • 20 GB of free disk space
  • Apache 2.x or Nginx as the web server
  • PHP 7.0 or newer
  • GCC and the standard build tools
  • Reliable network connectivity to everything you monitor

Installation Instructions

Nagios Core is distributed as source, so it is compiled and installed on the host.

Ubuntu / Debian

# Install the dependencies
sudo apt update
sudo apt install -y autoconf automake gcc libc6-dev make libgd-dev libssl-dev \
  libtool pkg-config libxml2-dev libxslt1-dev libcurl4-openssl-dev \
  libltdl-dev libperl-dev libmysqlclient-dev \
  apache2 apache2-utils libapache2-mod-php php php-cli php-gd php-mysql \
  mysql-server unzip wget
 
# Create the nagios user and group
sudo groupadd -r nagios
sudo useradd -r -g nagios -s /sbin/nologin -d /usr/local/nagios nagios
sudo usermod -aG nagios www-data
 
# Download Nagios Core 4.5.14
cd /tmp
curl -LO https://github.com/NagiosEnterprises/nagioscore/archive/nagios-4.5.14.tar.gz
tar xzf nagios-4.5.14.tar.gz
cd nagioscore-nagios-4.5.14
 
# Compile and install
sudo ./configure --with-httpd-conf=/etc/apache2/sites-enabled --with-httpd-user=www-data
sudo make all
sudo make install
sudo make install-daemoninit
sudo make install-commandmode
sudo make install-config
sudo make install-webconf
 
# Install the Nagios Plugins
cd /tmp
curl -LO https://github.com/nagios-plugins/nagios-plugins/archive/release-2.5.tar.gz
tar xzf release-2.5.tar.gz
cd nagios-plugins-release-2.5
sudo ./tools/setup
sudo ./configure
sudo make
sudo make install
 
# Create the web interface user
sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
 
# Enable the Apache modules
sudo a2enmod rewrite
sudo a2enmod cgi
sudo a2enmod php
 
# Start the services
sudo systemctl restart apache2
sudo systemctl enable --now nagios

Configuration

Hosts and Services

Create /usr/local/nagios/etc/objects/hosts.cfg:

define host {
    use                     linux-server
    host_name               web1
    alias                   Web Server 1
    address                 192.168.1.10
    check_command           check-host-alive
    max_check_attempts      5
    check_period            24x7
    notification_period     24x7
    notification_options    d,u,r
    contact                 nagiosadmin
}

Create /usr/local/nagios/etc/objects/services.cfg:

define service {
    host_name               web1
    service_description     HTTP
    check_command           check_http
    max_check_attempts      3
    check_interval          5
    retry_interval          1
    check_period            24x7
    notification_period     24x7
    notification_options    w,u,c,r
    contact                 nagiosadmin
}
 
define service {
    host_name               web1
    service_description     SSH
    check_command           check_ssh
    check_interval          5
    retry_interval          1
    max_check_attempts      3
}

Host Templates

/usr/local/nagios/etc/objects/templates.cfg:

define host {
    name                    linux-server
    use                     generic-host
    check_command           check-host-alive
    check_period            24x7
    notification_period     24x7
    notification_options    d,u,r
    contact                 nagiosadmin
    register                0
}
 
define host {
    name                    generic-host
    notifications_enabled   1
    event_handler_enabled   1
    flap_detection_enabled  1
    process_perf_data       1
    retain_status_information 1
    retain_nonstatus_information 1
    notification_period     24x7
    register                0
}

Verifying the Configuration

# Validate the configuration
sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
 
# Reload after a change (no downtime)
sudo systemctl reload nagios

Verifying the Installation

# The service is running
sudo systemctl status nagios
 
# The plugins work
sudo /usr/local/nagios/libexec/check_dns -H localhost -v
sudo /usr/local/nagios/libexec/check_http -H localhost -v
 
# The daemon is scheduling checks
ps aux | grep nagios

Open http://<server>/nagios and sign in with nagiosadmin and the password you set.

Notifications

Email

# /usr/local/nagios/etc/objects/contacts.cfg
define contact {
    use                     generic-contact
    alias                   Nagios Administrator
    email                   admin@example.com
}

Slack or Teams

define contact {
    use                     generic-contact
    alias                   Slack Notifier
    email                   slack-notifier@example.com
}
# Install a notification command
sudo wget -O /usr/local/nagios/libexec/slack_notifier.py \
  https://raw.githubusercontent.com/slagSHOT/slack_notifier/master/slack_notifier.py
sudo chmod +x /usr/local/nagios/libexec/slack_notifier.py
# /usr/local/nagios/etc/commands.cfg
define command {
    command_name    check_slack
    command_line    /usr/local/nagios/libexec/slack_notifier.py -c /etc/nagios/slack_channel -t "$NOTIFICATIONTYPE$ - $SERVICESTATE$ - $SERVICEDESC$" -m "$SERVICEOUTPUT$"
}

Troubleshooting

Best Practices

  1. Keep the configuration in Git and deploy it — Nagios configuration files are code.
  2. Validate before every reload with nagios -v.
  3. Use host and service templates so a new host takes three lines, not thirty.
  4. Set notification_options deliberately — d,u,r sends down, unreachable and recovery notifications.
  5. Use max_check_attempts above 1 for services so a single blip does not page anyone.
  6. Monitor Nagios itself — a check on the Nagios server from a second host.
  7. Prefer check_by_ssh or NRPE for checks that must run on the target host, so you do not open a network port to every machine.
  8. Prune old performance data with nagios --prune so the database does not grow without bound.
  9. Do not monitor everything — a noisy monitor gets ignored.

Next Steps