forked from jjneely/whisper-fsck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhisper-fsck.py
73 lines (60 loc) · 2.01 KB
/
whisper-fsck.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import sys
import os.path
import time
import optparse
sys.path.append("/opt/graphite/lib")
import whisper
def spinning_cursor():
while True:
for cursor in '|/-\\':
yield cursor
def fsck(path, fix=False):
try:
info = whisper.info(path)
except whisper.CorruptWhisperFile as e:
print "Found: %s" % str(e)
badname = path + ".corrupt.%s" % time.strftime("%Y%m%d-%H%M%S")
if fix:
print "Moving %s => %s" % (path, badname)
os.rename(path, badname)
return 1
except Exception as e:
print "ERROR (unhandled): %s" % str(e)
return 1
return 0
def main():
description = """Search for .WSP files under the given directory, or /opt/graphite/storage/whisper by default, for any Whisper database files that cannot be opened and read. Optionally move those files out of the way."""
usage = "usage: %prog [--fix|-f] [directory]"
parse = optparse.OptionParser(description=description, usage=usage)
parse.add_option("-f", "--fix",
action="store_true",
dest="fix",
default=False,
help="Move corrupt files out of the way.")
opts, args = parse.parse_args()
print "Graphite Whisper FSCK"
print "Written by Jack Neely <[email protected]>"
print
if len(args) > 1:
storage = args[1]
else:
storage = "/opt/graphite/storage/whisper"
spinner = spinning_cursor()
c = 0
errors = 0
sys.stdout.write("Scanning %s..." % storage)
for dirpath, dirnames, filenames in os.walk(storage):
sys.stdout.write(spinner.next())
sys.stdout.flush()
for i in filenames:
if i.endswith(".wsp"):
c += 1
errors += fsck(os.path.join(dirpath, i), opts.fix)
sys.stdout.write('\b')
sys.stdout.write('\n')
print "%s whisper files examined." % c
print "%s errors." % errors
print "Done!"
if __name__ == "__main__":
main()