lib/Nagios/NRPE/Client.pm untested

filter: Statementstmt 0.0% 0 / 110 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 189 CC 15 SCAR 37.7
CC
15
Cov
0%
create_socket
72.0
run
72.0
new
2.0
CRAP 43.5
1 =head1 NAME
2  
3 Nagios::NRPE::Client - A Nagios NRPE client
4  
5 =head1 SYNOPSIS
6  
7 use Nagios::NRPE::Client;
8  
9 my $client = Nagios::NRPE::Client->new( host => "localhost", check => 'check_cpu');
10 my $response = $client->run();
11 if(defined $response->{error}) {
12 print "ERROR: Couldn't run check " . $client->{check} . " because of: " . $response->{reason} . "\n";
13 } else {
14 print $response->{buffer}."\n";
15 }
16  
17 =head1 DESCRIPTION
18  
19 This Perl Module implements Version 2 and 3 of the NRPE-Protocol. With this module you can execute
20 checks on a remote server.
21  
22 =head1 COPYRIGHT AND LICENSE
23  
24 This software is copyright (c) 2013-2018 by the authors (see L<AUTHORS|https://github.com/stockholmuniversity/Nagios-NRPE/blob/master/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 package Nagios::NRPE::Client;
32  
33 our $VERSION = '2.0.13';
34  
35 use 5.010_000;
36  
37 use strict;
38 use warnings;
39  
40 use Carp;
41 use IO::Socket ();
42 use IO::Socket::INET6 ();
43 use IO::Socket::SSL qw(SSL_VERIFY_NONE $SSL_ERROR);
44 use Socket qw(SOCK_STREAM AF_INET6 AF_INET);
45 use Nagios::NRPE::Utils qw(return_error);
46 use Nagios::NRPE::Packet qw(NRPE_PACKET_VERSION_3
47 NRPE_PACKET_VERSION_2
48 NRPE_PACKET_QUERY
49 MAX_PACKETBUFFER_LENGTH
50 STATE_UNKNOWN
51 STATE_CRITICAL
52 STATE_WARNING
53 STATE_OK);
54  
55 =pod
56  
57 =head1 SUBROUTINES
58  
59 =over 2
60  
61 =item new()
62  
63 Constructor for the Nagios::NRPE::Client Object
64  
65 example
66  
67 my $client = Nagios::NRPE::Client->new( host => "localhost", check => 'check_cpu');
68  
69 Takes a hash of options:
70  
71 * host => <hostname or IP>
72  
73 The hostname or IP on which the NRPE-Server is running
74  
75 * port => <Portnumber>
76  
77 The port number at which the NRPE-Server is listening
78  
79 * timeout => <number in seconds>
80  
81 Timeout for TCP/IP communication
82  
83 * arglist => ["arg","uments"]
84  
85 List of arguments attached to the check
86  
87 * check => "check_command"
88  
89 Command defined in the nrpe.cfg on the NRPE-Server
90  
91 * ssl => 0,1
92  
93 Use or don't use SSL
94  
95 =back
96  
97 =cut
98  
99 sub new
100 {
101 my ($class, %hash) = @_;
102 my $self = {};
103 $self->{arglist} = delete $hash{arglist} || [];
104 $self->{bindaddr} = delete $hash{bindaddr} || 0;
105 $self->{check} = delete $hash{check} || "";
106 $self->{host} = delete $hash{host} || "localhost";
107 $self->{ipv4} = delete $hash{ipv4} || 0;
108 $self->{ipv6} = delete $hash{ipv6} || 0;
109 $self->{port} = delete $hash{port} || 5666;
110 $self->{ssl} = delete $hash{ssl} || 0;
111 $self->{timeout} = delete $hash{timeout} || 30;
112 $self->{SSL_cipher_list} = delete $hash{SSL_cipher_list} || 'ALL:!MD5:@STRENGTH:@SECLEVEL=0';
113 $self->{SSL_verify_mode} = delete $hash{SSL_verify_mode} || SSL_VERIFY_NONE;
114 $self->{SSL_version} = delete $hash{SSL_version};
115  
116 bless $self, $class;
117 }
118  
119 =pod
120  
121 =over 2
122  
123 =item create_socket()
124  
125 Helper function that can create either an INET socket or a SSL socket
126  
127 =back
128  
129 =cut
130  
131 sub create_socket
132 {
133 my ($self) = @_;
134 my $reason;
135 my $socket;
136  
137 my %socket_opts = (
138  
139 # where to connect
140 PeerHost => $self->{host},
141 PeerPort => $self->{port},
142 Timeout => $self->{timeout}
143 );
144 if ($self->{bindaddr})
145 {
146 $socket_opts{LocalAddr} = $self->{bindaddr};
147 }
148 if ($self->{ipv4})
149 {
150 $socket_opts{Domain} = AF_INET;
151 }
152 if ($self->{ipv6})
153 {
154 $socket_opts{Domain} = AF_INET6;
155 }
156 if ($self->{ssl})
157 {
158  
159 $socket_opts{SSL_cipher_list} = $self->{SSL_cipher_list};
160 $socket_opts{SSL_verify_mode} = $self->{SSL_verify_mode};
161 if ($self->{SSL_version})
162 {
163 $socket_opts{SSL_version} = $self->{SSL_version};
164 }
165  
166 $socket = IO::Socket::SSL->new(%socket_opts);
167 if ($SSL_ERROR)
168 {
169 $reason = "$!,$SSL_ERROR";
170 return return_error($reason);
171 }
172  
173 }
174 else
175 {
176 $socket_opts{Proto} = 'tcp';
177 $socket_opts{Type} = SOCK_STREAM;
178 $socket = IO::Socket::INET6->new(%socket_opts);
179 $reason = $@;
180 }
181  
182 if (!$socket)
183 {
184 return return_error($reason);
185 }
186  
187 return $socket;
188  
189 }
190  
191 =pod
192  
193 =over 2
194  
195 =item run()
196  
197 Runs the communication to the server and returns a hash of the form:
198  
199 my $response = $client->run();
200  
201 The output should be a hashref of this form for NRPE V3:
202  
203 {
204 version => NRPE_VERSION,
205 type => RESPONSE_TYPE,
206 crc32 => CRC32_CHECKSUM,
207 code => RESPONSE_CODE,
208 alignment => PACKET_ALIGNMENT,
209 buffer_length => OUTPUT_LENGTH,
210 buffer => CHECK_OUTPUT
211 }
212
213 and this for for NRPE V2:
214  
215 {
216 version => NRPE_VERSION,
217 type => RESPONSE_TYPE,
218 crc32 => CRC32_CHECKSUM,
219 code => RESPONSE_CODE,
220 buffer => CHECK_OUTPUT
221 }
222  
223 =back
224  
225 =cut
226  
227 sub run
228 {
229 my ($self) = @_;
230 my $check;
231 if (scalar @{$self->{arglist}} == 0)
232 {
233 $check = $self->{check};
234 }
235 else
236 {
237 $check = join '!', $self->{check}, @{$self->{arglist}};
238 }
239  
240 my $socket = $self->create_socket();
241 if (ref $socket eq "HASH")
242 {
243 return ($socket);
244 }
245 my $packet = Nagios::NRPE::Packet->new();
246 my $response;
247 my $assembled = $packet->assemble(
248 type => NRPE_PACKET_QUERY,
249 check => $check,
250 version => NRPE_PACKET_VERSION_3
251 );
252 print $socket $assembled;
253 while (<$socket>)
254 {
255 $response .= $_;
256 }
257 close($socket);
258  
259 if($response)
260 {
261 my $responsePacket = $packet->disassemble($response);
262 if ($responsePacket->{packet_version})
263 {
264 if ($responsePacket->{packet_version} == 3)
265 {
266 return $responsePacket;
267 }
268 }
269 else
270 {
271 my $reason = "Unknown response";
272 return return_error($reason);
273 }
274 }
275  
276 $socket = $self->create_socket();
277 if (ref $socket eq "HASH")
278 {
279 return ($socket);
280 }
281 $packet = Nagios::NRPE::Packet->new();
282 $response = undef;
283 $assembled = $packet->assemble(
284 type => NRPE_PACKET_QUERY,
285 check => $check,
286 version => NRPE_PACKET_VERSION_2
287 );
288  
289 print $socket $assembled;
290 while (<$socket>)
291 {
292 $response .= $_;
293 }
294 close($socket);
295  
296 if (!$response)
297 {
298 my $reason = "No output from remote host";
299 return return_error($reason);
300 }
301 return $packet->disassemble($response);
302 }
303  
304 =pod
305  
306 =head1 COPYRIGHT AND LICENSE
307  
308 This software is copyright (c) 2013-2018 by the authors (see AUTHORS file).
309  
310 This is free software; you can redistribute it and/or modify it under
311 the same terms as the Perl 5 programming language system itself.
312  
313 =cut
314  
315 1;