/[svn.andrew.net.au]/usbspindownd/diskstats.py
ViewVC logotype

Contents of /usbspindownd/diskstats.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 30 - (show annotations)
Thu Jan 31 02:21:39 2008 UTC (12 years, 4 months ago) by apollock
File MIME type: text/x-python
File size: 1542 byte(s)
Preliminary working code

1 #!/usr/bin/python
2
3 #
4 # Python module for parsing /proc/diskstats on Linux
5 #
6 # Author: Andrew Pollock <me@andrew.net.au>
7 # Copyright: (c) 2008 Andrew Pollock
8 #
9
10 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):
18 UserDict.__init__(self)
19 self["nrread"] = int(nrread)
20 self["nrmergedread"] = int(nrmergedread)
21 self["nrsectorsread"] = int(nrsectorsread)
22 self["msread"] = int(msread)
23 self["nrwrite"] = int(nrwrite)
24 self["nrmergedwrite"] = int(nrmergedwrite)
25 self["nrsectorswrite"] = int(nrsectorswrite)
26 self["mswrite"] = int(mswrite)
27 self["nrio"] = int(nrio)
28 self["msio"] = int(msio)
29 self["weightedmsio"] = int(weightedmsio)
30
31
32 class DiskStats:
33
34 def __init__(self, disk):
35 self.disk = disk.lstrip("/dev/")
36
37 def diskstat(self, key=None):
38 proc_diskstats = open("/proc/diskstats")
39 stat = None
40 while True:
41 line = proc_diskstats.readline()
42 if line == "":
43 break
44 else:
45 line = line.split()
46
47 if line[2] == self.disk:
48 stat = line[3:]
49 proc_diskstats.close()
50 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])
52 if not key:
53 return ds
54 else:
55 return ds[key]
56 else:
57 # Need to raise an exception here
58 raise Error("No such disk: %s" % (self.disk))
59
60 if __name__ == "__main__":
61 ds = DiskStats("/dev/sda")
62 print ds.diskstat("msio")

  ViewVC Help
Powered by ViewVC 1.1.22