initial checkin
[grml-etc.git] / etc / skel / .wmi / szs2.1 / bandwidth.py
1 #!/usr/bin/python
2 """
3 szs/bandwidth - a bandwidth module for SZS an statusbar script for WMI
4
5
6 CHANGELOG:
7
8    v0.5    2004-12-14
9                 * some cleanups
10                 * removed re dependency for performance reasons
11
12    v0.4    2004-11-29
13                 * replaced script interval with real interval
14                 * added empirical max down/up rate
15                 * fixed: crash if interface is down
16
17    v0.3    2004-11-26
18                 * added configurabel labels
19                 * fixed get up packets instead of bytes bug
20                 * some cleanups
21
22    v0.2    2004-11-24
23                 * fixed bug with /proc/net/dev parsing
24         * adapted to the szs module interface
25                 * added szs.cfg support
26                 * use now correct interval from szs config
27
28    v0.1    2004-10-20  
29         * initial Release
30
31
32 TODO:
33
34
35
36 COPYRIGHT:
37
38 Copyright 2004  Christoph Wegscheider <cw@wegi.net>
39
40
41 LICENSE:
42
43 This program is free software; you can redistribute it and/or modify
44 it under the terms of the GNU General Public License as published by
45 the Free Software Foundation; either version 2 of the License, or
46 (at your option) any later version.
47
48 This program is distributed in the hope that it will be useful,
49 but WITHOUT ANY WARRANTY; without even the implied warranty of
50 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
51 GNU General Public License for more details.
52
53 You should have received a copy of the GNU General Public License
54 along with this program; if not, write to the Free Software
55 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
56 """
57
58 import os
59 import time
60 import szstoolbox
61
62
63
64 count = 0
65
66
67
68 class Interface:
69                 
70         changetime = 0
71
72         def __init__(self, label, downmax=1, upmax=1, down=0, up=0):
73                 self.label = label
74                 self.downmax = downmax * 1024 
75                 self.upmax = upmax * 1024
76                 self.down = down
77                 self.up = up
78
79         def get_down_speed(self, down):
80                 old = self.down
81                 self.down = down
82                 downrate = (down - old) / interval
83                 if downrate > self.downmax and count > 1: 
84                         self.downmax = downrate
85                         msg.debug(self.label + ' newmax down [kB]: ' + str(self.downmax/1024), 3)
86                 return 100 * downrate /  self.downmax
87         
88         def get_up_speed(self, up):
89                 old = self.up
90                 self.up = up
91                 uprate = (up - old) / interval
92                 if uprate > self.upmax and count > 1: 
93                         self.upmax = uprate
94                         msg.debug(self.label + ' newmax up [kB]: ' + str(self.upmax/1024), 3)
95                 return 100 * uprate /  self.upmax
96
97
98
99 def get_data():
100         global interval
101
102         #get interval
103         changetime = time.time()
104         interval = changetime - Interface.changetime
105         Interface.changetime = changetime
106         
107         # get data
108         fd = open('/proc/net/dev')
109         data = {}
110         for it in fd.read().split('\n')[2:-1]:
111                 it = str(it[:6] + ' ' + it[7:]).split()
112                 if (len(it) > 2) and (it[0] in ifs): 
113                         data[it[0]] = it[1], it[9]
114         fd.close()
115         for it in ifs:
116                 if it not in data.keys():
117                         data[it] = 0, 0
118                         print it + ' is down'
119         return data
120         
121
122
123 def main():
124         global count
125         data = get_data()
126         returnli = ['']
127         for it in ifs:
128                 returnli.append(str(int(ifsdata[it].get_down_speed(int(data[it][0])))) + '%')
129                 returnli.append(str(int(ifsdata[it].get_up_speed(int(data[it][1])))) + '%' + ifsdata[it].label)
130                 
131         count += 1
132         return returnli
133
134
135
136 # load configuration
137 msg = szstoolbox.MSG()
138 cfg = szstoolbox.CFG('bandwidth')
139 ifs = cfg.read('ifs').split(',')
140 ifsdata = {}
141 for it in ifs:
142         it = it.split(':')
143         if len(it) == 4:
144                 ifsdata[it[0]] = Interface(it[1], int(it[2]), int(it[3]))       
145         else:
146                 ifsdata[it[0]] = Interface(it[1])       
147 ifs = [it.split(':')[0] for it in ifs]
148
149
150
151 if __name__ == '__main__':
152         import time
153
154         while True:
155                 print main()
156                 time.sleep(szstoolbox.interval)