Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»General Articles»How to Create Own Nagios Plugin using Bash Script

    How to Create Own Nagios Plugin using Bash Script

    By RahulApril 10, 20235 Mins Read

    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:

    Advertisement

    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.

    1. 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.

    2. 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 
      
    3. Then, edit the NRPE configuration file “/etc/nagios/nrpe.cfg” and add your command to monitor some disks of your system.

      1
      command[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.

    1. First edit the “/etc/nagios/objects/commands.cfg” configuration file and define the below command:

      1
      2
      3
      4
      define command{
              command_name    check_disk_uses
              command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c check_disk_uses
      }

    2. Then create a new service find to manage that service:

      1
      2
      3
      4
      5
      6
      define service {
              use                             generic-service
              host_name                       192.168.10.100
              service_description             service check
              check_command                   check_disk_uses
              }

    3. 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.

    4. 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.

    nagios NRPE plugin
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Setting Up Permissions in Home Directory on Linux

    Configuring Apache Userdir on Ubuntu and Debian: A Simplified Guide

    How to Permanently Disable Swap Partition in Linux

    View 2 Comments

    2 Comments

    1. GGN on January 5, 2023 3:07 pm

      Rahul Can you please update this with what should come in commands.cfg and services.cfg, That would be a great help

      Reply
    2. Dupont on May 7, 2021 8:59 am

      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.

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • The Difference Between Git Reset –soft, –mixed, and –hard
    • Understanding the Staging Area in Git’s Workflow
    • Python Function with Parameters, Return and Data Types
    • free Command in Linux (Check Memory Uses)
    • Git Rebase: A Practical Guide
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.