diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-03-03 17:24:15 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-03-03 17:24:15 +0100 |
commit | aa09c1088ada289f662c6849674ec4f8cbd6956c (patch) | |
tree | e5f0a4ce59668337f259a619cc9cbad80a4e8ca3 | |
parent | misc (diff) | |
download | xpybar-aa09c1088ada289f662c6849674ec4f8cbd6956c.tar.gz xpybar-aa09c1088ada289f662c6849674ec4f8cbd6956c.tar.bz2 xpybar-aa09c1088ada289f662c6849674ec4f8cbd6956c.tar.xz |
add network
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r-- | TODO | 1 | ||||
-rw-r--r-- | examples/plugin-test | 19 | ||||
-rw-r--r-- | src/plugins/network.py | 86 |
3 files changed, 102 insertions, 4 deletions
@@ -24,7 +24,6 @@ List of plugins to implement: Keyboard layout EWMH /proc/interrupts - /proc/net/dev /proc/net/sockstat /proc/net/sockstat6 /proc/net/wireless diff --git a/examples/plugin-test b/examples/plugin-test index 9cf449c..711ef11 100644 --- a/examples/plugin-test +++ b/examples/plugin-test @@ -17,9 +17,10 @@ from plugins.xdisplay import XDisplay from plugins.moc import MOC from plugins.cpu import CPU from plugins.clock import Clock +from plugins.network import Network -OUTPUT, HEIGHT, YPOS, TOP = 0, 24, 24, True +OUTPUT, HEIGHT, YPOS, TOP = 0, 3 * 12, 24, True clock = Clock(format = '%Y-(%m)%b-%d %T, %a w%V, %Z') @@ -179,9 +180,21 @@ def redraw(): cpu = colourise(cpu_usage(now_cpu_idle, now_cpu_total, last_cpu_idle, last_cpu_total)) cpu = 'Cpu: %s : %s' % (cpus, cpu) + gb = lambda x : x / 1024 ** 3 + net_ = Network('lo').devices + net_ = [(dev, gb(net_[dev]['rx_bytes']), gb(net_[dev]['tx_bytes'])) for dev in net_] + net_each = ['%s: %.2fGB|%.2fGB' % dev for dev in net_] + net_total = '%.2fGB|%.2fGB' % (sum([rx for _, rx, tx in net_]), sum([tx for _, rx, tx in net_])) + net = '%s : %s' % (' '.join(net_each), net_total) - text = '%s │ %s │ %s │ %s │ %s │ %s %s │ %s │ %s │ %s │ %s\n%s │ %s │ %s' - text %= (date, uptime, idle, loadavg, users, uname, xdisplay, mem, swp, shm, moc, discs, discstats, cpu) + + text = ['%s │ %s │ %s │ %s │ %s │ %s %s │ %s │ %s │ %s │ %s' + ,'%s │ %s │ %s' + ,'%s'] + text = '\n'.join(text) + text %= (date, uptime, idle, loadavg, users, uname, xdisplay, mem, swp, shm, moc, + discs, discstats, cpu, + net) bar.clear() bar.draw_coloured_text(0, 10, 0, 2, text) diff --git a/src/plugins/network.py b/src/plugins/network.py new file mode 100644 index 0000000..3406b38 --- /dev/null +++ b/src/plugins/network.py @@ -0,0 +1,86 @@ +# -*- python -*- +''' +xpybar – xmobar replacement written in python +Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see <http://www.gnu.org/licenses/>. +''' + + +class Network: + ''' + Retrieve network statistics + + @variable devices:dict<str, dict<str, int>> Map from device name, to data name, to data value + + Data names for receive: + + @key rx_bytes Bytes received + @key rx_packets Packets received + @key rx_errs Errors + @key rx_drop Dropped + @key rx_fifo FIFO + @key rx_frame Frame + @key rx_compressed Compressed + @key rx_multicast Multicast + + Data names for transmit: + + @key tx_bytes Bytes transmitted + @key tx_packets Packets transmitted + @key tx_errs Errors + @key tx_drop Dropped + @key tx_fifo FIFO + @key tx_colls Collisions + @key tx_carrier Carrier + @key tx_compressed Compressed + ''' + + + def __init__(self, *exclude): + ''' + Constructor + + @param exclude:*str Devices to exclude + ''' + stat = None + with open('/proc/net/dev', 'rb') as file: + stat = file.read() + stat = stat.decode('utf-8', 'replace') + stat = stat.replace('|', ' | ').replace(':', ' ') + stat = list(filter(lambda x : not x == '', stat.split('\n')[1:])) + stat = [list(filter(lambda x : not x == '', line.split(' '))) for line in stat] + stat[0] = stat[0][2:] + + exclude = set(exclude) + devices = {} + for line in stat[1:]: + if line[0] not in exclude: + devices[line[0]] = [int(x) for x in line[1:]] + + columns, prefix = [], 'rx_' + for column in stat[0]: + if column == '|': + prefix = 'tx_' + else: + columns.append(prefix + column) + + self.devices = {} + for dev in devices.keys(): + fields = {} + self.devices[dev] = fields + values = devices[dev] + for i in range(len(values)): + fields[columns[i]] = values[i] + |