Curious Minds

Clear answers to science and history questions

Nagios Monitoring Tutorial: Setup & Basics

· 7 min read · Science

This tutorial explains what Nagios is, how to install and configure it to monitor servers, services, and network devices, and how to interpret the alerts it generates. You will learn the core concepts, step-by-step setup for a basic host and service check, and how to extend Nagios to fit your environment.

What Nagios Actually Does

Nagios is an open-source monitoring system that watches hosts (servers, routers, switches) and services (HTTP, SSH, disk usage, CPU load) and alerts you when something goes wrong or recovers. It runs on a central server, uses plugins to perform checks, and stores results in a log and status database. The core engine is Nagios Core, while Nagios XI is a commercial distribution with a web GUI and extra features. This tutorial focuses on Nagios Core, which is free and still widely deployed.

Core Concepts You Need First

  • Host – a physical or virtual machine (e.g., a web server, a database server).
  • Service – a check run against a host (e.g., ping, HTTP response, disk space).
  • Plugin – an external executable (usually in C, Perl, or shell) that performs the actual check and returns an exit code: 0 OK, 1 WARNING, 2 CRITICAL, 3 UNKNOWN.
  • Command – a definition that maps a plugin to specific arguments.
  • Contact – a person or group to receive notifications (email, SMS, etc.).
  • Notification – an alert sent when a host or service changes state and stays in that state for a defined period.

How Nagios Works (The Check Cycle)

Nagios does not continuously monitor; it runs checks at scheduled intervals. For each host and service, you define a check interval (e.g., every 5 minutes). When a check runs, Nagios executes the plugin, reads the exit code and output, and updates the status. If a check returns CRITICAL, Nagios does not notify immediately – it waits for a configurable number of retries (max_check_attempts) before escalating to a hard state and sending a notification. This prevents alert storms from transient failures.

Nagios also supports passive checks, where an external application submits results to Nagios via a command pipe. This is useful for asynchronous events like backup completions or security alerts. Active checks (the default) are initiated by Nagios itself.

Installation and Basic Setup (Step-by-Step)

These steps are for a Linux server (Ubuntu/Debian or RHEL/CentOS). The exact package names may vary by distribution.

  1. Install Nagios Core and plugins – On Debian/Ubuntu: sudo apt install nagios4 nagios-plugins. On RHEL/CentOS: enable EPEL and run sudo yum install nagios nagios-plugins-all.
  2. Set the admin password – The web interface uses HTTP authentication. Run sudo htpasswd -c /etc/nagios4/htpasswd.users nagiosadmin (path may differ) and enter a strong password.
  3. Start the service – sudo systemctl enable --now nagios (or service nagios start on older systems).
  4. Verify the configuration – Run sudo nagios -v /etc/nagios4/nagios.cfg to check for errors before restarting.
  5. Access the web UI – Open http://your-server/nagios and log in with nagiosadmin.

After installation, you will see the default configuration that monitors localhost. To add a new host, edit the configuration files in /etc/nagios4/conf.d/ (or /etc/nagios/objects/ on RHEL). A typical host definition looks like this:

define host {
    use                     linux-server
    host_name               webserver1
    alias                   Production Web Server
    address                 192.168.1.10
}

Then add a service check for that host:

define service {
    use                     generic-service
    host_name               webserver1
    service_description     HTTP
    check_command           check_http
}

After editing, run the config verification again and restart Nagios to apply changes.

Choosing What to Monitor and How to Configure Checks

Start with the essentials: ping (host alive), SSH (port 22), HTTP/HTTPS, disk usage, CPU load, memory, and running processes. The standard plugins include check_ping, check_ssh, check_http, check_disk, check_load, and check_procs. Each plugin accepts arguments like warning and critical thresholds. For example, check_disk -w 20% -c 10% warns when disk free falls below 20% and becomes critical below 10%.

You can define custom commands in commands.cfg to wrap plugins with specific arguments. For instance:

define command {
    command_name    check_ssh_port
    command_line    $USER1$/check_ssh -p $ARG1$ $HOSTADDRESS$
}

Then use check_command check_ssh_port!2222 in a service definition to check SSH on a non-standard port.

Common Problems and How to Fix Them

  • Checks show UNKNOWN – Usually means the plugin cannot run (missing dependency, wrong path, or permission). Check the Nagios log and run the plugin manually from the command line.
  • Notifications not sent – Verify the contact has a valid email/phone, the service is in a hard state, and notification intervals are set. Also check that the notify-service and notify-host options are enabled in the contact definition.
  • Configuration errors – Always run nagios -v after editing. The most common errors are missing braces, duplicate object names, and undefined commands.
  • High load on the Nagios server – Too many hosts or too frequent checks. Increase check intervals, use passive checks where possible, or distribute checks with Nagios Fusion or a load balancer.
  • Plugins not found – Ensure the plugin path in resource.cfg ($USER1$) points to the correct directory (usually /usr/lib/nagios/plugins).

Nagios vs. Other Monitoring Tools

Nagios Core is a mature, battle-tested tool, but it has a steep learning curve and the configuration is file-based. Alternatives like Zabbix, Prometheus, and Icinga (a fork of Nagios) offer similar functionality with different strengths. Zabbix has a built-in web UI and auto-discovery. Prometheus uses a pull model with a powerful query language (PromQL) and is popular in cloud-native environments. Icinga2 is compatible with Nagios plugins and has a more modern architecture. If you need a simple, agentless solution for a small network, Nagios Core is fine. For large dynamic infrastructures, consider Prometheus or Zabbix.

Cost and Support Considerations

Nagios Core is free and open-source (GPL). You only pay for the hardware to run it and your own time for configuration. Support comes from community forums, documentation, and third-party books. Nagios XI is a paid product (starting around $1,995 per year for 100 nodes) that provides a web-based setup wizard, dashboards, and official support. Many organizations start with Core and later upgrade to XI if they need a GUI or easier management.

Safety and Best Practices

When configuring Nagios, avoid monitoring too aggressively – a check interval of 1 minute for every service on 100 hosts can overload the server. Use a check interval of 5 minutes for most services and 1 minute for critical ones. Always set notification thresholds to avoid alert fatigue; use notification_interval to resend alerts only after a certain period. Ensure the Nagios server itself is monitored (disk space, CPU, memory) because if it goes down, you lose visibility. Also, secure the web interface with HTTPS and restrict access to trusted IPs.

Frequently Asked Questions

How do I add a new host to Nagios?

Create a new host definition in a configuration file (e.g., hosts.cfg) with the host name, alias, address, and the template linux-server or windows-server. Then add service definitions for that host. Validate the config with nagios -v and restart Nagios.

What is the difference between a hard and soft state?

A soft state is a temporary status change that occurs before the maximum number of retries is reached. Nagios will keep checking and if the service recovers, it returns to OK without notifying. A hard state occurs after the retry limit is exceeded, and that is when notifications are sent.

Can Nagios monitor Windows machines?

Yes, but you need an agent like NSClient++ or check_nrpe. The agent runs on the Windows machine and executes checks locally, sending results back to the Nagios server. Alternatively, you can use WMI-based checks, but they are slower and require credentials.

Why is my service showing CRITICAL but no notification is sent?

Check if the service is in a soft state (still within max_check_attempts), if the contact is correctly assigned, if notifications are enabled for that contact and service, and if the notification period matches the current time. Also verify that the email/SMS gateway is working.

How do I monitor a service that requires a specific port?

Use a plugin that supports port specification, such as check_tcp or check_http -p port. Define a custom command with the port as an argument, then use it in the service definition.

Is Nagios still relevant in 2025?

Yes, many organizations still run Nagios Core because it is stable, lightweight, and has a huge ecosystem of plugins. However, for new deployments, tools like Prometheus or Zabbix are often preferred due to easier scaling and modern features. Nagios remains a solid choice for small to medium environments.

In this article
  1. What Nagios Actually Does
  2. Core Concepts You Need First
  3. How Nagios Works (The Check Cycle)
  4. Installation and Basic Setup (Step-by-Step)
  5. Choosing What to Monitor and How to Configure Checks
  6. Common Problems and How to Fix Them
  7. Nagios vs. Other Monitoring Tools
  8. Cost and Support Considerations
  9. Safety and Best Practices
  10. Frequently Asked Questions