aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/network.py
blob: fd74fd19c5ebf89ca32ba085495d98347f2839ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- python -*-
'''
xpybar – xmobar replacement written in python
Copyright © 2014, 2015, 2016, 2017, 2018, 2019  Mattias Andrée (maandree@kth.se)

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]