#!/usr/bin/perl
# -----------------------------------------------------------------------------
#  check_events_integrity : Nagios plugin for checking EventLog DB inregrity
# -----------------------------------------------------------------------------
#  Created by: Andriy Fomenko
#  Authors: Alex Titov, Alex Tsibulnik, Andriy Fomenko
#  QA by:
#  Copyright: videoNEXT Federal, Inc., 2015
# -----------------------------------------------------------------------------

use strict;
use warnings;

use Nagios::Plugin;
use Nagios::Plugin::Getopt;

my $np = Nagios::Plugin->new( usage => "Usage: %s [ -v|--verbose ]" );

$np->getopts;

my $APL=$ENV{APL} || $np->nagios_exit( UNKNOWN, 'APL environment variable is not defined' );
my $EVENT_INTEGRITY="$APL/var/elog/event_integrity";

my $ei='UNKNOWN';
if(open EI, $EVENT_INTEGRITY) {
    my %result=map{/(^\w+)=(.+)/} grep {/^\w+=.+/} <EI>;
    $ei=$result{EVENT_INTEGRITY};
    close EI;
}
else {
    $np->nagios_exit( UNKNOWN, "Could not read $EVENT_INTEGRITY" );
}

if($ei ne 'CONSISTENT') {
    $np->nagios_exit( CRITICAL, "Event Database has '$ei' state" );
}
else {
    $np->nagios_exit( OK, "Event Database has '$ei' state" );
}
