17Nov/101

NFS Monitoring Scripts

I've tried few different NFS mount check, but most of them hang or freeze when the remote server is not responding. So I created a simple script to monitor and automatically mount NFS which kill it self .

I put this script on crontab which then run every 15 minutes :
cron entries :

*/15 * * * * /root/bin/check_nfs.sh

check_nfs.sh :

#!/bin/bash
# Script to check if NFS is mounted properly
# Change Log :

# Script to check and monitor NFS mounted file system

# When NFS is not working, it may hang the process that try to access it.
# As a work around, we start a process in the background that will kill nfs_check script after 30 seconds
# However, if check command works then kill the process that was about to kill me ( killmyparrent.sh )

REMOTE_SERVER=remotenfsserver.example.com
mnt_partition=/remote/nfs/folder/

# Exit if another instance is running
if [ `ps auxwww | grep check_nfs.sh | wc -l` -gt 3 ]
then
echo `pgrep check_nfs.sh | wc -l`
exit
fi

if mount | grep -q $mnt_partition
then :

/root/bin/killmyparent.sh $ "Email Subject - eg : NFS mount failed on xxx server" "Some issue with $mnt_parition. Please check it will you !" &
killerpid=`pgrep killmyparent.sh`

# NFS file system appear to be mounted - lets check if we can access it ..
if df | grep -q $mnt_partition
then :
# df command works .. kill "killmyparent.sh" script before it kill us
kill $killerpid
exit 1;
fi
else
# Wait until server is contactible and then mount the partition
until [ `ping -q -c3 -w5 $REMOTE_SERVER > /dev/null 2>&1; echo $?` -eq 0 ]
do
sleep 10;
done
mount $mnt_partition
exit $?
fi

killmyparent.sh

#!/bin/bash
if [ -z $1 ]
then
echo "Usage $0 "
exit
fi
sleep 20
kill -9 $1
echo $3 | mail -s $2

Posted by Rianto Wahyudi

Comments (1) Trackbacks (0)
  1. Magic thanks, just what we needed as a starting point


Leave a comment


six × 2 =

No trackbacks yet.