#!/usr/bin/perl -w
#
# BACKGROUND
# This is a netsaint plugin which allows you to check the status of your 
# tape jukeboxes w/ Veritas NetBackup.  If you've got no jukeboxes, or 
# you aren't running VNB, look elsewhere.
#
# OPERATION
# Run the script.  Or configure it to work in Netsaint.  There are no 
# real options.  Just make sure the path to "tpconfig" is correct.  :)
#
# CONFIGURATION
# This script needs to run on the Veritas NetBackup server as root, or 
# the tpconfig utility needs to be setuid.  Or you could use sudo.  Or...
# well, you get the idea.  Also, it helps if you have perl installed.
# Oh, and having Veritas NetBackup installed and working is good too.
# Make sure the tape jukebox is configured and setup and has power and 
# all that good stuff too.  ;)
#
# ISSUES
# This script only works on the master server and must be run as root
# (well... see CONFIGURATION).  This is not as much a limitation of this
# wonderful, fascinating, and beautiful script as much as a limitation
# in the lousy utilities that Veritas bundles with NetBackup.
#
# TODO
# Make it conform (fight the power!  down with the man!) to the Netsaint plugin
# standards.  Add in all of the silly little flags for various things.  Maybe 
# make it more flexible.  Add in more than OK and CRITICAL states - if there 
# ARE states other than UP and DOWN.
#
# AUTHOR
# Nicholas Tang <ntang@mail.communityconnect.com>

use strict;

#######################################################
# SET THIS PATH CORRECTLY OR YOUR LIFE IS FORFEIT
#######################################################
my $TP_COMMAND = "/opt/openv/volmgr/bin/tpconfig -d";
#######################################################

#######################################################
# Please don't change these.
#######################################################
use constant OK		=> 0;
use constant WARNING	=> 1;
use constant CRITICAL	=> 2;
use constant UNKNOWN	=> 3;
use constant VERSION	=> 0.2;
#######################################################

my(@drive, $plural, $statusname);
my $drivesnotup = undef;
my $notupcounter = 0;
my $status = OK;

open IN,"$TP_COMMAND|" || die "Something has gone terribly, terribly wrong with $TP_COMMAND\n";

while (my $tpline=<IN>) {
  chomp($tpline);
  if ($tpline =~ /^Index DriveName/) { next; }
  if ($tpline =~ /^\*\*\*\*\* \*\*\*\*\*\*\*\*\*/) { next; } # use \Q \E maybe?
  if ($tpline =~ /(\d+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/) {
    my $id = $1;
    $drive[$id]{drivename}	= $2;
    $drive[$id]{drivepath}	= $3;
    $drive[$id]{drivetype}	= $4;
    $drive[$id]{multihost}	= $5;
    $drive[$id]{drvstatus}	= $6;
    #print "Read: $id - $drive[$id]{drivename} - $drive[$id]{drivepath} - $drive[$id]{drivetype} - $drive[$id]{multihost} - $drive[$id]{drvstatus}\n";
  }
}

for my $i ( 0 .. $#drive ) {
  #print "Drive check $i $drive[$i]{drvstatus}\n";
  if ( $drive[$i]{drvstatus} eq "UP" ) {
    next;
  } else {
    $drivesnotup .= " $drive[$i]{drivename} ($drive[$i]{drivetype} $drive[$i]{drivepath} - $drive[$i]{drvstatus})";
    $notupcounter++;
    if ($drive[$i]{drvstatus} eq "DOWN") { 
      $status = CRITICAL 
    } elsif ( $status == 0 ) { 
      $status = WARNING; 
    }
  }
}

if ( $status == 2 ) {
  $statusname = "CRITICAL";
} elsif ( $status == 0 ) {
  $statusname = "OK";
} else {
  $statusname = "WARNING";
}

if ($notupcounter) {
  if ($notupcounter > 1) {
    $plural = "drives are";
  } else {
    $plural = "drive is";
  }
  print "$statusname - $notupcounter $plural not up:$drivesnotup\n";
} else {
  print "OK - all drives are up.\n";
}

exit($status);
