code-dumps/py/fsro.py

59 lines
1.6 KiB
Python
Raw Normal View History

2024-10-24 23:09:21 +08:00
#!/usr/bin/env python
import sys, os, subprocess, socket, logging, logging.handlers, time
# script variables
checkPath = "/proc"
# 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'):
if time.time() - os.path.getmtime('/dev/shm/fsro-py.lock') > 3600:
os.remove('/dev/shm/fsro-py.lock');
printExit('OK Please health check ' + checkPath + ' and 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: ' + repr(e));
try:
with open(testFile, 'r') as fr:
fr.read();
fr.close();
except Exception as e:
printExit('MAJOR Mount ' + checkPath + ' not readable: ' + repr(e));
os.remove(testFile);
os.remove('/dev/shm/fsro-py.lock');
printExit('OK Mount ' + checkPath + ' healthy');