Nagios Core Monitoring

Level: Advanced Module: Monitoring & High Availability 9 min read Lesson 62 of 66

Overview

  • What you’ll learn: How to install, configure, and operate Nagios Core on Ubuntu Server for centralized monitoring of hosts, services, and network devices, including plugin architecture, notification rules, and web interface navigation.
  • Prerequisites: System Monitoring Fundamentals (Lesson 61), Apache or Nginx web server basics
  • Estimated reading time: 20 minutes

Introduction

Nagios Core is one of the most established open-source monitoring systems in the industry. Originally released in 1999 as NetSaint, Nagios has evolved into a powerful framework for monitoring infrastructure components including servers, switches, applications, and services. It operates on a simple but effective model: periodically execute check plugins, compare results against defined thresholds, and trigger notifications when problems are detected.

While newer monitoring platforms like Prometheus and Zabbix have emerged, Nagios remains widely deployed in enterprise environments. Understanding Nagios architecture gives you transferable knowledge applicable to any monitoring system, as the core concepts — hosts, services, checks, thresholds, and notifications — are universal.

In this lesson, we will install Nagios Core from source on Ubuntu Server, configure host and service monitoring, create custom check commands, set up email notifications, and navigate the Nagios web interface for incident management.

Nagios Architecture

Nagios Core follows a modular architecture built around four key components:

  • Nagios Daemon: The core scheduling engine that orchestrates check execution, processes results, and manages notifications.
  • Check Plugins: External programs that perform the actual monitoring checks. Nagios itself does not know how to check anything — it delegates all checks to plugins and interprets their exit codes.
  • Configuration Files: Object definitions that describe what to monitor (hosts, services), how to monitor (commands, time periods), and whom to notify (contacts, contact groups).
  • Web Interface (CGIs): A browser-based dashboard that displays current status, historical data, and allows operators to acknowledge problems and schedule downtime.

Nagios plugins communicate through a standardized interface based on exit codes:

  • 0 — OK (service is functioning normally)
  • 1 — WARNING (service is degraded but still functional)
  • 2 — CRITICAL (service has failed or is in a critical state)
  • 3 — UNKNOWN (the check could not determine the service state)

Installing Nagios Core on Ubuntu

We will compile Nagios Core from source to get the latest stable version and full control over the installation.

# Install build dependencies
$ sudo apt update
$ sudo apt install -y autoconf gcc libc6 make wget unzip 
    apache2 php libapache2-mod-php libgd-dev openssl libssl-dev

# Create Nagios user and group
$ sudo useradd nagios
$ sudo groupadd nagcmd
$ sudo usermod -aG nagcmd nagios
$ sudo usermod -aG nagcmd www-data

# Download and compile Nagios Core
$ cd /tmp
$ wget https://github.com/NagiosEnterprises/nagioscore/releases/download/nagios-4.5.1/nagios-4.5.1.tar.gz
$ tar xzf nagios-4.5.1.tar.gz
$ cd nagios-4.5.1
$ ./configure --with-httpd-conf=/etc/apache2/sites-enabled
$ make all

# Install Nagios components
$ sudo make install
$ sudo make install-daemoninit
$ sudo make install-commandmode
$ sudo make install-config
$ sudo make install-webconf

# Create Nagios web admin user
$ sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

# Enable Apache modules and restart
$ sudo a2enmod rewrite cgi
$ sudo systemctl restart apache2

# Start Nagios
$ sudo systemctl enable --now nagios

Installing Nagios Plugins

The monitoring-plugins package provides the standard set of check plugins that Nagios uses to monitor hosts and services.

# Download and compile standard plugins
$ cd /tmp
$ wget https://nagios-plugins.org/download/nagios-plugins-2.4.8.tar.gz
$ tar xzf nagios-plugins-2.4.8.tar.gz
$ cd nagios-plugins-2.4.8
$ ./configure --with-nagios-user=nagios --with-nagios-group=nagios
$ make
$ sudo make install

# Verify plugins are installed
$ ls /usr/local/nagios/libexec/
check_apt    check_disk   check_http   check_load   check_ping
check_procs  check_ssh    check_swap   check_tcp    check_users

