| 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 | my $threshold = $config->{nsca}->{threshold} | ||
| 38 | || 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, '|-', (split /\s+/, $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__ |