Tuesday, 8 March 2016

Install NRPE on Ubuntu 15.04, 14.04, 12.04 & LinuxMint



Install NRPE on Ubuntu 15.04, 14.04, 12.04 & LinuxMint
=======================================================

NRPE (Nagios Remote Plugin Executor) is used for executing Nagios plugins on remote client systems. In previous article we had described about installation of Nagios Server on Ubuntu operating system. This article will help you to install NRPE on Ubuntu 15.04, 14.04, 12.04 & LinuxMint systems.


Step 1. Install NRPE and Nagios Plugins

NRPE is available under default apt repositories of Ubuntu systems. Execute the following command to install it

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


Step 2. Configure NRPE

In NRPE configuration, first we need to nrpe to which nagios servers it accepts requests, For example your nagios server ip is 192.168.1.100, then add this ip to allowed hosts list.
Edit NRPE configuration file /etc/nagios/nrpe.cfg and make changes like


 allowed_hosts=127.0.0.1, 192.168.1.100

we can add more Nagios servers in allowed hosts by comma separated list.

Now restart NRPE service. Now its ready to listen requests from Nagios server


$ sudo /etc/init.d/nagios-nrpe-server restart


Let’s login to your Nagios server and verify that your Nagios server can communicate with NRPE service properly. Execute following command from nagios server plugin directory, and we are assuming that your nrpe client-server ip is 192.168.1.11.


# check_nrpe -H 192.168.1.11


NRPE v2.15


The output “NRPE v2.15” shows that nagios server successfully communicated with nrpe.
Step 3. Add Check Commands in NRPE

All the services check commands with the nagios plugins packages, which is by default installed in /usr/lib/nagios/plugins/ for 32 bit systems. 

If some plugins missing then add under /usr/lib/nagios/plugins/

chown -R root:root plugin_name
chmod +x pluginname

Default installation adds few commands in configuration file. Add more commands as per your requirements like below :


root@ip-172-31-15-24:~# vim /etc/nagios/nrpe.cfg


Comment existing and paste below:


command[check_users]=/usr/lib/nagios/plugins/check_users -w 4 -c 6
command[check_load]=/usr/lib/nagios/plugins/check_load -w 55,35,25,15 -c 85,75,65
command[check_disk]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10%
command[check_zombie_procs]=/usr/lib/nagios/plugins/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/lib/nagios/plugins/check_procs -w 140 -c 160
command[check_mem]=/usr/lib/nagios/plugins/check_mem -w 65,45 -c 85,75
command[check_cpu_perf]=/usr/lib/nagios/plugins/check_cpu_perf
command[check_uptime]=/usr/lib/nagios/plugins/check_uptime

:wq


$ sudo /etc/init.d/nagios-nrpe-server restart


How can I configure a service to run at startup
===============================================

sudo update-rc.d nagios-nrpe-server defaults




Step 4. Start/Stop NRPE Service

Use following commands to start, stop or restart nrpe service. Each time we make any changes in configuration file required to restart service

$ sudo /etc/init.d/nagios-nrpe-server stop
$ sudo /etc/init.d/nagios-nrpe-server start
$ sudo /etc/init.d/nagios-nrpe-server restart



Ref:


http://tecadmin.net/install-nrpe-on-ubuntu/#

=========================================
=========================================




Nagios Plugins :

Check_mem



#! /usr/bin/perl -w
#
# $Id: check_mem.pl 8 2008-08-23 08:59:52Z rhomann $
#
# check_mem v1.7 plugin for nagios
#
# uses the output of `free` to find the percentage of memory used
#
# Copyright Notice: GPL
#
# History:
# v1.8 Rouven Homann - rouven.homann@cimt.de
# + added findbin patch from Duane Toler
# + added backward compatibility patch from Timour Ezeev
#
# v1.7 Ingo Lantschner - ingo AT boxbe DOT com
# + adapted for systems with no swap (avoiding divison through 0)
#
# v1.6 Cedric Temple - cedric DOT temple AT cedrictemple DOT info
# + add swap monitoring
#       + if warning and critical threshold are 0, exit with OK
#       + add a directive to exclude/include buffers
#
# v1.5 Rouven Homann - rouven.homann@cimt.de
# + perfomance tweak with free -mt (just one sub process started instead of 7)
# + more code cleanup
#
# v1.4 Garrett Honeycutt - gh@3gupload.com
# + Fixed PerfData output to adhere to standards and show crit/warn values
#
# v1.3 Rouven Homann - rouven.homann@cimt.de
#   + Memory installed, used and free displayed in verbose mode
# + Bit Code Cleanup
#
# v1.2 Rouven Homann - rouven.homann@cimt.de
# + Bug fixed where verbose output was required (nrpe2)
#       + Bug fixed where perfomance data was not displayed at verbose output
# + FindBin Module used for the nagios plugin path of the utils.pm
#
# v1.1 Rouven Homann - rouven.homann@cimt.de
#     + Status Support (-c, -w)
# + Syntax Help Informations (-h)
#       + Version Informations Output (-V)
# + Verbose Output (-v)
#       + Better Error Code Output (as described in plugin guideline)
#
# v1.0 Garrett Honeycutt - gh@3gupload.com
#   + Initial Release
#
use strict;
use FindBin;
FindBin::again();
use lib $FindBin::Bin;
use utils qw($TIMEOUT %ERRORS &print_revision &support);
use vars qw($PROGNAME $PROGVER);
use Getopt::Long;
use vars qw($opt_V $opt_h $verbose $opt_w $opt_c);