# Test a plugin manually
$ /usr/local/nagios/libexec/check_load -w 5,4,3 -c 10,8,6
OK - load average: 0.42, 0.38, 0.35|load1=0.420;5.000;10.000;0; load5=0.380;4.000;8.000;0; load15=0.350;3.000;6.000;0;

Configuring Hosts and Services

Nagios configuration is organized into object definition files. The main configuration file is /usr/local/nagios/etc/nagios.cfg, which references directories containing host, service, and command definitions.

# Enable the servers configuration directory
$ sudo nano /usr/local/nagios/etc/nagios.cfg
# Uncomment or add:
cfg_dir=/usr/local/nagios/etc/servers

# Create the servers directory
$ sudo mkdir -p /usr/local/nagios/etc/servers

# Define a host — web server example
$ sudo tee /usr/local/nagios/etc/servers/webserver01.cfg > /dev/null <<'EOF'
define host {
    use                 linux-server
    host_name           webserver01
    alias               Production Web Server 01
    address             192.168.1.101
    max_check_attempts  5
    check_period        24x7
    notification_interval  30
    notification_period    24x7
}

define service {
    use                 generic-service
    host_name           webserver01
    service_description HTTP
    check_command       check_http
    check_interval      5
    retry_interval      1
    max_check_attempts  3
    notification_interval  30
}

define service {
    use                 generic-service
    host_name           webserver01
    service_description SSH
    check_command       check_ssh
    check_interval      5
    retry_interval      1
    max_check_attempts  3
}

define service {
    use                 generic-service
    host_name           webserver01
    service_description Disk Usage
    check_command       check_nrpe!check_disk
    check_interval      15
    retry_interval      5
    max_check_attempts  3
}

define service {
    use                 generic-service
    host_name           webserver01
    service_description CPU Load
    check_command       check_nrpe!check_load
    check_interval      5
    retry_interval      1
    max_check_attempts  3
}
EOF

# Validate the configuration
$ sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg

# Reload Nagios to apply changes
$ sudo systemctl reload nagios

NRPE — Remote Host Monitoring

The Nagios Remote Plugin Executor (NRPE) allows Nagios to execute check plugins on remote Linux hosts. The remote host runs the NRPE daemon, which listens for check requests from the Nagios server.

# On the REMOTE host — install NRPE and plugins
$ sudo apt install -y nagios-nrpe-server nagios-plugins

# Configure allowed hosts
$ sudo nano /etc/nagios/nrpe.cfg
# Set:
allowed_hosts=127.0.0.1,192.168.1.100    # Nagios server IP

# Define local checks
command[check_load]=/usr/lib/nagios/plugins/check_load -w 5,4,3 -c 10,8,6
command[check_disk]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /
command[check_mem]=/usr/lib/nagios/plugins/check_mem -w 80 -c 90
command[check_procs]=/usr/lib/nagios/plugins/check_procs -w 250 -c 300

$ sudo systemctl enable --now nagios-nrpe-server

# On the NAGIOS SERVER — install NRPE plugin
$ sudo apt install -y nagios-nrpe-plugin

# Test remote check from Nagios server
$ /usr/local/nagios/libexec/check_nrpe -H 192.168.1.101 -c check_load
OK - load average: 0.42, 0.38, 0.35

Notification Configuration

Nagios notifications alert administrators when service states change. Notifications can be sent via email, SMS, Slack webhooks, or any custom script.

# Define a contact
$ sudo tee -a /usr/local/nagios/etc/objects/contacts.cfg > /dev/null <<'EOF'
define contact {
    contact_name            sysadmin
    use                     generic-contact
    alias                   System Administrator
    email                   admin@example.com
    service_notification_period     24x7
    host_notification_period        24x7
    service_notification_options    w,u,c,r
    host_notification_options       d,u,r
    service_notification_commands   notify-service-by-email
    host_notification_commands      notify-host-by-email
}

define contactgroup {
    contactgroup_name   ops-team
    alias               Operations Team
    members             nagiosadmin,sysadmin
}
EOF

# Configure mail transport for notifications
$ sudo apt install -y mailutils postfix
# Select "Internet Site" during postfix configuration

Custom Check Plugins

Writing custom Nagios plugins is straightforward. A plugin is any executable that returns an appropriate exit code and a status line.

