Check threshold in perl script

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
fred-zen
Posts: 1
Joined: Fri Mar 27, 2020 8:52 am

Check threshold in perl script

Post by fred-zen »

Hello,

I wish to write a nagios probe for a specific need.
I use the "Nagios::Plugin" library.
For the test, I use the check_threshold function.
When the values of warning and critical are integers, the return of the function is OK, but when at least one of them is a range the return is no longer good.

Can you tell me how to use this function when the thresholds are in range form?

Here is the script I use to test this function

Code: Select all

#! /usr/bin/perl
# nagios: +epn
#--------------------------------------------------------------------------------
#       Nom     : check_threshold.pl
my $VERSION = '0.1';
my $DATE_TXT = '26 mars 2020';
#       Langage : Perl
#       Auteur  : F. BRACHOT
#--------------------------------------------------------------------------------

use strict;
use warnings;
use Nagios::Plugin;
use lib "/usr/lib/perl5/vendor_perl/5.10.0";
use vars qw/ $VERSION /;

my $LICENCE = "Written by Fred BRACHOT on $DATE_TXT";
my $output = '';
my $perfdata = '';
my $status;


my $plugin_nagios = Nagios::Plugin->new(
  version   => $VERSION,
  blurb     => 'Nagios plugin to check thresholds',
  usage     => 'Usage : %s -f <value to test> [-w <warning threshold>] [-c <critical threshold>] [-v]',
  extra     => "\nSample :\n $0 -f '1.7' -w '1:3' -c '3:'",
  license   => $LICENCE
);

$plugin_nagios->add_arg(
  spec     => 'value|f=f',
  help     => 'Value to test',
  required => 1
);

# Seuil d'alerte
$plugin_nagios->add_arg(
  spec     => 'warning|w=s',
  help     => "Number limit to return a warning status.",
  default  => '2:',
  required => 0,
  label => 'THRESHOLD'
);

# Seuil d'alarme
$plugin_nagios->add_arg(
  spec     => 'critical|c=s',
  help     => "Number limit to return a critical status.",
  default  => '5:',
  required => 0,
  label => 'THRESHOLD'
);

# Activer le parsing des options de ligne de commande
$plugin_nagios->getopts;
my $verbose = $plugin_nagios->{opts}->{verbose};

#Active le timeout
#$plugin_nagios->defalarm;


# DEBUG function
sub verbose {
    my $output_code = shift;
    my $text        = shift;
    if ( $verbose >= $output_code ) {
        printf "VERBOSE $output_code ===> %s\n", $text;
    }
}

$output = "Value ".$plugin_nagios->{opts}->{value};
$perfdata = "'Valeur'=$plugin_nagios->{opts}->{value};$plugin_nagios->{opts}->{warning};$plugin_nagios->{opts}->{critical} ";

&verbose( '1', "Value : ".$plugin_nagios->{opts}->{value}." , warning threshold : ".$plugin_nagios->{opts}->{warning}." , critical threshold : ".$plugin_nagios->{opts}->{critical});
$output .= ' |'.$perfdata;

$plugin_nagios->set_thresholds(warning => $plugin_nagios->{opts}->{warning}, critical => $plugin_nagios->{opts}->{critical});
$status = $plugin_nagios->check_threshold($plugin_nagios->{opts}->{value});

$plugin_nagios->nagios_exit($status, "$output");

__END__
return examples :
  • check_threshold.pl -f '1' -w '2:4' -c '5'
    THRESHOLD WARNING - Value 1 |'Valeur'=1;2:4;5
  • check_threshold.pl -f '1' -w '2' -c '5'
    THRESHOLD OK - Value 1 |'Valeur'=1;2;5
Regards,
Fred
User avatar
cdienger
Support Tech
Posts: 5045
Joined: Tue Feb 07, 2017 11:26 am

Re: Check threshold in perl script

Post by cdienger »

Maybe I am misunderstanding, but a WARNING for 'check_threshold.pl -f '1' -w '2:4' -c '5'' would be expected. See https://nagios-plugins.org/doc/guidelin ... HOLDFORMAT.

-w '2:4' means to trigger a warning if the value(1) is <2 or > 4.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
Locked