diff -r e81beebd168f -r a4b752ca44d7 submit-via-nsca.pl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/submit-via-nsca.pl Wed Nov 02 17:52:43 2011 +0100 @@ -0,0 +1,84 @@ +#! /usr/bin/perl +# © 2011 Heiko Schlittermann +# source: https://ssl.schlittermann.de/hg/check-by-nsca + +use 5.010; +use strict; +use warnings; +use Sys::Hostname; +use File::Basename; +use Getopt::Long; +use Pod::Usage; +use Readonly; + +Readonly my $ME => basename $0; + +delete @ENV{ grep /^(LC_|LANG)/ => keys %ENV }; +$ENV{LC_ALL} = "C"; + +my %o = ( + hostname => hostname(), + svcname => "-", + nsca_host => undef, + nsca_port => undef, + debug => undef, +); + +MAIN: { + Getopt::Long::Configure("bundling"); + GetOptions( + "H|hostname=s" => \$o{hostname}, + "S|servicename=s" => \$o{svcname}, + "nsca-host=s" => \$o{nsca_host}, + "p|nsca-port=i" => \$o{nsca_port}, + "h|help" => sub { pod2usage(-verbose => 1, -exit => 0) }, + "man" => sub { + pod2usage( + -verbose => 2, + -exit => 0, + -noperldoc => system("perldoc -V 2>/dev/null 1>/dev/null") + ); + }, + "d|debug" => \$o{debug}, + ) or pod2Usage(); + + my $cmdline = + "send_nsca -H '$o{nsca_host}'" + . (defined $o{nsca_port} ? " -p $o{nsca_port}" : ""); + + $_ = `@ARGV`; + my $rc = $?; + + if ($o{svcname} eq "-") { + /^(?\S+)\s/ or die "$ME: Can't guess servicename!\n"; + $o{svcname} = $+{service}; + } + $_ = join "\t" => $o{hostname}, length($o{svcname}) ? $o{svcname} : (), $_; + + if ($o{debug}) { + print $cmdline, "\n$_\n"; + exit; + } + + open(SEND, "|$cmdline") or die "$ME: Can't open `$cmdline': $!\n"; + print SEND $_; + close(SEND) + or die $! ? "$ME: Error closing `$cmdline': $!\n" + : "$ME: Error status from `$cmdline': $?\n"; + +} + +__END__ + +set +e + +output=$("$@") +rc=$? + +test "$svcname" || svcname="${output%% *}" + +tab=$'\x09' +echo "$hostname$tab$svcname$tab$rc$tab$output" \ + | $send -H "$nsca_host" ${nsca_port:+-p $nsca_port} + +# vim:sts=4 sw=4 aw ai sm et: