| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Nagios::NRPE::Daemon - A Nagios NRPE Daemon | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | use Nagios::NRPE::Daemon; | ||
| 8 | use Nagios::NRPE::Packet qw(STATE_UNKNOWN); | ||
| 9 | use IPC::Cmd qw(run_forked); | ||
| 10 | |||
| 11 | my $callback = sub { | ||
| 12 | my ($self, $check, @options) = @_; | ||
| 13 | my $commandlist = $self->commandlist(); | ||
| 14 | if ($commandlist->{$check}) | ||
| 15 | { | ||
| 16 | my $args = $commandlist->{$check}->{args}; | ||
| 17 | my $i = 0; | ||
| 18 | foreach (@options) | ||
| 19 | { | ||
| 20 | $i++; | ||
| 21 | $args =~ s/\$ARG$i\$/$_/; | ||
| 22 | } | ||
| 23 | my $result = | ||
| 24 | run_forked($commandlist->{$check}->{bin} . " " . $args, | ||
| 25 | {timeout => 20}); | ||
| 26 | my $stdout = $result->{stdout}; | ||
| 27 | chomp $stdout; | ||
| 28 | return ($result->{exit_code}, $stdout); | ||
| 29 | } | ||
| 30 | else | ||
| 31 | { | ||
| 32 | return (STATE_UNKNOWN, sprintf "No such check: '%s'", $check); | ||
| 33 | } | ||
| 34 | |||
| 35 | }; | ||
| 36 | |||
| 37 | my $daemon = Nagios::NRPE::Daemon->new( | ||
| 38 | listen => "127.0.0.1", | ||
| 39 | port => "5666", | ||
| 40 | pid_dir => '/var/run', | ||
| 41 | ssl => 0, | ||
| 42 | commandlist => { | ||
| 43 | "check_cpu" => { | ||
| 44 | bin => "/usr/lib/nagios/plugin/check_cpu", | ||
| 45 | args => "-w 50 -c 80" | ||
| 46 | } | ||
| 47 | }, | ||
| 48 | callback => $callback | ||
| 49 | ); | ||
| 50 | |||
| 51 | $daemon->start; | ||
| 52 | |||
| 53 | =head1 DESCRIPTION | ||
| 54 | |||
| 55 | A simple daemon implementation with the capabillity to add your own callbacks | ||
| 56 | and hooks in case you want to build your own NRPE Server. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | package Nagios::NRPE::Daemon; | ||
| 61 | |||
| 62 | our $VERSION = '2.0.13'; | ||
| 63 | |||
| 64 | use 5.010_000; | ||
| 65 | |||
| 66 | use strict; | ||
| 67 | use warnings; | ||
| 68 | |||
| 69 | use Carp; | ||
| 70 | use IO::Socket::INET6; | ||
| 71 | use Nagios::NRPE::Packet qw(NRPE_PACKET_VERSION_3 | ||
| 72 | NRPE_PACKET_VERSION_2 | ||
| 73 | NRPE_PACKET_RESPONSE | ||
| 74 | MAX_PACKETBUFFER_LENGTH | ||
| 75 | STATE_UNKNOWN | ||
| 76 | STATE_CRITICAL | ||
| 77 | STATE_WARNING | ||
| 78 | STATE_OK); | ||
| 79 | use Net::Subnet; | ||
| 80 | use Time::HiRes qw(usleep); | ||
| 81 | |||
| 82 | =pod | ||
| 83 | |||
| 84 | =head1 SUBROUTINES | ||
| 85 | |||
| 86 | =over | ||
| 87 | |||
| 88 | =item new() | ||
| 89 | |||
| 90 | Takes the following options as a hashref: | ||
| 91 | |||
| 92 | * listen: | ||
| 93 | |||
| 94 | Listen on this IP Address | ||
| 95 | |||
| 96 | * port: | ||
| 97 | |||
| 98 | Port to listen on | ||
| 99 | |||
| 100 | * pid_dir | ||
| 101 | |||
| 102 | The pidfile for this daemon | ||
| 103 | |||
| 104 | * ssl | ||
| 105 | |||
| 106 | Use ssl (1|0) | ||
| 107 | |||
| 108 | * commandlist | ||
| 109 | |||
| 110 | A hashref of the allowed commands on the daemon | ||
| 111 | |||
| 112 | * callback | ||
| 113 | |||
| 114 | A sub executed everytime a check should be run. Giving the daemon full control what should happen. | ||
| 115 | |||
| 116 | my $callback = sub { | ||
| 117 | my ($self, $check, @options) = @_; | ||
| 118 | my $commandlist = $self->commandlist(); | ||
| 119 | if ($commandlist->{$check}) | ||
| 120 | { | ||
| 121 | my $args = $commandlist->{$check}->{args}; | ||
| 122 | my $i = 0; | ||
| 123 | foreach (@options) | ||
| 124 | { | ||
| 125 | $i++; | ||
| 126 | $args =~ s/\$ARG$i\$/$_/; | ||
| 127 | } | ||
| 128 | my $result = | ||
| 129 | run_forked($commandlist->{$check}->{bin} . " " . $args, | ||
| 130 | {timeout => 20}); | ||
| 131 | my $stdout = $result->{stdout}; | ||
| 132 | chomp $stdout; | ||
| 133 | return ($result->{exit_code}, $stdout); | ||
| 134 | } | ||
| 135 | else | ||
| 136 | { | ||
| 137 | return (STATE_UNKNOWN, sprintf "No such check: '%s'", $check); | ||
| 138 | } | ||
| 139 | }; | ||
| 140 | |||
| 141 | =back | ||
| 142 | |||
| 143 | =cut | ||
| 144 | |||
| 145 | sub new | ||
| 146 | { | ||
| 147 | my ($class, %hash) = @_; | ||
| 148 | my $self = {}; | ||
| 149 | |||
| 150 | $self->{listen} = delete $hash{listen} || "0.0.0.0"; | ||
| 151 | $self->{port} = delete $hash{port} || "5666"; | ||
| 152 | $self->{pid_dir} = delete $hash{pid_dir} || "/var/run"; | ||
| 153 | $self->{ssl} = delete $hash{ssl} || 0; | ||
| 154 | $self->{SSL_cert_file} = delete $hash{SSL_cert_file} || undef; | ||
| 155 | $self->{SSL_key_file} = delete $hash{SSL_key_file} || undef; | ||
| 156 | $self->{SSL_cipher_list} = delete $hash{SSL_cipher_list} || undef; | ||
| 157 | $self->{Timeout} = delete $hash{Timeout} || 1; | ||
| 158 | $self->{commandlist} = delete $hash{commandlist} || {}; | ||
| 159 | $self->{callback} = delete $hash{callback} || sub { }; | ||
| 160 | |||
| 161 | foreach my $address (split(",", $hash{allowed_hosts})) | ||
| 162 | { | ||
| 163 | if ( $address !~ m#/\d{1,3}$#) { | ||
| 164 | my $suffix; | ||
| 165 | if ( $address =~ m/:/) { | ||
| 166 | $suffix = 128; | ||
| 167 | } else { | ||
| 168 | $suffix = 32; | ||
| 169 | }; | ||
| 170 | push(@{$self->{allowed_hosts}},"$address/$suffix") | ||
| 171 | } else { | ||
| 172 | push(@{$self->{allowed_hosts}},$address); | ||
| 173 | } | ||
| 174 | } | ||
| 175 | delete $hash{allowed_hosts}; | ||
| 176 | $self->{matcher} = subnet_matcher @{$self->{allowed_hosts}}; | ||
| 177 | |||
| 178 | bless $self, $class; | ||
| 179 | } | ||
| 180 | |||
| 181 | =pod | ||
| 182 | |||
| 183 | |||
| 184 | =over | ||
| 185 | |||
| 186 | =item start() | ||
| 187 | |||
| 188 | Starts the server and enters the Loop listening for packets | ||
| 189 | |||
| 190 | =back | ||
| 191 | |||
| 192 | =cut | ||
| 193 | |||
| 194 | sub start | ||
| 195 | { | ||
| 196 | my $self = shift; | ||
| 197 | my $packet = Nagios::NRPE::Packet->new(); | ||
| 198 | my $callback = $self->{callback}; | ||
| 199 | my ($socket, $s); | ||
| 200 | |||
| 201 | $socket = $self->create_socket(); | ||
| 202 | |||
| 203 | while (1) | ||
| 204 | { | ||
| 205 | while (($s = $socket->accept())) | ||
| 206 | { | ||
| 207 | my $peerhost = $s->peerhost; | ||
| 208 | |||
| 209 | if ($peerhost =~ /^::ffff:(.+)$/) { | ||
| 210 | $peerhost = $1; | ||
| 211 | }; | ||
| 212 | if (!$self->{matcher}->($peerhost)){ | ||
| 213 | # We found that ignoring the request created the most | ||
| 214 | # consistant errors from the client-side - a time-out. | ||
| 215 | last; | ||
| 216 | }; | ||
| 217 | |||
| 218 | my $request; | ||
| 219 | $s->sysread($request, 1036); | ||
| 220 | my $unpacked_request = $packet->disassemble($request); | ||
| 221 | my $buffer = $unpacked_request->{buffer}; | ||
| 222 | my $version = $unpacked_request->{packet_version}; | ||
| 223 | if (!$version) | ||
| 224 | { | ||
| 225 | # Ignore non valid NRPE Packets. | ||
| 226 | close($s); | ||
| 227 | last; | ||
| 228 | }; | ||
| 229 | my ($command, @options) = split /!/, $buffer; | ||
| 230 | |||
| 231 | my ($code, $return) = $self->{callback}($self, $command, @options); | ||
| 232 | eval { | ||
| 233 | print $s $packet->assemble( | ||
| 234 | version => $version, | ||
| 235 | type => NRPE_PACKET_RESPONSE, | ||
| 236 | result_code => $code, | ||
| 237 | check => $return | ||
| 238 | ); | ||
| 239 | }; | ||
| 240 | |||
| 241 | close($s); | ||
| 242 | } | ||
| 243 | } | ||
| 244 | } | ||
| 245 | |||
| 246 | =pod | ||
| 247 | |||
| 248 | =over | ||
| 249 | |||
| 250 | =item commandlist() | ||
| 251 | |||
| 252 | A hashref of elements that are valid commands. | ||
| 253 | An example for it is: | ||
| 254 | |||
| 255 | "check_cpu" => { bin => "/usr/lib/nagios/plugin/check_cpu", | ||
| 256 | args => "-w 50 -c 80" } | ||
| 257 | |||
| 258 | C<args> can contain $ARG1$ elements like normal nrpe.cfg command elements. | ||
| 259 | |||
| 260 | =back | ||
| 261 | |||
| 262 | =cut | ||
| 263 | |||
| 264 | sub commandlist | ||
| 265 | { | ||
| 266 | my $self = shift; | ||
| 267 | return $self->{commandlist}; | ||
| 268 | } | ||
| 269 | |||
| 270 | =pod | ||
| 271 | |||
| 272 | =over | ||
| 273 | |||
| 274 | =item create_socket() | ||
| 275 | |||
| 276 | A shorthand function returning either an encrypted or unencrypted socket | ||
| 277 | depending on whether ssl is set to 1 or 0. | ||
| 278 | |||
| 279 | =back | ||
| 280 | |||
| 281 | =cut | ||
| 282 | |||
| 283 | sub create_socket | ||
| 284 | { | ||
| 285 | my $self = shift; | ||
| 286 | my $socket; | ||
| 287 | |||
| 288 | if ($self->{ssl}) | ||
| 289 | { | ||
| 290 | eval { | ||
| 291 | # required for new IO::Socket::SSL versions | ||
| 292 | require IO::Socket::SSL; | ||
| 293 | IO::Socket::SSL->import(); | ||
| 294 | IO::Socket::SSL::set_ctx_defaults(SSL_verify_mode => 0); | ||
| 295 | }; | ||
| 296 | my $options = { | ||
| 297 | Listen => 5, | ||
| 298 | LocalAddr => $self->{listen}, | ||
| 299 | LocalPort => $self->{port}, | ||
| 300 | Proto => 'tcp', | ||
| 301 | Reuse => 1, | ||
| 302 | SSL_verify_mode => 0x01, | ||
| 303 | Timeout => $self->{Timeout}, | ||
| 304 | Type => SOCK_STREAM | ||
| 305 | }; | ||
| 306 | if ($self->{SSL_cipher_list}) | ||
| 307 | { | ||
| 308 | $options->{SSL_cipher_list} = $self->{SSL_cipher_list}; | ||
| 309 | } | ||
| 310 | if ($self->{SSL_cert_file} && $self->{SSL_key_file}) | ||
| 311 | { | ||
| 312 | $options->{SSL_cert_file} = $self->{SSL_cert_file}; | ||
| 313 | $options->{SSL_key_file} = $self->{SSL_key_file}; | ||
| 314 | } | ||
| 315 | $socket = IO::Socket::SSL->new(%{$options}) | ||
| 316 | or die(IO::Socket::SSL::errstr()); | ||
| 317 | } | ||
| 318 | else | ||
| 319 | { | ||
| 320 | $socket = IO::Socket::INET6->new( | ||
| 321 | Listen => 5, | ||
| 322 | LocalAddr => $self->{listen}, | ||
| 323 | LocalPort => $self->{port}, | ||
| 324 | Reuse => 1, | ||
| 325 | Proto => 'tcp', | ||
| 326 | Type => SOCK_STREAM | ||
| 327 | ) or die "ERROR: $@ \n"; | ||
| 328 | } | ||
| 329 | return $socket; | ||
| 330 | } | ||
| 331 | |||
| 332 | =pod | ||
| 333 | |||
| 334 | =head1 COPYRIGHT AND LICENSE | ||
| 335 | |||
| 336 | This software is copyright (c) 2013-2018 by the authors (see L<AUTHORS|https://github.com/stockholmuniversity/Nagios-NRPE/blob/master/AUTHORS> file). | ||
| 337 | |||
| 338 | This is free software; you can redistribute it and/or modify it under | ||
| 339 | the same terms as the Perl 5 programming language system itself. | ||
| 340 | |||
| 341 | =cut | ||
| 342 | |||
| 343 | 1; |