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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #!/usr/bin/env bash ### ======================================================================= ### ### A Nagios plugin to check disk uses for a given disk or mount point ### ### Uses: ./check_disk_uses.sh / ### ### ./check_disk_uses.sh /mnt ### ### ./check_disk_uses.sh /dev/sda1 ### ### ======================================================================= ### ### ======================================================================= ### ### FUNCTIONS ### ### ======================================================================= ### calculate_disk_uses(){ # Calculate disk uses USED_DISK_SPACE=`df -h ${MOUNT_POINT} | grep -v Filesystem | awk '{print $5}' | sed 's/%//g'` if (($USED_DISK_SPACE>=0 && $USED_DISK_SPACE<=80)); then echo "OK - ${USED_DISK_SPACE}% of disk space used." exit 0 elif (($USED_DISK_SPACE>=81 && $USED_DISK_SPACE<=90)); then echo "WARNING - ${USED_DISK_SPACE}% of disk space used." exit 1 elif (($USED_DISK_SPACE>=91 && $USED_DISK_SPACE <=100)); then echo "CRITICAL - ${USED_DISK_SPACE}% of disk space used." exit 2 else echo "UNKNOWN - ${USED_DISK_SPACE}% of disk space used." exit 3 fi } ### ======================================================================= ### ### SCRIPT EXECUTION STARTS HERE ### ### ======================================================================= ### if [[ -z "$1" ]] then echo "Missing parameters! Syntax: ./`basename $0` mount_point/disk" exit 3 else MOUNT_POINT=$1 fi calculate_disk_uses ### ======================================================================= ### ### END OF 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.1command[check_disk_uses]=/usr/lib/nagios/plugins/check_disk_uses.sh /dev/sda1
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 check_nrpe
Command
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:1234define command{command_name check_disk_usescommand_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c check_disk_uses}
- Then create a new service find to manage that service:123456define service {use generic-servicehost_name 192.168.10.100service_description service checkcheck_command check_disk_uses}
- 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
Conclusion
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.
2 Comments
Rahul Can you please update this with what should come in commands.cfg and services.cfg, That would be a great help
Hello,
I have a problem with this tutorial. I have a version 3 for check_nrpe. I have a message “check_disk_uses not defined since the server centreon.I have add the command on the file nrpe.cfg, enable to 1 to “dont_blame_nrpe” . Since the client machine (debian) our command works proprely.
Do you have a solution ?
Thanks for our answer.