#!/usr/bin/perl -w

# conserver check module

# Version: $Revision: 0.1 $
# Author: Benjamin Oshrin
# Date: $Date: 2006/01/24 23:39:16 $
#
# Copyright (c) 2005
# The Trustees of Columbia University in the City of New York
# Academic Information Systems
# 
# License restrictions apply, see doc/license.html for details.

use strict;
use FindBin;
use lib "$FindBin::Bin/../common";
use Survivor::Check;

my %argfmt = ();

my $sc = new Survivor::Check();

$sc->GetOpt(\%argfmt, \&ConserverValidate);

my $CONSOLE = $sc->Which('console');

if(!defined $CONSOLE) {
    $sc->ExitError(MODEXEC_MISCONFIG, 0, 'unable to find "console"');
}

my $r = $sc->Exec(\&ConserverCheck);

exit($r);

sub ConserverValidate {
    my $self = shift;

    return(MODEXEC_OK, 0, "Conserver OK");
}

sub ConserverCheck {
    my $self = shift;
    my $host = shift;

    if($host ne 'localhost') {
	return(MODEXEC_MISCONFIG, 0, "Module must run on 'localhost'");
    }

    my @down = ();
    my @up = ();
    
    open(ANSWER, "$CONSOLE -i 2>/dev/null |") ||
	return(MODEXEC_MISCONFIG, 0, "Failed to exec $CONSOLE");

    while(<ANSWER>) {
	# There's lots of useful info, here, but all we look for right
	# now is if a console is down
	
	chomp;
	my @cinfo = split(/:/, $_);

	if($cinfo[5] eq "up") {
	    push(@up, $cinfo[0]);
	} else {
	    # Append connector info

	    push(@down, $cinfo[0]."@".$cinfo[3]);
	}
    }
    
    close(ANSWER);

    if(scalar(@down) > 0) {
	return(MODEXEC_PROBLEM, scalar(@down),
	       scalar(@down) . " consoles down (" . scalar(@up) . " OK): ".
	       join(',', @down));
    } else {
	return(MODEXEC_OK, 0, scalar(@up) . " consoles OK");
    }
}
