Skip to content

Instantly share code, notes, and snippets.

@mpasternacki
Created December 23, 2011 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpasternacki/1515713 to your computer and use it in GitHub Desktop.
Save mpasternacki/1515713 to your computer and use it in GitHub Desktop.
Hysteresis support for nagios service checks' limit
#!/usr/bin/perl -w
#
# check_with_hysteresis.pl - hysteresis for nagios service checks' limits
#
# Usage: check_with_hysteresis.pl $LASTSERVICESTATE$ default check --- STATE1 check for state1 --- STATE2 check for state2 ...
#
# If $LASTSERVICESTATE$ is STATE1, "check for state1" will be
# executed, if it's STATE2, "check for state2" will be executed, and
# so on; if state is undefined, "default check" wil be executed.
#
# Usage with NRPE (without NRPE arguments, yay!):
#
# nrpe.cfg (simplified - add something like -x /dev/shm -X nfs -i /boot for better effects):
# command[check_all_disks/ok]=/usr/lib/nagios/plugins/check_disk -w 45% -c 34% -A
# command[check_all_disks/warning]=/usr/lib/nagios/plugins/check_disk -w 50% -c 34% -A
# command[check_all_disks/critical]=/usr/lib/nagios/plugins/check_disk -w 50% -c 40% -A
#
# commands.cfg:
# define command {
# command_name check_all_disks
# command_line \
# $USER1$/check_with_hysteresis.pl $LASTSERVICESTATE$ \
# $USER1$/check_nrpe -H $HOSTADDRESS$ -c check_all_disks/ok -t 20 \
# --- WARNING \
# $USER1$/check_nrpe -H $HOSTADDRESS$ -c check_all_disks/warning -t 20 \
# --- CRITICAL \
# $USER1$/check_nrpe -H $HOSTADDRESS$ -c check_all_disks/critical -t 20
# }
#
# In this setup, service goes into WARNING when disk free space drops
# below 45%, but it has to go over 50% to go back to OK, similar with
# CRITICAL.
use strict;
our $LASTSERVICESTATE = shift @ARGV;
our %cmdlines;
our $current = -1; # sentinel for default command
# Split & parse command line
foreach ( @ARGV ) {
if ( $_ eq '---' ) {
undef $current;
} elsif ( !defined $current ) {
$current = $_;
$cmdlines{$current} = [];
} else {
push @{$cmdlines{$current}}, $_;
}
}
# Select correct command
our @command = @{$cmdlines{$LASTSERVICESTATE} || $cmdlines{-1}};
our ( $pid, $out, $rv );
# Exec @command in child process
exec(@command) unless ( $pid=open(PIPE, '-|') );
# Slurp @command's whole output, newlines and all
{ $/ = undef;
binmode PIPE;
$out = <PIPE>; }
# Get return value
close PIPE;
my $rv = $? >> 8;
print "[was:$LASTSERVICESTATE] $out";
exit $rv;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment