Beta website

Nagios NRPE Server Ansible Role

Overview

NRPE allows you to remotely execute Nagios plugins on other Linux/Unix machines. This allows you to monitor remote machine metrics (disk usage, CPU load, etc.). Deploying NRPE is easy through your favourite package manager (apt-get install nagios-nrpe-server or yum install nrpe), configuring your Nagios server’s IP, adding your checks on NRPE’s end and letting Nagios know its there.

But if you’re deploying this often (Or not), why not automate it? I created an Ansible role which installs and configures NRPE from scratch on both Debian and CentOS based servers. All you need to do now is configure it in Nagios and you’re rolling.

Latest version:

v3.0.1

How it works

It uses Ansible for deployment and configuration (If you aren’t familiar with Ansible, I highly recommend you watch their video introduction here). The first play installs NRPE and any dependences (NRPE doesn’t come with any plugins on RedHat, wtf?), the second play then configures nrpe.cfg from a Jinja2 template (Ansible’s standard template language of choice). The third play installs nrpe_ansible.cfg where you can place all your custom checks (Again from a Jinja2 template) and finally the final play ensures its running and configured to start at boot.

The Role

---
# Nagios NRPE Server role by James Loh

# Include variables at the start so they're in scope
- name: Include OS-Specific variables
  include_tasks: "variables.yml"

# Install our needed packages for each specific OS
- include: "packages-{{ ansible_os_family }}.yml"

# Create our config
- name: Create nrpe.cfg from template
  template:
    src: "nrpe.cfg.j2"
    dest: "{{ nagios_nrpe_server_dir }}/nrpe.cfg"
    owner: root
    group: root
    mode: 0644
  notify: restart nagios-nrpe-server

# Create nrpe_ansible.cfg
- name: Create nrpe_ansible.cfg from template
  template:
    src: "nrpe_ansible.cfg.j2"
    dest: "{{ nagios_nrpe_server_dir }}/nrpe_ansible.cfg"
    owner: root
    group: root
    mode: 0644
  notify: restart nagios-nrpe-server

# Sync our plugins
- name: Install global plugins
  copy:
    src: "{{ nagios_nrpe_server_plugins_src_dir }}/global/"
    dest: "{{ nagios_nrpe_server_plugins_dir }}"
    owner: root
    group: root
    mode: 0755
  failed_when: false

# Install per-server plugins
- name: Install per-server plugins
  copy:
    src: "{{ nagios_nrpe_server_plugins_src_dir }}/{{ inventory_hostname }}/"
    dest: "{{ nagios_nrpe_server_plugins_dir }}"
    owner: root
    group: root
    mode: 0755
  failed_when: false

# Ensure NRPE server is running and will start at boot
- name: Ensure NRPE server is running
  service:
    name: "{{ nagios_nrpe_server_service }}"
    state: started
    enabled: yes

As you can see above, its mostly through Jinja2 templates and OS variables. I’ve tested it on CentOS 6, Debian 7 and Ubuntu 14.x. On all systems it worked without a hitch.

Configuration Options

The role allows a few configuration options (see below) although most are either OS Specific and are likely not going to be changed.

  • nagios_nrpe_server_bind_address: 127.0.0.1
  • nagios_nrpe_server_port: 5666
  • nagios_nrpe_server_allowed_hosts: 127.0.0.1

And some OS Specific Variables

RedHat:

  • nagios_nrpe_server_pid: /var/run/nrpe/nrpe.pid
  • nagios_nrpe_server_user: nrpe
  • nagios_nrpe_server_group: nrpe
  • nagios_nrpe_server_repo_redhat: epel
  • nagios_nrpe_server_service: nrpe

Debian:

  • nagios_nrpe_server_pid: /var/run/nagios/nrpe.pid
  • nagios_nrpe_server_user: nagios
  • nagios_nrpe_server_group: nagios
  • nagios_nrpe_server_service: nagios-nrpe-server
  • nagios_nrpe_server_plugins_dir: /usr/lib/nagios/plugins

You can of course override the OS Specific variables I set above through either a hostgroup var or on a per host variable, if your plugins live in a different directory other then /usr/lib/nagios/plugins for example.

Wrap Up

Thats my role, its lightweight and gets the job done. If you’ve got any questions or feel its missing a feature, feel free to ask it below or open an issue on GitHub. Like the role? Don’t forget to rate it on Ansible Galaxy!