get_data.pl
A perl script for running via NRPE that returns one row of data saved by run_passive_check.sh.
Size 4.5 kB - File type text/x-perlFile contents
#!/usr/bin/perl
#
# check_plw: Nagios reporting for the Perl Log Watcher
# Copyright (C) 2008 Ithaka Harbors, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
=head1 NAME
get_data - An NRPE program to transfer data collected by run_passive_check.sh.
=head1 SYNOPSIS
B<get_data.pl>
[B<-h>, B<--help>]
[B<-V>, B<--version>]
[B<-t>, B<--timeout>]
[B<-v>, B<--verbose>]
=head1 DESCRIPTION
Configure the "my $datafile = " line, below. Then put a line like:
=over 4
command[get_data]=/usr/lib/nagios/plugins/local/get_data.pl
=back
in your nrpe.cfg. When called by NRPE, this script returns one line of data
from the configured data file, removes that line and save the rest (if any) for
the next time get_data.pl gets run.
Use the nrpe_client.py program to call the NRPE system to run get_data.pl.
=head1 REQUIREMENTS
=over 4
=item B<Nagios::Plugin>
This provides the return codes that Nagios wants.
=back
=head1 COMMAND LINE OPTIONS
=over 4
=item B<-h> or B<--help>
Prints usage information and exits.
=item B<-V> or B<--version>
Prints version information and exits.
=item B<-t> or B<--timeout>
The maximum number of seconds that get_data.pl should run.
=item B<-v> or B<--verbose>
Outputs debugging information on STDERR.
=back
=head1 AUTHOR
Alan Brenner - alan.brenner@ithaka.org; Initial developer.
=head1 BUGS
Undoubtedly there are some in here. I (Alan Brenner) have endevored to keep this
simple.
=head1 SEE ALSO
B<nrpe_client.py> and B<run_passive_check.sh>
=head1 AVAILABILITY
Download get_data.pl from http://tid.ithaka.org/software/nagios.
=head1 COPYRIGHT
Copyright (C) 2008 Ithaka Harbors, Inc.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
=cut
use strict;
use warnings;
use Fcntl ':flock';
use Nagios::Plugin;
use vars qw($PROGNAME $VERSION $datafile $pending);
$PROGNAME = "get_data";
$VERSION = '1.0';
my $np = Nagios::Plugin->new(
usage => "Usage: %s",
version => $VERSION,
plugin => $PROGNAME,
shortname => uc($PROGNAME),
blurb => 'Get data fetched by local checks',
timeout => 30,
);
### Configure this to point to the location run_passive_check.sh stores data
### (without the .txt file extension):
my $datafile = '/var/local/nagios/passive_data';
$np->getopts;
my $verbose = $np->opts->verbose;
alarm $np->opts->timeout;
if (not -f "$datafile.txt" and
(not -f "$datafile.pending" or -z "$datafile.pending")) {
unlink "$datafile.pending" if -z "$datafile.pending";
$np->nagios_exit(OK, 'no data available');
}
if ( -f "$datafile.pending" ) {
warn "pending file found at $datafile.pending\n" if $verbose;
if ( -z "$datafile.pending" ) {
warn "empty pending file\n" if $verbose;
unlink "$datafile.pending";
if (-f "$datafile.txt") {
link "$datafile.txt", "$datafile.pending";
unlink "$datafile.txt";
} else {
$np->nagios_exit(OK, 'no data available');
}
}
} else {
warn "moving $datafile.txt to $datafile.pending\n" if $verbose;
link "$datafile.txt", "$datafile.pending";
unlink "$datafile.txt";
}
open DATA, "<$datafile.pending";
flock DATA, LOCK_EX;
my $pending = <DATA>;
warn "got: $pending" if $verbose;
$np->add_perfdata(label => "data", value => $pending);
$pending = 0;
open TEMP, ">$datafile.temp";
while (<DATA>) {
print TEMP $_;
$pending++;
}
unlink "$datafile.pending";
flock DATA, LOCK_UN;
close DATA;
close TEMP;
link "$datafile.temp", "$datafile.pending" if ($pending);
unlink "$datafile.temp";
if ($pending) {
$np->nagios_exit(OK, "fetched 1 record with $pending more waiting");
} else {
$np->nagios_exit(OK, 'fetched 1 record');
}
Click here to get the file