# Create a custom plugin to check a web application response
$ sudo tee /usr/local/nagios/libexec/check_webapp > /dev/null <<'SCRIPT'
#!/bin/bash
# check_webapp - verify application health endpoint
URL="$1"
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" --max-time 10 "$URL")

if [ "$RESPONSE" = "200" ]; then
    echo "OK - $URL returned HTTP $RESPONSE"
    exit 0
elif [ "$RESPONSE" = "000" ]; then
    echo "CRITICAL - $URL is unreachable (timeout)"
    exit 2
else
    echo "WARNING - $URL returned HTTP $RESPONSE"
    exit 1
fi
SCRIPT

$ sudo chmod +x /usr/local/nagios/libexec/check_webapp

# Define the command in Nagios
$ sudo tee -a /usr/local/nagios/etc/objects/commands.cfg > /dev/null <<'EOF'
define command {
    command_name    check_webapp
    command_line    $USER1$/check_webapp $ARG1$
}
EOF

Key Takeaways

  • Nagios Core uses a plugin architecture where check programs return exit codes (0=OK, 1=WARNING, 2=CRITICAL, 3=UNKNOWN) to communicate service states.
  • Host and service definitions in configuration files describe what to monitor, how often, and what thresholds trigger alerts.
  • NRPE enables Nagios to execute check plugins on remote hosts, providing agent-based monitoring of internal metrics like CPU, memory, and disk.
  • Notifications are highly configurable with contact definitions, time periods, escalation rules, and custom notification commands.
  • Custom plugins are simple scripts that follow the exit code convention, making Nagios extensible to any monitoring use case.
  • Always validate configuration with nagios -v before reloading the daemon to catch syntax errors.

What’s Next

In the next lesson, we will explore Log Management with Logwatch and Journald — learning how to centralize, filter, and analyze system logs for security monitoring and operational awareness.

繁體中文

概述

  • 您將學到:如何在 Ubuntu Server 上安裝、設定和操作 Nagios Core,用於集中監控主機、服務和網路裝置,包括外掛架構、通知規則和網頁介面導覽。
  • 先決條件:系統監控基礎(第 61 課)、Apache 或 Nginx 網頁伺服器基礎
  • 預計閱讀時間:20 分鐘

介紹

Nagios Core 是業界最成熟的開源監控系統之一。最初於 1999 年以 NetSaint 的名稱發布,Nagios 已發展成為一個強大的框架,用於監控基礎設施元件,包括伺服器、交換機、應用程式和服務。它基於一個簡單但有效的模型運作:定期執行檢查外掛、將結果與定義的閾值進行比較,並在檢測到問題時觸發通知。

雖然 Prometheus 和 Zabbix 等新監控平台已經出現,但 Nagios 仍在企業環境中廣泛部署。了解 Nagios 架構為您提供了適用於任何監控系統的可轉移知識。

Nagios 架構

Nagios Core 遵循模組化架構,圍繞四個關鍵元件構建:

  • Nagios 守護程序:核心排程引擎,協調檢查執行、處理結果和管理通知。
  • 檢查外掛:執行實際監控檢查的外部程式。Nagios 本身不知道如何檢查任何東西——它將所有檢查委託給外掛並解釋它們的退出碼。
  • 設定檔:描述要監控什麼(主機、服務)、如何監控(命令、時段)以及通知誰(聯絡人、聯絡群組)的物件定義。
  • 網頁介面(CGI):基於瀏覽器的儀表板,顯示當前狀態、歷史數據,並允許操作員確認問題和安排停機時間。

外掛透過基於退出碼的標準化介面進行通訊:

  • 0 — 正常(服務運作正常)
  • 1 — 警告(服務降級但仍可運作)
  • 2 — 嚴重(服務已故障或處於嚴重狀態)
  • 3 — 未知(檢查無法確定服務狀態)

在 Ubuntu 上安裝 Nagios Core

# 安裝建置依賴
$ sudo apt update
$ sudo apt install -y autoconf gcc libc6 make wget unzip 
    apache2 php libapache2-mod-php libgd-dev openssl libssl-dev

# 建立 Nagios 使用者和群組
$ sudo useradd nagios
$ sudo groupadd nagcmd
$ sudo usermod -aG nagcmd nagios
$ sudo usermod -aG nagcmd www-data

