Rev 5399 | 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 thismy $opt_verbose = 1;my $opt_help = 0;my $opt_target;my $opt_rundir = 'run';my @opt_data;my %stats;my $exitStatus = 0;my $exitMsg = "OK";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" => \@opt_data, # 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 optionsError ("Target machine not specified") unless ($opt_target);# Determine the 'run' directoryunless ($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;}## Insert the required performance data#foreach my $items (@opt_data){foreach my $item ( split(/\s*,\s*/, $items)){my $data = 'Unknown';$data = $stats{lc $item} if (defined $stats{lc $item});push @perfData, $item . '=' . $data;}}## Prepare the output#print($exitMsg);if (@perfData){print("|");foreach my $item ( @perfData){print("$item; ");}}print("\n");exit $exitStatus;#-------------------------------------------------------------------------------# 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 checkreturn 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=head1blatNagios - Nagios Plugin for BLAT=head1 SYNOPSISblatNagios.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. 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.=back=head1 EXAMPLEperl blatNagios.pl -target=auawsaarc001 -data=txCount,delCount -data=linkErrors=cut