$PROGNAME = "check_mem";
$PROGVER = "1.8";

# add a directive to exclude buffers:
my $DONT_INCLUDE_BUFFERS = 0;

sub print_help ();
sub print_usage ();

Getopt::Long::Configure('bundling');
GetOptions ("V"   => \$opt_V, "version"    => \$opt_V,
  "h"   => \$opt_h, "help"       => \$opt_h,
        "v" => \$verbose, "verbose"  => \$verbose,
  "w=s" => \$opt_w, "warning=s"  => \$opt_w,
  "c=s" => \$opt_c, "critical=s" => \$opt_c);

if ($opt_V) {
  print_revision($PROGNAME,'$Revision: '.$PROGVER.' $');
  exit $ERRORS{'UNKNOWN'};
}

if ($opt_h) {
  print_help();
  exit $ERRORS{'UNKNOWN'};
}

print_usage() unless (($opt_c) && ($opt_w));

my ($mem_critical, $swap_critical);
my ($mem_warning, $swap_warning);
($mem_critical, $swap_critical) = ($1,$2) if ($opt_c =~ /([0-9]+)[%]?(?:,([0-9]+)[%]?)?/);
($mem_warning, $swap_warning)   = ($1,$2) if ($opt_w =~ /([0-9]+)[%]?(?:,([0-9]+)[%]?)?/);

# Check if swap params were supplied
$swap_critical ||= 100;
$swap_warning  ||= 100;

# print threshold in output message
my $mem_threshold_output = " (";
my $swap_threshold_output = " (";

if ( $mem_warning > 0 && $mem_critical > 0) {
  $mem_threshold_output .= "W> $mem_warning, C> $mem_critical";
}
elsif ( $mem_warning > 0 ) {
  $mem_threshold_output .= "W> $mem_warning";
}
elsif ( $mem_critical > 0 ) {
  $mem_threshold_output .= "C> $mem_critical";
}

if ( $swap_warning > 0 && $swap_critical > 0) {
  $swap_threshold_output .= "W> $swap_warning, C> $swap_critical";
}
elsif ( $swap_warning > 0 ) {
  $swap_threshold_output .= "W> $swap_warning";
}
elsif ( $swap_critical > 0 )  {
  $swap_threshold_output .= "C> $swap_critical";
}

$mem_threshold_output .= ")";
$swap_threshold_output .= ")";

my $verbose = $verbose;

my ($mem_percent, $mem_total, $mem_used, $swap_percent, $swap_total, $swap_used) = &sys_stats();
my $free_mem = $mem_total - $mem_used;
my $free_swap = $swap_total - $swap_used;

# set output message
my $output = "Memory Usage: ". $mem_percent.'%';
$output .= "Swap Usage: ". $swap_percent.'%';

# set verbose output message
my $verbose_output = "Memory Usage:".$mem_threshold_output.": ". $mem_percent.'% '."- Total: $mem_total MB, used: $mem_used MB, free: $free_mem MB<br>";
$verbose_output .= "Swap Usage:".$swap_threshold_output.": ". $swap_percent.'% '."- Total: $swap_total MB, used: $swap_used MB, free: $free_swap MB<br>";

# set perfdata message
my $perfdata_output = "MemUsed=$mem_percent\%;$mem_warning;$mem_critical";
$perfdata_output .= " SwapUsed=$swap_percent\%;$swap_warning;$swap_critical";


