new: fsro python script

This commit is contained in:
xpk 2019-03-01 21:51:02 +08:00
parent db6b4c5831
commit 86be101a70
Signed by: xpk
GPG Key ID: CD4FF6793F09AB86

57
py/fsro.py Executable file
View File

@ -0,0 +1,57 @@
#!/usr/bin/env python
import sys, os, subprocess, socket, logging, logging.handlers
# script variables
checkPath = "/tmp"
# some filesystems are only writable by root
if os.geteuid() != 0:
subprocess.call(['sudo', 'python', sys.argv[0]]);
sys.exit(0);
# log to syslog
log = logging.getLogger(__file__)
log.setLevel(logging.DEBUG)
handler = logging.handlers.SysLogHandler(address = '/dev/log');
formatter = logging.Formatter('%(name)s %(levelname)s: %(message)s');
handler.setFormatter(formatter);
log.addHandler(handler);
# print message and exit
def printExit(msg):
print (msg);
log.info(msg);
exit(0);
# program begins
if os.path.isfile('/dev/shm/fsro-py.lock'):
printExit('MINOR Please health check CIFS mounts and then delete /dev/shm/fsro-py.lock.')
else:
open('/dev/shm/fsro-py.lock', 'a').close();
mountExists = subprocess.call(["grep", "-q", checkPath, "/proc/mounts"]);
if mountExists != 0:
printExit('MAJOR Mount ' + checkPath + ' missing');
testFile = checkPath + "/." + socket.gethostname() + "-test.dat";
try:
with open(testFile, 'wb') as fw:
fw.write(os.urandom(10*1024*1024));
fw.close();
except Exception as e:
printExit('MAJOR Mount ' + checkPath + ' not writable: ' + e);
try:
with open(testFile, 'r') as fr:
fr.read();
fr.close();
except Exception as e:
printExit('MAJOR Mount ' + checkPath + ' not readable: ' + e);
os.remove(testFile);
os.remove('/dev/shm/fsro-py.lock');
printExit('OK Mount ' + checkPath + ' healthy');