# 下載並編譯 Nagios Core
$ cd /tmp
$ wget https://github.com/NagiosEnterprises/nagioscore/releases/download/nagios-4.5.1/nagios-4.5.1.tar.gz
$ tar xzf nagios-4.5.1.tar.gz && cd nagios-4.5.1
$ ./configure --with-httpd-conf=/etc/apache2/sites-enabled
$ make all
$ sudo make install install-daemoninit install-commandmode install-config install-webconf

# 建立網頁管理使用者並啟動
$ sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
$ sudo a2enmod rewrite cgi
$ sudo systemctl restart apache2
$ sudo systemctl enable --now nagios

設定主機和服務

# 啟用伺服器設定目錄
$ sudo mkdir -p /usr/local/nagios/etc/servers

# 定義主機
define host {
    use                 linux-server
    host_name           webserver01
    alias               生產網頁伺服器 01
    address             192.168.1.101
    max_check_attempts  5
    check_period        24x7
}

# 定義服務
define service {
    use                 generic-service
    host_name           webserver01
    service_description HTTP
    check_command       check_http
}

# 驗證設定
$ sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
$ sudo systemctl reload nagios

NRPE — 遠端主機監控

NRPE(Nagios 遠端外掛執行器)允許 Nagios 在遠端 Linux 主機上執行檢查外掛。

# 在遠端主機上安裝 NRPE
$ sudo apt install -y nagios-nrpe-server nagios-plugins

# 設定允許的主機
allowed_hosts=127.0.0.1,192.168.1.100

# 從 Nagios 伺服器測試
$ /usr/local/nagios/libexec/check_nrpe -H 192.168.1.101 -c check_load

通知設定

Nagios 通知在服務狀態變更時提醒管理員。通知可透過電子郵件、SMS、Slack webhook 或任何自訂腳本發送。

自訂檢查外掛

撰寫自訂 Nagios 外掛很簡單。外掛是任何返回適當退出碼和狀態行的可執行檔。

重點總結

  • Nagios Core 使用外掛架構,檢查程式返回退出碼(0=正常、1=警告、2=嚴重、3=未知)來傳達服務狀態。
  • 設定檔中的主機和服務定義描述了要監控什麼、多頻繁、以及什麼閾值觸發警報。
  • NRPE 使 Nagios 能在遠端主機上執行檢查外掛,提供 CPU、記憶體和磁碟等內部指標的代理式監控。
  • 通知高度可設定,包含聯絡人定義、時段、升級規則和自訂通知命令。
  • 自訂外掛是遵循退出碼慣例的簡單腳本,使 Nagios 可擴展到任何監控用例。
  • 在重新載入守護程序之前,務必使用 nagios -v 驗證設定以捕獲語法錯誤。

下一步

在下一課中,我們將探討使用 Logwatch 和 Journald 進行日誌管理——學習如何集中化、過濾和分析系統日誌以進行安全監控和營運感知。

日本語

概要

  • 学習内容:Ubuntu Server 上での Nagios Core のインストール、設定、運用方法。ホスト、サービス、ネットワークデバイスの集中監視、プラグインアーキテクチャ、通知ルール、Web インターフェースのナビゲーションを含む。
  • 前提条件:システム監視の基礎(レッスン61)、Apache または Nginx Web サーバーの基礎
  • 推定読了時間:20分

はじめに

Nagios Core は業界で最も歴史のあるオープンソース監視システムの1つです。1999年に NetSaint として最初にリリースされ、サーバー、スイッチ、アプリケーション、サービスなどのインフラストラクチャコンポーネントを監視するための強力なフレームワークに発展しました。定期的にチェックプラグインを実行し、結果を定義されたしきい値と比較し、問題が検出されたときに通知をトリガーするという、シンプルだが効果的なモデルで動作します。

Prometheus や Zabbix のような新しい監視プラットフォームが登場していますが、Nagios はエンタープライズ環境で広く展開されています。Nagios のアーキテクチャを理解することで、あらゆる監視システムに適用できる転用可能な知識が得られます。

