#!/usr/bin/perl -wT
#
# Veritas Cluster System monitor for Nagios.
# Written by Aaron Bostick (abostick@mydoconline.com)
# Last modified: 05-22-2002
#
# Usage: check_vcs {-g <vcs_group> | -r <vcs_resource> } -s <vcs_system> [-n]
#
# Description:
#
# This plugin is just a perl wrapper to the vcs commands hagrp and hares. 
# You specify what group/resource and system you want the status for, and
# the plugin returns a status code based on the output of either hagrp or 
# hares.
#
# Normal hagrp/hares status codes are ONLINE and OFFLINE depending on where the 
# cluster service currently lives.  I have added an option, -n, which makes
# the expected state to be OFFLINE rather than ONLINE so you can run the 
# plugin on both sides of the cluster and will receive critical alerts when
# the cluster fails over i.e.  a proper failover will make the standby node
# go from OFFLINE to ONLINE for the group, so an ONLINE status should alert
# you! (You do want to know when the cluster fails over, right? :))
#
# Output:
#
# This plugin returns OK when hagrp/hares -state <grp> -sys <system> returns 
# ONLINE (or OFFLINE if -n is specified).  Any other hagrp/hares string returns 
# CRITICAL...  Would a WARNING ever be justified???  UNKNOWN is returned if 
# hagrp/hares cannot run for some reason.
# 
# Examples:
#
# Make sure group oracle is ONLINE on server dbserver1:
#   check_vcs -g oracle -s dbserver1
#
# Make sure group oracle is OFFLINE on server dbserver2:
#   check_vcs -g oracle -s dbserver2 -n
#
# Make sure resource oraip is ONLINE on server dbserver1:
#   check_vcs -r oraip -s dbserver1
#
# Make sure resource oraip is OFFLINE on server dbserver2:
#   check_vcs -r oraip -s dbserver2 -n
#

require 5.004;
use strict;
use lib utils.pm ;
use Plugin;
use Plugin::Parameter qw(:DEFAULT);

use vars qw($PROGNAME $vcs_group $vcs_resource $vcs_system $vcs_negate);

use utils qw(%ERRORS);

my $g_opt = new Plugin::Parameter(-name => "group", -flags => [ 'g', 'group' ],
				  -optional => "yes", -valueoptional => "no", -type => "VCSGROUP",
				  -binding => \$vcs_group,
				  -checker => sub { my ($opt, $parameter, $plugin) = @_;
						    if (defined $$opt) {
						      $$opt =~ m/^(.*)$/;
						      $$opt = $1;
						    }
						  },
				  -description => "The HA group to be validated");
my $r_opt = new Plugin::Parameter(-name => "resource", -flags => [ 'r', 'resource' ],
				  -optional => "yes", -valueoptional => "no", -type => "VCSRESOURCE",
				  -binding => \$vcs_resource,
				  -checker => sub { my ($opt, $parameter, $plugin) = @_;
						    if (defined $$opt) {
						      $$opt =~ m/^(.*)$/;
						      $$opt = $1;
						    }
						  },
				  -description => "The HA resource to be validated");
my $s_opt = new Plugin::Parameter(-name => "system", -flags => [ 's', 'system' ],
				  -optional => "no", -valueoptional => "no", -type => "VCSSYSTEM",
				  -binding => \$vcs_system,
				  -checker => sub { my ($opt, $parameter, $plugin) = @_;
						    if (defined $$opt) {
						      $$opt =~ m/^(.*)$/;
						      $$opt = $1;
						    }
						  },
				  -description => "The HA system where the group/resource lives");
my $n_opt = new Plugin::Parameter(-name => "negate", -flags => [ 'n', 'negate' ],
				  -optional => "yes", -valueoptional => "yes", -type => "NONE",
				  -binding => \$vcs_negate,
				  -description => "Set the expected result to OFFLINE instead of ONLINE");
my $plugin = new Plugin(-revision => '$Revision: 1.1 $',
			-copyright => "2002 Aaron Bostick <abostick\@mydoconline.com>, 2004 Howard Wilkinson <howard\@cohtech.com>",
			-shortcomment => "Validate output from hagrp/hares command.",
			-checker => sub { my ($plugin) = @_;
					  if ($vcs_group && $vcs_resource) {
					    $plugin->usage();
					    utils::usage("$PROGNAME UNKNOWN: Please specify either a group of a resource, but not both\n");
					  }
					  if (!$vcs_group && !$vcs_resource) {
					    $plugin->usage();
					    utils::usage("$PROGNAME UNKNOWN: HA group or resource not specified\n");
					  }
					},
			-parameterlists => [ [ $s_opt, $g_opt, $r_opt, $n_opt, $t_opt ], $h_opts, $V_opts ]);

# Initialize strings
my $vcs_bin = '/opt/VRTSvcs/bin';
my $vcs_command = '';
my $vcs_arg = '';

my $vcs_result = '';
my $vcs_expected_result = 'ONLINE';

$plugin->init();

(!$vcs_negate) || ($vcs_expected_result = 'OFFLINE');

# Specify proper command
if ($vcs_group) {
  $vcs_command = 'hagrp';
  $vcs_arg = $vcs_group;
} else {
  $vcs_command = 'hares';
  $vcs_arg = $vcs_resource;
}

$ENV{PATH} = '';
$ENV{BASH_ENV} = '';
# Run command and save output
$vcs_result = `$vcs_bin/$vcs_command -state $vcs_arg -sys $vcs_system`;
chomp ($vcs_result);

# If empty result, return UNKNOWN
if (!$vcs_result) {
  print "$PROGNAME UNKNOWN: Problem running $vcs_command...\n";
  exit $ERRORS{'UNKNOWN'};
}
# If result is expected, return OK
# If result is not expected, return CRITICAL
if ($vcs_result eq $vcs_expected_result) {
  print "$PROGNAME OK: $vcs_command $vcs_arg is $vcs_result\n";
  exit $ERRORS{'OK'};
} else {
  print "$PROGNAME CRITICAL: $vcs_command $vcs_arg is $vcs_result\n";
  exit $ERRORS{'CRITICAL'};
}

# NOT REACHED.
