Permalink
Fetching contributors…
Cannot retrieve contributors at this time
executable file 183 lines (116 sloc) 3.92 KB
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
use Config::Tiny;
use Getopt::Std;
use IPC::Run qw(harness);
our $VERSION = '0.1';
my %opt;
getopts( 'H:R:c:', \%opt );
my $conffile = $opt{c} || die("Usage: $0 -c <config> [intervals]\n");
my $cfg = Config::Tiny->read($conffile);
my $hostname = $opt{H} || qx{hostname -f};
my @intervals = @ARGV;
my $results = q{};
my $remote_host = $opt{R} || $cfg->{_}->{nsca_host} || die("no remote host specified!\n");
my $send_nsca = harness(
[ 'send_nsca', '-H', $remote_host ],
'<' => \$results,
'>' => sub { },
'2>' => \*STDERR,
);
sub make_sections {
my ( $host, @intervals ) = @_;
return ( $host, map { "$host/$_" } @intervals );
}
sub run_check {
my ( $host, $check, $cmd_string ) = @_;
my @cmd = split( / /, $cmd_string );
my $out;
my $harness = harness(
\@cmd,
'<' => \undef,
'>&' => \$out,
);
$harness->run();
chomp $out;
return sprintf( "%s\t%s\t%s\t%s\n",
$host, $check, $harness->result() || 0, $out, );
}
chomp($hostname);
for my $host ( 'common', $hostname ) {
for my $section ( make_sections( $host, @intervals ) ) {
for my $name ( keys %{ $cfg->{$section} // {} } ) {
$results
= run_check( $hostname, $name, $cfg->{$section}->{$name} );
$send_nsca->run() or warn("send_nsca failed\n");
}
}
}
__END__
=head1 NAME
submit_checks - Run local nagios checks and submit as passive results via nsca
=head1 SYNOPSIS
B<submit_checks> [B<-H> I<host>] [B<-R> I<nsca_host>] B<-c> I<conffile> [I<intervals ...>]
=head1 VERSION
version 0.1
=head1 DESCRIPTION
B<submit_checks> reads a list of checks from a config file, runs them and uses
send_nsca(1) to report their results to a remote nagios server running
nsca(1).
Compared to other passive/active check methods, it should use less resources -
it does not use SSH and even the encrypted nsca session is only opened one
time.
=head1 OPTIONS
=over
=item B<-H> I<hostname>
By default, B<submit_checks> will use the system's FQDN (as reported by C<<
hostname -f >>) when reading the config and reporting checks to the NSCA
server. You can override that hostname with this option.
=item B<-R> I<nsca_host>
Override B<nsca_host> configuration parameter.
=item B<-c> I<conffile>
Read config from I<conffile> Mandatory.
=back
=head1 EXIT STATUS
Zero unless something went completely wrong.
The exit status is not affected by the executed nagios checks.
=head1 CONFIGURATION
I<conffile> is read using Config::Tiny(3pm).
Its first statement must set the B<nsca_host> to the server running the NSCA.
After that, it may contain an arbitrary number of sections, each one either of
the form C<< [hostname] >> or C<< [hostname/interval] >>. The sections define
the checks to be executed, in the form C<< Check Name = /path/to/plugin with
arguments >>.
I<hostname> is either the systems hostname or the specaf value B<commen>,
which means that the checks defined there will be run on any host.
Sections with an I<interval> will only be used when that B<interval> was also
specified on the commandline. Checks in a section without interval are always
run.
=head2 EXAMPLE
nsca_host = nagios.finalrewind.org
# Executed on any host
[common]
Load = /usr/lib/nagios/plugins/check_load -w 15,10,5 -c 30,25,20
Packages = /usr/lib/nagios/plugins/check_packages
Processes = /usr/lib/nagios/plugins/check_procs -w 200 -c 250
Users = /usr/lib/nagios/plugins/check_users -w 14 -c 20
# Same, but only when submit_checks got a 60 as argument
[common/60]
Kernel Image = /usr/lib/nagios/plugins/check_kernel
# Executed only when 'hostname -f' is vm.chaosdorf.de
[vm.chaosdorf.de]
RAID = /usr/lib/nagios/plugins/check_raid
=head1 DEPENDENCIES
=over
=item * Config::Tiny(3pm)
=item * IPC::Run(3pm)
=item * nsca_send(1)
=back
=head1 BUGS AND LIMITATIONS
Unknown.
=head1 AUTHOR
Copyright (C) 2011 by Daniel Friesel E<lt>derf@finalrewind.orgE<gt>
=head1 LICENSE
0. You just DO WHAT THE FUCK YOU WANT TO.