Nagios Configuration Automation : Configuration as code (CAC)
“Server Monitoring with NRPE Plugin Integration”
Nagios Configuration Automation : Configuration as code (CAC)
“Server Monitoring with NRPE Plugin Integration”
Introduction
Nagios is a widely used open-source monitoring system that helps keep track of your servers and ensures that critical services are running smoothly. Implementing a monitoring system like Nagios is crucial for any production environment because it allows you to proactively address issues before they become major problems or before users start reporting them.
With Nagios, you have the ability to monitor your remote hosts and their services from a single window. It provides warnings and alerts you when something goes wrong with your servers, enabling you to detect and resolve problems before they escalate. This proactive approach helps reduce downtime and minimize the potential losses for your business.
By continuously monitoring parameters such as uptime, CPU usage, and disk space, Nagios empowers you to take preventive actions and avoid service disruptions. This guarantees the optimal operation of your systems, providing a better experience for your users and minimizing any negative impact on your business.

There are two popular solutions for Red Hat Enterprise Linux monitoring:
· Nagios XI
· Nagios Core
This guide offers easy-to-follow directions for installing and setting up the newest Nagios Core edition on Red Hat Enterprise Linux (RHEL) and RHEL-based distributions such as CentOS Stream, Rocky Linux, AlmaLinux, and Fedora using a Shell Script.
The installation process is designed to be straightforward, and within just 30 minutes, you can start monitoring your local machine. No advanced installation procedures are required, only basic steps that are compatible with most Linux servers commonly used today. By following this guide, you’ll have Nagios Core up and running quickly, allowing you to effectively monitor your system’s performance.
Benefits
Implementing Red Hat Enterprise Linux monitoring with Nagios provides several valuable benefits:
· Increased server, services, and application availability
· Fast detection of network outages and protocol failure
🎯 Setting up the Nagios Monitoring System :
# Monitoring Host →
- Installing Nagios Core and Nagios Plugin on Linux
By following the provided instructions correctly, you will achieve the following outcomes:
- Nagios and its plugins will be installed in the designated directory, typically located at /usr/local/nagios.
- Nagios will be set up to monitor specific services on your local machine, such as Disk Usage, CPU Load, Current Users, Total Processes, and more.
- You will have access to the Nagios web interface through **http://Your-server-IP-address/nagios**.
These steps will allow you to successfully install Nagios, configure it to monitor essential services on your local machine, and access the Nagios web interface for effective monitoring and management.
- Setting User Data and Passing Argument in AWS EC2 Console:
To pass this shell script as user data and provide the argument in AWS, you can follow these steps:
· Log in to the AWS Management Console and go to the EC2 service.
· Launch a new EC2 instance or select an existing instance.
· In the “Configure Instance” section, scroll down to the “Advanced Details” or “UserData” section.
· Paste the shell script in “User data — optional” as the user data.
· Continue with the rest of the EC2 instance configuration, and launch the instance.
· The shell script provided will be executed as soon as the instance starts up, and the argument will be passed to it. Please note that this assumes you have the necessary permissions to launch and configure EC2 instances in AWS.
- User-data Script for Installing Nagios Monitoring
#!/bin/bash
echo "#!/bin/bash
# Check if password is provided as an argument
if [ \$# -ne 1 ]; then
echo \"Usage: \$0 <password>\"
exit 1
fi
password=\$1
username=\"nagiosadmin\"
htpasswd_file=\"/usr/local/nagios/etc/htpasswd.users\"
# Install required packages
yum install -y httpd httpd-tools php gcc glibc glibc-common gd gd-devel make net-snmp
# Create users and groups
useradd nagios
groupadd nagcmd
usermod -G nagcmd nagios
usermod -G nagcmd apache
# Download Nagios and Nagios plugins
mkdir /root/nagios
cd /root/nagios
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.9.tar.gz
wget https://nagios-plugins.org/download/nagios-plugins-2.3.3.tar.gz
tar -xf nagios-4.4.9.tar.gz
tar -xf nagios-plugins-2.3.3.tar.gz
# Configure and install Nagios
cd nagios-4.4.9/
yum install openssl-devel -y
./configure --with-command-group=nagcmd
make all
make install
make install-init
make install-commandmode
make install-config
# Configure Email Address
sudo cat << _EOF_ >> /usr/local/nagios/etc/objects/contacts.cfg
define contact {
contact_name Example ; Short name of user
use generic-contact ; Inherit default values from generic-contact template (defined above)
alias Example ; Full name of user
email admin@example.com ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}
_EOF_
make install-webconf
# Create nagiosadmin user and set password
useradd \$username
echo \"\$username:\$password\" | chpasswd
# Create htpasswd file if it doesn't exist
if [ ! -e \"\$htpasswd_file\" ]; then
touch \$htpasswd_file
fi
# Add nagiosadmin entry to htpasswd file
htpasswd -bs \"\$htpasswd_file\" \"\$username\" \"\$password\"
systemctl restart httpd
# Install Nagios plugins
cd /root/nagios
cd nagios-plugins-2.3.3
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install
# Verify Nagios configuration
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
# Enable and start services
systemctl enable nagios
systemctl enable httpd
systemctl restart nagios
# Configure firewall
yum install firewalld -y
systemctl start firewalld
systemctl enable firewalld
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload
# Install NRPE Plugin in Nagios
cd /root/nagios
wget https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-4.0.2/nrpe-4.0.2.tar.gz
tar xzf nrpe-4.0.2.tar.gz
cd nrpe-4.0.2
# Compile and install the NRPE addon
./configure
make all
make install-plugin
make install-daemon
make install-init
# Creating Nagios Host and Services File
cd /usr/local/nagios/etc/
touch hosts.cfg
touch services.cfg
# Add two files to the main Nagios configuration file
sudo cat << _EOF_ >> /usr/local/nagios/etc/nagios.cfg
# You can specify individual object config files as shown below:
cfg_file=/usr/local/nagios/etc/hosts.cfg
cfg_file=/usr/local/nagios/etc/services.cfg
_EOF_
# hosts.cfg file
sudo cat << _EOF_ >> /usr/local/nagios/etc/hosts.cfg
## Default Linux Host Template ##
define host{
name linux ; Name of this template
use generic-host ; Inherit default values
check_period 24x7
check_interval 5
retry_interval 1
max_check_attempts 10
check_command check-host-alive
notification_period 24x7
notification_interval 30
notification_options d,r
contact_groups admins
register 0 ; DONT REGISTER THIS - ITS A TEMPLATE
}
## Default
define host{
use linux ; Inherit default values from a template
host_name Nagios-Core-Slave ; The name we're giving to this server
alias Amazon-linux ; A longer name for the server
address 43.205.98.140 ; IP address of Remote Linux host
}
_EOF_
# services.cfg file
sudo cat << _EOF_ >> /usr/local/nagios/etc/services.cfg
define service{
use generic-service
host_name Nagios-Core-Slave
service_description CPU-Load
check_command check_nrpe!check_load
}
define service{
use generic-service
host_name Nagios-Core-Slave
service_description Total-Processes
check_command check_nrpe!check_total_procs
}
define service{
use generic-service
host_name Nagios-Core-Slave
service_description Current-Users
check_command check_nrpe!check_users
}
define service{
use generic-service
host_name Nagios-Core-Slave
service_description Check-xvda1
check_command check_nrpe!check_xvda1
}
define service{
use generic-service
host_name Nagios-Core-Slave
service_description Check-Swap
check_command check_nrpe!check_swap
}
define service{
use generic-service
host_name Nagios-Core-Slave
service_description Check-HTTP
check_command check_nrpe!check_http
notifications_enabled 0
}
_EOF_
# commands.cfg file
sudo cat << '_EOF_' >> /usr/local/nagios/etc/objects/commands.cfg
###############################################################################
# NRPE CHECK COMMAND
#
# Command to use NRPE to check remote host systems
###############################################################################
define command {
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
_EOF_
# Verify Nagios Configuration files and Restart Nagios
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
systemctl restart nagios
" > nagios_install.sh
# Provide Executable permission to nagios_install.sh file
chmod +x nagios_install.sh
# Run the shell script with the argument "nagios"
./nagios_install.sh nagios
Code Overview:
Step 1 : Ensure Password Argument and Sets Variables for Nagios Admin
The given code snippet checks if a password is provided as an argument when executing the script. If the number of arguments is not equal to 1, it displays a usage message indicating the correct usage format and exits the script with a status code of 1.
If a password is provided, it assigns the password value to the variable password and sets the username as “nagiosadmin”. The variable htpasswd_file is set to the path “/usr/local/nagios/etc/htpasswd.users”, which represents the file where the username and password will be stored.
# Check if password is provided as an argument
if [ $# -ne 1 ]; then
echo "Usage: $0 <password>"
exit 1
fi
password=$1
username="nagiosadmin"
htpasswd_file="/usr/local/nagios/etc/htpasswd.users"
Step 2: Installing Apache and PHP Packages
To install Apache, PHP, and required libraries like gcc, glibc, glibc-common, and GD libraries, as well as their development libraries, before installing Nagios from source, you can utilize the yum package installer.
yum install -y httpd httpd-tools php gcc glibc glibc-common gd gd-devel make net-snmp
Step 3: Creating Nagios User and Group.
To set up Nagios, create a new user with the useradd command, and a nagcmd group account. Then, set a password and add both the Nagios and Apache users to the nagcmd group using usermod.
useradd nagios
groupadd nagcmd
usermod -G nagcmd nagios
usermod -G nagcmd apache
Step 4: Download Nagios Core and Nagios Plugin components
Create a directory for your Nagios installation and for any future downloads.
Now, use the wget command to download the latest Nagios Core and Nagios Plugins packages.
After downloading, extract the packages using the tar command as follows.
mkdir /root/nagios
cd /root/nagios
wget https://assets.nagios.com/downloads/nagioscore/releases/nagios-4.4.9.tar.gz
wget https://nagios-plugins.org/download/nagios-plugins-2.3.3.tar.gz
tar -xf nagios-4.4.9.tar.gz
tar -xf nagios-plugins-2.3.3.tar.gz
Step 5: Installing and Configuring Nagios Core
To configure Nagios Core, navigate to the Nagios directory and execute the configure file. If the configuration is successful, it will display the output as sample output.
After configuring, compile and install all the binaries using the make all and make install commands to install the necessary libraries on your machine.
To enable Nagios to work from the command line, install command-mode.
Finally, proceed by installing sample Nagios files with the provided command.
yum install openssl-devel -y
./configure - with-command-group=nagcmd
make all
make install
make install-init
make install-commandmode
make install-config
Step 6: Customizing Nagios Configuration
Provided code snippet is used to configure an email address for a contact in the Nagios monitoring system. Nagios allows you to define contacts who will receive notifications when certain events occur.
In this case, the code adds a contact definition to the contacts.cfg file.
# Configure Email Address
sudo cat << _EOF_ >> /usr/local/nagios/etc/objects/contacts.cfg
define contact {
contact_name Example ; Short name of user
use generic-contact ; Inherit default values from generic-contact template
alias Example ; Full name of user
email admin@example.com ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ******
}
_EOF_
Step 7: Installing and Configuring the Web Interface for Nagios
We have completed all the configurations in the backend. Now, to set up the Web Interface for Nagios, use the following command. It will configure the Web interface and create a web admin user named “nagiosadmin”.
make install-webconf
Step 8: Create nagiosadmin user and set password
The provided code snippet creates a user with the username “nagiosadmin” and sets a password for that user. It then checks if an htpasswd file exists, and if not, it creates an empty file.
Next, it adds an entry for the “nagiosadmin” user with the specified password to the htpasswd file using the htpasswd command.
Finally, it restarts the HTTP server to apply the changes made to the htpasswd file.
# Create nagiosadmin user and set password
useradd $username
echo "$username:$password" | chpasswd
# Create htpasswd file if it doesn't exist
if [ ! -e "$htpasswd_file" ]; then
touch $htpasswd_file
fi
# Add nagiosadmin entry to htpasswd file
htpasswd -bs $htpasswd_file $username $password
systemctl restart httpd
Step 9: Compile Plugin and Install Nagios
These steps ensure that the Nagios plugins are properly configured, compiled, and installed for use with Nagios monitoring system.
# Install Nagios plugins
cd /root/nagios
cd nagios-plugins-2.3.3
./configure - with-nagios-user=nagios - with-nagios-group=nagios
make
make install
Step 10: Verify the Nagios configuration files
Now, to verify the Nagios configuration, please use the following command. If everything is set up correctly, the output will be similar to the example below.
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Step 11: Add Nagios services to the System Startup
To ensure Nagios works across reboots, enable Nagios and HTTPD services at system startup using the systemctl command.
Next, restart Nagios to make the new settings take effect.
# Enable and start services
systemctl enable nagios
systemctl enable httpd
systemctl restart nagios
Step 12: Allowing Nagios Web in the Firewall
To access the Nagios web interface, open the Apache ports by executing the following commands:
# Configure firewall
yum install firewalld -y
systemctl start firewalld
systemctl enable firewalld
firewall-cmd - permanent - zone=public - add-service=http
firewall-cmd - permanent - zone=public - add-service=https
firewall-cmd - reload
Step 13: Installing NRPE Plugin in Nagios.
Navigate to the Nagios download directory and either download the latest NRPE Plugin or use the following wget command.
Unpack the NRPE source code tarball.
#Install NRPE Plugin in Nagios
cd /root/nagios
wget https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-4.0.2/nrpe-4.0.2.tar.gz
tar xzf nrpe-4.0.2.tar.gz
cd nrpe-4.0.2
Compile and install the NRPE addon.
#Compile and install the NRPE addon
./configure
make all
make install-plugin
make install-daemon
make install-init
Step 14: Adding Remote Linux Host to Nagios Monitoring Server
To add a remote host, create two new files named “hosts.cfg” and “services.cfg” under the “/usr/local/nagios/etc/” location.
#Creating Nagios Host and Services File
cd /usr/local/nagios/etc/
touch hosts.cfg
touch services.cfg
Step 15: Add two files to the main Nagios configuration file
The code snippet adds two file entries to the main Nagios configuration file nagios.cfg. These files are hosts.cfg and services.cfg. This allows for specifying individual object configuration files.
# Add two files to the main Nagios configuration file
sudo cat << _EOF_ >> /usr/local/nagios/etc/nagios.cfg
# You can specify individual object config files as shown below:
cfg_file=/usr/local/nagios/etc/hosts.cfg
cfg_file=/usr/local/nagios/etc/services.cfg
_EOF_
Step 16: Configuring Nagios Host File
The hosts.cfg file defines a default Linux host template. It sets various parameters such as name, check intervals, notification settings, and contact groups.
Additionally, it defines a specific host entry with the name “Nagios-Core-Slave”, an alias “Amazon-linux”, and an IP address of “43.205.98.140” for a remote Linux host.
#Hosts.cfg file
sudo cat << _EOF_ >> /usr/local/nagios/etc/hosts.cfg
## Default Linux Host Template ##
define host{
name linux ; Name of this template
use generic-host ; Inherit default values
check_period 24x7
check_interval 5
retry_interval 1
max_check_attempts 10
check_command check-host-alive
notification_period 24x7
notification_interval 30
notification_options d,r
contact_groups admins
register 0 ; DONT REGISTER THIS - ITS A TEMPLATE
}
## Default
define host{
use linux ; Inherit default values from a template
host_name Nagios-Core-Slave ; The name we're giving to this server
alias Amazon-linux ; A longer name for the server
address 43.205.98.140 ; IP address of Remote Linux host
}
_EOF_
Step 17: Configuring Nagios Services File
The provided code snippet adds service configurations to the “services.cfg” file in Nagios. These configurations define various services to be monitored on the Nagios-Core-Slave host. Each service has a service description, a check command using NRPE (Nagios Remote Plugin Executor), and additional settings such as notification options. The services include monitoring CPU load, total processes, current users, disk usage (Check-xvda1), swap usage, and HTTP availability.
#Services.cfg file
sudo cat << _EOF_ >> /usr/local/nagios/etc/services.cfg
define service{
use generic-service
host_name Nagios-Core-Slave
service_description CPU-Load
check_command check_nrpe!check_load
}
define service{
use generic-service
host_name Nagios-Core-Slave
service_description Total-Processes
check_command check_nrpe!check_total_procs
}
define service{
use generic-service
host_name Nagios-Core-Slave
service_description Current-Users
check_command check_nrpe!check_users
}
define service{
use generic-service
host_name Nagios-Core-Slave
service_description Check-xvda1
check_command check_nrpe!check_xvda1
}
define service{
use generic-service
host_name Nagios-Core-Slave
service_description Check-Swap
check_command check_nrpe!check_swap
}
define service{
use generic-service
host_name Nagios-Core-Slave
service_description Check-HTTP
check_command check_nrpe!check_http
notifications_enabled 0
}
_EOF_
Step 18: Configuring NRPE Command Definition
The given command adds a new configuration block to the Nagios monitoring system’s commands.cfg file. This configuration block defines a command called check_nrpe that uses NRPE to check the status of remote host systems. The check_nrpe command is defined with a command line that includes the path to the check_nrpe plugin and the necessary arguments for connecting to the remote host and performing the check. This addition enables Nagios to execute NRPE checks on remote hosts and monitor their status.
sudo cat << '_EOF_' >> /usr/local/nagios/etc/objects/commands.cfg
###################################################################
# NRPE CHECK COMMAND
#
# Command to use NRPE to check remote host systems
###################################################################
define command {
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
_EOF_
Step 19: Verify Nagios Configuration files and restart Nagios
Lastly, ensure to check the Nagios Configuration files for any errors, and proceed to restart Nagios to apply the recent configuration changes.
#Verify Nagios Configuration files and Restart Nagios
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
systemctl restart nagios
Step 20: Monitoring a Remote Linux Server in Nagios
To monitor the Remote Linux Server in Nagios, access the Nagios Monitoring Web interface at “http://Your-server-IP-address/nagios". Provide the username “nagiosadmin” and the password. Verify that the Remote Linux Host has been added and is being actively monitored.

🎯 Installation of NRPE Plugin in Remote Host using Ansible
# Remote Host →
INTRODUCTION
In this tutorial, you will discover how to deploy Nagios NRPE Agents using Ansible. Ansible, an open-source automation tool, is utilized for configuration management, application deployment, and task automation purposes. It is designed to simplify IT automation by providing a way to automate tasks across a large number of computers.
When you need to monitor multiple machines, it’s a lot of work to install each machine manually. Instead, you want to run just a single command, or click a few buttons, to configure all those machines at once, and then keep them updated over time.
Ansible provides a powerful set of modules that can be used to perform the most common tasks in just few lines of self-explanatory code. Another important reason is that Ansible is agentless, using OpenSSH to execute tasks on remote machines.
What is NRPE ?
The NRPE (Nagios Remote Plugin Executor) plugin enables monitoring of remote Linux/Unix services, network devices, or resources, such as CPU load, Swap, Memory usage, Online users, etc., on local/remote Linux machines.
Since these local resources are not typically exposed to external machines, it is essential to install and configure an NRPE agent on the remote machines. This setup allows Nagios to gather crucial monitoring data and perform checks efficiently.
Note: The NRPE addon necessitates the installation of Nagios Plugins on the remote Linux machine. Without these plugins, the NRPE daemon will not function properly and will not be able to monitor any services or resources. Therefore, it is essential to have the Nagios Plugins installed to ensure the successful operation of NRPE.
Prerequisites:
Before getting started, make sure you have the following prerequisites:
· Ansible installed on the control machine.
· A target remote host where you want to install NRPE.
· SSH access to the remote host with appropriate privileges.
Step:1 Create an Ansible playbook
· Create a new directory for your Ansible project, e.g., nrpe-installation.
· Inside the project directory, create a new playbook file, e.g., nrpe-install.yml.
· In /mnt directory, create a file ,e.g., nrpe-install.sh and provide executable permissions to the file and copy-paste the shell script for NRPE Installation and save the file.
Step 2: Define Inventory
To define the inventory for Ansible, follow these steps:
· Open the Ansible inventory file, typically located at /etc/ansible/hosts, using a text editor.
· Specify the IP address or hostname of the remote host under an appropriate group name, such as [nagios_slave].
Step:3 Write Playbook tasks
· Open the nrpe-install.yml playbook file and define the necessary tasks.
Here’s an playbook structure:
--- #NRPE PLUGIN INSTALLATION
- name: Run shell script on Nagios slave nodes
hosts: nagios_slave
user: ansible
become: true
tasks:
- name: Copy shell script to remote host
copy:
src: /mnt/nrpe-install.sh
dest: /tmp/nrpe-install.sh
mode: '0755'
- name: Execute shell script
command: /tmp/nrpe-install.sh nagios
This playbook is designed to install the NRPE plugin on Nagios slave nodes using a shell script. Here’s a brief explanation of the playbook:
· The playbook targets the hosts specified under the “nagios_slave” group.
· The “ansible” user is used to connect to the remote hosts.
· Privilege escalation is enabled (become: true) to execute tasks with elevated privileges.
· Two tasks are defined within the playbook:
a. The first task copies a shell script (nrpe-install.sh) from the local machine to the remote host at /tmp/nrpe-install.sh with permissions set to ‘0755’.
b. The second task executes the shell script on the remote host with the argument “nagios”.
Shell Script — NRPE Installation
# nrpe-install.sh
#!/bin/bash
# Check if password is provided as an argument
if [ $# -ne 1 ]; then
echo "Usage: $0 <password>"
exit 1
fi
username="nagios"
password=$1
# Install required packages
yum install -y gcc glibc glibc-common gd gd-devel make net-snmp openssl-devel tar wget
# Create nagios user
useradd $username
echo "$username:$password" | chpasswd
# Download and install Nagios plugins
mkdir /root/nagios
cd /root/nagios
wget https://nagios-plugins.org/download/nagios-plugins-2.3.3.tar.gz
tar -xvf nagios-plugins-2.3.3.tar.gz
cd nagios-plugins-2.3.3
./configure
make
make install
chown nagios.nagios /usr/local/nagios
chown -R nagios.nagios /usr/local/nagios/libexec
# Download, compile, and install NRPE
cd /root/nagios
wget https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-4.0.2/nrpe-4.0.2.tar.gz
tar xzf nrpe-4.0.2.tar.gz
cd nrpe-4.0.2
./configure
make all
make install-plugin
make install-daemon
make install-config
make install-init
# Configure NRPE Plugin to add IP Address of the Nagios Monitoring Server.
sudo cat << _EOF_ >> /usr/local/nagios/etc/nrpe.cfg
allowed_hosts=127.0.0.1,13.232.43.17
_EOF_
systemctl enable nrpe
systemctl restart nrpe
# Configure firewall
yum install firewalld -y
systemctl start firewalld
firewall-cmd --zone=public --add-port=5666/tcp
firewall-cmd --zone=public --add-port=5666/tcp --permanent
netstat -na | grep "5666"
# Test NRPE connection
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1
###############################################################################
# Customize NRPE Commands:
# Hardcoded Command Arguments
###############################################################################
sudo cat << _EOF_ >> /usr/local/nagios/etc/nrpe.cfg
command[check_mem]=/usr/local/nagios/libexec/check_mem.pl -f -w 30 -c 10
command[check_xvda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/xvda1
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
command[check_http]=/usr/local/nagios/libexec/check_http -H 52.66.199.206 -p 80
command[check_disk_home]=/usr/local/nagios/libexec/check_disk -w 30% -c 10% -p /home
_EOF_
Code Overview
Step 1: Provide Password as Argument
# Check if password is provided as an argument
if [ $# -ne 1 ]; then
echo "Usage: $0 <password>"
exit 1
fi
username="nagios"
password=$1
This code snippet checks if a password is provided as a command-line argument. If a password is not provided, it displays a usage message explaining how to use the script and exits with an error status. If a password is provided, it assigns the value “nagios” to the username variable and assigns the provided password to the password variable.
In summary, this section ensures that the script is executed with the correct number of arguments and handles the assignment of the username and password variables for further usage in the script.
Step 2: Install Required Dependencies
yum install -y gcc glibc glibc-common gd gd-devel make net-snmp openssl-devel tar wget
We need to install required libraries like gcc, glibc, glibc-common, and GD and its development libraries using the yum package manager.
Step 3: Create Nagios User
# Create nagios user
useradd $username
echo "$username:$password" | chpasswd
The code snippet creates a new user named “nagios” and sets the user’s password based on the value stored in the $password variable.
Step 4: Install the Nagios Plugins
# Download and install Nagios plugins
mkdir /root/nagios
cd /root/nagios
wget https://nagios-plugins.org/download/nagios-plugins-2.3.3.tar.gz
tar -xvf nagios-plugins-2.3.3.tar.gz
cd nagios-plugins-2.3.3
./configure
make
make install
To set up a directory for Nagios plugin installation and future downloads, create a new folder. Then, use the wget command to download the latest Nagios Plugins package. Afterward, extract the source code tarball using the tar command.
After that, use the provided commands to compile and install Nagios Plugins.
chown nagios.nagios /usr/local/nagios
chown -R nagios.nagios /usr/local/nagios/libexec
Additionally, set the permissions on the plugin directory using the chown command.
Step 5: Installing NRPE Plugin
# Download, compile, and install NRPE
cd /root/nagios
wget https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-4.0.2/nrpe-4.0.2.tar.gz
tar xzf nrpe-4.0.2.tar.gz
cd nrpe-4.0.2
./configure
make all
make install-plugin
make install-daemon
make install-config
make install-init
To install the NRPE plugin, begin by downloading the latest version using the provided wget command or obtain it from an appropriate source
Unpack the NRPE source code tarball.
Compile and install the NRPE addon.
Afterward, proceed to install the NRPE daemon as a systemd service on your system. This ensures the proper functioning and management of NRPE.
Step 6: Configure NRPE Plugin
# Configure NRPE Plugin to add IP Address of the Nagios Monitoring Server.
sudo cat << _EOF_ >> /usr/local/nagios/etc/nrpe.cfg
allowed_hosts=127.0.0.1,13.232.43.17
_EOF_
To set up the “/usr/local/nagios/etc/nrpe.cfg” file, use a text editor and create the configuration file. Then, include the local host and the IP address of the Nagios Monitoring Server within the file. Be sure to add the appropriate IP address to the “allowed_hosts” configuration line to grant access for monitoring.
# Enable and Restart Service
systemctl enable nrpe
systemctl restart nrpe
Next, enable and restart the nrpe service.
Step 7: Open NRPE Port in Firewall
# Configure firewall
yum install firewalld -y
systemctl start firewalld
firewall-cmd --zone=public --add-port=5666/tcp
firewall-cmd --zone=public --add-port=5666/tcp --permanent
netstat -na | grep "5666"
To enable access to the NRPE daemon from remote servers, ensure that the Firewall on the local machine allows incoming connections on the NRPE port. Use the following iptables command to achieve this.
Step 8: Test NRPE Daemon Connection Locally
# Test NRPE connection
/usr/local/nagios/libexec/check_nrpe -H 127.0.0.1
Verify the NRPE daemon is functioning properly by running the “check_nrpe” command that was installed earlier for testing purposes.
Step 9: Customize NRPE Commands
The provided code snippet is used to customize the NRPE (Nagios Remote Plugin Executor) configuration by adding custom commands. NRPE allows Nagios to remotely execute plugins and monitor various aspects of a system.
The added commands are as follows:
· check_mem: Monitors memory usage, including buffers and cache. It sets warning at 30% and critical at 10%.
· check_xvda1: Monitors disk usage of /dev/xvda1. It sets warning at 20% and critical at 10%.
· check_swap: Monitors swap space usage. It sets warning at 20% and critical at 10%.
· check_http: Performs an HTTP check on the host with IP address 52.66.199.206 on port 80.
· check_disk_home: Monitors disk usage of the /home directory. It sets warning at 30% and critical at 10%.
· These commands are appended to the nrpe.cfg file, which is used by Nagios for remote monitoring and execution of plugins.
###############################################################################
# Customize NRPE Commands:
# Hardcoded Command Arguments
###############################################################################
sudo cat << _EOF_ >> /usr/local/nagios/etc/nrpe.cfg
command[check_mem]=/usr/local/nagios/libexec/check_mem.pl -f -w 30 -c 10
command[check_xvda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/xvda1
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
command[check_http]=/usr/local/nagios/libexec/check_http -H 52.66.199.206 -p 80
command[check_disk_home]=/usr/local/nagios/libexec/check_disk -w 30% -c 10% -p /home
_EOF_ 메타데이터
- post_id
- e239ee9673aa
- slug
- nagios-configuration-automation-configuration-as-code-cac-e239ee9673aa
- url
- https://blog.technocirrus.com/nagios-configuration-automation-configuration-as-code-cac-e239ee9673aa
- canonical_url
- https://blog.technocirrus.com/nagios-configuration-automation-configuration-as-code-cac-e239ee9673aa
- author_url
- https://medium.com/@sushant.karanjikar
- status
- ok
- fetched_at
- 2026-07-25 12:44:45