NAME

Nagios::NRPE::Daemon - A Nagios NRPE Daemon

SYNOPSIS

use Nagios::NRPE::Daemon;
use Nagios::NRPE::Packet qw(STATE_UNKNOWN);
use IPC::Cmd qw(run_forked);

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 $result =
          run_forked($commandlist->{$check}->{bin} . " " . $args,
                     {timeout => 20});
        my $stdout = $result->{stdout};
        chomp $stdout;
        return ($result->{exit_code}, $stdout);
    }
    else
    {
        return (STATE_UNKNOWN, sprintf "No such check: '%s'", $check);
    }

};

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
);

$daemon->start;

DESCRIPTION

A simple daemon implementation with the capabillity to add your own callbacks and hooks in case you want to build your own NRPE Server.

SUBROUTINES

new()

Takes the following options as a hashref:

* listen:

Listen on this IP Address

* port:

Port to listen on

* pid_dir

The pidfile for this daemon

* ssl

Use ssl (1|0)

* commandlist

A hashref of the allowed commands on the daemon

* callback

A sub executed everytime a check should be run. Giving the daemon full control what should happen.

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 $result =
          run_forked($commandlist->{$check}->{bin} . " " . $args,
                     {timeout => 20});
        my $stdout = $result->{stdout};
        chomp $stdout;
        return ($result->{exit_code}, $stdout);
    }
    else
    {
        return (STATE_UNKNOWN, sprintf "No such check: '%s'", $check);
    }
};
start()

Starts the server and enters the Loop listening for packets

commandlist()

A hashref of elements that are valid commands. An example for it is:

"check_cpu" => { bin => "/usr/lib/nagios/plugin/check_cpu",
                 args => "-w 50 -c 80" }

args can contain $ARG1$ elements like normal nrpe.cfg command elements.

create_socket()

A shorthand function returning either an encrypted or unencrypted socket depending on whether ssl is set to 1 or 0.

COPYRIGHT AND LICENSE

This software is copyright (c) 2013-2018 by the authors (see AUTHORS file).

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.