#!/usr/bin/perl -w

# oncall check module

# Version: $Revision: 0.7 $
# Author: Benjamin Oshrin and Matt Selsky
# Date: $Date: 2003/01/29 01:20:23 $
#
# Copyright (c) 2002 - 2003
# 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 %argformat = ("instance" => "string",
		 "list"     => "string list");

my $SCEXEC = "";

sub OncallValidate {
    my ($self) = @_;
    
    if(!defined $SCEXEC) {
	return(MODEXEC_MISCONFIG, 0, "sc executable not found");
    } else {			   
	return(MODEXEC_OK, 0, "Oncall OK");
    }
}

sub OncallCheck {
    my ($self, $host) = @_;

    my ($r, $s, $err) = OncallValidate($self);

    if($r == MODEXEC_OK) {
	$err = "";
	$r = MODEXEC_PROBLEM;
	my $instance = $self->Arg("instance");
	my @lists = $self->Arg("list");
    
	foreach my $l (@lists) {
	    open(SC, "$SCEXEC -i $instance clstat $l 2>&1 |");
	    
	    while(<SC>) {
		if(/is now on call/) {
		    my($x,$who,$junk);
		    ($x, $x, $who, $junk) = split(/ /, $_, 4);
		    $who =~ s/@.*//;
		    
		    $err = sprintf("%s%s on call for $l,", $err, $who);
		    $r = MODEXEC_NOTICE;
		}
	    }
	}
	
	close(SC);
	
	if($r == MODEXEC_NOTICE) {
	    chop($err);
	} else {
	    $err = "Misconfiguration or call list does not exist";
	}
    }

    return($r, $s, $err);
}

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

$SCEXEC = $sc->Which("sc");

$sc->GetOpt(\%argformat, \&OncallValidate);

# Threading and forking generally won't help us, especially as we should
# only run for one host (localhost).
$sc->UseThreads(0);
$sc->UseFork(0);

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

exit($r);
