w3ntp has asked for the wisdom of the Perl Monks concerning the following question:

I would like to use Perl to monitor an NFS mounted disk on a Solaris 9 Sun box. If I use the "open" command to look for a particular file on the NFS mounted disk, and the NFS session drops, then the Open command hangs rather than fails. Hence I can not use if(!open(TESTFILE,$testfile)... I have also tried using a netstat -a|grep for nfs and found that that is unreliable. Any suggestions to easily monitor if I have a disk properly NFS mounted using a perl command that will not hang. That is it should return a success or failure rather than hang. thanks W3NTP

Replies are listed 'Best First'.
Re: How to monitor an NFS mount with perl
by cowboy (Friar) on Feb 08, 2005 at 17:29 UTC
    You could try using an alarm (untested code)
    $SIG{ALRM} = sub { die "Seems hung"; }; # 30 seconds alarm(30); if (open(TESTFILE, $testfile)) { alarm(0); # disable alarm }
    Edit: Make sure to listen to Fletches advice below as well.

      Make sure that the filesystem was mounted with the intr option (or soft,intr depending on the OS; consult your local sysadmin and/or mount(8) manpage), otherwise the open(2) system call will get stuck in system wait and won't return until the NFS server's responding again alarm or no.

        This will probably work, but I've seen systems that don't respond if you wait too long to interrupt the process.

        One of the problems with hard NFS mounts is that anything you do underneath the mount point will cause a hang if the server is unavailable and sometimes not even a SIGKILL can get rid of the process. One way to handle this might be to fork a child process, which performs the check and quits. As long as the child doesn't exit you know the mount point is down.

      Hi I tried the Sig alarm idea and found the Signal was blocked by the open file hanging. Had the following code The Sigalarm subroutine never got called after the NFS got dropped.. $SIG{SIGALRM)=sub {do something}; while(true) { alarm 5; # set a 5 second alarm if(!open(TESTMOUNT,"$testfile")) { do something } else { do somethineelse } close(TESTMOUNT); alarm 0; # disable alarm sleep 5; }
      Tried the above and it did not work thanks W3NTP
Re: How to monitor an NFS mount with perl
by penguinfuz (Pilgrim) on Feb 09, 2005 at 11:27 UTC
    I've written NFS Watcher that monitors an NFS mounted partition from crontab, remounts the partition if necessary and sends email if things go all pear-shaped. Maybe you could find it useful for your situation.