code-dumps/py/fsro.py

59 lines
1.6 KiB
Python
Raw Normal View History

2019-03-01 21:51:02 +08:00
#!/usr/bin/env python
2019-03-02 16:42:17 +08:00
import sys, os, subprocess, socket, logging, logging.handlers, time
2019-03-01 21:51:02 +08:00
# script variables
2019-03-02 16:42:17 +08:00
checkPath = "/proc"
2019-03-01 21:51:02 +08:00
# 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'):
2019-03-02 16:42:17 +08:00
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')
2019-03-01 21:51:02 +08:00
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:
2019-03-02 16:42:17 +08:00
printExit('MAJOR Mount ' + checkPath + ' not writable: ' + repr(e));
2019-03-01 21:51:02 +08:00
try:
with open(testFile, 'r') as fr:
fr.read();
fr.close();
except Exception as e:
2019-03-02 16:42:17 +08:00
printExit('MAJOR Mount ' + checkPath + ' not readable: ' + repr(e));
2019-03-01 21:51:02 +08:00
os.remove(testFile);
os.remove('/dev/shm/fsro-py.lock');
printExit('OK Mount ' + checkPath + ' healthy');