Nagios アーキテクチャ

  • Nagios デーモン:チェック実行を調整し、結果を処理し、通知を管理するコアスケジューリングエンジン。
  • チェックプラグイン:実際の監視チェックを実行する外部プログラム。Nagios 自体は何もチェックする方法を知らず、すべてのチェックをプラグインに委任し、その終了コードを解釈する。
  • 設定ファイル:何を監視するか(ホスト、サービス)、どのように監視するか(コマンド、時間帯)、誰に通知するか(連絡先、連絡先グループ)を記述するオブジェクト定義。
  • Web インターフェース(CGI):現在のステータス、履歴データを表示し、オペレーターが問題を確認しダウンタイムをスケジュールできるブラウザベースのダッシュボード。

プラグインの終了コード規約:

  • 0 — OK(サービス正常)
  • 1 — WARNING(サービス劣化だが機能中)
  • 2 — CRITICAL(サービス障害または危機的状態)
  • 3 — UNKNOWN(チェックがサービス状態を判定できない)

Ubuntu への Nagios Core インストール

# ビルド依存関係をインストール
$ sudo apt update
$ sudo apt install -y autoconf gcc libc6 make wget unzip 
    apache2 php libapache2-mod-php libgd-dev openssl libssl-dev

# Nagios ユーザーとグループを作成
$ sudo useradd nagios
$ sudo groupadd nagcmd

# Nagios Core をダウンロードしてコンパイル
$ cd /tmp
$ wget https://github.com/NagiosEnterprises/nagioscore/releases/download/nagios-4.5.1/nagios-4.5.1.tar.gz
$ tar xzf nagios-4.5.1.tar.gz && cd nagios-4.5.1
$ ./configure --with-httpd-conf=/etc/apache2/sites-enabled
$ make all
$ sudo make install install-daemoninit install-commandmode install-config install-webconf

# Web 管理ユーザーを作成して起動
$ sudo htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
$ sudo a2enmod rewrite cgi
$ sudo systemctl restart apache2
$ sudo systemctl enable --now nagios

ホストとサービスの設定

# ホスト定義
define host {
    use                 linux-server
    host_name           webserver01
    address             192.168.1.101
    max_check_attempts  5
    check_period        24x7
}

# サービス定義
define service {
    use                 generic-service
    host_name           webserver01
    service_description HTTP
    check_command       check_http
}

# 設定を検証してリロード
$ sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
$ sudo systemctl reload nagios

NRPE — リモートホスト監視

NRPE(Nagios Remote Plugin Executor)により、Nagios はリモート Linux ホスト上でチェックプラグインを実行できます。

# リモートホストに NRPE をインストール
$ sudo apt install -y nagios-nrpe-server nagios-plugins

# 許可ホストを設定
allowed_hosts=127.0.0.1,192.168.1.100

# Nagios サーバーからテスト
$ /usr/local/nagios/libexec/check_nrpe -H 192.168.1.101 -c check_load

通知設定

Nagios の通知はサービス状態が変化したときに管理者にアラートを送信します。通知はメール、SMS、Slack Webhook、またはカスタムスクリプトで送信できます。

カスタムチェックプラグイン

カスタム Nagios プラグインの作成は簡単です。プラグインは適切な終了コードとステータス行を返す実行可能ファイルです。

重要ポイント

  • Nagios Core はプラグインアーキテクチャを使用し、チェックプログラムが終了コード(0=OK、1=WARNING、2=CRITICAL、3=UNKNOWN)でサービス状態を伝達する。
  • 設定ファイルのホストとサービス定義が、何を監視するか、頻度、アラートをトリガーするしきい値を記述する。
  • NRPE により Nagios はリモートホスト上でチェックプラグインを実行でき、CPU、メモリ、ディスクなどの内部メトリクスのエージェントベース監視を提供する。
  • 通知は連絡先定義、時間帯、エスカレーションルール、カスタム通知コマンドで高度に設定可能。
  • カスタムプラグインは終了コード規約に従うシンプルなスクリプトで、Nagios をあらゆる監視ユースケースに拡張可能にする。
  • デーモンをリロードする前に、必ず nagios -v で設定を検証して構文エラーを検出する。

次のステップ

次のレッスンでは、Logwatch と Journald によるログ管理について学びます。セキュリティ監視と運用の可視性のために、システムログを集中化、フィルタリング、分析する方法を学びます。

You Missed