#!/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('hor:w:c:', \%opts);

my $VERSION = "Version 1.88";
my $AUTHOR = '(o) 2016 Edwin van Ree <support@vanree.com>';
my $SCRIPT = "Nakivo Repository";
my $NAKIVO_USER = "user";
my $NAKIVO_PWD = "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 repository id is provided use it:
my $repositoryid = "1";
if ($opts{'r'}) {
	$repositoryid = $opts{'r'};
}

# If a warning level is provided use it:
my $warninglevel = 1000;
if ($opts{'w'}) {
	$warninglevel = $opts{'w'};
}

# If a critical level is provided use it:
my $criticallevel = 200;
if ($opts{'c'}) {
	$criticallevel = $opts{'c'};
}

# Setup the Nakivo check script
my $script_path = "/opt/nakivo/director/bin";
my $script_to_check = "$script_path/cli.sh";
my $script_args = "-ri $repositoryid -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('\n', $cmd_result);

my @name;
my $index = 0;
my $i;
for ($i = 0; $i < @part; $i++) {
	if ($part[$i] =~ /Name /) { $index = $i }
}
@name = split(':', $part[$index]);
$name[1] =~ s/^\s+|\s+$//g;		# Job name

my @state;
$index = 0;
for ($i = 0; $i < @part; $i++) {
	if ($part[$i] =~ /State /) { $index = $i }
}
@state = split(':', $part[$index]);
$state[1] =~ s/^\s+|\s+$//g;	# State

my @space;
$index = 0;
for ($i = 0; $i < @part; $i++) {
	if ($part[$i] =~ /Usage data /) { $index = $i }
}
@space = split(':', $part[$index]);
$space[1] =~ s/^\s+|\s+$//g;	# Space info

my @status;
$index = 0;
for ($i = 0; $i < @part; $i++) {
	if ($part[$i] =~ /Status /) { $index = $i }
}
@status = split(':', $part[$index]);
$status[1] =~ s/^\s+|\s+$//g;	# Status

if ($state[1] =~ /OK/) {
	# get the free space value
	my @freespace = split(',', $space[1]);
	my @freespaceitem;
	if ($freespace[1] =~ /free/) {
		@freespaceitem = split(' ', $freespace[1]);
	} else {
		@freespaceitem = split(' ', $freespace[1] . $freespace[2]);
	}
	
	my $freeMB = $freespaceitem[0];
	if ($freespaceitem[1] =~ /GB/) {
		$freeMB *= 1000;
	} elsif ($freespaceitem[1] =~ /TB/) {
		$freeMB *= 1000000;
	}

	if ($freeMB <= $criticallevel) {
		$message = "$SCRIPT is CRITICAL (< $criticallevel MB)";
		$error = $STATE_CRITICAL;

	} elsif ($freeMB <= $warninglevel) {
		$message = "$SCRIPT is WARNING (< $warninglevel MB)";
		$error = $STATE_WARNING;

	} elsif ($status[1] ne "None") {
		$message = "$SCRIPT is WARNING (Maintenance active)";
		$error = $STATE_WARNING;
	
	} else {
		$message = "$SCRIPT is OK";
		$error = $STATE_OK;
	}

} else {
	$message = "$SCRIPT is CRITICAL State=$state[1]";
	$error = $STATE_CRITICAL;
}
if ($opts{'o'}) {}
else {
	$message .= " - $name[1]: $space[1] - $status[1]";
}

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

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

####################################
# Help:
####################################
sub print_help() {
        print << "EOF";
Check the state and free space of a Nakivo repository
$VERSION
$AUTHOR
Do not forget to set the Nakivo user and password in this script

Options:
-h
	Print this help screen

-r
	repositoryID (required)
	The ID (number) of the Nakivo repository. Find this with ./cli.sh -rl.

-w
	warning level of free space in MB, default = 1000 MB

-c
	critical level of free space in MB, default = 200 MB

-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
}

