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