summaryrefslogtreecommitdiffstats
path: root/contrib/nagios_blocker_checker.pl
blob: 0e292c8c982b2b5d193e26e6d707bb523aa4afe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/perl

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

use strict;
use warnings;

use FindBin qw($Bin);
use lib "$Bin/..";
use lib "$Bin/../lib";

use Bugzilla;
use Bugzilla::Constants;
use Bugzilla::Product;
use Bugzilla::User;
use Getopt::Long;

Bugzilla->usage_mode(USAGE_MODE_CMDLINE);


my $config = {
    # Filter by assignee or product
    assignee        => '',
    product         => '',
    unassigned      => 'nobody@mozilla.org',
    # Time in hours to wait before paging/warning
    major_alarm     => 24,
    major_warn      => 20,
    critical_alarm  => 8,
    critical_warn   => 5,
    blocker_alarm   => 0,
    blocker_warn    => 0,
};

my $usage = <<EOF;
FILTERS

  the filter determines which bugs to check, either by assignee or product.
  for backward compatibility, if just an email address is provided, it will be
  used as the assignee.

  --assignee <email>    filter bugs by assignee
  --product <name>      filter bugs by product name
  --unassigned <email>  set the unassigned user (default: $config->{unassigned})

TIMING

  time in hours to wait before paging or warning

  --major_alarm <hours> (default: $config->{major_alarm})
  --major_warn  <hours> (default: $config->{major_warn})
  --critical_alarm <hours> (default: $config->{critical_alarm})
  --critical_warn  <hours> (default: $config->{critical_warn})
  --blocker_alarm <hours> (default: $config->{blocker_alarm})
  --blocker_warn  <hours> (default: $config->{blocker_warn})

EXAMPLES

  nagios_blocker_checker.pl --assignee server-ops\@mozilla-org.bugs
  nagios_blocker_checker.pl server-ops\@mozilla-org.bugs
  nagios_blocker_checker.pl --product 'mozilla developer network'
EOF

die($usage) unless GetOptions(
    'assignee=s'        => \$config->{assignee},
    'product=s'         => \$config->{product},
    'major_alarm=i'     => \$config->{major_alarm},
    'major_warn=i'      => \$config->{major_warn},
    'critical_alarm=i'  => \$config->{critical_alarm},
    'critical_warn=i'   => \$config->{critical_warn},
    'blocker_alarm=i'   => \$config->{blocker_alarm},
    'blocker_warn=i'    => \$config->{blocker_warn},
    'help|?'            => \$config->{help},
);
$config->{assignee} = $ARGV[0] if !$config->{assignee} && @ARGV;
die $usage if
    $config->{help}
    || !($config->{assignee} || $config->{product})
    || ($config->{assignee} && $config->{product});

#

use constant NAGIOS_OK          => 0;
use constant NAGIOS_WARNING     => 1;
use constant NAGIOS_CRITICAL    => 2;
use constant NAGIOS_NAMES       => [qw( OK WARNING CRITICAL )];

my($where, @values);

if ($config->{assignee}) {
    $where = 'bugs.assigned_to = ?';
    push @values, Bugzilla::User->check({ name => $config->{assignee} })->id;
} else {
    $where = 'bugs.product_id = ? AND bugs.assigned_to = ?';
    push @values, Bugzilla::Product->check({ name => $config->{product} })->id;
    push @values, Bugzilla::User->check({ name => $config->{unassigned} })->id;
}

my $sql = <<EOF;
    SELECT bug_id, bug_severity, UNIX_TIMESTAMP(bugs.creation_ts) AS ts
      FROM bugs
     WHERE $where
           AND COALESCE(resolution, '') = ''
           AND bug_severity IN ('blocker', 'critical', 'major')
EOF

my $bugs = {
    'major'     => [],
    'critical'  => [],
    'blocker'   => [],
};
my $current_state = NAGIOS_OK;
my $current_time = time;

my $dbh = Bugzilla->switch_to_shadow_db;
foreach my $bug (@{ $dbh->selectall_arrayref($sql, { Slice => {} }, @values) }) {
    my $severity = $bug->{bug_severity};
    my $age = ($current_time - $bug->{ts}) / 3600;

    if ($age > $config->{"${severity}_alarm"}) {
        $current_state = NAGIOS_CRITICAL;
        push @{$bugs->{$severity}}, "https://bugzil.la/" . $bug->{bug_id};

    } elsif ($age > $config->{"${severity}_warn"}) {
        if ($current_state < NAGIOS_WARNING) {
            $current_state = NAGIOS_WARNING;
        }
        push @{$bugs->{$severity}}, "https://bugzil.la/" . $bug->{bug_id};

    }
}

print "bugs " . NAGIOS_NAMES->[$current_state] . ": ";
if ($current_state == NAGIOS_OK) {
    print "No blocker, critical, or major bugs found."
}
foreach my $severity (qw( blocker critical major )) {
    my $list = $bugs->{$severity};
    if (@$list) {
        printf "%s %s bug(s) found " . join(' , ', @$list) . " ", scalar(@$list), $severity;
    }
}
print "\n";

exit $current_state;