YATG/Store/NSCA.pm untested

filter: Statementstmt 0.0% 0 / 131 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 53 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 249 CC 30 SCAR 66.1
CC
30
Cov
0%
store
870.0
echo
6.0
BEGIN
2.0
CRAP 742.6
1 package YATG::Store::NSCA;
2 {
3 $YATG::Store::NSCA::VERSION = '5.140510';
4 }
5  
6 use strict;
7 use warnings FATAL => 'all';
8  
9 use YATG::SharedStorage;
10 YATG::SharedStorage->factory(qw( ifOperStatus ifInErrors ifInDiscards ));
11  
12 # initialize cache of previous run's data
13 YATG::SharedStorage->ifOperStatus({});
14 YATG::SharedStorage->ifInErrors({});
15 YATG::SharedStorage->ifInDiscards({});
16  
17 sub echo { main::to_log(shift) if $ENV{YATG_DEBUG} }
18  
19 sub store {
20 my ($config, $stamp, $results) = @_;
21  
22 my $ignore_ports = $config->{nsca}->{ignore_ports};
23 my $ignore_descr = $config->{nsca}->{ignore_descr};
24  
25 my $ignore_status_descr = $config->{nsca}->{ignore_status_descr};
26 my $ignore_error_descr = $config->{nsca}->{ignore_error_descr};
27 my $ignore_discard_descr = $config->{nsca}->{ignore_discard_descr};
28  
29 my $send_nsca_cmd = $config->{nsca}->{send_nsca_cmd};
30 my $send_nsca_cfg = $config->{nsca}->{config_file};
31 my $service_prefix = $config->{nsca}->{service_prefix};
32  
33 my $ifOperStatusCache = YATG::SharedStorage->ifOperStatus();
34 my $ifInErrorsCache = YATG::SharedStorage->ifInErrors();
35 my $ifInDiscardsCache = YATG::SharedStorage->ifInDiscards();
36  
37 # TODO: should be configurable
38 my $threshold = int($config->{yatg}->{interval} / 60);
39  
40 my $cache = YATG::SharedStorage->cache();
41 my $nsca_server = $config->{nsca}->{nsca_server}
42 or die "Must specify an nsca server in configuration.\n";
43 my $nsca_port = $config->{nsca}->{nsca_port};
44  
45 # results look like this:
46 # $results->{device}->{leaf}->{port} = {value}
47 # build instead
48 # $status->{device}->{port}->{leaf} = {value}
49  
50 my $status = {};
51 foreach my $device (keys %$results) {
52 foreach my $leaf (keys %{$results->{$device}}) {
53 foreach my $port (keys %{$results->{$device}->{$leaf}}) {
54 $status->{$device}->{$port}->{$leaf}
55 = $results->{$device}->{$leaf}->{$port};
56 } # port
57 } # leaf
58 } # device
59  
60 # get handle in outer scope
61 open my $oldout, '>&', \*STDOUT or die "Can't dup STDOUT: $!";
62  
63 # back up STDOUT then redirect it to quieten send_nsca command
64 unless ($ENV{YATG_DEBUG} || $config->{yatg}->{debug}) {
65 open STDOUT, '>', '/dev/null' or die "Can't redirect STDOUT: $!";
66 }
67  
68 # open connection to send_nsca
69 open(my $send_nsca, '|-', $send_nsca_cmd,
70 '-H', $nsca_server, '-p', $nsca_port,
71 '-c', $send_nsca_cfg, '-d', '!', '-to', 1)
72 or die "can't fork send_nsca: $!";
73  
74 # build and send report for each host
75 foreach my $device (keys %$status) {
76 my $status_report = q{}; # combine the error messages to fit in nagios report
77 my $errors_report = q{}; # combine the error messages to fit in nagios report
78 my $discards_report = q{}; # combine the error messages to fit in nagios report
79  
80 my $tot_oper = 0;
81 my $tot_down = 0;
82 my $tot_err = 0;
83 my $tot_with_err = 0;
84 my $tot_dis = 0;
85 my $tot_with_dis = 0;
86  
87 my @ports_list = exists $cache->{'interfaces_for'}->{$device}
88 ? keys %{ $cache->{'interfaces_for'}->{$device} }
89 : keys %{ $status->{$device} };
90  
91 #use Data::Printer;
92 #p $status->{$device};
93  
94 foreach my $port (@ports_list) {
95 next if $port =~ m/$ignore_ports/;
96  
97 my $ifOperStatus = $status->{$device}->{$port}->{ifOperStatus};
98 my $ifInErrors = $status->{$device}->{$port}->{ifInErrors};
99 my $ifInDiscards = $status->{$device}->{$port}->{ifInDiscards};
100  
101 my $ifAlias = $status->{$device}->{$port}->{ifAlias} || '';
102 next if length $ifAlias and $ifAlias =~ m/$ignore_descr/;
103  
104 my $skip_oper = (length $ifAlias and $ignore_status_descr
105 and $ifAlias =~ m/$ignore_status_descr/) ? 1 : 0;
106 my $skip_err = (length $ifAlias and $ignore_error_descr
107 and $ifAlias =~ m/$ignore_error_descr/) ? 1 : 0;
108 my $skip_disc = (length $ifAlias and $ignore_discard_descr
109 and $ifAlias =~ m/$ignore_discard_descr/) ? 1 : 0;
110  
111 if (exists $status->{$device}->{$port}->{ifOperStatus}) {
112 if (not $skip_oper and $ifOperStatus !~ m/^(?:up|dormant)/) {
113 $status_report ||= 'NOT OK - DOWN: ';
114 $status_report .= "$port($ifAlias) ";
115 ++$tot_down;
116 }
117  
118 # update cache
119 $ifOperStatusCache->{$device}->{$port} = $ifOperStatus;
120 ++$tot_oper;
121  
122 if ($ifOperStatus !~ m/^(?:up|dormant)/) {
123 # can skip rest of this port's checks and reports
124 $ifInErrorsCache->{$device}->{$port} = $ifInErrors
125 if exists $status->{$device}->{$port}->{ifInErrors};
126 $ifInDiscardsCache->{$device}->{$port} = $ifInDiscards
127 if exists $status->{$device}->{$port}->{ifInDiscards};
128 next;
129 }
130 }
131  
132 if (exists $status->{$device}->{$port}->{ifInErrors}) {
133 # compare cache
134 if (not $skip_err
135 and exists $ifInErrorsCache->{$device}->{$port}) {
136  
137 my $diff = ($ifInErrors - $ifInErrorsCache->{$device}->{$port});
138 if ($diff > $threshold) {
139 $errors_report ||= 'NOT OK - Errors: ';
140 $errors_report .= "$port(+$diff)($ifAlias) ";
141 ++$tot_with_err;
142 }
143 }
144  
145 # update cache
146 $ifInErrorsCache->{$device}->{$port} = $ifInErrors;
147 ++$tot_err;
148 }
149  
150 if (exists $status->{$device}->{$port}->{ifInDiscards}) {
151 # compare cache
152 if (not $skip_disc
153 and exists $ifInDiscardsCache->{$device}->{$port}) {
154  
155 my $diff = ($ifInDiscards - $ifInDiscardsCache->{$device}->{$port});
156 if ($diff > $threshold) {
157 $discards_report ||= 'NOT OK - Discards: ';
158 $discards_report .= "$port(+$diff)($ifAlias) ";
159 ++$tot_with_dis;
160 }
161 }
162  
163 # update cache
164 $ifInDiscardsCache->{$device}->{$port} = $ifInDiscards;
165 ++$tot_dis;
166 }
167 } # port
168  
169 $status_report = "NOT OK - $tot_down (of $tot_oper polled) interfaces are DOWN."
170 if $tot_down > 10;
171 $errors_report = "NOT OK - $tot_with_err (of $tot_err UP) interfaces have errors."
172 if $tot_with_err > 10;
173 $discards_report = "NOT OK - $tot_with_dis (of $tot_dis UP) interfaces have discards."
174 if $tot_with_dis > 10;
175  
176 my $host = exists $cache->{host_for} ? $cache->{host_for}->{$device}
177 : $device;
178  
179 # $ECHO "$SERVER;$SERVICE;$RESULT;$OUTPUT" | $CMD -H $DEST_HOST -c $CFG -d ";"
180  
181 if (exists $results->{$device}->{ifOperStatus}) {
182 if (length $status_report) {
183 my $output = "$host!$service_prefix Status!2!$status_report\n";
184 echo $output;
185 print $send_nsca $output;
186 }
187 else {
188 my $output = "$host!$service_prefix Status!0!OK: $tot_oper interfaces are UP and running.\n";
189 echo $output;
190 print $send_nsca $output;
191 }
192 }
193  
194 if (exists $results->{$device}->{ifInErrors}) {
195 if (length $errors_report) {
196 my $output = "$host!$service_prefix Errors!2!$errors_report\n";
197 echo $output;
198 print $send_nsca $output;
199 }
200 else {
201 my $output = "$host!$service_prefix Errors!0!OK: No errors on $tot_err UP interfaces.\n";
202 echo $output;
203 print $send_nsca $output;
204 }
205 }
206  
207 if (exists $results->{$device}->{ifInDiscards}) {
208 if (length $discards_report) {
209 my $output = "$host!$service_prefix Discards!2!$discards_report\n";
210 echo $output;
211 print $send_nsca $output;
212 }
213 else {
214 my $output = "$host!$service_prefix Discards!0!OK: No discards on $tot_dis UP interfaces.\n";
215 echo $output;
216 print $send_nsca $output;
217 }
218 }
219 } # host
220  
221 # close connection to send_nsca (will chirp)
222 close $send_nsca or die "can't close send_nsca: $!";
223  
224 # restore STDOUT
225 open STDOUT, '>&', $oldout or die "Can't dup \$oldout: $!";
226  
227 return 1;
228 }
229  
230 1;
231  
232 # ABSTRACT: Back-end module to send polled data to a Nagios service
233  
234  
235 __END__
236 =pod
237  
238 =head1 NAME
239  
240 YATG::Store::NSCA - Back-end module to send polled data to a Nagios service
241  
242 =head1 VERSION
243  
244 version 5.140510
245  
246 =head1 DESCRIPTION
247  
248 This module checks interface status, errors and discard counts and sends
249 a result to Nagios C<nsca> process for each.
250  
251 Only one check result per device is submitted (i.e. I<not> one result per
252 port). If there are multiple ports in an alarm state on the same device, then
253 they will all be mentioned in the single service check report.
254  
255 When all enabled ports are connected, an OK result is returned.
256  
257 =head1 CONFIGURATION
258  
259 At a minimum, you must provide details of the location of your Nagios NSCA
260 server, in the main configuration file:
261  
262 nsca:
263 nsca_server: '192.0.2.1'
264  
265 In your YATG configuration file, you must also include this store module on
266 the OIDs required to generate a check result:
267  
268 oids:
269 "ifAlias": [ifindex, nsca]
270 "ifOperStatus": [ifindex, nsca]
271 "ifInErrors": [ifindex, nsca]
272 "ifInDiscards": [ifindex, nsca]
273  
274 Note that each of the C<ifOperStatus>, C<ifInErrors> and C<ifInDiscards> is
275 optional, and that you should provide at least one. The C<ifAlias> is also
276 optional but helps in the status report.
277  
278 =head2 Optional Configuration
279  
280 You can also supply the following settings in the main configuration file to
281 override builtin defaults, like so:
282  
283 yatg:
284 dbi_host_query: 'SELECT ip, host AS name from hosts'
285 dbi_community_query: 'SELECT ip, snmp_community FROM hosts'
286 dbi_interfaces_query: 'SELECT name FROM hostinterfaces WHERE ip = ?'
287 nsca:
288 nsca_port: '5667'
289 send_nsca_cmd: '/usr/bin/send_nsca'
290 config_file: '/etc/send_nsca.cfg'
291 ignore_ports: '^(?:Vlan|Po)\d+$'
292 ignore_descr: '(?:SPAN)'
293 ignore_oper_descr: '(?:TEST)'
294 ignore_error_descr: '(?:NOERR)'
295 ignore_discard_descr: '(?:NODIS)'
296 service_prefix: 'Interfaces'
297  
298 =over 4
299  
300 =item C<dbi_host_query>
301  
302 You can choose to submit results by host name instead of IP. To allow this,
303 you need a configuration entry with an SQL query. The query must return two
304 columns, named I<ip> and I<name>. For example:
305  
306 yatg:
307 dbi_host_query: 'SELECT ip, host AS name from hosts'
308  
309 =item C<dbi_community_query>
310  
311 For performance you can retrieve community strings from a database instead
312 of trying a list in turn for each device (which is very slow indeed). Pass
313 an SQL statement which returns the IP and community string for each device.
314 If used, this option causes YATG to ignore the C<communities> configuration.
315  
316 yatg:
317 dbi_community_query: 'SELECT ip, snmp_community FROM hosts'
318  
319 =item C<dbi_interfaces_query>
320  
321 This option allows filtering of submitted results according to a list of
322 Interface names on the device. The SQL in this case needs one "bind var" for
323 the device IP, and must return a single list of names (again, used in
324 C<DBI::selectcol_arrayref>):
325  
326 yatg:
327 dbi_interfaces_query: 'SELECT name FROM hostinterfaces WHERE ip = ?'
328  
329 With this option you have an explicit list of interface names. You can also
330 use the C<ignore_*> options (see below) to filter interfaces based on a
331 regular expression.
332  
333 =item C<send_nsca_cmd>
334  
335 The location of the C<send_nsca> command on your system. YATG will default to
336 C</usr/bin/send_nsca> and if you supply a value it must be a fully qualified
337 path.
338  
339 =item C<nsca_port>
340  
341 The port where the NSCA daemon is listening, and the C<send_nsca> command
342 should connect and submit results.
343  
344 =item C<config_file>
345  
346 The location of the configuration file for the C<send_nsca> program. This
347 defaults to C</etc/send_nsca.cfg>.
348  
349 =item C<ignore_ports>
350  
351 Device port names (OID C<ifDescr>) to skip when submitting results. This
352 defaults to anything like a Vlan interface, or Cisco PortChannel. Supply the
353 content of a Perl regular expression, as in the example above.
354  
355 =item C<ignore_descr>
356  
357 Device port description fields matching this value cause the port to be
358 skipped when submitting results. This defaults to anything containing the word
359 "SPAN". Supply the content of a Perl regular expression, as in the example
360 above.
361  
362 =item C<ignore_oper_descr>, C<ignore_error_descr>, C<ignore_discard_descr>
363  
364 This setting has the same effect as C<ignore_descr> but applies only to the
365 port status, port error count, and port discard count checks, respectively.
366 There is no default setting for these options.
367  
368 =item C<service_prefix>
369  
370 Prefix of he Nagios Service Check name to use when submitting results. To this
371 is added the name of the data check such as "Status" or "Errors". This must
372 match the configured name on your Nagios server, and defaults to "Interfaces".
373  
374 =back
375  
376 =head1 SEE ALSO
377  
378 =over 4
379  
380 =item Nagios NSCA at L<http://docs.icinga.org/latest/en/nsca.html>
381  
382 =back
383  
384 =head1 AUTHOR
385  
386 Oliver Gorwits <oliver@cpan.org>
387  
388 =head1 COPYRIGHT AND LICENSE
389  
390 This software is copyright (c) 2014 by University of Oxford.
391  
392 This is free software; you can redistribute it and/or modify it under
393 the same terms as the Perl 5 programming language system itself.
394  
395 =cut
396