Subversion Repositories DevTools

Rev

Rev 5398 | Rev 5404 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#! /usr/bin/perl
########################################################################
# Copyright (c) VIX TECHNOLOGY (AUST) LTD
#
# Module name   : blatNagios.pl
# Module type   : Nagios Plug in
# Compiler(s)   : Perl
# Environment(s): jats
#
# Description   : A Nagios plugin to interface to BLAT data
#                 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 this
my $opt_verbose = 1;
my $opt_help = 0;
my $opt_target;
my $opt_rundir = 'run';
my @opt_data;
my @opt_text;
my %stats;
my $exitStatus = 0;
my $exitMsg = "OK";
my @exitMsgTxt;
my @perfData;

#-------------------------------------------------------------------------------
# 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
                "target=s"      => \$opt_target,        # String
                "rundir=s"      => \$opt_rundir,        # String
                "data=s"        => sub{push @opt_data, split(/\s*,\s*/,$_[1])},          # Strings
                "text=s"        => sub{push @opt_text, 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);

# Validate options
Error ("Target machine not specified") unless ($opt_target);

# Determine the 'run' directory
unless ($opt_rundir =~ m~^/~)
{
    $opt_rundir = catdir($FindBin::Bin , $opt_rundir);
}
Error ("Rundir not found: $opt_rundir") unless -d $opt_rundir;

#
# See if the named BLAT target is running
#   It must have a PID pid file
#
Error('BLAT daemon not running') unless testPid('blat');
Error('Transfer daemon not running') unless testPid($opt_target);

#
# Read in the statistics file
#   Format is is key:data
# 
my $statsFile = catfile( $opt_rundir, $opt_target . '.stats');
Warning('No Statistics available') unless -f $statsFile;
open my $fh, $statsFile || Error("Cannot read statistics");
while (<$fh>)
{
    m~(.*):(.*)~;
    $stats{lc($1)} = $2;
}

#
#   Determine the Nagios State. Will be CRITICAL if
#       state is NOT OK
#       timeStamp is more than 5 minutes old
#       
unless (defined $stats{state} && $stats{state} eq 'OK')
{
    $exitStatus = 2;
    $exitMsg = $stats{state} || 'Unknown state' ;
}

my $dataAge = time() - $stats{timestamp};
if ($dataAge > (10 * 60))
{
    $exitStatus = 2;
    $exitMsg = 'Data too old(Secs):' . $dataAge;
}

#
#   Inert 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); 
}

#
# Prepare the output
#   STATUS, Status Text, Performance Data
# 
print($exitMsg);
print (' - ',join(' ', @exitMsgTxt)) if (@exitMsgTxt);
print ('|',join('; ', @perfData), ';') if (@perfData);
print("\n");
exit $exitStatus;

#-------------------------------------------------------------------------------
# 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        : testPid 
#
# Description     : See if a PID exists    
#
# Inputs          : $name               - Name of pid file to examine
#
# Returns         : TRUE - looks OK
#
sub testPid
{
    my ($name) = @_;
    my $pidfile = catfile( $opt_rundir, $name . '.pid');
    open (my $fh, $pidfile) || return 0;
    my ($pid) = <$fh>;
    close $fh;

    if (defined $pid)
    {
        chomp $pid;
        $pid = int $pid;
        if ($pid > 0 )
        {
            # Brute force - unix specific check
            return 1 if -d catdir( '/proc', $pid);
        }
    }
    return 0;
}


#-------------------------------------------------------------------------------
# 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

=head1

blatNagios - Nagios Plugin for BLAT

=head1 SYNOPSIS

 blatNagios.pl [options]

 Options:
    -help              - brief help message
    -help -help        - Detailed help message
    -man               - Full documentation
    -target=name       - Target Machine name
    -rundir=path       - Alternate run directory
    -data=item         - Data item to report as performance data. Multiple allowed
    -text=item         - Data item to report as text. 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<-rundir=path>

The path to the 'run' directory in which the targets PID and STATS files are stored.

If a relative path is provided, then it is relative to this script.

The default value if 'run'.

=item B<-data=item>

One or more data items to be returned as a part of the performance data.

Multiple 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.

=back

=head1 EXAMPLE

perl blatNagios.pl -target=auawsaarc001 -data=txCount,delCount -data=linkErrors

=cut