Blame | Last modification | View Log | RSS feed
#! /usr/bin/perl######################################################################### Copyright (c) VIX TECHNOLOGY (AUST) LTD## Module name : quarantineNagios.pl# Module type : Nagios Plug in# Compiler(s) : Perl# Environment(s): jats## Description : A Nagios plugin to interface to quarantine tool# This is a simple stand alone Perl Program# It does not require the JATS environment## Usage : See POD at the end of this file##......................................................................#require 5.008_002;use strict;use warnings;use Pod::Usage;use Getopt::Long;use File::Basename;use Data::Dumper;use File::Spec::Functions;use FindBin; # Determine the current directory## Globals#my $VERSION = "1.0.0"; # Update thismy $opt_verbose = 1;my $opt_statsFile;my $opt_help = 0;my @opt_data;my @opt_text;my @opt_warn;my %stats;my $exitMsg = "OK";my @exitMsgTxt;my @perfData;my $setWarning;my $setCritical;#-------------------------------------------------------------------------------# Function : Main Entry## Description :## Inputs :## Returns :#my $result = GetOptions ("help:+" => \$opt_help, # flag, multiple use allowed"manual:3" => \$opt_help, # flag"verbose:+" => \$opt_verbose, # flag"statsFile=s" => \$opt_statsFile, # flag"data=s" => sub{push @opt_data, split(/\s*,\s*/,$_[1])}, # Strings"text=s" => sub{push @opt_text, split(/\s*,\s*/,$_[1])}, # Strings"warn=s" => sub{push @opt_warn, split(/\s*,\s*/,$_[1])}, # Strings);## Process help and manual options#pod2usage(-verbose => 0, -message => "Version: $VERSION") if ($opt_help == 1 || ! $result);pod2usage(-verbose => 1) if ($opt_help == 2 );pod2usage(-verbose => 2) if ($opt_help > 2);## Locate the statistics file# Its location is configied within the jats_quarantine#Error("No statistics file specified") unless defined($opt_statsFile);Error("No statistics file: $opt_statsFile") unless -f $opt_statsFile;## Read in the statistics file# Format is is key:data#Warning('No Statistics available') unless -f $opt_statsFile;open my $fh, $opt_statsFile || Error("Cannot read statistics");while (<$fh>){m~(.*):(.*)~;$stats{lc($1)} = $2;}close $fh;## Determine the Nagios State. Will be CRITICAL if# state is NOT OK# timeStamp is more than 25 hours old#unless (defined $stats{state} && $stats{state} eq 'OK'){$setCritical = 1;$exitMsg = $stats{state} || 'Unknown state' ;}my $dataAge = time() - $stats{timestamp};if ($dataAge > (25 * 60 * 60)){$setCritical = 1;$exitMsg = 'Data too old(Secs):' . $dataAge;}## Insert Text data - not perf data.# This will be displayed as a part of the output#foreach my $item (@opt_text){push @exitMsgTxt, $item . '=' . getDataItem($item);}## Insert the required performance data#foreach my $item (@opt_data){push @perfData, $item . '=' . getDataItem($item);}## Process warning thresholds#foreach my $item (@opt_warn){my ($Name, $warn, $critical) = split(/:/, $item);my $name = lc $Name;my $isCritical;if (exists $stats{$name}){my $value = int($stats{$name});if (defined $critical){if ($value > int($critical) ){$isCritical = 1;$setCritical = 1;push @exitMsgTxt, $Name . ' is Critical';}}if (! $isCritical){if (defined $warn ){if ($value > int($warn) ){$setWarning = 1;push @exitMsgTxt, $Name . ' is Warning';}}else{push @exitMsgTxt, "$item bad format";$setWarning = 1;}}}else{push @exitMsgTxt, "$Name not known";$setWarning = 1;}}## Prepare the output# STATUS, Status Text, Performance Data#print($exitMsg);print (' - ',join(', ', @exitMsgTxt)) if (@exitMsgTxt);print ('|',join('; ', @perfData), ';') if (@perfData);print("\n");exit 2 if $setCritical;exit 1 if $setWarning;exit 0;#-------------------------------------------------------------------------------# Function : getDataItem## Description : Get an item of statistical data## Inputs : $name - Name of the item to get## Returns : The value of the item or the text 'Unknown'#sub getDataItem{my ($name) = @_;$name = lc $name;return 'Unknown' unless exists $stats{$name};return $stats{$name};}#-------------------------------------------------------------------------------# Function : Error## Description : Report an error to Nagios# Returns an UNKNOWN exit code for Nagios## Inputs : Error string## Returns : Does not return#sub Error{print("ERROR: @_\n");exit 3;}#-------------------------------------------------------------------------------# Function : Warning## Description : Report an warning to Nagios# Returns an WARNING exit code for Nagios## Inputs : Error string## Returns : Does not return#sub Warning{print("Warning: @_\n");exit 1;}#-------------------------------------------------------------------------------# Documentation#=pod=head1blatNagios - Nagios Plugin for BLAT=head1 SYNOPSISblatNagios.pl [options]Options:-help - brief help message-help -help - Detailed help message-man - Full documentation-statsFile=path - Path to the statistics file-data=item - Data item to report as performance data. Multiple allowed-text=item - Data item to report as text. Multiple allowed-warn=item:vw:vc - Report warnings for item. Multiple allowed=head1 OPTIONS=over 8=item B<-help>Print a brief help message and exits.=item B<-help -help>Print a detailed help message with an explanation for each option.=item B<-man>Prints the manual page and exits.=item B<-target=name>The name of the target BLAT server. This will be used to locate the PID and STATS files=item B<statsFile=path>The path to the statisics file to be processedMultiple items are comma seperated. The argument may be used more than once.=item B<-text=item>One or more data items to be returned as a part of the text message.Multiple items are comma seperated. The argument may be used more than once.=item B<-warn=item:vw:vc>Report a Nagios error state if the value of the named item exceeds the specified value.vw is the warning threshold. vc is the critical threshold.Multiple items are comma seperated. The argument may be used more than once.=back=head1 EXAMPLEperl quarantineNagios.pl -statsFile=quarantine.stats -data=Quarantine,TotalPackages -warn=QuarantineError:1:1 -warn=S3TransferError:1:1=cut