# if threshold are 0, exit with OK
if ( $mem_warning == 0 ) { $mem_warning = 101 };
if ( $swap_warning == 0 ) { $swap_warning = 101 };
if ( $mem_critical == 0 ) { $mem_critical = 101 };
if ( $swap_critical == 0 ) { $swap_critical = 101 };


if ($mem_percent>$mem_critical || $swap_percent>$swap_critical) {
    if ($verbose) { print "<b>CRITICAL: ".$verbose_output."</b>|".$perfdata_output."\n";}
    else { print "<b>CRITICAL: ".$output."</b>|".$perfdata_output."\n";}
    exit $ERRORS{'CRITICAL'};
} elsif ($mem_percent>$mem_warning || $swap_percent>$swap_warning) {
    if ($verbose) { print "<b>WARNING: ".$verbose_output."</b>|".$perfdata_output."\n";}
    else { print "<b>WARNING: ".$output."</b>|".$perfdata_output."\n";}
    exit $ERRORS{'WARNING'};
} else {
    if ($verbose) { print "OK: ".$verbose_output."|".$perfdata_output."\n";}
    else { print "OK: ".$output."|".$perfdata_output."\n";}
    exit $ERRORS{'OK'};
}

sub sys_stats {
    my @memory = split(" ", `free -mt`);
    my $mem_total = $memory[7];
    my $mem_used;
    if ( $DONT_INCLUDE_BUFFERS) { $mem_used = $memory[15]; }
    else { $mem_used = $memory[8];}
    my $swap_total = $memory[18];
    my $swap_used = $memory[19];
    my $mem_percent = ($mem_used / $mem_total) * 100;
    my $swap_percent;
    if ($swap_total == 0) {
  $swap_percent = 0;
    } else {
  $swap_percent = ($swap_used / $swap_total) * 100;
    }
    return (sprintf("%.0f",$mem_percent),$mem_total,$mem_used, sprintf("%.0f",$swap_percent),$swap_total,$swap_used);
}

sub print_usage () {
    print "Usage: $PROGNAME -w <warn> -c <crit> [-v] [-h]\n";
    exit $ERRORS{'UNKNOWN'} unless ($opt_h);
}

sub print_help () {
    print_revision($PROGNAME,'$Revision: '.$PROGVER.' $');
    print "Copyright (c) 2005 Garrett Honeycutt/Rouven Homann/Cedric Temple\n";
    print "\n";
    print_usage();
    print "\n";
    print "-w <MemoryWarn>,<SwapWarn> = Memory and Swap usage to activate a warning message (eg: -w 90,25 ) .\n";
    print "-c <MemoryCrit>,<SwapCrit> = Memory and Swap usage to activate a critical message (eg: -c 95,50 ).\n";
    print "-v = Verbose Output.\n";
    print "-h = This screen.\n\n";
    support();
}

============================



Check_cpu_perf:


#!/bin/bash

# ========================================================================================

# CPU Utilization Statistics plugin for Nagios

#

# Written by    : Andreas Baess based on a script by Steve Bosek

# Release    : 1.1

# Creation date : 3 May 2008

# Package       : DTB Nagios Plugin

# Description   : Nagios plugin (script) to check cpu utilization statistics.

#        This script has been designed and written on Unix plateform (Linux, Aix, Solaris),

#        requiring iostat as external program. The locations of these can easily

#        be changed by editing the variables $IOSTAT at the top of the script.

#        The script is used to query 4 of the key cpu statistics (user,system,iowait,idle)

#        at the same time.

#

# Usage         : ./check_cpu.sh [-w <warn>] [-c <crit]

#                                [-uw <user_cpu warn>] [-uc <user_cpu crit>]

#                                [-sw <sys_cpu warn>] [-sc <sys_cpu crit>]

#                                [-iw <io_wait_cpu warn>] [-ic <io_wait_cpu crit>]

#                                [-i <intervals in second>] [-n <report number>]

# ----------------------------------------------------------------------------------------

# ========================================================================================

#

# HISTORY :

#     Release    |     Date    |    Authors    |     Description

# --------------+---------------+---------------+------------------------------------------

#    1.1    |    03.05.08    | Andreas Baess    | Changed script to use vmstat on Linux because

#               |               |               | iostat does not use integers

#               |               |               | Fixed output to display the IO-wait warning threshhold

# --------------+---------------+---------------+------------------------------------------

#    1.0    |    03.05.08    | Andreas Baess    | Changed script so that thresholds are global

