This commit is contained in:
Zane C. B-H 2019-06-02 03:43:18 -05:00
parent defc8b0dff
commit 2c96112d58
1 changed files with 97 additions and 0 deletions

View File

@ -20,6 +20,9 @@ my $invert;
my $print_search;
my $print_results;
my $help;
my $warn;
my $critical;
my $check;
GetOptions(
's=s' => \$search,
'g=s' => \$options,
@ -31,8 +34,33 @@ GetOptions(
'i' => \$invert,
'h' => \$help,
'help' => \$help,
'n=s' => \$check,
'w=s' => \$warn,
'c=s' => \$critical,
);
# if -n is set, make sure we have -w and -c
if ( defined( $check ) &&
(
( !defined( $warn ) ) ||
( !defined( $critical) )
)
){
warn('-n is set, but either -c or -w is not');
exit 255;
}
#make sure we have a valid value for check if it is defined
if ( defined( $check ) &&
( $check ne 'gt' ) &&
( $check ne 'gte' ) &&
( $check ne 'lt' ) &&
( $check ne 'lte' )
){
warn('-n is set, but is not gt, gte, lt, or lte');
exit 255;
}
# Use module as the base to use allowing
# the other settings to override it if defined.
if (defined( $module )){
@ -61,9 +89,25 @@ if ( $help ){
-S Print the search out after filling it in and exit.
-R Run the search and print it via Data::Dumper.
-i Invert the results.
-n <check> Operate as a nagios style check.
-w <warn> Number of hits to throw a warning at.
-c <crit> Number of hits to throw a critical at.
-h Print the help.
--help Print the help.
In check mode, -w and -c both most be specified. The value
of -n is used to determine the equality to use when evaluating
-w and -c. The table is as below.
gt >
gte >=
lt <
lte <=
The order is as below.
$hits $check $warn/critical
The printed help after this line varies based on the loaded
search template. If one is found it will be printed.
@ -95,6 +139,59 @@ if ($print_results){
print Dumper($results);
exit;
}
# act as a nagios/ichinga2 style check instead of running the output section if requested
if ( defined( $check ) ){
if (
( ref( $results ) ne 'HASH' ) ||
( !defined( $results->{hits} ) )||
( !defined( $results->{hits}{hits} ) )
){
print "UNKNOWN - search failed;\n";
exit 3;
}
my $found=$#{ $results->{hits}{hits} } + 1;
my $is_critical;
my $is_warn;
if ( $check eq 'gt' ){
if ( $found > $critical ){
$is_critical=1;
}elsif( $found > $warn ){
$is_warn=1;
}
}elsif( $check eq 'gte' ){
if ( $found >= $critical ){
$is_critical=1;
}elsif( $found >= $warn ){
$is_warn=1;
}
}elsif( $check eq 'lt' ){
if ( $found < $critical ){
$is_critical=1;
}elsif( $found < $warn ){
$is_warn=1;
}
}elsif( $check eq 'lte' ){
if ( $found <= $critical ){
$is_critical=1;
}elsif( $found <= $warn ){
$is_warn=1;
}
}
if ( $is_warn ){
print "WARNING - ".$found.' '.$check.' '.$warn." hits;\n";
exit 1;
}elsif( $is_critical ){
print "CRITICAL - ".$found.' '.$check.' '.$critical." hits;\n";
exit 2;
}
print "OK - ".$found." hits;\n";
exit 0;
}
# processes the results
$ess->load_output;