#!/usr/bin/perl -w
################################################################################
#                                                                              #
#   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  #
#                                                                              #
################################################################################

use strict;
use Getopt::Std;
$| = 1;

my %opts;
getopts('hoj:', \%opts);

my $VERSION = "Version 1.7";
my $AUTHOR = '(o) 2013 Edwin van Ree <support@vanree.com>';
my $SCRIPT = "Nakivo Job";
my $NAKIVO_USER = "<my username>";
my $NAKIVO_PWD = "<my password>";

# Exit codes
my $STATE_OK = 0;
my $STATE_WARNING = 1;
my $STATE_CRITICAL = 2;
my $STATE_UNKNOWN = 3;
my $error = $STATE_OK;

# Parse command line options
if ($opts{'h'}) {
	&print_help();
	exit($STATE_OK);
}

# If a job id is provided use it:
my $jobid = "1";
if ($opts{'j'}) {
	$jobid = $opts{'j'};
}

# Setup the Nakivo check script
my $script_path = "/opt/nakivo/director/bin";
my $script_to_check = "$script_path/cli.sh";
my $script_args = "-ji $jobid -u $NAKIVO_USER -pwd $NAKIVO_PWD";
my $cmd = "$script_to_check $script_args" . ' 2>&1';

# the Nakivo script uses relative paths, so we need to jump there first
chdir $script_path;

# run the Nakivo command and get the result:
my $cmd_result = `$cmd`;
chomp($cmd_result);

my $message;
my @part;
@part = split('\|', $cmd_result);
$part[4] =~ s/^\s+|\s+$//g;		# Job name
$part[5] =~ s/^\s+|\s+$//g;		# State
$part[6] =~ s/^\s+|\s+$//g;		# Last run result

if ($part[6] =~ /Successful/) {
	$message = "$SCRIPT is OK";
	$error = $STATE_OK;

} elsif ($part[5] =~ /Running/) {
	$message = "$SCRIPT is WARNING (Running)";
	$error = $STATE_WARNING;

} else {
	$message = "$SCRIPT is CRITICAL";
	$error = $STATE_CRITICAL;
}
if ($opts{'o'}) {}
else {
	$message .= " - $part[4]: $part[6]";
}

if ($message eq '') {
	print "Error: program failed to generate output\n";
	exit($STATE_UNKNOWN);
}

print "$message\n";
exit($error);

sub print_help() {
        print << "EOF";
Check the last Run status of a Nakivo backup job
$VERSION
$AUTHOR
Do not forget to set the Nakivo user and password in this script

Options:
-h
	Print this help screen

-j
	jobID (required)
	The ID (number) of the Nakivo job. Find this with ./cli.sh -jl.

-o
	This is the "SUPPRESS OUTPUT" flag. Some programs have a long output
	(like iptables), this flag suppresses that output so it is not printed
	as a part of the nagios message.
EOF
}

