#! /bin/sh
#
#   Copyright (c) International Business Machines  Corp., 2001
#
#   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 2 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 implie; 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, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#  FILE   : nfsstat
#
#  PURPOSE: Tests the 'nfsstat' command.  This test runs locally, so no 
#           actual network connection is needed.
#
#  SETUP: The nfs server/client daemons must be active.
#
#
#  HISTORY:
#    07/30/01 Robbie Williamson (robbiew@us.ibm.com)
#      -Written
#
#**********************************************************************

#Uncomment line below for debug output.
#trace_logic=${trace_logic:-"set -x"}

$trace_logic

#-----------------------------------------------------------------------
# Initialize local variables
#-----------------------------------------------------------------------

PID=$$

TC=nfsstat01
TCbin=${TCbin:=`pwd`}
EXPORTDIR=${EXPORTDIR:=/tmp/$TC$PID}
HOST=`hostname`
CLEANUP=${CLEANUP:="ON"}
VERSION=${VERSION:=2}
TESTDIR=${TESTDIR:=/tmp/$TC$PID.testdir}
NFS_TYPE=${NFS_TYPE:nfs}

if [ "x$NFS_TYPE" != "xnfs4" ]; then
    OPTS=${OPTS:="-o vers=$VERSION "}
fi

#---------------------------------------------------------------------#
# FUNCTION: do_setup
# PURPOSE:  To create the necessary files to carry out the test
# INPUT:    None.
# OUTPUT:   None.
#---------------------------------------------------------------------#
do_setup()
{
$trace_logic

   echo "do_setup $TC"

   echo "This test runs LOCALLY."

   IAM=${IAM:=`whoami`}
   [ $IAM = "root" ] || end_testcase "Must be root user"

   #Check for mountd.
   rpcinfo -p | grep mountd
   [ $? -eq 0 ] || end_testcase "Mountd is NOT running"
   
   #Check for nfsd.
   rpcinfo -p | grep nfs
   [ $? -eq 0 ] || end_testcase "NFS server daemon (nfsd) is NOT running"

   #Create export directory
   mkdir -p $EXPORTDIR
   [ $? -eq 0 ] || end_testcase "Could not create $EXPORTDIR"

   #Export the data directory on HOST
   if [ "x$NFS_TYPE" = "xnfs4" ]; then
        mkdir -p /export$EXPORTDIR
        [ $? = 0 ] || end_testcase "Could not create /export$EXPORTDIR locally"
        mount --bind $EXPORTDIR /export$EXPORTDIR
        [ $? = 0 ] || end_testcase "Could not bind $EXPORTDIR to /export"
        /usr/sbin/exportfs -o no_root_squash,rw,nohide,insecure,no_subtree_check $HOST:$EXPORTDIR > /dev/null
        [ $? = 0 ] || end_testcase "Could not export $EXPORTDIR on $HOST"
    else
       /usr/sbin/exportfs -i -o rw,no_root_squash $HOST:/$EXPORTDIR > /dev/null
       [ $? -eq 0 ] || end_testcase "Could not export $EXPORTDIR on $HOST"
   fi

   #Verify export
   /usr/sbin/showmount -e $HOST | grep $EXPORTDIR > /dev/null
   [ $? -eq 0 ] || end_testcase "$EXPORTDIR not exported"

   #Create mount point
   mkdir -p $TESTDIR
   [ $? -eq 0 ] || end_testcase "Could not create $TESTDIR"

   #Mount from HOST.
   mount -t $NFS_TYPE $OPTS $HOST:$EXPORTDIR $TESTDIR
   [ $? -eq 0 ] || end_testcase "Could not mount from $HOST"

}


