#!/usr/bin/perl -w
#
# BACKGROUND
# This checks the length of the queue.  You give it a warning and a critical level.
#
# OPERATION
# Run this with a warning and critical level... and that's it.
#
# CONFIGURATION
# Set the path for "nbdevquery".
#
# ISSUES
#
# TODO
#
# AUTHOR
# Daniel Steiner <daniel.steiner@trivadis.com>

use strict;
use warnings;
use File::Basename;

#######################################################
# SET THIS PATH CORRECTLY OR YOUR LIFE IS FORFEIT
#######################################################
my $nbbin = "/usr/openv/netbackup/bin/admincmd/nbdevquery";
my $BPDBJOBS = "$nbbin -listdv -stype BasicDisk -l";
#######################################################

#######################################################
# Please don't change these.
#######################################################
use constant OK		=> 0;
use constant WARNING	=> 1;
use constant CRITICAL	=> 2;
use constant UNKNOWN	=> 3;
use constant VERSION	=> 1.0;
#######################################################
use vars qw($status $warning $critical $queued);
#######################################################
# warning and critical levels (defaults), can be overwriten from commandline:
my $warning = 85;
my $critical = 95;
# variable init:
my $disk = "NONE";
my $progname = basename($0);
my $progversion = "1.0";
my @output = ();
my $l = "";
my @lines = ();
my $amount = 0;
my $i = 0;
#######################################################

##### Main:
&readOpts();
if (!-x "$nbbin") {
  print ("Error:\tExecutable, '$nbbin', not found on this server, is netbackup installed, or the binary somewhere else intalled?\n\n");
  exit 2;
}
$status = OK;

# get output from NB command:
@output = `$BPDBJOBS`;
chomp @output;
foreach $l (@output) {
  if ("$disk" eq "NONE") {
	&getparms($l);
  } else {
	if ($l =~ /$disk/i) {
	  &getparms($l);
	}
  }
}
# printout the statuses:
foreach $l (@lines) {
  print ("$l\n");
}

exit $status;

##### Subs:
# get parameters from line:
sub getparms {
  my $l = $_[0];
  my $name = "";
  my $used = "";
  my $free = "";
  my $type = "";
  my $percent = 0;
  # NB version BasicDisk name     Type                         Used    Free   Used %
  # V7.0       STU_SBKS002_DSSU01 BasicDisk Internal_16 @aaaab 2952.92 743.86 74     1 0 1 0 0 14
  $l =~ /^V\d+\.\d+\s+(\w+)\s+(\w+)\s+\w+\s+\@\w+\s+(\d+\.\d+)\s+(\d+\.\d+)\s+(\d+)\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+$/;
  ($name, $type, $used, $free, $percent) = ($1, $2, $3, $4, $5);
  if ($percent > $critical) {
		$status = CRITICAL;
		$lines[$i] = "CRITICAL - ";
  } elsif ($percent > $warning) {
		$status = WARNING;
		$lines[$i] = "WARNING - ";
  } else {
		$lines[$i] = "OK - ";
  }
  if ($amount == 1) {
		$warning = ($warning / 100) * $used;
		$critical = ($critical / 100) * $used;
		$lines[$i] = "$lines[$i]"."Disk usage in GB (Used/Free) for $name: [ ${used}/${free} ] | 'Disk usage'=${used}GB;$warning;$critical";
  } else {
		$lines[$i] = "$lines[$i]"."Disk usage in % for $name: [ $percent ] | 'Disk usage'=${percent}%;$warning;$critical;0;100";
  }
  $i++;
}

# read options:
sub readOpts {
  my $i = 0;
  my $next = 0;
  my $check = 0;
  my $ad = "";
  my $fi = 0;
  if (@ARGV > 0) {
	for ($i = 0; $i < @ARGV; $i++) {
	  if ($next == 1) {
	  $next = 0;
		if ($ARGV[$i] =~ /^-/) {
		  print ("Error:\tThis is probably a typing mistake, I need here an other argument....\n");
		  &usage();
		}
	  } elsif ($next > 1) {
		$next--;
	  } else {
		if ($ARGV[$i] =~ /^-\w|^--\w/) {
		  if ($ARGV[$i] =~ /^-c$|^--critical$/) {
			$critical = $ARGV[$i+1];
			if ($critical =~ /^!\d+$/) {
			  print ("Error:\tYou must give a digit (number) here...\n");
			}
			$next = 1;
		  } elsif ($ARGV[$i] =~ /^-w$|^--warning$/) {
			$warning = "$ARGV[$i+1]";
			if ($warning =~ /^!\d+$/) {
			  print ("Error:\tYou must give a digit (number) here...\n");
			}
			$next = 1;
		  } elsif ($ARGV[$i] =~ /^-d$|^--disk$/) {
			$disk = "$ARGV[$i+1]";
			$next = 1;
		  } elsif ($ARGV[$i] =~ /^-a$|^--amount$/) {
			$amount = 1;
		  } elsif ($ARGV[$i] =~ /^-h$|^--help$/) {
			&usage();
		  } else {
			print ("Error:\tOption, '$ARGV[$i]' is not valid, please try again!\n");
			&usage();
		  }
		}
	  }
	}
  }
}

# print out usage and exit:
sub usage {
  print ("\n$progname\t[ -c|--critical <digit> ]  [ -w|--warning <digit> ] [ -d|--disk <BasicDisk (name)> ] [ -a|--amount ]\n\n");
  print ("\t--amount :\tShows the amount in GB (Used/Free) instead of % usage\n");
  print ("\n\n");
  exit 1;
}
