#!/usr/bin/perl -w

# mailtest check module

# Version: $Revision: 1.3 $
# Author: Eric Garrido
# Date: $Date: 2006/01/19 04:43:38 $
#
# Copyright (c) 2006
# The Trustees of Columbia University in the City of New York
# Columbia University Information Technology
# 
# License restrictions apply, see doc/license.html for details. 

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

my %argformat = ( 'conf' => 'file' );

sub CyrusstuckmailboxValidate {
    return(MODEXEC_OK, 0, "Test OK");  
}

sub CyrusstuckmailboxCheck {

    my $self = shift;
    my $host = shift 
	|| return(MODEXEC_MISCONFIG, 0, "no hostname provided");

    if($host eq ""){
	return(MODEXEC_MISCONFIG, 0, "no hostname provided");
    }
    my $confFile = $self->Arg('conf');
     
    if(-r $confFile){
	open IN, $confFile;
    }
    else {
    	return(MODEXEC_PROBLEM, 0, "cannot read supplied configuration file");
    }

    my $secondary;
    my $username;
    my $password;

    # parse the sync_host line from imapd.conf to find
    # this server's backup, username, and password
    foreach my $line (<IN>){
	if($line =~ /^sync_host:\s*(.+)/) {   
	    if(defined($1)) {
		$secondary = $1;
	    } else {
		return(MODEXEC_PROBLEM, 0, "unable to find secondary server in conf file");
	    }
	}
	elsif($line =~/^sync_authname:\s*(.+)/) {
	    if(defined($1) && $1 ne ""){
		$username = $1;
	    } else {
		return(MODEXEC_PROBLEM, 0, "unable to find username in conf file");
	    }
	}
	elsif(/^sync_password:\s*(.+)/) {
	    if(defined($1 && $1 ne "")){
		$password = $1;
	    } else {
		return(MODEXEC_PROBLEM, 0, "unable to find password in conf file");
	    }
	}
	
	if($password ne "" && $username ne "" && $secondary ne ""){
	    last;
	}
    }

    if($password eq "" || $username ne "" || $secondary ne ""){
	return(MODEXEC_PROBLEM, 0, "unable to find necessary entry in conf file");
    }
    
    close IN;

    # get the output of cyr_quota from each host
    my @quotaPrimary = `cyr_quota`;
    
    # ssh to the other server to get the cyr_quota output there
    my $ssh = Net::SSH::Perl->new($host);
    if(! $ssh){
	return(MODEXEC_PROBLEM, 0, "unable to create ssh session with host $host");
    }

    if (! $ssh->login($username, $password)){
	return(MODEXEC_PROBLEM, 0, "unable to create ssh session with host $host");
    }
    my ($stdout, $stderr, $exit) = $ssh->cmd('cyr_quota');
    
    if($exit != 0){
	return(MODEXEC_PROBLEM, 0, "unable to create ssh session with host $host");	
    }

    my @quotaSecondary = split(/\n/,$stdout);

    # if the number of lines differ, we know we have a problem
    if(scalar(@quotaPrimary) != scalar(@quotaSecondary)){
	return(MODEXEC_PROBLEM, 0, "server pair appears out of sync");
    }    

    @quotaPrimary = map (chomp($_), @quotaPrimary);
    @quotaSecondary = map (chomp($_),@quotaSecondary);

    
    # iterate through the list, comparing each line of output
    for(my $i = 0; $i < scalar(@quotaPrimary); $i++){
	if($quotaPrimary[$i] ne $quotaSecondary[$i]){
	    return (MODEXEC_PROBLEM,0, "server pair appears out of sync");
	}
    }
    
    return(MODEXEC_OK, 1, "server pair is in sync");
}

# begin main block
{

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

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

my $result = $sc->Exec(\&CyrusstuckmailboxCheck);

exit($result);

}
