#!/usr/bin/perl
# -----------------------------------------------------------------------------
#  check_patrol : Nagios plugin for checking "cam_patrol" run/health status
# -----------------------------------------------------------------------------
#  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_VAR=$ENV{APL_VAR} || $np->nagios_exit( UNKNOWN, 'APL_VAR environment variable is not defined' );

my $STATUSFILE     ="$APL_VAR/wd/status";

my $status = 'unknown';
if(open STATUS,$STATUSFILE) {
        $status=<STATUS>;
        chomp $status;
        close STATUS;
}
else {
        $np->nagios_exit( UNKNOWN, 'could not read patrol status file');
}

if ($status eq 'run') {
        $np->nagios_exit( OK, 'patrol is running' );
}elsif($status eq 'stop') {
        $np->nagios_exit( WARNING, 'patrol was stopped by command' );
}else{
        $np->nagios_exit( CRITICAL, 'patrol failed' );
}

$np->nagios_exit( OK, 'patrol is running' );
