Skip to content

Instantly share code, notes, and snippets.

@ashleykleynhans
Last active August 13, 2020 17:00
Show Gist options
  • Save ashleykleynhans/8f3c9e6ed31dde017d989f60bc81d246 to your computer and use it in GitHub Desktop.
Save ashleykleynhans/8f3c9e6ed31dde017d989f60bc81d246 to your computer and use it in GitHub Desktop.
Python script to monitor stale/unmounted NFS mounts and automatically remount them
#!/bin/python
import signal
import os
import subprocess
from scandir import scandir
# Create a new Alarm Exception
class Alarm(Exception):
pass
def alarm_handler(signum, frame):
raise Alarm
# Timeout for signalling the alarm handler
TIMEOUT = 120
stream = os.popen("cat /etc/fstab | grep nfs |awk '{print $2}'")
nfsMounts = stream.read()
nfsMounts = nfsMounts.splitlines()
for pathToNFSMount in nfsMounts:
signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(TIMEOUT)
try:
try:
proc = subprocess.call('stat '+pathToNFSMount, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
if proc != 0:
raise Exception('Stale')
contents = scandir(pathToNFSMount)
file = contents.next()
signal.alarm(0)
except StopIteration:
if not os.path.ismount(pathToNFSMount):
raise Exception('Not mounted')
except Alarm:
raise Exception('Timeout')
except Exception as e:
signal.alarm(0)
print('Remounting: {}'.format(pathToNFSMount))
os.system('umount -l {}; mount {}'.format(pathToNFSMount, pathToNFSMount))
print('{} : OK'.format(pathToNFSMount))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment