script/nrpe-server.pl untested

filter: Statementstmt 0.0% 0 / 114 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 200 CC 2 SCAR 9.3
CC
2
Cov
0%
parse_config
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.5
1 #!/usr/local/bin/perl
2  
3 =pod
4  
5 =head1 NAME
6  
7 nrpe-server - A Nagios NRPE Server
8  
9 =head1 SYNOPSIS
10  
11 ./nrpe-server --conf ./nrpe-server.conf --pid /var/run/pid --listen 127.0.0.1 --port 5666
12  
13 =head1 DESCRIPTION
14  
15 This is a simple perl implementation of the nagios-nrpe server you can see SYNOPSIS for how to start it.
16  
17 After it is started it will listen on a given port (either by config or on the commandline) and run checks
18 as defined in your config.
19  
20 NOTE: Options set on the commandline can overwrite options defined in the config file.
21  
22 =head1 COPYRIGHT AND LICENSE
23  
24 This software is copyright (c) 2013-2018 by the authors (see AUTHORS file).
25  
26 This is free software; you can redistribute it and/or modify it under
27 the same terms as the Perl 5 programming language system itself.
28  
29 =cut
30  
31 use 5.010_000;
32  
33 use strict;
34 use warnings;
35  
36 use Data::Dumper;
37 use Getopt::Long;
38 use IO::File;
39 use Pod::Usage;
40 use Config::File;
41 use IPC::Cmd qw(run_forked);
42 use Nagios::NRPE::Daemon;
43 use Nagios::NRPE::Packet qw(STATE_UNKNOWN);
44 use threads;
45  
46 our $VERSION = '2.0.13';
47  
48 use constant {NRPE_CONF_DIR => '/etc/nagios'};
49 my ($listen_cmd, $port_cmd, $config_cmd, $pid_cmd, $ssl_cmd, $adh_cmd);
50  
51 my $result = GetOptions(
52 "l|listen=s" => \$listen_cmd,
53 "p|port=s" => \$port_cmd,
54 "c|config=s" => \$config_cmd,
55 "P|pid=s" => \$pid_cmd,
56 "s|ssl" => \$ssl_cmd,
57 "d|use-adh=i" => \$adh_cmd,
58 "h|help" => sub {
59 pod2usage(
60 -exitval => 0,
61 -verbose => 99,
62 -noperldoc => 1
63 );
64 }
65 );
66  
67 my (
68 $listen, $port, $log_facillity,
69 $pid, $user, $ssl,
70 $nrpe_group, $allowed_hosts, $debug,
71 $command_timeout, $connection_timeout, $commandlist,
72 $SSL_cert_file, $SSL_key_file, $SSL_cipher_list
73 );
74  
75 my $config_hash;
76  
77 if (defined $config_cmd)
78 {
79 $config_hash = Config::File::read_config_file($config_cmd)
80 or die "ERROR: Can't open config. Reason: $!";
81 }
82 elsif (-e NRPE_CONF_DIR . "/nrpe.cfg")
83 {
84 $config_hash = Config::File::read_config_file(NRPE_CONF_DIR . "/nrpe.cfg")
85 or die "ERROR: Can't open config. Reason: $!";
86 }
87  
88 my $include_dir = $config_hash->{include_dir};
89 if ($include_dir)
90 {
91 opendir(my $dh, $include_dir) || die "Can't opendir $include_dir: $!";
92 my @cfgs = grep { /\.cfg$/ && -f "$include_dir/$_" } readdir($dh);
93 closedir $dh;
94 foreach my $cfg (@cfgs)
95 {
96 parse_config("$include_dir/$cfg");
97 }
98 }
99  
100 my $include = $config_hash->{include};
101 if ($include)
102 {
103 parse_config($include);
104 }
105  
106 $commandlist = $config_hash->{command};
107  
108 $listen = $listen_cmd || $config_hash->{server_address} || "127.0.0.1";
109 $port = $port_cmd || $config_hash->{server_port} || "5666";
110 $pid = $pid_cmd || $config_hash->{pid_file} || "/var/run/nagios/nrpe.pid";
111 $ssl = 1 if defined $ssl_cmd;
112 $allowed_hosts = $config_hash->{allowed_hosts} || "0.0.0.0";
113 $command_timeout = $config_hash->{command_timeout} || "60";
114 $connection_timeout = $config_hash->{connection_timeout} || 300;
115 $SSL_cert_file = $config_hash->{SSL_cert_file}
116 || "/etc/ssl/certs/ssl-cert-snakeoil.pem";
117 $SSL_key_file = $config_hash->{SSL_key_file}
118 || "/etc/ssl/private/ssl-cert-snakeoil.key";
119  
120 die "No Commands to execute given." if (not defined $commandlist);
121  
122 foreach (keys %$commandlist)
123 {
124 my @args = split /\ /, $commandlist->{$_};
125 my $hashref = {};
126 $hashref->{bin} = $args[0];
127 my $length = scalar @args;
128 my $argstr = "";
129 for (my $i = 1 ; $i < $length ; $i++)
130 {
131 $argstr .= " " . $args[$i];
132 }
133  
134 $hashref->{args} = $argstr;
135 $commandlist->{$_} = $hashref;
136 }
137 my %dopts = (
138 allowed_hosts => $allowed_hosts,
139 callback => sub {
140 my ($self, $check, @options) = @_;
141 my $commandlist = $self->commandlist();
142 if ($commandlist->{$check})
143 {
144 my $args = $commandlist->{$check}->{args};
145 my $i = 0;
146 foreach (@options)
147 {
148 $i++;
149 $args =~ s/\$ARG$i\$/$_/;
150 }
151 my $result =
152 run_forked($commandlist->{$check}->{bin} . " " . $args,
153 {timeout => 20});
154 my $stdout = $result->{stdout};
155 chomp $stdout;
156 return ($result->{exit_code}, $stdout);
157 }
158 else
159 {
160 return (STATE_UNKNOWN, sprintf "No such check: '%s'", $check);
161 }
162 },
163 commandlist => $commandlist,
164 listen => $listen,
165 pid_dir => $pid,
166 port => $port,
167 ssl => $ssl,
168 );
169  
170  
171 if (defined($adh_cmd))
172 {
173 $dopts{SSL_cipher_list} = "ADH";
174 }
175 else
176 {
177 $dopts{SSL_cert_file} = $SSL_cert_file;
178 $dopts{SSL_key_file} = $SSL_key_file;
179 }
180 my $daemon = Nagios::NRPE::Daemon->new(%dopts);
181  
182 $daemon->start();
183  
184 sub parse_config
185 {
186 my ($file) = @_;
187 my $additional_config_hash = Config::File::read_config_file($file)
188 or die "ERROR: Can't open config. Reason: $!";
189  
190 my $command_hash = $additional_config_hash->{command};
191 if ($command_hash)
192 {
193 foreach my $key (keys %$command_hash)
194 {
195 my $value = $additional_config_hash->{command}->{$key};
196 $config_hash->{command}->{$key} = $value;
197 }
198 delete $additional_config_hash->{command};
199 }
200 $config_hash = {%$config_hash, %$additional_config_hash};
201 }