Automating Nagios XI Monitoring with Ansible: Installation and Configuration Guide

Picture of Jack Brisben
Jack Brisben
ansible-1200x630-og

Introduction

Managing IT infrastructure efficiently requires robust monitoring solutions and automation tools. Nagios XI provides powerful monitoring capabilities, ensuring system health and performance, while Ansible automates deployments and configurations across multiple servers. By integrating Ansible with Nagios XI, organizations can streamline monitoring setups, automate configurations, and scale monitoring environments with ease.

Why Use Ansible for Nagios XI?

1. Automation and Efficiency

Ansible automates the deployment, configuration, and management of Nagios XI, eliminating manual setup processes and reducing errors.

2. Consistency Across Infrastructure

With Ansible’s infrastructure-as-code approach, organizations can ensure consistent configurations and monitoring setups across multiple servers, minimizing configuration drift.

3. Scalability

Adding new hosts, services, or devices to be monitored is seamless with Ansible, allowing IT teams to scale their monitoring infrastructure efficiently.

4. Simplified Updates and Maintenance

Keeping Nagios XI and its plugins up to date across multiple servers becomes effortless with automated Ansible playbooks.

5. Custom Monitoring Setup

Ansible can deploy custom monitoring scripts or plugins, tailoring Nagios XI to specific monitoring needs.

Prerequisites

Before automating Nagios XI with Ansible, ensure the following:

  • Ansible Installed on a control node.
  • Nagios XI Installed and running.
  • SSH Access to target servers.
  • Administrative Privileges for installations and configurations.

Installing the Ansible Monitoring Agent for Nagios XI

To monitor target servers, install the Nagios XI agent (NRPE or NCPA) using an Ansible playbook.

Example Ansible Playbook for Installing NRPE:

- name: Install Nagios NRPE Agent
  hosts: all
  become: yes
  tasks:
    - name: Install required packages
      apt:
        name: ["nagios-nrpe-server", "nagios-plugins"]
        state: present
        update_cache: yes

    - name: Configure NRPE to allow Nagios XI Server
      lineinfile:
        path: /etc/nagios/nrpe.cfg
        regexp: '^allowed_hosts='
        line: 'allowed_hosts=127.0.0.1,{{ nagios_server_ip }}'

    - name: Restart NRPE Service
      service:
        name: nagios-nrpe-server
        state: restarted

Steps:

  1. Save the playbook as install_nrpe.yml.
  2. Run the playbook:ansible-playbook -i inventory install_nrpe.yml
  3. Verify the agent installation using Nagios XI.

Configuring Nagios XI with Ansible

Ansible can automate the configuration of hosts and services in Nagios XI by modifying the configuration files.

Example Ansible Playbook to Add a Host to Nagios XI:

- name: Add new host to Nagios XI
  hosts: localhost
  tasks:
    - name: Add host configuration
      lineinfile:
        path: /usr/local/nagios/etc/servers/{{ inventory_hostname }}.cfg
        line: "define host {\n use linux-server\n host_name {{ inventory_hostname }}\n address {{ ansible_host }}\n check_command check-host-alive\n}"
        create: yes

    - name: Restart Nagios service
      service:
        name: nagios
        state: restarted

Deploying Custom Monitoring Scripts with Ansible

Custom monitoring scripts help tailor Nagios XI to an organization’s specific needs. Ansible can push these scripts to multiple servers efficiently.

Example Playbook to Deploy a Custom Script:


- name: Deploy custom monitoring script
  hosts: all
  become: yes
  tasks:
    - name: Copy script to remote servers
      copy:
        src: check_custom.sh
        dest: /usr/local/nagios/libexec/check_custom.sh
        mode: '0755'

Scaling and Managing Nagios XI with Ansible

When adding new hosts, Ansible can automate the discovery and integration into Nagios XI, ensuring continuous scalability without manual intervention.

Steps to Scale Monitoring Setup:

  1. Use Ansible’s dynamic inventory to detect new hosts.
  2. Apply predefined playbooks to install agents and add configurations.
  3. Automate the restart of Nagios services to apply changes.

Testing and Troubleshooting

Verification Steps:

  • Confirm NRPE or NCPA service status: systemctl status nagios-nrpe-server
  • Check Nagios XI configuration syntax: nagios -v /usr/local/nagios/etc/nagios.cfg
  • Restart Nagios XI service if needed: systemctl restart nagios

Conclusion

By integrating Ansible with Nagios XI, organizations can streamline monitoring deployments, automate configurations, and scale their infrastructure effortlessly. This approach ensures consistency, reduces manual intervention, and enhances the overall efficiency of IT monitoring processes. Implementing automation with Ansible empowers IT teams to maintain reliable, up-to-date, and scalable monitoring environments with minimal effort. For additional support, visit the Nagios Support Forum or Nagios Knowledgebase.

Share: