#!/usr/bin/perl -w

# mounts check module

# Version: $Revision: 0.11 $
# Author: Benjamin Oshrin and Matt Selsky
# Date: $Date: 2003/01/29 01:18:27 $
#
# 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 %argfmt = ("filesystem" => "optional string list");

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

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

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

exit($r);

sub MountsValidate {
    my $self = shift;

    $self->ShouldBeMountedFilesystems('all');
    $self->MountedFilesystems();

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

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

    my $errors = '';
    my $found = 0;
    my $rc = MODEXEC_OK;

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

    # Filesystems to check for
    my @filesystems;
    if(scalar $self->Arg('filesystem')) {
	@filesystems = $self->Arg('filesystem');
    } else {
	# Use everything in system files
	push @filesystems, $self->ShouldBeMountedFilesystems('all');
    }

    my %are = %{$self->MountedFilesystems()};

    foreach my $fs (@filesystems) {
	if(exists $are{$fs}) {
	    # $fs is mounted on $are{$fs}, but for brevity's sake we don't
	    # note it in the return string (though we could)
	    $found++;
	} else {
	    $errors .= "$fs not mounted,";
	    $rc = MODEXEC_PROBLEM;
	}
    }
    
    if($rc == MODEXEC_OK) {
	$errors = 'All filesystems are mounted';
    } else {
	chop($errors);
    }
    
    return($rc, $found, $errors);
}