#               |               |               | and output can be parsed by perfprocessing

#               |               |               | changed default warning to 70 critical to 90

# =========================================================================================



# Paths to commands used in this script.  These may have to be modified to match your system setup.



IOSTAT=/usr/bin/vmstat



# Nagios return codes

STATE_OK=0

STATE_WARNING=1

STATE_CRITICAL=2

STATE_UNKNOWN=3



# Plugin parameters value if not define

WARNING_THRESHOLD=${WARNING_THRESHOLD:="70"}

CRITICAL_THRESHOLD=${CRITICAL_THRESHOLD:="90"}

INTERVAL_SEC=${INTERVAL_SEC:="1"}

NUM_REPORT=${NUM_REPORT:="3"}

U_CPU_W=${WARNING_THRESHOLD}

S_CPU_W=${WARNING_THRESHOLD}

IO_CPU_W=${WARNING_THRESHOLD}

U_CPU_C=${CRITICAL_THRESHOLD}

S_CPU_C=${CRITICAL_THRESHOLD}

IO_CPU_C=${CRITICAL_THRESHOLD}



# Plugin variable description

PROGNAME=$(basename $0)

RELEASE="Revision 1.0"

AUTHOR="by Andreas Baess <ab@gun.de> based on a work from Steve Bosek (sbosek@mac.com)"



if [ ! -x $IOSTAT ]; then

    echo "UNKNOWN: iostat not found or is not executable by the nagios user."

    exit $STATE_UNKNOWN

fi



# Functions plugin usage

print_release() {

    echo "$RELEASE $AUTHOR"

}



print_usage() {

    echo ""

    echo "$PROGNAME $RELEASE - CPU Utilization check script for Nagios"

    echo ""

    echo "Usage: check_cpu.sh [flags]"

    echo ""

    echo "Flags:"

    echo "  -w  <number> : Global Warning level in % for user/system/io-wait cpu"

    echo "  -uw <number> : Warning level in % for user cpu"

    echo "  -iw <number> : Warning level in % for IO_wait cpu"

    echo "  -sw <number> : Warning level in % for system cpu"

    echo "  -c  <number> : Global Critical level in % for user/system/io-wait cpu"

    echo "  -uc <number> : Critical level in % for user cpu"

    echo "  -ic <number> : Critical level in % for IO_wait cpu"

    echo "  -sc <number> : Critical level in % for system cpu"

    echo "  -i  <number> : Interval in seconds for iostat (default : 1)"

    echo "  -n  <number> : Number report for iostat (default : 3)"

    echo "  -h  Show this page"

    echo ""

    echo "Usage: $PROGNAME"

    echo "Usage: $PROGNAME --help"

    echo ""

}



print_help() {

    print_usage

        echo ""

        echo "This plugin will check cpu utilization (user,system,iowait,idle in %)"

        echo ""

    exit 0

}



# Parse parameters

while [ $# -gt 0 ]; do

    case "$1" in

        -h | --help)

            print_help

            exit $STATE_OK

            ;;

        -v | --version)

                print_release

                exit $STATE_OK

                ;;

        -w | --warning)

                shift

                WARNING_THRESHOLD=$1

        U_CPU_W=$1

        S_CPU_W=$1

        IO_CPU_W=$1

                ;;

        -c | --critical)

               shift

                CRITICAL_THRESHOLD=$1

        U_CPU_C=$1

        S_CPU_C=$1

        IO_CPU_C=$1

                ;;

        -uw | --uwarn)

               shift

        U_CPU_W=$1

                ;;

        -uc | --ucrit)

               shift

        U_CPU_C=$1

                ;;

        -sw | --swarn)

               shift

        S_CPU_W=$1

                ;;

        -sc | --scrit)

               shift

        S_CPU_C=$1

                ;;

        -iw | --iowarn)

               shift

        IO_CPU_W=$1

                ;;

        -ic | --iocrit)

               shift

        IO_CPU_C=$1

                ;;

        -i | --interval)

               shift

               INTERVAL_SEC=$1

                ;;

        -n | --number)

               shift

               NUM_REPORT=$1

                ;;       

        *)  echo "Unknown argument: $1"

            print_usage

            exit $STATE_UNKNOWN

            ;;

        esac

shift

done



# CPU Utilization Statistics Unix Plateform ( Linux,AIX,Solaris are supported )

case `uname` in

#    Linux ) CPU_REPORT=`iostat -c $INTERVAL_SEC $NUM_REPORT |  tr -s ' ' ';' | sed '/^$/d' | tail -1`

#            CPU_USER=`echo $CPU_REPORT | cut -d ";" -f 2`

#            CPU_SYSTEM=`echo $CPU_REPORT | cut -d ";" -f 4`

#            CPU_IOWAIT=`echo $CPU_REPORT | cut -d ";" -f 5`

#            CPU_IDLE=`echo $CPU_REPORT | cut -d ";" -f 6`

#            ;;

     AIX ) CPU_REPORT=`iostat -t $INTERVAL_SEC $NUM_REPORT | sed -e 's/,/./g'|tr -s ' ' ';' | tail -1`

            CPU_USER=`echo $CPU_REPORT | cut -d ";" -f 4`

            CPU_SYSTEM=`echo $CPU_REPORT | cut -d ";" -f 5`

            CPU_IOWAIT=`echo $CPU_REPORT | cut -d ";" -f 7`

            CPU_IDLE=`echo $CPU_REPORT | cut -d ";" -f 6`

            ;;

      Linux ) CPU_REPORT=`vmstat -n $INTERVAL_SEC $NUM_REPORT | tail -1`

                  CPU_USER=`echo $CPU_REPORT | awk '{ print $13 }'`

            CPU_SYSTEM=`echo $CPU_REPORT | awk '{ print $14 }'`

            CPU_IOWAIT=`echo $CPU_REPORT | awk '{ print $16 }'`

            CPU_IDLE=`echo $CPU_REPORT | awk '{ print $15 }'`

            ;;

      SunOS ) CPU_REPORT=`iostat -c $INTERVAL_SEC $NUM_REPORT | tail -1`

                  CPU_USER=`echo $CPU_REPORT | awk '{ print $1 }'`

            CPU_SYSTEM=`echo $CPU_REPORT | awk '{ print $2 }'`

            CPU_IOWAIT=`echo $CPU_REPORT | awk '{ print $3 }'`

            CPU_IDLE=`echo $CPU_REPORT | awk '{ print $4 }'`

            ;;

    *)         echo "UNKNOWN: `uname` not yet supported by this plugin. Coming soon !"

            exit $STATE_UNKNOWN

        ;;

    esac



# Return



# Are we in a critical state?

if [ ${CPU_IOWAIT} -ge ${IO_CPU_C} -o ${CPU_USER} -ge ${U_CPU_C} -o ${CPU_SYSTEM} -ge ${S_CPU_C}  ];

then

    echo "CPU CRITICAL : user=${CPU_USER}% system=${CPU_SYSTEM}% iowait=${CPU_IOWAIT}% idle=${CPU_IDLE}% | cpu_user=${CPU_USER}%;${U_CPU_W};${U_CPU_C}; cpu_sys=${CPU_SYSTEM}%;${S_CPU_W};${S_CPU_C}; cpu_iowait=${CPU_IOWAIT}%;${IO_CPU_W};${IO_CPU_C}; cpu_idle=${CPU_IDLE}%;"

    exit $STATE_CRITICAL

fi



# Are we in a warning state?

if [ ${CPU_IOWAIT} -ge ${IO_CPU_W} -o ${CPU_USER} -ge ${U_CPU_W} -o ${CPU_SYSTEM} -ge ${S_CPU_W}  ];

then

    echo "CPU WARNING : user=${CPU_USER}% system=${CPU_SYSTEM}% iowait=${CPU_IOWAIT}% idle=${CPU_IDLE}% | cpu_user=${CPU_USER}%;${U_CPU_W};${U_CPU_C}; cpu_sys=${CPU_SYSTEM}%;${S_CPU_W};${S_CPU_C}; cpu_iowait=${CPU_IOWAIT}%;${IO_CPU_W};${IO_CPU_C}; cpu_idle=${CPU_IDLE}%;"

    exit $STATE_WARNING

fi



# If we got this far, everything seems to be OK - IDLE has no threshold

echo "CPU OK : user=${CPU_USER}% system=${CPU_SYSTEM}% iowait=${CPU_IOWAIT}% idle=${CPU_IDLE}% | cpu_user=${CPU_USER}%;${U_CPU_W};${U_CPU_C}; cpu_sys=${CPU_SYSTEM}%;${S_CPU_W};${S_CPU_C}; cpu_iowait=${CPU_IOWAIT}%;${IO_CPU_W};${IO_CPU_C}; cpu_idle=${CPU_IDLE}%;"

exit $STATE_OK

No comments: