1#! /bin/sh
2#
3#   Copyright (c) International Business Machines  Corp., 2001
4#
5#   This program is free software;  you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation; either version 2 of the License, or
8#   (at your option) any later version.
9#
10#   This program is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY;  without even the implie; warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13#   the GNU General Public License for more details.
14#
15#   You should have received a copy of the GNU General Public License
16#   along with this program;  if not, write to the Free Software
17#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18#
19#  FILE   : nfsstat
20#
21#  PURPOSE: Tests the 'nfsstat' command.  This test runs locally, so no
22#           actual network connection is needed.
23#
24#  SETUP: The nfs server/client daemons must be active.
25#
26#
27#  HISTORY:
28#    07/30/01 Robbie Williamson (robbiew@us.ibm.com)
29#      -Written
30#
31#**********************************************************************
32
33#Uncomment line below for debug output.
34#trace_logic=${trace_logic:-"set -x"}
35
36$trace_logic
37
38#-----------------------------------------------------------------------
39# Initialize local variables
40#-----------------------------------------------------------------------
41
42PID=$$
43
44TC=nfsstat01
45TCbin=${TCbin:=`pwd`}
46EXPORTDIR=${EXPORTDIR:=/tmp/$TC$PID}
47HOST=`hostname`
48CLEANUP=${CLEANUP:="ON"}
49VERSION=${VERSION:=3}
50TESTDIR=${TESTDIR:=/tmp/$TC$PID.testdir}
51NFS_TYPE=${NFS_TYPE:=nfs}
52export TCID=$TC
53export TST_TOTAL=1
54export TST_COUNT=1
55
56if [ "x$NFS_TYPE" != "xnfs4" ]; then
57    OPTS=${OPTS:="-o vers=$VERSION "}
58fi
59
60#---------------------------------------------------------------------#
61# FUNCTION: do_setup
62# PURPOSE:  To create the necessary files to carry out the test
63# INPUT:    None.
64# OUTPUT:   None.
65#---------------------------------------------------------------------#
66do_setup()
67{
68$trace_logic
69
70   echo "do_setup $TC"
71
72   echo "This test runs LOCALLY."
73
74   IAM=${IAM:=`whoami`}
75   [ $IAM = "root" ] || end_testcase "Must be root user"
76
77   #Check for mountd.
78   rpcinfo -p | grep mountd
79   [ $? -eq 0 ] || end_testcase "Mountd is NOT running"
80
81   #Check for nfsd.
82   rpcinfo -p | grep nfs
83   [ $? -eq 0 ] || end_testcase "NFS server daemon (nfsd) is NOT running"
84
85   #Create export directory
86   mkdir -p $EXPORTDIR
87   [ $? -eq 0 ] || end_testcase "Could not create $EXPORTDIR"
88
89   #Export the data directory on HOST
90   if [ "x$NFS_TYPE" = "xnfs4" ]; then
91        mkdir -p /export$EXPORTDIR
92        [ $? = 0 ] || end_testcase "Could not create /export$EXPORTDIR locally"
93        mount --bind $EXPORTDIR /export$EXPORTDIR
94        [ $? = 0 ] || end_testcase "Could not bind $EXPORTDIR to /export"
95        /usr/sbin/exportfs -o no_root_squash,rw,nohide,insecure,no_subtree_check $HOST:$EXPORTDIR > /dev/null
96        [ $? = 0 ] || end_testcase "Could not export $EXPORTDIR on $HOST"
97    else
98       /usr/sbin/exportfs -i -o rw,no_root_squash $HOST:/$EXPORTDIR > /dev/null
99       [ $? -eq 0 ] || end_testcase "Could not export $EXPORTDIR on $HOST"
100   fi
101
102   #Verify export
103   showmount -e $HOST | grep $EXPORTDIR > /dev/null
104   [ $? -eq 0 ] || end_testcase "$EXPORTDIR not exported"
105
106   #Create mount point
107   mkdir -p $TESTDIR
108   [ $? -eq 0 ] || end_testcase "Could not create $TESTDIR"
109
110   #Mount from HOST.
111   mount -t $NFS_TYPE $OPTS $HOST:$EXPORTDIR $TESTDIR
112   [ $? -eq 0 ] || end_testcase "Could not mount from $HOST"
113
114}
115
116
117#---------------------------------------------------------------------#
118# FUNCTION: do_test
119# PURPOSE:  Performs simple copies and removes to verify statistic
120#           tracking using the 'nfsstat' command and /proc/net/rpc
121#---------------------------------------------------------------------#
122do_test()
123{
124$trace_logic
125  echo "do_test $TC"
126
127  echo "Checking RPC calls for server and client"
128
129  # Server
130  SERVER_CALLS=`cat /proc/net/rpc/nfsd | grep rpc | awk '{print $2}'`
131  nfsstat -sr | grep $SERVER_CALLS
132  [ $? -eq 0 ] || end_testcase "Check of server RPC calls failed"
133
134  # Client
135  CLIENT_CALLS=`cat /proc/net/rpc/nfs | grep rpc | awk '{print $2}'`
136  nfsstat -cr | grep $CLIENT_CALLS
137  [ $? -eq 0 ] || end_testcase "Check of client RPC calls failed"
138
139
140  echo "Checking for tracking of RPC calls for server and client"
141  cat /proc/cpuinfo > $TESTDIR/nfsstat01.tmp
142  NEW_SERVER_CALLS=`cat /proc/net/rpc/nfsd | grep rpc | awk '{print $2}'`
143  [ $NEW_SERVER_CALLS -gt $SERVER_CALLS ] || end_testcase "Server RPC calls not increased"
144  NEW_CLIENT_CALLS=`cat /proc/net/rpc/nfs | grep rpc | awk '{print $2}'`
145  [ $NEW_CLIENT_CALLS -gt $CLIENT_CALLS ] || end_testcase "Client RPC calls not increased"
146
147  nfsstat -sr | grep $NEW_SERVER_CALLS
148  [ $? -eq 0 ] || end_testcase "nfsstat not reporting change in server RPC calls"
149  nfsstat -cr | grep $NEW_CLIENT_CALLS
150  [ $? -eq 0 ] || end_testcase "nfsstat not reporting change in client RPC calls"
151
152
153  echo "Checking NFS calls for server and client"
154
155  # Server
156  if [ "$VERSION" = "2" ]; then
157   SERVER_CALLS=`cat /proc/net/rpc/nfsd | grep proc2 | awk '{print $13}'`
158  else
159   SERVER_CALLS=`cat /proc/net/rpc/nfsd | grep proc3 | awk '{print $15}'`
160  fi
161  nfsstat -sn | grep $SERVER_CALLS
162  [ $? -eq 0 ] || end_testcase "Check of server NFS calls failed"
163
164  # Client
165  if [ "$VERSION" = "2" ]; then
166    CLIENT_CALLS=`cat /proc/net/rpc/nfs | grep proc2 | awk '{print $13}'`
167  else
168    CLIENT_CALLS=`cat /proc/net/rpc/nfs | grep proc3 | awk '{print $15}'`
169  fi
170  nfsstat -cn | grep $CLIENT_CALLS
171  [ $? -eq 0 ] || end_testcase "Check of client NFS calls failed"
172
173
174  echo "Checking for tracking of NFS calls for server and client"
175
176  rm -f $TESTDIR/nfsstat01.tmp
177  if [ "$VERSION" = "2" ]; then
178   NEW_SERVER_CALLS=`cat /proc/net/rpc/nfsd | grep proc2 | awk '{print $13}'`
179  else
180   NEW_SERVER_CALLS=`cat /proc/net/rpc/nfsd | grep proc3 | awk '{print $15}'`
181  fi
182  [ $NEW_SERVER_CALLS -gt $SERVER_CALLS ] || end_testcase "Server NFS calls not increased"
183
184  if [ "$VERSION" = "2" ]; then
185    NEW_CLIENT_CALLS=`cat /proc/net/rpc/nfs | grep proc2 | awk '{print $13}'`
186  else
187    NEW_CLIENT_CALLS=`cat /proc/net/rpc/nfs | grep proc3 | awk '{print $15}'`
188  fi
189  [ $NEW_CLIENT_CALLS -gt $CLIENT_CALLS ] || end_testcase "Client NFS calls not increased"
190
191  nfsstat -sn | grep $NEW_SERVER_CALLS
192  [ $? -eq 0 ] || end_testcase "nfsstat not reporting change in server NFS calls"
193  nfsstat -cn | grep $NEW_CLIENT_CALLS
194  [ $? -eq 0 ] || end_testcase "nfsstat not reporting change in client NFS calls"
195
196
197}
198
199#---------------------------------------------------------------------#
200# FUNCTION: do_cleanup
201# PURPOSE:  To delete all the files created to run this test.
202# INPUT:    None.
203# OUTPUT:   None.
204#---------------------------------------------------------------------#
205do_cleanup()
206{
207$trace_logic
208   echo "do_cleanup $TC "
209
210   echo "Unmounting TESTDIR"
211   umount $TESTDIR
212   sleep 3
213   rm -rf $TESTDIR
214   sleep 3
215
216   echo "Unmounting EXPORTDIR"
217   /usr/sbin/exportfs -u $HOST:$EXPORTDIR
218
219   if [ "x$NFS_TYPE" = "xnfs4" ]; then
220       umount $EXPORTDIR
221       sleep 3
222       rm -rf /export$EXPORTDIR
223   fi
224
225   rm -rf $EXPORTDIR
226
227}
228
229#=============================================================================
230# FUNCTION NAME:        end_testcase
231#
232# FUNCTION DESCRIPTION: Clean up
233#
234# PARAMETERS:           None.
235#
236# RETURNS:              None.
237#=============================================================================
238end_testcase()
239{
240$trace_logic
241    if [ "$CLEANUP" = "ON" ]; then
242       do_cleanup
243    fi
244
245    [ $# = 0 ] && { tst_resm TPASS "Test Successful"; exit 0; }
246    tst_resm TFAIL "Test Failed: $@"
247    exit 1
248}
249
250#=============================================================================
251#---------------------------------------------------------------------#
252# FUNCTION: MAIN
253# PURPOSE:  To invoke the functions to perform the tasks described in
254#           the prologue.
255# INPUT:    None.
256# OUTPUT:   A testcase run log with the results of the execution of this
257#           test.
258#---------------------------------------------------------------------#
259do_setup
260do_test
261end_testcase
262