#!/usr/bin/env perl # Last Update: 9 June 2010 # Designed by: Dusan U. Baljevic (dusan.baljevic@ieee.org) # Coded by: Dusan U. Baljevic (dusan.baljevic@ieee.org) # # Copyright 2010-2015 Dusan Baljevic # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # Define PATH $ENV{'PATH'} = "/bin:/usr/sbin:/sbin:/usr/bin:/usr/local/bin"; # Define Shell # $ENV{'SHELL'} = '/bin/sh' if $ENV{'SHELL'} ne ''; $ENV{'IFS'} = '' if $ENV{'IFS'} ne ''; use strict; # Local variables # my $NFSMNT = q{}; my @NFSARR = (); my %opts = (); my $OSNAME = $^O; my $NFSCHK = q{}; my $TIMEOUT = 20; my $i = 0; my $System = q{}; my $Hostname = q{}; my $Maj = q{}; my $Version = q{}; my $Hardware = q{}; my $VH = q{}; my $Major = q{}; my $Minor = q{}; my $Patch = q{}; # Do not allow to run as unprivileged user # if ( $> != 0 ) { print "ERROR: The script should be run with root privileges\n"; exit(1); } # Display usage if "-h" option is used # sub Usage { print < -f NFS mount point -h Print help message MYMSG ; exit(0); } if ( eval "require Getopt::Std" ) { import Getopt::Std; getopts( 'hf:', \%opts ); if ( $opts{h} ) { &Usage; } $NFSMNT = $opts{f}; } else { warn "ERROR: Perl module Getopt::Std not found\n"; } if ( eval "require POSIX" ) { import POSIX 'uname'; import POSIX qw(locale_h); use POSIX qw/getpgrp tcgetpgrp/; ( $System, $Hostname, $Maj, $Version, $Hardware ) = uname(); if ( defined $Maj ) { ( $Major, $Minor, $Patch ) = split( /\./, $Maj ); } } else { warn "WARN: Perl module POSIX not found\n"; } if ( !"$Hostname" ) { $VH = `uname -a 2>&1`; ( $System, $Hostname, $Maj, undef, $Hardware, undef ) = split( /\s+/, $VH ); $Version = $Maj; ( $Major, $Minor, $Patch ) = split( /\./, $Maj ); } if ( ! "$NFSMNT" ) { &Usage; } sub NFSTOUT { # Ensure to time out if NFS hung after around $TIMEOUT seconds # my $NFSMNT2 = shift; $SIG{"ALRM"} = sub {die "ERROR: Timed out waiting to df(1) $NFSMNT2\n"}; alarm $TIMEOUT; while (1) { $i++; $NFSCHK = `df -F nfs $NFSMNT2 2>&1`; if ( "$NFSCHK" ) { last; } } chomp($NFSCHK); # if ( grep(/Warning|no file|error/, "$NFSCHK" )) { if ( grep(/^df:/, "$NFSCHK") ) { print "FAIL: $NFSMNT2 seemingly not NFS-mounted\n"; } else { print "PASS: $NFSMNT2 seemingly NFS-mounted\n"; } } # Ensure to time out if NFS hung after around $TIMEOUT seconds # $SIG{"ALRM"} = sub {die "ERROR: Timed out waiting to stat() $NFSMNT\n"}; alarm $TIMEOUT; while (1) { $i++; @NFSARR = stat($NFSMNT); if ( "@NFSARR" ) { last; } } print "INFO: Operating system $OSNAME\n"; # These are some of entries that $OSNAME holds # # dec_osf # solaris # hpux # linux if ( $NFSARR[0] < 0 ) { # Operating system like AIX generates negative sdevs for NFS # print "PASS: $NFSMNT seemingly NFS-mounted\n"; } elsif ( "$OSNAME" eq "hpux" ) { if ( $NFSARR[$#NFSARR] == 0 ) { print "FAIL: $NFSMNT seemingly not NFS-mounted\n"; } else { NFSTOUT($NFSMNT); } } elsif ( "$OSNAME" eq "linux" ) { $NFSCHK = `stat -f -L -c %T $NFSMNT`; chomp($NFSCHK); if ( "$NFSCHK" eq "nfs" ) { print "PASS: $NFSMNT seemingly NFS-mounted\n"; } else { print "FAIL: $NFSMNT seemingly not NFS-mounted\n"; } } elsif ( "$OSNAME" eq "solaris" ) { # This worked on old SunOS 4.x but I need to test it on SunOS 5.x # if ( "$Major" < 5 ) { if ( $NFSARR[0] && 0x8000 ) { print "PASS: $NFSMNT seemingly NFS-mounted\n"; } else { print "FAIL: $NFSMNT seemingly not NFS-mounted\n"; } } else { # if ( $NFSARR[1] = 2 ) { # print "PASS: $NFSMNT seemingly NFS-mounted\n"; # } # else { # print "FAIL: $NFSMNT seemingly not NFS-mounted\n"; # } NFSTOUT($NFSMNT); } } else { NFSTOUT($NFSMNT); } exit(0);