IT-INFO

Where IT meets innovation, IT-INFO lights the path to technological brilliance

Configuring Nagios Using Docker

Configuring Nagios Using Docker

Introduction

Nagios is a powerful monitoring tool used for monitoring IT infrastructure to ensure systems, applications, and services are running smoothly. Docker provides a convenient way to deploy Nagios with all its dependencies in a containerized environment, simplifying setup and management.

Prerequisites

Before proceeding, ensure you have the following:

  • Docker installed on your system
  • Basic understanding of Docker commands and concepts
  • Access to a terminal or command line interface

Step-by-Step Configuration

1. Docker Setup

If you haven't installed Docker yet, follow the installation instructions for your operating system from the official Docker documentation.

2. Docker Compose File

Create a docker-compose.yml file in your preferred directory. This file will define the services required for Nagios:

nagios:
  image: jasonrivers/nagios:latest
  container_name: nagios
  ports:
    - "8080:80"
  network_mode: "host"
  environment:
    - NAGIOS_FQDN=nagios.local
    - NAGIOSADMIN_USER=nagiosadmin
    - NAGIOSADMIN_PASS=nagios
  volumes:
    - ./nagios:/usr/local/nagios
    - ./nagios/etc/nagios.cfg:/opt/nagios/etc/nagios.cfg
    - ./logs:/var/log

3. Explanation of Docker Compose File

  • Image: jasonrivers/nagios:latest pulls the latest Nagios image from Docker Hub.
  • Ports: Maps host port 8080 to container port 80 for Nagios web interface.
  • Environment: Sets up Nagios admin credentials (NAGIOSADMIN_USER and NAGIOSADMIN_PASS). Change these values for production setups.
  • Volumes: Mounts local directories ./nagios and /usr/local/nagios to persist configuration and runtime data.

4. Starting Nagios

Navigate to the directory containing docker-compose.yml and run:

docker-compose up

5. Accessing Nagios Web Interface

Open your web browser and go to http://localhost:8080/nagios. Log in with the credentials specified in the docker-compose.yml file (nagiosadmin/nagios by default).

6. Custom Nagios Structure

The etc folder contains custom hosts and services, and Nagios config file:

  • The libexec folder contains the plugins.
  • The var folder is created by Nagios; we don’t modify it.

Inside the etc folder, we have the objects folder where we add hosts and services. Each file has its own host and service.

In the following example, we have fastapi and postgres files:

  • The commands file contains some commands that are executed when a service is running.
  • The cgi.cfg file contains some Nagios configurations.
  • The nagios.cfg file is the main config file of Nagios.

7. Nagios Config Files

The cgi.cfg file content:

main_config_file=/opt/nagios/etc/nagios.cfg
physical_html_path=/usr/local/nagios/share
url_html_path=/nagios
show_context_help=1
use_authentication=1
authorized_for_system_information=nagiosadmin
authorized_for_configuration_information=nagiosadmin
authorized_for_system_commands=nagiosadmin
authorized_for_all_services=nagiosadmin
authorized_for_all_hosts=nagiosadmin
authorized_for_all_service_commands=nagiosadmin
authorized_for_all_host_commands=nagiosadmin
setenforce=0

The main nagios.cfg contains a lot of configs, but the important thing for us is to add the paths to objects files. Example:

cfg_file=/usr/local/nagios/etc/objects/commands.cfg
cfg_file=/usr/local/nagios/etc/objects/fastapi.cfg
cfg_file=/usr/local/nagios/etc/objects/postgres.cfg

The FastAPI Config File

Here is how we create the hosts and services. By default, there are some templates created by Nagios like:

  • generic-host
  • generic-service

The templates contain some default important configs:

# nagios/etc/objects/fastapi.cfg
define host {
    use             generic-host
    host_name       api
    alias           FastAPI Application
    address         localhost:8080
    contact_groups  admins
    max_check_attempts      3
}
define service {
    use                     generic-service
    host_name               api
    service_description     HTTP
    check_command           check_http
    notifications_enabled   1
    max_check_attempts      1
    check_interval          1
}
define service {
    use                     generic-service
    host_name               api
    service_description     FastAPI Log Monitoring
    check_command           check_logfiles!/usr/local/nagios/etc/check_logfiles.cfg
    notifications_enabled   1
    check_period            24x7
    contact_groups          admins
    max_check_attempts      1
    check_interval          1
}

Created on Aug. 28, 2024, 1:29 p.m.