#!/usr/bin/env perl
# vi: set ts=4 sw=4 :
#____________________________________________________________________________
#
#   MusicBrainz -- the open internet music database
#
#   Copyright (C) 1998 Robert Kaye
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#    This script originally by Dave Evans.
#____________________________________________________________________________

=pod

This is a Nagios (http://www.nagios.org/) check script to see that the
"last_replication_date" of a slave (RT_SLAVE) MusicBrainz server is not too
old.

=cut

use FindBin;
use lib "$FindBin::Bin/../../lib";

use strict;
use DBDefs;
use MusicBrainz::Server::Context;
use Sql;
use integer;

use Getopt::Long;

my $warn = 3600 * 6;
my $crit = 3600 * 24;

GetOptions(
    "warn|w=i"  => \$warn,
    "critical|c=i"      => \$crit,
) or exit 2;

@ARGV == 0 or die "Usage: $0 [-w SECONDS] [-c SECONDS]\n";

if (DBDefs->REPLICATION_TYPE != DBDefs::RT_SLAVE)
{
    print "This is not a slave database!\n";
    exit 2;
}

my $c = MusicBrainz::Server::Context->create_script_context;

my $row = $c->sql->select_single_row_hash(
    "
    SELECT
    (last_replication_date < NOW() - ?::INTERVAL) as warn,
    (last_replication_date < NOW() - ?::INTERVAL) as crit,
    last_replication_date,
    NOW() - last_replication_date as age
    FROM replication_control
    ",
    "$warn seconds",
    "$crit seconds",
);

if (not $row)
{
    print "No replication control data!\n";
    exit 2;
}

print ($row->{crit} ? "CRITICAL" : $row->{warn} ? "WARNING" : "OK");
print ": ";
print "Last replication date is $row->{last_replication_date} (age = $row->{age})\n";
exit ($row->{crit} ? 2 : $row->{warn} ? 1 : 0);

# eof
