-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetDiskUsage.py
More file actions
61 lines (53 loc) · 1.84 KB
/
Copy pathgetDiskUsage.py
File metadata and controls
61 lines (53 loc) · 1.84 KB
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
import os
from collections import namedtuple
disk_ntuple = namedtuple('partition', 'device mountpoint fstype')
usage_ntuple = namedtuple('usage', 'total used free percent')
stat_ntuple = namedtuple('w', 'x y z')
def disk_partitions(all=False):
"""Return all mountd partitions as a nameduple.
If all == False return phyisical partitions only.
"""
phydevs = []
f = open("/proc/filesystems", "r")
for line in f:
if not line.startswith("nodev"):
phydevs.append(line.strip())
retlist = []
f = open('/etc/mtab', "r")
for line in f:
if not all and line.startswith('none'):
continue
fields = line.split()
device = fields[0]
mountpoint = fields[1]
fstype = fields[2]
if not all and fstype not in phydevs:
continue
if device == 'none':
device = ''
ntuple = disk_ntuple(device, mountpoint, fstype)
retlist.append(ntuple)
return retlist
def disk_usage(path):
"""Return disk usage associated with path."""
st = os.statvfs(path)
free = (st.f_bavail * st.f_frsize) / 1073741824
total = (st.f_blocks * st.f_frsize) / 1073741824
used = ((st.f_blocks - st.f_bfree) * st.f_frsize) / 1073741824
try:
percent = ret = (float(used) / total) * 100
except ZeroDivisionError:
percent = 0
# NB: the percentage is -5% than what shown by df due to
# reserved blocks that we are currently not considering:
# http://goo.gl/sWGbH
# return usage_ntuple(total, used, free, round(percent, 1))
return stat_ntuple(path,total,(round(percent,0)))
if __name__ == '__main__':
for part in disk_partitions():
# print part
pt1 = disk_usage(part.mountpoint)
# print " %s\n" % str(disk_usage(part.mountpoint))
print pt1.x
print pt1.y
print pt1.z