#---------------------------------------------------------------------#
# FUNCTION: do_test
# PURPOSE:  Performs simple copies and removes to verify statistic 
#           tracking using the 'nfsstat' command and /proc/net/rpc
#---------------------------------------------------------------------#
do_test() 
{
$trace_logic 
  echo "do_test $TC"
   
  echo "Checking RPC calls for server and client"
  
  # Server
  SERVER_CALLS=`cat /proc/net/rpc/nfsd | grep rpc | awk '{print $2}'`
  nfsstat -sr | grep $SERVER_CALLS
  [ $? -eq 0 ] || end_testcase "Check of server RPC calls failed"
  
  # Client
  CLIENT_CALLS=`cat /proc/net/rpc/nfs | grep rpc | awk '{print $2}'`
  nfsstat -cr | grep $CLIENT_CALLS
  [ $? -eq 0 ] || end_testcase "Check of client RPC calls failed"
 
  
  echo "Checking for tracking of RPC calls for server and client"
  cat /proc/cpuinfo > $TESTDIR/nfsstat01.tmp
  NEW_SERVER_CALLS=`cat /proc/net/rpc/nfsd | grep rpc | awk '{print $2}'`
  [ $NEW_SERVER_CALLS -gt $SERVER_CALLS ] || end_testcase "Server RPC calls not increased"
  NEW_CLIENT_CALLS=`cat /proc/net/rpc/nfs | grep rpc | awk '{print $2}'`
  [ $NEW_CLIENT_CALLS -gt $CLIENT_CALLS ] || end_testcase "Client RPC calls not increased"

  nfsstat -sr | grep $NEW_SERVER_CALLS
  [ $? -eq 0 ] || end_testcase "nfsstat not reporting change in server RPC calls" 
  nfsstat -cr | grep $NEW_CLIENT_CALLS
  [ $? -eq 0 ] || end_testcase "nfsstat not reporting change in client RPC calls"


  echo "Checking NFS calls for server and client"

  # Server
  if [ "$VERSION" = "2" ]; then
   SERVER_CALLS=`cat /proc/net/rpc/nfsd | grep proc2 | awk '{print $13}'`
  else
   SERVER_CALLS=`cat /proc/net/rpc/nfsd | grep proc3 | awk '{print $15}'`
  fi 
  nfsstat -sn | grep $SERVER_CALLS
  [ $? -eq 0 ] || end_testcase "Check of server NFS calls failed"

  # Client
  if [ "$VERSION" = "2" ]; then
    CLIENT_CALLS=`cat /proc/net/rpc/nfs | grep proc2 | awk '{print $13}'`
  else
    CLIENT_CALLS=`cat /proc/net/rpc/nfs | grep proc3 | awk '{print $15}'`
  fi
  nfsstat -cn | grep $CLIENT_CALLS
  [ $? -eq 0 ] || end_testcase "Check of client NFS calls failed"

  
  echo "Checking for tracking of NFS calls for server and client"
  
  rm -f $TESTDIR/nfsstat01.tmp
  if [ "$VERSION" = "2" ]; then
   NEW_SERVER_CALLS=`cat /proc/net/rpc/nfsd | grep proc2 | awk '{print $13}'`
  else
   NEW_SERVER_CALLS=`cat /proc/net/rpc/nfsd | grep proc3 | awk '{print $15}'`
  fi
  [ $NEW_SERVER_CALLS -gt $SERVER_CALLS ] || end_testcase "Server NFS calls not increased"

  if [ "$VERSION" = "2" ]; then
    NEW_CLIENT_CALLS=`cat /proc/net/rpc/nfs | grep proc2 | awk '{print $13}'`
  else
    NEW_CLIENT_CALLS=`cat /proc/net/rpc/nfs | grep proc3 | awk '{print $15}'`
  fi
  [ $NEW_CLIENT_CALLS -gt $CLIENT_CALLS ] || end_testcase "Client NFS calls not increased"

  nfsstat -sn | grep $NEW_SERVER_CALLS
  [ $? -eq 0 ] || end_testcase "nfsstat not reporting change in server NFS calls"
  nfsstat -cn | grep $NEW_CLIENT_CALLS
  [ $? -eq 0 ] || end_testcase "nfsstat not reporting change in client NFS calls"


}

#---------------------------------------------------------------------#
# FUNCTION: do_cleanup
# PURPOSE:  To delete all the files created to run this test.
# INPUT:    None.
# OUTPUT:   None.
#---------------------------------------------------------------------#
do_cleanup() 
{
$trace_logic
   echo "do_cleanup $TC "

   echo "Unmounting TESTDIR"
   umount $TESTDIR
   sleep 3
   rm -rf $TESTDIR
   sleep 3

   echo "Unmounting EXPORTDIR"
   /usr/sbin/exportfs -u $HOST:$EXPORTDIR 
   umount $EXPORTDIR
   sleep 3
   rm -rf $EXPORTDIR
   if [ "x$NFS_TYPE" = "xnfs4" ]; then
       rm -rf /export$EXPORTDIR
   fi
}

#=============================================================================
# FUNCTION NAME:        end_testcase
#
# FUNCTION DESCRIPTION: Clean up
#
# PARAMETERS:           None.
#
# RETURNS:              None.
#=============================================================================
end_testcase()
{
$trace_logic
    if [ "$CLEANUP" = "ON" ]; then
       do_cleanup
    fi

    [ $? = 0 ] && { echo "Test PASS"; exit 0; }
    echo "Test FAIL: $@"
    exit 1
}

#=============================================================================
#---------------------------------------------------------------------#
# FUNCTION: MAIN
# PURPOSE:  To invoke the functions to perform the tasks described in
#           the prologue.
# INPUT:    None.
# OUTPUT:   A testcase run log with the results of the execution of this
#           test.
#---------------------------------------------------------------------#
do_setup
do_test 
end_testcase
