7 |
# Copyright: (c) 2008 Andrew Pollock |
# Copyright: (c) 2008 Andrew Pollock |
8 |
# |
# |
9 |
|
|
10 |
class DiskStat: |
from UserDict import UserDict |
11 |
|
|
12 |
|
class Error(Exception): |
13 |
|
pass |
14 |
|
|
15 |
|
class DiskStat(UserDict): |
16 |
|
|
17 |
def __init__(self, nrread, nrmergedread, nrsectorsread, msread, nrwrite, nrmergedwrite, nrsectorswrite, mswrite, nrio, msio, weightedmsio): |
def __init__(self, nrread, nrmergedread, nrsectorsread, msread, nrwrite, nrmergedwrite, nrsectorswrite, mswrite, nrio, msio, weightedmsio): |
18 |
self.nrread = nrread |
UserDict.__init__(self) |
19 |
self.nrmergedread = nrmergedread |
self["nrread"] = int(nrread) |
20 |
self.nrsectorsread = nrsectorsread |
self["nrmergedread"] = int(nrmergedread) |
21 |
self.msread = msread |
self["nrsectorsread"] = int(nrsectorsread) |
22 |
self.nrwrite = nrwrite |
self["msread"] = int(msread) |
23 |
self.nrmergedwrite = nrmergedwrite |
self["nrwrite"] = int(nrwrite) |
24 |
self.nrsectorswrite = nrsectorswrite |
self["nrmergedwrite"] = int(nrmergedwrite) |
25 |
self.mswrite = mswrite |
self["nrsectorswrite"] = int(nrsectorswrite) |
26 |
self.nrio = nrio |
self["mswrite"] = int(mswrite) |
27 |
self.msio = msio |
self["nrio"] = int(nrio) |
28 |
self.weightedmsio = weightedmsio |
self["msio"] = int(msio) |
29 |
|
self["weightedmsio"] = int(weightedmsio) |
30 |
|
|
31 |
|
|
32 |
class DiskStats: |
class DiskStats: |
34 |
def __init__(self, disk): |
def __init__(self, disk): |
35 |
self.disk = disk.lstrip("/dev/") |
self.disk = disk.lstrip("/dev/") |
36 |
|
|
37 |
def diskstat(self): |
def diskstat(self, key=None): |
38 |
proc_diskstats = open("/proc/diskstats") |
proc_diskstats = open("/proc/diskstats") |
39 |
stat = None |
stat = None |
40 |
while True: |
while True: |
49 |
proc_diskstats.close() |
proc_diskstats.close() |
50 |
if stat: |
if stat: |
51 |
ds = DiskStat(stat[0], stat[1], stat[2], stat[3], stat[4], stat[5], stat[6], stat[7], stat[8], stat[9], stat[10]) |
ds = DiskStat(stat[0], stat[1], stat[2], stat[3], stat[4], stat[5], stat[6], stat[7], stat[8], stat[9], stat[10]) |
52 |
return ds |
if not key: |
53 |
|
return ds |
54 |
|
else: |
55 |
|
return ds[key] |
56 |
else: |
else: |
57 |
# Need to raise an exception here |
# Need to raise an exception here |
58 |
pass |
raise Error("No such disk: %s" % (self.disk)) |
59 |
|
|
60 |
if __name__ == "__main__": |
if __name__ == "__main__": |
61 |
ds = DiskStats("/dev/hda") |
ds = DiskStats("/dev/sda") |
62 |
print ds.diskstat() |
print ds.diskstat("msio") |