Created
August 6, 2014 09:02
-
-
Save b10m/d4d4c56ac7fe05e4d1ba to your computer and use it in GitHub Desktop.
A little nsca proxy server that accepts connections and dumps the passive results in the gearman queue for mod_gearman to pick up.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use Config::Simple; | |
use Crypt::Rijndael; | |
use Gearman::Client; | |
use MIME::Base64; | |
use POE; | |
use POE::Component::Server::NSCA; | |
my $c = Config::Simple->new('/etc/nagios/nsca2gearman.cfg.erb') or die Config::Simple->error(); | |
my $cipher = Crypt::Rijndael->new($c->param('gearman'), Crypt::Rijndael::MODE_ECB()); | |
my $gearman = Gearman::Client->new; | |
$gearman->job_servers(qw(localhost:4730)); | |
my $nscad = POE::Component::Server::NSCA->spawn( | |
password => $c->param('nsca'), | |
encryption => 1, | |
); | |
POE::Session->create( | |
package_states => [ | |
'main' => [qw(_start _message)], | |
] | |
); | |
$poe_kernel->run(); | |
exit 0; | |
sub _start { | |
$poe_kernel->post( | |
$nscad->session_id(), | |
'register', | |
event => '_message', | |
context => 'moooo!' | |
); | |
return; | |
} | |
sub _message { | |
my ($message,$context) = @_[ARG0,ARG1]; | |
my $time = time(); | |
my $string = sprintf "type=passive\nstart_time=%d\nfinish_time=%d\nlatency=0.0", $time, $time; | |
if ( $message->{svc_description} ) { | |
$string = sprintf "%s\nhost_name=%s\nservice_description=%s\nreturn_code=%d\noutput=%s", | |
$string, $message->{host_name}, $message->{svc_description}, | |
$message->{return_code}, $message->{plugin_output}; | |
} else { | |
$string = sprintf "%s\nhost_name=%s\nreturn_code=%d\noutput=%s", | |
$string, $message->{host_name}, $message->{return_code}, $message->{plugin_output}; | |
} | |
if ( length($string) % 16 != 0 ) { | |
$string .= "\0" x ( 16 - ( length($string) % 16 ) ); | |
} | |
my $encrypted = $cipher->encrypt($string); | |
my $encoded = encode_base64($encrypted); | |
my $taskset = $gearman->new_task_set; | |
$taskset->add_task( 'check_results' => $encoded ); | |
$taskset->wait; | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment