Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»General Articles»How to Create Own Nagios Plugin using Bash Shell Script

    How to Create Own Nagios Plugin using Bash Shell Script

    RahulBy RahulNovember 1, 20172 Mins ReadUpdated:December 1, 2017

    How to Create Own Nagios Plugin using Bash Shell Scripts. This is useful for monitoring infrastructure as your own setup. This tutorial will help you to understand, how to write a script and use them with NRPE and Nagios for monitoring.

    Step 1 – Understand Return Codes

    Every Nagios plugin must return with a status code which is called return code. On the basis of return codes, Nagios core service takes decisions and appropriate action for the corresponding host or service.

    Hosts:

    Return Code / Host status
    0 => UP
    1 => DOWN
    Other Maintains last known state

    Services:
    Return code / Service status
    0 => OK
    1 => WARNING
    2 => CRITICAL
    3 => UNKNOWN
    Other CRITICAL : unknown return code

    Step 2 – Install NRPE Client

    Let’s install the NRPE client on your system using the following commands.

    sudo apt-get update
    sudo apt-get install nagios-nrpe-server nagios-plugins
    

    The above commands are for Debian based systems. To install NRPE on Redhat based system, visit this tutorial.

    Step 3 – Write A Shell Script

    Now the time to write a shell script to monitor any service on your system. For this example below script will monitor the disk space uses.

    vim /usr/lib/nagios/plugins/check_disk_uses.sh
    

    Add the below script.

    Shell
    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
    49
    50
    51
    52
    #!/bin/bash
     
    ### ======================================================================= ###
    ###     A nagios plugin to check disk uses for given disk or mountpoint     ###
    ###     Uses: ./check_disk_uses.sh /                                        ###
    ###           ./check_disk_uses.sh /mnt                                     ###
    ###           ./check_disk_uses.sh /dev/sda1                                ###
    ### ======================================================================= ###
     
    ### ======================================================================= ###
    ###                         FUNCTIONS                                       ###
    ### ======================================================================= ###
     
    calculate_disk_uses(){
    USED_DISK_SPACE=`df -h ${MOUNT_POINT} | grep -v Filesystem | awk '{print $5}' | sed 's/%//g'`
    case ${USED_DISK_SPACE} in
    [1-80]*)
    echo "OK - ${USED_DISK_SPACE}% of disk space used."
    exit 0
    ;;
    [81-85]*)
    echo "WARNING - ${USED_DISK_SPACE}% of disk space used."
    exit 1
    ;;
    [86-100]*)
    echo "CRITICAL - ${USED_DISK_SPACE}% of disk space used."
    exit 2
    ;;
    *)
    echo "UNKNOWN - ${USED_DISK_SPACE}% of disk space used."
    exit 3
    ;;
    esac
    }
     
    ### ======================================================================= ###
    ###                         SCRIPT EXECUTION START 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                                   ###
    ### ======================================================================= ###

    Now set the execute permission on the new script.

    chmod +x /usr/lib/nagios/plugins/check_disk_uses.sh
    

    Step 4 – Update NRPE Configuration

    Now edit NRPE configuration file /etc/nagios/nrpe.cfg and add your command to monitor some disk of your system.

    command[check_disk_uses]=/usr/lib/nagios/plugins/check_disk_uses.sh /dev/sda1
    

    You can call check_disk_uses command from Nagios server using 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.1.100 is the IP of the NRPE client system.

    ./check_nrpe -H 192.168.1.100 -c check_disk_uses
    
    OK - 22% of disk space used.
    
    nagios NRPE plugin
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleHow to Deploy Ruby App with Nginx and Passenger on Ubuntu and Debian
    Next Article Delete All root User Emails from a Shell in Linux

    Related Posts

    How to Find Django Install Location in Linux

    Updated:April 27, 20221 Min Read

    (Resolved) – ReactJS 404 Error on Page Reload

    2 Mins Read

    Adding a New SSH Key in GitHub

    Updated:April 1, 20223 Mins Read

    13 Best Linux Terminal Emulators and Bash Editors

    8 Mins Read

    How To Install Oracle VirtualBox on Debian 11

    2 Mins Read

    10 Best Linux FTP Clients in 2022

    5 Mins Read

    1 Comment

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

    Recent Posts
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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