Services DevOps DevSecOps Cloud Consulting Infrastructure Automation Managed Services AIOps MLOps DataOps Microservices 🔐 Private AINEW Solutions DevOps Transformation CI/CD Automation Platform Engineering Security Automation Zero Trust Security Compliance Automation Cloud Migration Kubernetes Migration Cloud Cost Optimisation AI-Powered Operations Data Platform Modernisation SRE & Observability Legacy Modernisation Managed IT Services 🔐 Private AI DeploymentNEW Products ✨ ZippyOPS AINEW 🛡️ ArmorPlane 🔒 DevSecOpsAsService 🖥️ LabAsService 🤝 Collab 🧪 SandboxAsService 🎬 DemoAsService Bootcamp 🔄 DevOps Bootcamp ☁️ Cloud Engineering 🔒 DevSecOps 🛡️ Cloud Security ⚙️ Infrastructure Automation 📡 SRE & Observability 🤖 AIOps & MLOps 🧠 AI Engineering 🎓 ZOLS — Free Learning Company About Us Projects Careers Get in Touch

Nagios Plugins with Bash on CentOS: Step-by-Step Guide

How to Create Nagios Plugins with Bash on CentOS

Nagios is a powerful monitoring tool used by system administrators to keep track of critical system metrics. One of its key features is the ability to create custom plugins to extend its functionality. In this guide, we’ll walk you through how to create Nagios plugins with Bash on a CentOS system, from installation to script configuration.

Whether you’re monitoring disk space, server health, or application status, these plugins provide valuable insights. As a result, automating and streamlining monitoring tasks becomes much easier.

Nagios plugins creation with Bash script on CentOS for system monitoring

Step 1: Install the RPMForge Repository and NRPE on Client VPS

Before you can start creating custom plugins, you need to set up the Nagios NRPE (Nagios Remote Plugin Executor) on your CentOS VPS. Follow these steps:

  1. Install the RPMForge repository: rpm -ivh http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
  2. Install NRPE: yum -y install nagios-nrpe
  3. Create the NRPE user and enable it to start on boot: useradd nrpe && chkconfig nrpe on

With NRPE installed, your server is ready to accept Nagios plugin scripts.

Step 2: Create Your Bash Script

Next, you’ll create a Bash script to monitor disk usage and trigger alerts based on thresholds. To start, navigate to the Nagios plugins directory and create a new script.

  1. Navigate to the Nagios plugins directory: cd /usr/lib64/nagios/plugins/
  2. Create your script (check_warnings.sh): cat > check_warnings.sh
  3. Insert the following script: #!/bin/bash countWarnings=$(df -h / | grep -v Filesystem | awk '{print $5}' | sed 's/%//g') if (($countWarnings <= 5)); then echo "OK - Usage of / is $countWarnings%" exit 0 elif ((6 <= $countWarnings && $countWarnings <= 9)); then echo "WARNING - Usage of / is $countWarnings%" exit 1 elif ((10 <= $countWarnings && $countWarnings <= 99)); then echo "CRITICAL - Usage of / is $countWarnings%" servername=nagios.example.com filesystem=/ echo "$servername $filesystem" curl -D - \ -X "POST" -H "Accept: application/json" \ -H "Content-Type: application/json" \ -H "X-Rundeck-Auth-Token: 8h7eFvQ1Nb5JrhulUdYrHvAKzwWw18gp" \ -d "{\"argString\":\"-servername $servername -filesystem $filesystem \"}" \ http://192.168.1.10:4440/api/16/job/2ed8d8c9-3f46-46d3-ba5f-e71d3211a40c/executions exit 2 else echo "UNKNOWN - $countWarnings" exit 3 fi
  4. Make the script executable: chmod +x check_warnings.sh

Step 3: Add the Script to NRPE Configuration on the Client Host

Now that your Bash script is ready, it’s time to integrate it into the NRPE configuration so Nagios can execute it remotely.

  1. Edit the NRPE configuration file: vi /etc/nagios/nrpe.cfg
  2. Configure the NRPE settings: log_facility=daemon pid_file=/var/run/nrpe/nrpe.pid server_port=5666 nrpe_user=nrpe nrpe_group=nrpe allowed_hosts=198.211.117.251 dont_blame_nrpe=1 debug=0 command_timeout=60 connection_timeout=300 include_dir=/etc/nrpe.d/
  3. Add the custom command to the NRPE config: command[usedspace_bash]=/usr/lib64/nagios/plugins/check_warnings.sh
  4. Restart NRPE to apply the changes: service nrpe restart

Step 4: Add the New Command to Nagios Checks on the Monitoring Server

Lastly, you’ll add your custom command to the Nagios monitoring server to monitor disk space on your client VPS.

  1. Edit the Nagios commands configuration: vi /usr/local/nagios/etc/objects/commands.cfg
  2. Define the new check command: define command{ command_name check_warnings command_line $USER1$/check_warnings.sh }
  3. Configure the service for monitoring: vi /usr/local/nagios/etc/server/jira.cfg Add the following service definition: define service { use generic-service host_name jira.example.com service_description CentOS Disk Usage check_command check_warnings }
  4. Restart Nagios to apply the configuration: service nagios restart

Conclusion: Streamline Your Monitoring with Custom Nagios Plugins

By following these steps, you’ve successfully created a Nagios plugin with Bash on CentOS to monitor disk space. This approach offers a flexible and scalable solution for tracking server performance. With ZippyOPS’ consulting, implementation, and managed services, you can further optimize your monitoring infrastructure, integrating advanced DevOps, DevSecOps, and AIOps practices.

Whether you’re working on cloud infrastructure, microservices, or security operations, ZippyOPS helps streamline your systems management. Reach out to ZippyOPS for customized solutions tailored to your needs.

For further information or inquiries, contact us at sales@zippyops.com.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top