#!/usr/bin/perl
# -----------------------------------------------------------------------------
#  check_install_result : Nagios plugin for checking install/update result
# -----------------------------------------------------------------------------
#  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 $INSTALL_RESULT ="$APL_VAR/vctl/result";

my ($iresult,$uresult);

if(open(RESULT, $INSTALL_RESULT)) {
  my %result=map{/(^\w+)=(.+)/} grep {/^\w+=.+/} <RESULT>;
  $iresult=$result{ACTIVATE};
  $uresult=$result{UPDATE};
  close RESULT;
}
if(!$iresult) {
    $np->nagios_exit( CRITICAL, "Software installation status: 'UNKNOWN'" );
} elsif($iresult ne 'SUCCESS') {
    $np->nagios_exit( CRITICAL, "Software installation status: '$iresult'" );
}

if(!$uresult) {
    $np->nagios_exit( CRITICAL, "Software update result: 'UNKNOWN'" );
} elsif($uresult ne 'SUCCESS') {
    $np->nagios_exit( CRITICAL, "Software update result: '$uresult'" );
}

$np->nagios_exit( OK, 'Install/update status is NORMAL' );