submit-via-nsca.pl
changeset 6 aa38ce4e9c64
parent 5 3ce1738b5b5c
equal deleted inserted replaced
5:3ce1738b5b5c 6:aa38ce4e9c64
     1 #! /usr/bin/perl
       
     2 # © 2011 Heiko Schlittermann <hs@schlittermann.de>
       
     3 # source: https://ssl.schlittermann.de/hg/check-by-nsca
       
     4 
       
     5 use strict;
       
     6 use warnings;
       
     7 use Sys::Hostname;
       
     8 use File::Basename;
       
     9 use Getopt::Long;
       
    10 use Pod::Usage;
       
    11 use Readonly;
       
    12 
       
    13 Readonly my $ME => basename $0;
       
    14 
       
    15 delete @ENV{ grep /^(LC_|LANG)/ => keys %ENV };
       
    16 $ENV{LC_ALL} = "C";
       
    17 
       
    18 my %o = (
       
    19     hostname  => hostname(),
       
    20     svcname   => "-",
       
    21     nsca_host => undef,
       
    22     nsca_port => undef,
       
    23     debug     => undef,
       
    24 );
       
    25 
       
    26 MAIN: {
       
    27     Getopt::Long::Configure("bundling");
       
    28     GetOptions(
       
    29         "H|hostname=s"    => \$o{hostname},
       
    30         "S|servicename=s" => \$o{svcname},
       
    31         "nsca-host=s"     => \$o{nsca_host},
       
    32         "p|nsca-port=i"   => \$o{nsca_port},
       
    33         "h|help"          => sub { pod2usage(-verbose => 1, -exit => 0) },
       
    34         "m|man"           => sub {
       
    35             pod2usage(
       
    36                 -verbose   => 2,
       
    37                 -exit      => 0,
       
    38                 -noperldoc => system("perldoc -V 2>/dev/null 1>/dev/null")
       
    39             );
       
    40         },
       
    41         "d|debug" => \$o{debug},
       
    42     ) or pod2usage();
       
    43 
       
    44     my $cmdline = "send_nsca -H '$o{nsca_host}'"
       
    45       . (defined $o{nsca_port} ? " -p $o{nsca_port}" : "");
       
    46 
       
    47     $_ = `@ARGV`;
       
    48     my $rc = $? >> 8;
       
    49 
       
    50     if ($o{svcname} eq "-") {
       
    51         /^(?<service>\S+)\s/ or die "$ME: Can't guess servicename!\n";
       
    52         $o{svcname} = $+{service};
       
    53     }
       
    54     $_ =
       
    55       join "\t" => $o{hostname},
       
    56       length($o{svcname})
       
    57       ? $o{svcname}
       
    58       : (), $rc, $_;
       
    59 
       
    60     if ($o{debug}) {
       
    61         print $cmdline, "\n$_\n";
       
    62         exit;
       
    63     }
       
    64 
       
    65     open(SEND, "|$cmdline") or die "$ME: Can't open `$cmdline': $!\n";
       
    66     print SEND $_;
       
    67     close(SEND)
       
    68       or die $!
       
    69       ? "$ME: Error closing `$cmdline': $!\n"
       
    70       : "$ME: Error status from `$cmdline': $?\n";
       
    71 
       
    72 }
       
    73 
       
    74 __END__
       
    75 
       
    76 =head1 NAME
       
    77 
       
    78  submit-via-nsca - submit service, or host check results via nsca
       
    79 
       
    80 =head1 SYNOPSIS
       
    81 
       
    82  submit-via nsca --nsca-host {host} [options] -- {check} [check-options]
       
    83 
       
    84 =head1 DESCRIPTION
       
    85 
       
    86 This simple script submits the result of nagios check plugins to an NSCA
       
    87 receiver. The script does not send the result itself, it's merely a
       
    88 wrapper around C<send_nsca>.
       
    89 
       
    90 =head1 OPTIONS
       
    91 
       
    92 =over
       
    93 
       
    94 =item B<-H> | B<--hostname> I<hostname>
       
    95 
       
    96 The hostname to be sent along with the result. (defaults to the local
       
    97 host name).
       
    98 
       
    99 =item B<-S> | B<--servicename> I<servicename>
       
   100 
       
   101 The servicename to be sent along with the result. If a single "-" (dash)
       
   102 is used, the servicename is guessed from service check output, if ""
       
   103 (empty), a host check is submitted. (default: "-")
       
   104 
       
   105 =item B<--nsca-host> I<hostname>
       
   106 
       
   107 The destination host (the host, where the NSCA listener is running).
       
   108 (no default).
       
   109 
       
   110 =item B<--nsca-port> I<port>
       
   111 
       
   112 The destination port. (default depends on the C<send_nsca> utility)
       
   113 
       
   114 =item B<-h> | B<--help>
       
   115 
       
   116 =item B<-m> | B<--man>
       
   117 
       
   118 The long or short help text.
       
   119 
       
   120 =back
       
   121 
       
   122 =head1 AUTHORS
       
   123 
       
   124 Heiko Schlittermann L<mailto:hs@schlittermann.de>, the source can be
       
   125 found at L<https://ssl.schlittermann.de/hg/submit-via-nsca>.