Nagios is a popular open-source monitoring system that can be used to monitor the availability and performance of various resources, such as servers, networks, and services. One way to extend the capabilities of Nagios is by creating custom plugins using bash scripts. Here is a general outline of the steps involved in creating a Nagios plugin using a bash script:
It is important to note that a Nagios plugin should be designed to run quickly and efficiently, as it will be executed at regular intervals. This means that the plugin should avoid performing any unnecessary or time-consuming tasks, and should return output as quickly as possible.
There are a few best practices to keep in mind when creating a Nagios plugin:
- Use clear and concise output: The output of the plugin should be easy to understand and provide clear information about the status of the resource being monitored. Avoid using complex or jargon-filled language, and try to keep the output as short as possible.
- Return appropriate status codes: The plugin should return the correct status code based on the status of the resource being monitored. For example, if the resource is functioning correctly, the plugin should return a status code of 0 (success). If there is a problem with the resource, the plugin should return a status code of 1 (warning) or 2 (critical) depending on the severity of the issue.
- Return performance data: If relevant, the plugin should return performance data in the form of name=value[uom];[warn];[crit];[min];[max]. This allows Nagios to track the performance of the resource over time and alert the user if it falls outside of acceptable thresholds.
- Use check_nrpe when possible: If the plugin will be running on a remote server, it is generally more efficient to use the check_nrpe utility to execute the plugin rather than running it directly over SSH. This avoids the overhead of establishing an SSH connection for each plugin execution.
By following these best practices, you can create efficient and effective Nagios plugins that will help you to monitor the resources that are important to your organization.
Step 1: Determine the purpose of the plugin
The first step in creating a Nagios plugin is to determine what the plugin will be used for. Will it be used to check the status of a service, monitor the performance of a server, or perform some other task? This will help you to design the plugin and write the necessary code.
Step 2: Write A Shell Script
Once you know what the plugin will be used for, you can start writing the bash script. The script should include the necessary logic to perform the task that the plugin is designed to do. It should also include the output in the format expected by Nagios, which consists of a status code (e.g., 0 for success, 1 for warning, 2 for critical), a message, and optional performance data.
sudo vim check_disk_uses.sh
Add the below script.
Save your script and verify by running it manually
Step 3: Test the script
Before using the script as a Nagios plugin, it is important to test it to ensure that it works correctly and produces the expected output. You can test the script by running it from the command line and verifying that it produces the correct output.
bash check_disk_uses.sh /
This should work properly, before configuring it with the Nagios server.
Step 4: Install the Plugin
NRPE is the Nagios remote plugin execution that runs on client machines, accepts requests from the Nagios server, processes that request, and sends the result back to the Nagios server.
- If you do not have NRPE installed on your system. Use the following commands to install the NRPE client on your Debian-based systems.
sudo apt update
sudo apt install nagios-nrpe-server
The above commands are for Debian-based systems. To install NRPE on Redhat based system, visit this tutorial.
- Once the script is tested and working correctly, you can install it as a Nagios plugin by copying it to the “/usr/local/nagios/libexec” directory on the Nagios server.
sudo mv check_disk_uses.sh /usr/lib/nagios/plugins/check_disk_uses.sh
sudo chmod +x /usr/lib/nagios/plugins/check_disk_uses.sh
- Then, edit the NRPE configuration file “/etc/nagios/nrpe.cfg” and add your command to monitor some disks of your system.
You can call the `check_disk_uses` command from the Nagios server using the `check_nrpe` command and get the results back.
Step 5: Test with
Now, run the below command from your Nagios server, where 192.168.10.100 is the IP of the NRPE client system.
./check_nrpe -H 192.168.10.100 -c check_disk_uses
OK - 22% of disk space used.
Step 6: Configure Nagios to Use this Plugin
After installing the plugin, you will need to configure Nagios to use it. This involves creating a command definition in the Nagios configuration file (e.g.,
/usr/local/nagios/etc/objects/commands.cfg) and creating a service definition to specify how and when the plugin should be run.
- First edit the “/etc/nagios/objects/commands.cfg” configuration file and define the below command:
- Then create a new service find to manage that service:
- In order to verify the configuration files, run the
`nagios -v`command like:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
If any error is displayed, fix that before restarting the Nagios service.
- Finally restart the Nagios service to apply changes.
service nagios restart
This is a general outline with a real-world example of the process involved in creating a Nagios plugin using a bash script. There may be additional steps or considerations depending on the specific requirements of your plugin.