#!/usr/bin/perl
# -----------------------------------------------------------------------------
#  check_events_count : Nagios plugin for EventLog event count check
# -----------------------------------------------------------------------------
#  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;

use SKM::DB;
use Node::Conf "DB_Version";

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

$np->getopts;

my ($ret, $dbh, %DBVer);

# Try to connect to db
eval {
    $dbh = DBMaster({ PrintError=>0, RaiseError=>1, FetchHashKeyName=>'NAME_uc' });
    $dbh->selectall_arrayref("SELECT COUNT(1) FROM _objs");
};

if($@) { 
    $np->nagios_exit( CRITICAL, 'Database is DOWN' );
}

my $cnt;
eval {
    my $arr = $dbh->selectrow_arrayref("SELECT COUNT(eventid) FROM event" );
    $cnt = $arr->[0];
};

if($@) { 
    $np->nagios_exit( CRITICAL, 'Can not query events' );
} elsif(!defined $cnt) {
    $np->nagios_exit( UNKNOWN, 'Undefined number of events' );
} elsif(!$cnt) {
    $np->nagios_exit( WARNING, 'No events registered' );
}
else {
    $np->nagios_exit( OK, "Events total: $cnt" );
}
