update ov debian/overrides
[grml-etc.git] / etc / skel / .wmi / szs2.1 / load.py
1 #!/usr/bin/python
2 """
3 szs/load - a load and cpu usage module for SZS a statusbar script for WMI
4
5
6
7 CHANGELOG:
8
9    v0.4    2004-12-14
10                 * some cleanups
11                 * added support for 2.4 Kernels
12
13    v0.3    2004-11-26  
14                 * implemented configurable label
15                 * some cleanups
16    
17    v0.2    2004-11-26  
18                 * implemented cpu/io utilisation
19
20    v0.1    2004-11-24  
21         * initial Release
22                 * implemented system load
23
24
25 TODO:
26
27
28
29 COPYRIGHT:
30
31 Copyright 2004  Christoph Wegscheider <cw@wegi.net>
32
33
34 LICENSE:
35
36 This program is free software; you can redistribute it and/or modify
37 it under the terms of the GNU General Public License as published by
38 the Free Software Foundation; either version 2 of the License, or
39 (at your option) any later version.
40
41 This program is distributed in the hope that it will be useful,
42 but WITHOUT ANY WARRANTY; without even the implied warranty of
43 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
44 GNU General Public License for more details.
45
46 You should have received a copy of the GNU General Public License
47 along with this program; if not, write to the Free Software
48 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
49 """
50
51 import os
52 import szstoolbox
53
54
55 # init
56 oldcpusum = oldcpuutil = oldioutil = 0
57
58 # load configuration
59 cfg = szstoolbox.CFG('load')
60 loadlabel = cfg.read('loadlabel')
61 cpulabel = cfg.read('cpulabel')
62 showcpu = int(cfg.read('showcpu'))
63 maxload = int(cfg.read('maxload'))
64
65
66
67 def main():
68         global oldcpusum
69         global oldcpuutil
70         global oldioutil
71         bars = [] 
72
73         # cpu
74         if showcpu:
75                 file = open('/proc/stat')
76                 values = [int(it) for it in file.readline().split()[1:]]
77                 file.close()
78                 newcpusum = newcpuutil = newioutil = 0
79                 for it in values: newcpusum += it
80                 for it in values[:3]: newcpuutil += it
81                 if szstoolbox.kernel_version[:3] == '2.6':
82                         for it in values[4:]: newioutil += it
83                 cpusum = newcpusum - oldcpusum
84                 bars.append(str((newcpuutil - oldcpuutil) * 100 / cpusum) + '%')
85                 if szstoolbox.kernel_version[:3] == '2.6':
86                         bars.append(str((newioutil - oldioutil) * 100 / cpusum) + '%' + cpulabel)
87                 oldcpusum = newcpusum
88                 oldcpuutil = newcpuutil
89                 oldioutil = newioutil
90         
91         # load
92         if maxload > 0:
93                 load = os.getloadavg()
94                 for it in load:
95                         bars.append(str(int(it / maxload * 100)) + '%')
96                 bars[-1] += loadlabel 
97
98         returnli =  ['']
99         returnli.extend(bars)
100         return returnli
101
102
103
104 if __name__ == '__main__':
105         import time
106
107         while True:
108                 print main()
109                 time.sleep(szstoolbox.interval)
110