-
Notifications
You must be signed in to change notification settings - Fork 0
/
frag_info.py
executable file
·57 lines (50 loc) · 1.89 KB
/
frag_info.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
#!/usr/bin/env python3
import sys, subprocess, os, re
if len(sys.argv) > 1:
wd = sys.argv[1]
if not os.path.exists(wd):
print ('Usage: frag_info.py [path_to_scan]\n'
'When path_to_scan is not specified then it scans from current directory')
quit()
else:
wd = os.getcwd()
print('Fragmentation within %s\n' % wd)
cmd = ("find \"%s\" -xdev -type f -exec filefrag '{}' + | " % wd +
r"sed -e 's/^\(.*\): \(.*\) extent.* found$/\2 \1/' | "
"sort -nr > /tmp/frag.log")
subprocess.call([cmd], shell=True)
total = totalf = fragmented = frags = 0
cnt = [0, 0, 0, 0]
with open('/tmp/frag.log', 'rt', newline='\n') as f:
for line in f:
totalf += 1
fr, fn = re.findall(r'(\d+) (.*)', line)[0]
fr = int(fr)
if fr > 0:
total += 1
frags += fr
if fr > 1:
fragmented += 1
if fr == 2:
cnt[0] += 1
elif fr <= 10:
cnt[1] += 1
elif fr <= 100:
cnt[2] += 1
else:
cnt[3] += 1
if fragmented:
cnt = [c / fragmented * 100 for c in cnt]
exfrags = frags - total
tot = 'Totally: %d files (%d not empty) stored within %d fragments\n' % (totalf, total, frags)
tot += ('Totally %d fragments in %d fragmented files\n' % (exfrags, fragmented) if fragmented
else '')
tot += ('Fragmentation factor by files {fragmented/total in %%}: %0.2f%%\n' %
(fragmented / totalf * 100))
tot += 'Fragmentation factor by fragments {(fragments - files)/ files}: %0.3f\n' % (exfrags / total)
tot += '\nFragments per files:\n'
tot += (' 2 fragments: %0.2f%%\n 3-10 fragments: %0.2f%%\n'
'11-100 fragments: %0.2f%%\n > 100 fragments: %0.2f%%'
% (cnt[0], cnt[1], cnt[2], cnt[3]) if fragmented else '')
tot += '\n\nSee detailed log in /tmp/frag.log'
print(tot)