aboutsummaryrefslogtreecommitdiffstats
path: root/xpybar/config/mynetwork.py
blob: 79cd271d3dbff23083d21cf3043a30deb816aab6 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# -*- python -*-
from plugins.network import Network
from plugins.ping    import Ping

from common import *

class MyNetwork(Entry):
    def __init__(self, *args, limits = None, ignore = None, pings = None, **kwargs):
        self.limits = { 'rx_bytes' : None # Download speed in bytes (not bits)
                      , 'tx_bytes' : None # Upload speed in bytes (not bits)
                      , 'rx_total' : None # Download cap in bytes
                      , 'tx_total' : None # Upload cap in bytes
                      }
        if limits is not None:
            self.limits = limits
        if pings is None:
            pings = ['gateway']
        elif isinstance(pings, str) or isinstance(pings, Ping):
            pings = [pings]
        pings = [(Ping(targets = Ping.get_nics(p)) if isinstance(p, str) else p).monitors for p in pings]
        self.pings = {}
        for ping in pings:
            for nic in ping.keys():
                if nic not in self.pings:
                    self.pings[nic] = ping[nic]
                else:
                    self.pings[nic] += ping[nic]
        self.ignore = ['lo'] if ignore is None else ignore
        self.net_time = time.monotonic()
        self.net_last = {}
        self.show_all = True
        self.show_name = True
        self.show_value = 0
        self.labels = ['bytes', 'total', 'packets', 'errs',
                       'drop', 'fifo', 'frame', 'colls',
                       'carrier', 'compressed', 'multicast']
        self.in_bytes = False # in bits if showing total
        Entry.__init__(self, *args, **kwargs)
    
    def action(self, col, button, x, y):
        if button == LEFT_BUTTON:
            self.show_all = not self.show_all
        elif button == MIDDLE_BUTTON:
            self.show_name = not self.show_name
        elif button == RIGHT_BUTTON:
            self.in_bytes = not self.in_bytes
        elif button == SCROLL_UP:
            n = self.show_value + 1
            if n >= len(self.labels):
                return
            self.show_value = n
        elif button == SCROLL_DOWN:
            n = self.show_value - 1
            if self.show_value < 0:
                return
            self.show_value = n
        else:
            return
        self.invalidate()
    
    def colourise(self, value, percent):
        colour = '39'
        if percent > 25:  colour = '32'
        if percent > 50:  colour = '33'
        if percent > 90:  colour = '31'
        return '\033[%sm%3.0f\033[0m' % (colour, value)
    
    def KBps(self, network, device, direction, net_tdiff):
        direction += '_bytes'
        value = network[device][direction]
        limit = None
        if direction in self.limits:
            limit = self.limits[direction]
        if limit is None:
            limit = 12500000 # 100 mbps
        if device in self.net_last:
            value -= self.net_last[device][direction]
        else:
            value = 0
        percent = value * 100 / (limit * net_tdiff)
        value /= 1024 * net_tdiff
        return self.colourise(value, percent)
    
    def kbps(self, network, device, direction, net_tdiff):
        direction += '_bytes'
        value = network[device][direction]
        limit = None
        if direction in self.limits:
            limit = self.limits[direction]
        if limit is None:
            limit = 12500000 # 100 mbps
        if device in self.net_last:
            value -= self.net_last[device][direction]
        else:
            value = 0
        percent = value * 100 / (limit * net_tdiff)
        value /= 128 * net_tdiff
        return self.colourise(value, percent)
    
    def total(self, network, device, direction, bits):
        direction += '_bytes'
        value = network[device][direction]
        limit = self.limits[direction] if direction in self.limits else None
        if limit is not None:
            percent = value * 100 / limit
            colour = '32'
            if percent >=  25:  colour = '39'
            if percent >=  50:  colour = '33'
            if percent >=  75:  colour = '31'
            if percent >=  95:  colour = '7;33'
            if percent >= 100:  colour = '7;31'
        else:
            colour = '39'
        if bits:
            value *= 8
        unit = 0
        if bits:
            units = ['', 'k', 'm', 'g', 't', 'p', 'e']
        else:
            units = ['', 'K', 'M', 'G', 'T', 'P', 'E']
        while (unit + 1 < len(units)) and (value >= 1024):
            value /= 1024
            unit += 1
        return '\033[%sm%.1f\033[0m%s' % (colour, value, units[unit])
    
    def ping(self, monitor):
        monitor.semaphore.acquire()
        try:
            latency = monitor.get_latency(True)[1]
            droptime = monitor.dropped_time(True)
            if droptime:
                return ' \033[31m%4is\033[00m' % droptime
            elif latency is None:
                return ' \033[31mdrop?\033[00m'
            colour = '31'
            if   latency <  5:  colour = '32'
            elif latency < 10:  colour = '39'
            elif latency < 20:  colour = '33'
            return ' \033[%sm%5.2f\033[00m' % (colour, latency)
        finally:
            monitor.semaphore.release()
    
    def function(self):
        net_now = time.monotonic()
        net_tdiff, self.net_time = net_now - self.net_time, net_now
        network = Network(*self.ignore).devices
        label = self.labels[self.show_value]
        show_total = label == 'total'
        show_bytes = label == 'bytes'
        xlabel = 'bytes' if show_total else label
        suffix = ''
        if show_total and self.in_bytes:
            f = lambda dev, dir : self.total(network, dev, dir, True) + 'b'
        elif show_total:
            f = lambda dev, dir : self.total(network, dev, dir, False) + 'B'
        elif show_bytes and self.in_bytes:
            f = lambda dev, dir : self.KBps(network, dev, dir, net_tdiff) + 'KB/s'
        elif show_bytes:
            f = lambda dev, dir : self.kbps(network, dev, dir, net_tdiff) + 'kbps'
        else:
            f = lambda dev, dir : '%i' % network[dev][dir + '_' + label]
            suffix = ' ' + label
        def create(lbl, dev):
            ret = ''
            if lbl is not None:
                ret += lbl + ': '
            have_down = ('rx_' + xlabel) in network[dev]
            have_up   = ('tx_' + xlabel) in network[dev]
            if have_down:
                ret += f(dev, 'rx') + '↓'
                if have_up:
                    ret += ' '
            if have_up:
                ret += f(dev, 'tx') + '↑'
            ret += suffix
            if show_bytes or show_total:
                if dev == '*':
                    for dev in self.pings.keys():
                        for ping in self.pings[dev]:
                            ret += self.ping(ping)
                elif dev in self.pings:
                    for ping in self.pings[dev]:
                        ret += self.ping(ping)
            return ret
        if self.show_all:
            if self.show_name:
                net = [create(dev, dev) for dev in network]
            else:
                net = [create(None, dev) for dev in network]
            net = (SEPARATOR if self.show_name else ' ').join(net)
        else:
            devsum = {}
            for dev in network.keys():
                table = network[dev]
                for key in table.keys():
                    if key not in devsum:
                        devsum[key] = 0
                    devsum[key] += table[key]
            network['*'] = devsum
            if self.show_name:
                net = create('Net', '*')
            else:
                net = create(None, '*')
        self.net_last = network
        return net