#!/usr/bin/perl

# Make sure that the NFS mounts we have are not stale.
# Expected to be called locally on the box and state is communicated back
# to mon via a special snmp var.
# Augie Schwer <augie@corp.sonic.net>
# Mon Mar 24 11:32:47 PDT 2008

use strict;
use warnings;
use English;

my @failed_dirs = ();
my @nfs_mounts  = ();
my @mount_lines = ();
my ($line,$dir,$pid);
my $timeout	= 3;

# Get the list of currently mounted NFS mounts.
@mount_lines = `mount -t nfs`;

foreach $line (@mount_lines)
{
	push @nfs_mounts , (split / / , $line)[2];
}

chdir('/opt/sonic-monitoring/');
umask(0022);
close(STDIN);
close(STDOUT);
close(STDERR);
open(STDIN, '/dev/null');
open(STDOUT, '>/dev/null');
open(STDERR, '>/dev/null');

# Do a readdir on all the mounts; if we don't come back within the timeout, then 
# alert to break the hanging proc., then add the dir. to the list of failed dirs.
foreach $dir (@nfs_mounts)
{
	$pid = fork;

	if ($pid)
	{	#parent
		eval
		{
			local $SIG{'ALRM'} = sub { die 'Timeout Alarm' };
			alarm $timeout;

			waitpid($pid,0);
		};

		if ($EVAL_ERROR =~ 'Timeout Alarm')
		{
			push @failed_dirs , $dir;
		}
	}
	else
	{	#child
		opendir(DIR, $dir);
		readdir(DIR);
		closedir(DIR);
		exit;
	}
}

exit 1 if @failed_dirs;

exit 0;

