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

Annotation of /usbspindownd/diskstats.py

Parent Directory Parent Directory | Revision Log Revision Log


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

1 apollock 29 #!/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 apollock 30 from UserDict import UserDict
11 apollock 29
12 apollock 30 class Error(Exception):
13     pass
14    
15     class DiskStat(UserDict):
16    
17 apollock 29 def __init__(self, nrread, nrmergedread, nrsectorsread, msread, nrwrite, nrmergedwrite, nrsectorswrite, mswrite, nrio, msio, weightedmsio):
18 apollock 30 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 apollock 29
31    
32     class DiskStats:
33    
34     def __init__(self, disk):
35     self.disk = disk.lstrip("/dev/")
36    
37 apollock 30 def diskstat(self, key=None):
38 apollock 29 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 apollock 30 if not key:
53     return ds
54     else:
55     return ds[key]
56 apollock 29 else:
57     # Need to raise an exception here
58 apollock 30 raise Error("No such disk: %s" % (self.disk))
59 apollock 29
60     if __name__ == "__main__":
61 apollock 30 ds = DiskStats("/dev/sda")
62     print ds.diskstat("msio")

  ViewVC Help
Powered by ViewVC 1.1.22