| 1 | package Bot::BasicBot::Pluggable::Module::Nagios; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.04'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use base 'Bot::BasicBot::Pluggable::Module'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Nagios::Scrape; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Bot::BasicBot::Pluggable::Module::Nagios - report Nagios alerts to IRC | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | A module for IRC bots powered by L<Bot::BasicBot::Pluggable> to monitor a Nagios | ||||||||||||||||||||||||||||
| 19 | install and report alerts to IRC. | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | Multiple Nagios instances are supported; these could be separate Nagios systems, | ||||||||||||||||||||||||||||
| 22 | or just the same Nagios install but using different credentials. As each | ||||||||||||||||||||||||||||
| 23 | configured instance can have specific target channels defined, this means you | ||||||||||||||||||||||||||||
| 24 | could have the bot check with the username "development" and report all visible | ||||||||||||||||||||||||||||
| 25 | problems to the C<#development> channel, then check again with the "sysad" | ||||||||||||||||||||||||||||
| 26 | username and report problems visible to that user to the C<#sysads> channel. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | Actual monitoring is done using L<Nagios::Scrape>, which scrapes the information | ||||||||||||||||||||||||||||
| 29 | from the C<status.cgi> script which powers Nagios' web interface. This means | ||||||||||||||||||||||||||||
| 30 | that, assuming your Nagios setup is configured to be viewable over the web, you | ||||||||||||||||||||||||||||
| 31 | need no further setup to allow the bot to monitor it. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | Load the module as you would any other L<Bot::BasicBot::Pluggable> module, then | ||||||||||||||||||||||||||||
| 37 | configure it to watch a Nagios install and report problems to the desired | ||||||||||||||||||||||||||||
| 38 | channel(s) with the C<nagios add> command. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | In a direct message to the bot: | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | <user> nagios add http://nagios.example.com/cgi-bin/status.cgi username password #channel | ||||||||||||||||||||||||||||
| 43 | <bot> OK | ||||||||||||||||||||||||||||
| 44 | <user> nagios list | ||||||||||||||||||||||||||||
| 45 | <bot> I'm currently monitoring the following Nagios instances: | ||||||||||||||||||||||||||||
| 46 | .. 1 : http://example.com/cgi-bin/status.cgi as dave for #chan | ||||||||||||||||||||||||||||
| 47 | <user> nagios del 1 | ||||||||||||||||||||||||||||
| 48 | <bot> OK, deleted instance 1 | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | (You can supply a list of channel names separated by commas, if you want reports | ||||||||||||||||||||||||||||
| 51 | from a given instance to be announced to more than one channel.) | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | sub help { | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | return <<USAGE; | ||||||||||||||||||||||||||
|
Subroutine
help: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | A module to report Nagios alerts to IRC channels. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | nagios add http://example.com/cgi-bin/status.cgi username password #chan | ||||||||||||||||||||||||||||
| 61 | nagios list | ||||||||||||||||||||||||||||
| 62 | nagios del 1 | ||||||||||||||||||||||||||||
| 63 | nagios set setting_name value | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | Say "nagios set" with no setting name for a list of valid settings. | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | Full help is available at http://p3rl.org/Bot::BasicBot::Pluggable::Module::Nagios | ||||||||||||||||||||||||||||
| 68 | USAGE | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | sub told { | ||||||||||||||||||||||||||||
| 72 | 0 | ▶ | my ($self, $mess) = @_; | ||||||||||||||||||||||||||
|
Subroutine
told: not called CC=20 SCAR=60.4
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | return unless $mess->{address} && $mess->{body} =~ s/^nagios\s+//i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$mess->{"address"} and $mess->{"body"} =~ s/^nagios\s+//i
MC/DC0%0/2
$mess->{"address"} and $mess->{"body"} =~ s/^nagios\s+//i
$mess->{"address"}
$mess->{"body"} =~ s/^nagios\s+//i
Truth table0%0/3
A and B
A= $mess->{"address"}B= $mess->{"body"} =~ s/^nagios\s+//i
| |||||||||||||||||||||||||||||
| 75 | 0 | my ($command, $params) = split /\s+/, $mess->{body}, 2; | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | if (lc $command eq 'add') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | my($url, $user, $pass, $channel_list) = split /\s+/, $params, 4; | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | if ($url !~ /^http/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | return "URL looks invalid!"; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 0 | my @channels = split /\s+|,/, $channel_list; | |||||||||||||||||||||||||||
| 82 | 0 | ▶ | my $instances = $self->get('instances') || []; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->get('instances') || []
MC/DC0%0/1
$self->get('instances') || []
$self->get('instances')
Truth table0%0/2
A
A= $self->get('instances')
| |||||||||||||||||||||||||||||
| 83 | 0 | push @$instances, { | |||||||||||||||||||||||||||
| 84 | url => $url, | ||||||||||||||||||||||||||||
| 85 | user => $user, | ||||||||||||||||||||||||||||
| 86 | pass => $pass, | ||||||||||||||||||||||||||||
| 87 | channels => \@channels, | ||||||||||||||||||||||||||||
| 88 | }; | ||||||||||||||||||||||||||||
| 89 | 0 | $self->set('instances' => $instances); | |||||||||||||||||||||||||||
| 90 | 0 | return "OK, added Nagios instance to monitor"; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 0 | ▶ | if (lc $command eq 'list') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | my $instances = $self->get('instances') || []; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->get('instances') || []
MC/DC0%0/1
$self->get('instances') || []
$self->get('instances')
Truth table0%0/2
A
A= $self->get('instances')
| |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | if (!@$instances) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | return "I'm not currently monitoring any Nagios instances."; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | 0 | my $response = "I'm currently monitoring the following instances:\n"; | |||||||||||||||||||||||||||
| 98 | 0 | my $num = 0; | |||||||||||||||||||||||||||
| 99 | 0 | for my $instance (@$instances) { | |||||||||||||||||||||||||||
| 100 | $response .= sprintf "%d : %s as %s for %s\n", | ||||||||||||||||||||||||||||
| 101 | $num++, | ||||||||||||||||||||||||||||
| 102 | $instance->{url}, | ||||||||||||||||||||||||||||
| 103 | $instance->{user}, | ||||||||||||||||||||||||||||
| 104 | 0 | join ',', @{ $instance->{channels} }; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 0 | return $response; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if (lc $command eq 'del' || lc $command eq 'delete') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
lc $command eq "del" or lc $command eq "delete"
MC/DC0%0/2
lc $command eq "del" or lc $command eq "delete"
lc $command eq "del"
lc $command eq "delete"
Truth table0%0/3
A or B
A= lc $command eq "del"B= lc $command eq "delete"
| |||||||||||||||||||||||||||||
| 109 | 0 | my $num = $params; | |||||||||||||||||||||||||||
| 110 | 0 | ▶ | if ($num !~ /^\d+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | return "Usage: nagios del instancenum (e.g. 'nagios del 1')"; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 0 | ▶ | my $instances = $self->get('instances') || []; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->get('instances') || []
MC/DC0%0/1
$self->get('instances') || []
$self->get('instances')
Truth table0%0/2
A
A= $self->get('instances')
| |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if (!$instances->[$num]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | return "No such instance"; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 0 | splice @$instances, $num; | |||||||||||||||||||||||||||
| 118 | 0 | $self->set('instances', $instances); | |||||||||||||||||||||||||||
| 119 | 0 | return "OK, deleted instance $num"; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if (lc $command eq 'set') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | my ($setting, $value) = split /\s+/, $params, 2; | |||||||||||||||||||||||||||
| 123 | 0 | $setting = lc $setting; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # Validator for durations, which recognises e.g. 30m and turns into | ||||||||||||||||||||||||||||
| 126 | # seconds | ||||||||||||||||||||||||||||
| 127 | my $validate_duration = sub { | ||||||||||||||||||||||||||||
| 128 | 0 | ▶ | my $value = lc shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if (my($num,$unit) = $value =~ m{^ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | (\d+) | ||||||||||||||||||||||||||||
| 131 | ( | ||||||||||||||||||||||||||||
| 132 | s(?:ecs?)? | ||||||||||||||||||||||||||||
| 133 | | | ||||||||||||||||||||||||||||
| 134 | m(?:mins?)? | ||||||||||||||||||||||||||||
| 135 | | | ||||||||||||||||||||||||||||
| 136 | h(?:hours?)? | ||||||||||||||||||||||||||||
| 137 | )? | ||||||||||||||||||||||||||||
| 138 | $}x) { | ||||||||||||||||||||||||||||
| 139 | my $multiplier = { | ||||||||||||||||||||||||||||
| 140 | s => 1, | ||||||||||||||||||||||||||||
| 141 | m => 60, | ||||||||||||||||||||||||||||
| 142 | h => 60 * 60, | ||||||||||||||||||||||||||||
| 143 | 0 | ▶ | }->{$unit} || 1; | ||||||||||||||||||||||||||
|
Condition0%0/2
{'s', 1, 'm', 60, 'h', 3600}->{$unit} || 1
MC/DC0%0/1
{'s', 1, 'm', 60, 'h', 3600}->{$unit} || 1
{'s', 1, 'm', 60, 'h', 3600}->{$unit}
Truth table0%0/2
A
A= {'s', 1, 'm', 60, 'h', 3600}->{$unit}
| |||||||||||||||||||||||||||||
| 144 | 0 | return $num * $multiplier; | |||||||||||||||||||||||||||
| 145 | } else { | ||||||||||||||||||||||||||||
| 146 | 0 | return; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 0 | }; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # Declare the settings we accept, what they should look like, and their | ||||||||||||||||||||||||||||
| 151 | # description | ||||||||||||||||||||||||||||
| 152 | 0 | my $duration_help = " (in seconds, or with unit, e.g. 60, 3m, 5h)"; | |||||||||||||||||||||||||||
| 153 | my %valid_settings = ( | ||||||||||||||||||||||||||||
| 154 | poll_interval => { | ||||||||||||||||||||||||||||
| 155 | description => "Interval between polls to Nagios" | ||||||||||||||||||||||||||||
| 156 | . $duration_help, | ||||||||||||||||||||||||||||
| 157 | validator => $validate_duration, | ||||||||||||||||||||||||||||
| 158 | }, | ||||||||||||||||||||||||||||
| 159 | repeat_delay => { | ||||||||||||||||||||||||||||
| 160 | description => "Time between repeated notifications " | ||||||||||||||||||||||||||||
| 161 | . "of the same issue" . $duration_help, | ||||||||||||||||||||||||||||
| 162 | validator => $validate_duration, | ||||||||||||||||||||||||||||
| 163 | }, | ||||||||||||||||||||||||||||
| 164 | report_statuses => { | ||||||||||||||||||||||||||||
| 165 | description => "List of statuses we should notify for" | ||||||||||||||||||||||||||||
| 166 | . " (default: CRITICAL, OK)", | ||||||||||||||||||||||||||||
| 167 | validator => sub { | ||||||||||||||||||||||||||||
| 168 | 0 | ▶ | my @statuses = split /[\s,]+/, uc shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 169 | 0 | ▶ | return unless @statuses; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | return if | ||||||||||||||||||||||||||||
| 171 | 0 | ▶ | grep { !/^(OK|WARNING|CRITICAL|UNKNOWN)$/ } @statuses; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 0 | return \@statuses; | |||||||||||||||||||||||||||
| 173 | }, | ||||||||||||||||||||||||||||
| 174 | }, | ||||||||||||||||||||||||||||
| 175 | 0 | ); | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | # If we were called without a setting name, reply with the settings | ||||||||||||||||||||||||||||
| 178 | # which are valid: | ||||||||||||||||||||||||||||
| 179 | 0 | ▶ | if (!$setting) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 0 | my $reply = "Valid settings are:\n"; | |||||||||||||||||||||||||||
| 181 | 0 | for my $setting (keys %valid_settings) { | |||||||||||||||||||||||||||
| 182 | $reply .= sprintf "%s (%s)\n", | ||||||||||||||||||||||||||||
| 183 | 0 | $setting, $valid_settings{$setting}->{description}; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | 0 | return $reply; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 0 | my $validator = $valid_settings{$setting}->{validator}; | |||||||||||||||||||||||||||
| 189 | 0 | ▶ | if (!$validator) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 0 | return sprintf "Unknown setting '%s' (known settings: %s)", | |||||||||||||||||||||||||||
| 191 | $setting, | ||||||||||||||||||||||||||||
| 192 | keys %valid_settings; | ||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | # The validator will return the value (possibly canonicalised) if it was | ||||||||||||||||||||||||||||
| 196 | # acceptable, or undef if not: | ||||||||||||||||||||||||||||
| 197 | 0 | ▶ | if (defined(my $valid_value = $validator->($value))) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 0 | $self->set($setting, $valid_value); | |||||||||||||||||||||||||||
| 199 | 0 | return "OK, set $setting to '$valid_value'"; | |||||||||||||||||||||||||||
| 200 | } else { | ||||||||||||||||||||||||||||
| 201 | 0 | return "Value '$value' is not valid for setting $setting"; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 1 | my $last_polled = 0; | |||||||||||||||||||||||||||
| 207 | 1 | my %last_status; | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | sub tick { | ||||||||||||||||||||||||||||
| 210 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
tick: not called CC=30 SCAR=68.4
Pod
documented
| |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 0 | ▶ | my $repeat_delay = $self->get('repeat_delay') || 15 * 60; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->get('repeat_delay') || 900
MC/DC0%0/1
$self->get('repeat_delay') || 900
$self->get('repeat_delay')
Truth table0%0/2
A
A= $self->get('repeat_delay')
| |||||||||||||||||||||||||||||
| 213 | 0 | ▶ | my $poll_delay = $self->get('poll_interval') || 120; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->get('poll_interval') || 120
MC/DC0%0/1
$self->get('poll_interval') || 120
$self->get('poll_interval')
Truth table0%0/2
A
A= $self->get('poll_interval')
| |||||||||||||||||||||||||||||
| 214 | 0 | ▶ | return if (time - $last_polled < $poll_delay); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | 0 | $last_polled = time; | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | # Find out what statuses we should report; do this here, so it's ready for | ||||||||||||||||||||||||||||
| 218 | # use in the loop later (we don't want to re-do it for every service :) ) | ||||||||||||||||||||||||||||
| 219 | 0 | ▶ | my $report_statuses = $self->get('report_statuses') | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->get('report_statuses') || ['CRITICAL', 'WARNING', 'OK']
MC/DC0%0/1
$self->get('report_statuses') || ['CRITICAL', 'WARNING', 'OK']
$self->get('report_statuses')
Truth table0%0/2
A
A= $self->get('report_statuses')
| |||||||||||||||||||||||||||||
| 220 | || [ qw( CRITICAL WARNING OK ) ]; | ||||||||||||||||||||||||||||
| 221 | 0 | my %should_report = map { $_ => 1 } @$report_statuses; | |||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | 0 | ▶ | my $instances = $self->get('instances') || []; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->get('instances') || []
MC/DC0%0/1
$self->get('instances') || []
$self->get('instances')
Truth table0%0/2
A
A= $self->get('instances')
| |||||||||||||||||||||||||||||
| 225 | instance: | ||||||||||||||||||||||||||||
| 226 | 0 | for my $instance (@$instances) { | |||||||||||||||||||||||||||
| 227 | my $ns = Nagios::Scrape->new( | ||||||||||||||||||||||||||||
| 228 | username => $instance->{user}, | ||||||||||||||||||||||||||||
| 229 | password => $instance->{pass}, | ||||||||||||||||||||||||||||
| 230 | url => $instance->{url}, | ||||||||||||||||||||||||||||
| 231 | 0 | ); | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | # Get a list of all hosts, and assemble a lookup hash so we can | ||||||||||||||||||||||||||||
| 234 | # easily look up whether a host is down in order to skip reporting | ||||||||||||||||||||||||||||
| 235 | # services | ||||||||||||||||||||||||||||
| 236 | 0 | $ns->host_state(14); # All hosts, including OK ones | |||||||||||||||||||||||||||
| 237 | 0 | my @all_hosts = $ns->get_host_status; | |||||||||||||||||||||||||||
| 238 | my %host_down = | ||||||||||||||||||||||||||||
| 239 | 0 | map { $_->{host} => 1 } | |||||||||||||||||||||||||||
| 240 | 0 | grep { $_->{status} eq 'DOWN' } | |||||||||||||||||||||||||||
| 241 | @all_hosts; | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | # Get services in all states except PENDING - we want OK ones, too, so | ||||||||||||||||||||||||||||
| 245 | # we can easily report problem -> OK transitions | ||||||||||||||||||||||||||||
| 246 | # PENDING 1 OK 2 WARNING 4 UNKNOWN 8 CRITICAL 16 | ||||||||||||||||||||||||||||
| 247 | # 16 + 8 + 4 + 2 = 30 = OK/WARNING/UNKNOWN/CRITICAL | ||||||||||||||||||||||||||||
| 248 | # TODO: make state filter configurable | ||||||||||||||||||||||||||||
| 249 | 0 | $ns->service_state(30); | |||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 0 | my @service_statuses = $ns->get_service_status; | |||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | # Key to use for this instance in %last_status | ||||||||||||||||||||||||||||
| 255 | 0 | my $instance_key = join '_', $instance->{url}, $instance->{user}; | |||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 0 | ▶ | my $instance_statuses = $last_status{$instance_key} ||= {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$last_status{$instance_key} ||= {}
MC/DC0%0/1
$last_status{$instance_key} ||= {}
$last_status{$instance_key}
Truth table0%0/2
A
A= $last_status{$instance_key}
| |||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | # Firstly, report host status changes: | ||||||||||||||||||||||||||||
| 260 | 0 | my @host_reports; | |||||||||||||||||||||||||||
| 261 | host: | ||||||||||||||||||||||||||||
| 262 | 0 | for my $host (@all_hosts) { | |||||||||||||||||||||||||||
| 263 | 0 | ▶ | if (my $last_status = $instance_statuses->{$host->{host}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | # If it was UP before and is still UP, move on swiftly | ||||||||||||||||||||||||||||
| 265 | next if $last_status->{status} eq 'UP' | ||||||||||||||||||||||||||||
| 266 | 0 | ▶ | and $host->{status} eq 'UP'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$last_status->{"status"} eq "UP" and $host->{"status"} eq "UP"
MC/DC0%0/2
$last_status->{"status"} eq "UP" and $host->{"status"} eq "UP"
$last_status->{"status"} eq "UP"
$host->{"status"} eq "UP"
Truth table0%0/3
A and B
A= $last_status->{"status"} eq "UP"B= $host->{"status"} eq "UP"
| |||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | # If we've reported it as down recently, don't do so again yet | ||||||||||||||||||||||||||||
| 269 | 0 | ▶ | if ($last_status->{status} eq $host->{status} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$last_status->{"status"} eq $host->{"status"} and time - $last_status->{"timestamp"} < $repeat_delay
MC/DC0%0/2
$last_status->{"status"} eq $host->{"status"} and time - $last_status->{"timestamp"} < $repeat_delay
$last_status->{"status"} eq $host->{"status"}
time - $last_status->{"timestamp"} < $repeat_delay
Truth table0%0/3
A and B
A= $last_status->{"status"} eq $host->{"status"}B= time - $last_status->{"timestamp"} < $repeat_delay
| |||||||||||||||||||||||||||||
| 270 | && time - $last_status->{timestamp} < $repeat_delay) | ||||||||||||||||||||||||||||
| 271 | { | ||||||||||||||||||||||||||||
| 272 | 0 | next host; | |||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | # OK, announce that this host is down, and remember that did | ||||||||||||||||||||||||||||
| 276 | # so | ||||||||||||||||||||||||||||
| 277 | 0 | push @host_reports, $host; | |||||||||||||||||||||||||||
| 278 | } else { | ||||||||||||||||||||||||||||
| 279 | # We've not seen this one before; if it's 'UP', just remember | ||||||||||||||||||||||||||||
| 280 | # but don't announce it, otherwise we'd send a flood of UP | ||||||||||||||||||||||||||||
| 281 | # notifications on first run | ||||||||||||||||||||||||||||
| 282 | 0 | ▶ | if ($host->{status} eq 'UP') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 283 | $instance_statuses->{$host->{host}} = | ||||||||||||||||||||||||||||
| 284 | 0 | { timestamp => time(), status => $host->{status} }; | |||||||||||||||||||||||||||
| 285 | 0 | next host; | |||||||||||||||||||||||||||
| 286 | } else { | ||||||||||||||||||||||||||||
| 287 | # First result for this host, but it's down - announce: | ||||||||||||||||||||||||||||
| 288 | 0 | push @host_reports, $host; | |||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | # Now we've decided if we need to report any hosts, go ahead and | ||||||||||||||||||||||||||||
| 293 | # do so, and remember we did | ||||||||||||||||||||||||||||
| 294 | 0 | for my $host (@host_reports) { | |||||||||||||||||||||||||||
| 295 | 0 | for my $channel (@{ $instance->{channels} }) { | |||||||||||||||||||||||||||
| 296 | 0 | $self->tell($channel, | |||||||||||||||||||||||||||
| 297 | "NAGIOS: Host $host->{host} is $host->{status}" | ||||||||||||||||||||||||||||
| 298 | ); | ||||||||||||||||||||||||||||
| 299 | $instance_statuses->{$host->{host}} = | ||||||||||||||||||||||||||||
| 300 | 0 | { timestamp => time(), status => $host->{status} }; | |||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | # Group problems by host, ignoring any which we've already reported | ||||||||||||||||||||||||||||
| 309 | # recently, or which are on a host which is down. | ||||||||||||||||||||||||||||
| 310 | 0 | my %service_by_host; | |||||||||||||||||||||||||||
| 311 | service: | ||||||||||||||||||||||||||||
| 312 | 0 | for my $service (@service_statuses) { | |||||||||||||||||||||||||||
| 313 | 0 | ▶ | next if $host_down{$service->{host}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | # See how many check attempts have found the service in this status; | ||||||||||||||||||||||||||||
| 317 | # if it's not enough for Nagios to send alerts, don't alert on IRC. | ||||||||||||||||||||||||||||
| 318 | # Don't do this if the status is "OK", though, as "OK" services | ||||||||||||||||||||||||||||
| 319 | # always show as e.g. 1/4. | ||||||||||||||||||||||||||||
| 320 | 0 | my ($attempt, $max_attempts) = split '/', $service->{attempts}; | |||||||||||||||||||||||||||
| 321 | 0 | ▶ | next service if $service->{status} ne 'OK' && $attempt < $max_attempts; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$service->{"status"} ne "OK" and $attempt < $max_attempts
MC/DC0%0/2
$service->{"status"} ne "OK" and $attempt < $max_attempts
$service->{"status"} ne "OK"
$attempt < $max_attempts
Truth table0%0/3
A and B
A= $service->{"status"} ne "OK"B= $attempt < $max_attempts
| |||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 0 | my $service_key = join '_', $service->{host}, $service->{service}; | |||||||||||||||||||||||||||
| 324 | 0 | ▶ | if (my $last_status = $instance_statuses->{$service_key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | # If it was OK before and still OK now, move on swiftly | ||||||||||||||||||||||||||||
| 326 | next if $last_status->{status} eq 'OK' | ||||||||||||||||||||||||||||
| 327 | 0 | ▶ | and $service->{status} eq 'OK'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$last_status->{"status"} eq "OK" and $service->{"status"} eq "OK"
MC/DC0%0/2
$last_status->{"status"} eq "OK" and $service->{"status"} eq "OK"
$last_status->{"status"} eq "OK"
$service->{"status"} eq "OK"
Truth table0%0/3
A and B
A= $last_status->{"status"} eq "OK"B= $service->{"status"} eq "OK"
| |||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | # If we've already seen it in this status, don't report it again | ||||||||||||||||||||||||||||
| 330 | # until the $repeat_delay is up | ||||||||||||||||||||||||||||
| 331 | 0 | ▶ | if ($last_status->{status} eq $service->{status} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$last_status->{"status"} eq $service->{"status"} and time - $last_status->{"timestamp"} < $repeat_delay
MC/DC0%0/2
$last_status->{"status"} eq $service->{"status"} and time - $last_status->{"timestamp"} < $repeat_delay
$last_status->{"status"} eq $service->{"status"}
time - $last_status->{"timestamp"} < $repeat_delay
Truth table0%0/3
A and B
A= $last_status->{"status"} eq $service->{"status"}B= time - $last_status->{"timestamp"} < $repeat_delay
| |||||||||||||||||||||||||||||
| 332 | && time - $last_status->{timestamp} < $repeat_delay) | ||||||||||||||||||||||||||||
| 333 | { | ||||||||||||||||||||||||||||
| 334 | 0 | next service; | |||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | } else { | ||||||||||||||||||||||||||||
| 338 | # We've not seen this one before; if it's 'OK', just remember it | ||||||||||||||||||||||||||||
| 339 | # but don't announce it, otherwise we'd send a flood of OK | ||||||||||||||||||||||||||||
| 340 | # notifications on first run | ||||||||||||||||||||||||||||
| 341 | 0 | ▶ | if ($service->{status} eq 'OK') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 342 | $instance_statuses->{$service_key} = | ||||||||||||||||||||||||||||
| 343 | 0 | { timestamp => time(), status => $service->{status} }; | |||||||||||||||||||||||||||
| 344 | 0 | next service; | |||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | # See if it's a status we should report. | ||||||||||||||||||||||||||||
| 349 | 0 | ▶ | next service if !$should_report{ $service->{status} }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | # Note that we're about to bitch about this one, and add it to | ||||||||||||||||||||||||||||
| 352 | # %service_by_host ready for reporting | ||||||||||||||||||||||||||||
| 353 | $instance_statuses->{$service_key} = { | ||||||||||||||||||||||||||||
| 354 | timestamp => time(), status => $service->{status}, | ||||||||||||||||||||||||||||
| 355 | 0 | }; | |||||||||||||||||||||||||||
| 356 | 0 | push @{ $service_by_host{ $service->{host} } }, $service; | |||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 0 | for my $host (sort keys %service_by_host) { | |||||||||||||||||||||||||||
| 360 | my $msg = "NAGIOS: $host : " | ||||||||||||||||||||||||||||
| 361 | . join ', ', | ||||||||||||||||||||||||||||
| 362 | 0 | map { "$_->{service} is $_->{status} ($_->{information})" } | |||||||||||||||||||||||||||
| 363 | 0 | @{ $service_by_host{$host} }; | |||||||||||||||||||||||||||
| 364 | 0 | for my $channel (@{$instance->{channels}}) { | |||||||||||||||||||||||||||
| 365 | 0 | $self->tell($channel, $msg); | |||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | =head1 TODO | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | Plenty of improvements are planned, including: | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | =over 4 | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | =item * Acknowledging problems | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | It should probably be possible to acknowledge a reported problem, preventing | ||||||||||||||||||||||||||||
| 382 | repeated reports of the same service/host in the same state. | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | =item * Configurable reporting hours | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | It would make sense to be able to configure the bot to only report problems | ||||||||||||||||||||||||||||
| 387 | during hours in which staff/volunteers are likely to be awake and paying | ||||||||||||||||||||||||||||
| 388 | attention to the IRC channel. | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | =item * Configurable report templates | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | It would be nice to be able to configure the format used for report messages - | ||||||||||||||||||||||||||||
| 393 | perhaps including colour codes to colourise elements of the message, where the | ||||||||||||||||||||||||||||
| 394 | channel allows it and users clients support it. | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | =back | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | David Precious, C<< <davidp at preshweb.co.uk> >> | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | This module is developed on GitHub: | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | L<https://github.com/bigpresh/Bot-BasicBot-Pluggable-Module-Nagios> | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | Pull requests / suggestions / bug reports are welcomed. | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | If you feel like it, even a "I'm using this and find it useful" mail to | ||||||||||||||||||||||||||||
| 411 | C<davidp@preshweb.co.uk> would be appreciated - it's nice to know when people | ||||||||||||||||||||||||||||
| 412 | find your work useful. | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | (Reviews on cpanratings and/or ++'s on MetaCPAN are also very welcome.) | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | =head1 BUGS | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | Please report any bugs or feature requests to C<bug-bot-basicbot-pluggable-module-nagios at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 420 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Bot-BasicBot-Pluggable-Module-Nagios>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 421 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | perldoc Bot::BasicBot::Pluggable::Module::Nagios | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | You can also look for information at: | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | =over 4 | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Bot-BasicBot-Pluggable-Module-Nagios> | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | L<http://annocpan.org/dist/Bot-BasicBot-Pluggable-Module-Nagios> | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | L<http://cpanratings.perl.org/d/Bot-BasicBot-Pluggable-Module-Nagios> | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | L<http://search.cpan.org/dist/Bot-BasicBot-Pluggable-Module-Nagios/> | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | =back | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | Copyright 2011 David Precious. | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 464 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 465 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | =cut | ||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||
| 472 | 1 | 1; # End of Bot::BasicBot::Pluggable::Module::Nagios | |||||||||||||||||||||||||||