3 szs/bandwidth - a bandwidth module for SZS an statusbar script for WMI
10 * removed re dependency for performance reasons
13 * replaced script interval with real interval
14 * added empirical max down/up rate
15 * fixed: crash if interface is down
18 * added configurabel labels
19 * fixed get up packets instead of bytes bug
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
38 Copyright 2004 Christoph Wegscheider <cw@wegi.net>
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.
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.
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
72 def __init__(self, label, downmax=1, upmax=1, down=0, up=0):
74 self.downmax = downmax * 1024
75 self.upmax = upmax * 1024
79 def get_down_speed(self, 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
88 def get_up_speed(self, up):
91 uprate = (up - old) / interval
92 if uprate > self.upmax and count > 1:
94 msg.debug(self.label + ' newmax up [kB]: ' + str(self.upmax/1024), 3)
95 return 100 * uprate / self.upmax
103 changetime = time.time()
104 interval = changetime - Interface.changetime
105 Interface.changetime = changetime
108 fd = open('/proc/net/dev')
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]
116 if it not in data.keys():
118 print it + ' is down'
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)
137 msg = szstoolbox.MSG()
138 cfg = szstoolbox.CFG('bandwidth')
139 ifs = cfg.read('ifs').split(',')
144 ifsdata[it[0]] = Interface(it[1], int(it[2]), int(it[3]))
146 ifsdata[it[0]] = Interface(it[1])
147 ifs = [it.split(':')[0] for it in ifs]
151 if __name__ == '__main__':
156 time.sleep(szstoolbox.interval)