From aa09c1088ada289f662c6849674ec4f8cbd6956c Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 3 Mar 2014 17:24:15 +0100 Subject: add network MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/plugins/network.py | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/plugins/network.py (limited to 'src') 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 . +''' + + +class Network: + ''' + Retrieve network statistics + + @variable devices:dict> 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] + -- cgit v1.2.3-70-g09d2