|
1 #! /usr/bin/perl |
|
2 # © 2011 Heiko Schlittermann <hs@schlittermann.de> |
|
3 # source: https://ssl.schlittermann.de/hg/check-by-nsca |
|
4 |
|
5 use 5.010; |
|
6 use strict; |
|
7 use warnings; |
|
8 use Sys::Hostname; |
|
9 use File::Basename; |
|
10 use Getopt::Long; |
|
11 use Pod::Usage; |
|
12 use Readonly; |
|
13 |
|
14 Readonly my $ME => basename $0; |
|
15 |
|
16 delete @ENV{ grep /^(LC_|LANG)/ => keys %ENV }; |
|
17 $ENV{LC_ALL} = "C"; |
|
18 |
|
19 my %o = ( |
|
20 hostname => hostname(), |
|
21 svcname => "-", |
|
22 nsca_host => undef, |
|
23 nsca_port => undef, |
|
24 debug => undef, |
|
25 ); |
|
26 |
|
27 MAIN: { |
|
28 Getopt::Long::Configure("bundling"); |
|
29 GetOptions( |
|
30 "H|hostname=s" => \$o{hostname}, |
|
31 "S|servicename=s" => \$o{svcname}, |
|
32 "nsca-host=s" => \$o{nsca_host}, |
|
33 "p|nsca-port=i" => \$o{nsca_port}, |
|
34 "h|help" => sub { pod2usage(-verbose => 1, -exit => 0) }, |
|
35 "man" => sub { |
|
36 pod2usage( |
|
37 -verbose => 2, |
|
38 -exit => 0, |
|
39 -noperldoc => system("perldoc -V 2>/dev/null 1>/dev/null") |
|
40 ); |
|
41 }, |
|
42 "d|debug" => \$o{debug}, |
|
43 ) or pod2Usage(); |
|
44 |
|
45 my $cmdline = |
|
46 "send_nsca -H '$o{nsca_host}'" |
|
47 . (defined $o{nsca_port} ? " -p $o{nsca_port}" : ""); |
|
48 |
|
49 $_ = `@ARGV`; |
|
50 my $rc = $?; |
|
51 |
|
52 if ($o{svcname} eq "-") { |
|
53 /^(?<service>\S+)\s/ or die "$ME: Can't guess servicename!\n"; |
|
54 $o{svcname} = $+{service}; |
|
55 } |
|
56 $_ = join "\t" => $o{hostname}, length($o{svcname}) ? $o{svcname} : (), $_; |
|
57 |
|
58 if ($o{debug}) { |
|
59 print $cmdline, "\n$_\n"; |
|
60 exit; |
|
61 } |
|
62 |
|
63 open(SEND, "|$cmdline") or die "$ME: Can't open `$cmdline': $!\n"; |
|
64 print SEND $_; |
|
65 close(SEND) |
|
66 or die $! ? "$ME: Error closing `$cmdline': $!\n" |
|
67 : "$ME: Error status from `$cmdline': $?\n"; |
|
68 |
|
69 } |
|
70 |
|
71 __END__ |
|
72 |
|
73 set +e |
|
74 |
|
75 output=$("$@") |
|
76 rc=$? |
|
77 |
|
78 test "$svcname" || svcname="${output%% *}" |
|
79 |
|
80 tab=$'\x09' |
|
81 echo "$hostname$tab$svcname$tab$rc$tab$output" \ |
|
82 | $send -H "$nsca_host" ${nsca_port:+-p $nsca_port} |
|
83 |
|
84 # vim:sts=4 sw=4 aw ai sm et: |