Skip to content

Commit

Permalink
lib/Nagios/NRPE/Daemon.pm: complete daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas-marschke committed Jan 12, 2013
1 parent 1656772 commit 75744f8
Showing 1 changed file with 99 additions and 35 deletions.
134 changes: 99 additions & 35 deletions lib/Nagios/NRPE/Daemon.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,42 @@ Nagios::NRPE::Daemon - A Nagios NRPE Daemon
=head1 SYNOPSIS
use Nagios::NRPE::Daemon;
use IPC::Cmd qw(can_run run run_forked);
my $commandlist = {
"check_cpu" => { bin => "/usr/lib/nagios/plugin/check_cpu",
args => "-w 50 -c 80" }
# create the commandlist we accept
my $commandlist =
};
my $callback = sub {
my ($self,$check,@options) = @_;
my $commandlist = $self->commandlist();
if ($commandlist->{$check}) {
my $args = $commandlist->{$check}->{args};
my $i = 0;
foreach (@options) {
$i++;
$args =~ "s/\$ARG$i\$/$_/";
}
my $buffer;
if (scalar run(command => $commandlist->{$check}->{bin} . " " . $args,
verbose => 0,
buffer => \$buffer,
timeout => 20)) {
return $buffer;
}
}
};
my $daemon = Nagios::NRPE::Daemon->new(
listen => "127.0.0.1",
port => "5666",
pid_dir => '/var/run',
ssl => 0,
commandlist => {
"check_cpu" => { bin => "/usr/lib/nagios/plugin/check_cpu",
args => "-w 50 -c 80" }
},
callback => $callback
);
=head1 DESCRIPTION
=cut
Expand Down Expand Up @@ -46,53 +76,87 @@ sub new {
$self->{pid_dir} = delete $hash{pid_dir} || "/var/run";
$self->{ssl} = delete $hash{ssl} || 0;
$self->{commandlist} = delete $hash{commandlist} || {};
$self->{callback} = delete $hash{callback} || sub{};

bless $self,$class;
}

sub start{
my $self = shift;
my $socket;
my $packet = Nagios::NRPE::Packet->new();
my $callback = $self->{callback};
my ($socket,$s);
$socket = $self->create_socket();

while (1) {
while(($s = $socket->accept())) {
my $request;
$s->recv($request,1036);
my $unpacked_request = $packet->deassemble($request);
my $buffer = $unpacked_request->{buffer};
my ($command,@options) = split /!/,$buffer;

my $return = $self->{callback}($self,$command,@options);

print Dumper($unpacked_request);
print $s $packet->assemble(version =>NRPE_PACKET_VERSION_2,
type => NRPE_PACKET_RESPONSE,
check => $return
);

close($s);
}
}
}

sub stop {
my $self = shift;
}

sub commandlist {
my $self = shift;
return $self->{commandlist};
}

sub create_socket {
my $self = shift;
my $socket;

if ($self->{ssl}) {
# eval {
# # required for new IO::Socket::SSL versions
# require IO::Socket::SSL;
# IO::Socket::SSL->import();
# IO::Socket::SSL::set_ctx_defaults( SSL_verify_mode => 0 );
# };
# $socket = IO::Socket::SSL->new($self->{host}.':'.$self->{port})
# or die(IO::Socket::SSL::errstr());
eval {
# required for new IO::Socket::SSL versions
require IO::Socket::SSL;
IO::Socket::SSL->import();
IO::Socket::SSL::set_ctx_defaults( SSL_verify_mode => 0 );
};
$socket = IO::Socket::SSL->new(
Listen => 5,
LocalAddr => $self->{host},
LocalPort => $self->{port},
Proto => 'tcp',
Reuse => 1,
SSL_verify_mode => 0x01,
Type => SOCK_STREAM)
or die(IO::Socket::SSL::errstr());
} else {
$socket = IO::Socket::INET->new(
Listen => 5,
LocalAddr => $self->{host},
LocalPort => $self->{port},
Listen => 100,
Reuse => 1,
Proto => 'tcp',
Type => SOCK_STREAM) or die "ERROR: $@ \n";
}
$socket->listen();
$socket->autoflush(1);
my $client;
return $socket;
}
1;








while ( $client = $socket->accept() ) {
my $response;
while (<$client>) {

print Dumper($packet->deassemble($_));
$response .= $_;
}
print $client $packet->assemble(type => NRPE_PACKET_RESPONSE,
check => "Thanks This was Helpfull!",
version => NRPE_PACKET_VERSION_2 );
close $client;
last;
}
$client->close;
print "Finished communication!\n";
}

sub stop {
my $self = shift;
}
1;

0 comments on commit 75744f8

Please sign in to comment.