-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbandwidth.py
131 lines (110 loc) · 3.7 KB
/
bandwidth.py
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
# -*- coding: utf-8 -*-
#
# Copyright (c) 2009 by xt <[email protected]>
#
# 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/>.
#
#
#
#
# to make bandwidth visible you have to add ",[bandwidth]" (without "")
# in your weechat.bar.status.items
#
# History:
#
# 2009-10-15, xt <[email protected]>:
# version 0.2: error checking from output command
# 2009-10-14, xt <[email protected]>:
# version 0.1: initial release inspired by nils' perl script
#
SCRIPT_NAME = "bandwidth"
SCRIPT_AUTHOR = "xt <[email protected]>"
SCRIPT_VERSION = "0.2"
SCRIPT_LICENSE = "GPL3"
SCRIPT_DESC = "Displays network interface bandwidth on a bar"
import os
import_ok = True
try:
import weechat
except:
print "This script must be run under WeeChat."
print "Get WeeChat now at: http://www.weechat.org/"
import_ok = False
# script options
bandwidth_settings = {
"device" : "eth0", # Network interface
"refresh_rate" : "5", # in s
"display_unit" : "on", # on/off
}
last_i = 0
last_o = 0
def bandwidth_timer_cb(data, remaining_calls):
weechat.bar_item_update('bandwidth')
return weechat.WEECHAT_RC_OK
def bandwidth_item_cb(data, buffer, args):
""" Callback for building hlpv item. """
global last_i, last_o
device = weechat.config_get_plugin('device')
pipe = os.popen(''' awk -v interface="%s" 'BEGIN { gsub(/\./, "\\\\.", interface) } \\
$1 ~ "^" interface ":" {
split($0, a, /: */); $0 = a[2]; \
print "" $1 "\\n" $9 \
}' /proc/net/dev''' %device)
pipeoutput = pipe.read()
if not pipeoutput:
# Problem with reading
return ''
if not len(pipeoutput.split()) == 2:
# Problem with reading
return ''
cur_i, cur_o = pipeoutput.split()
pipe.close()
cur_i = float(cur_i)
cur_o = float(cur_o)
i = (cur_i - last_i)/1024 # KiB
o = (cur_o - last_o)/1024 # KiB
if last_i == cur_i:
return ''
last_i = cur_i
last_o = cur_o
if not last_i or not last_o:
return ''
i_unit = ''
o_unit = ''
if weechat.config_get_plugin('display_unit') == 'on':
i_unit = 'KiB/s'
o_unit = 'KiB/s'
if i > 1023:
i = i/1024
i_unit = 'MiB/s'
if o > 1023:
o = o/1024
o_unit = 'MiB/s'
return "i: %(in)d %(i_unit)s, o: %(out)d %(o_unit)s" %{
'in': i,
'out': o,
'i_unit': i_unit,
'o_unit': o_unit,
}
if __name__ == "__main__" and import_ok:
if weechat.register(SCRIPT_NAME, SCRIPT_AUTHOR, SCRIPT_VERSION,
SCRIPT_LICENSE, SCRIPT_DESC,
"", ""):
# set default settings
for option, default_value in bandwidth_settings.iteritems():
if not weechat.config_is_set_plugin(option):
weechat.config_set_plugin(option, default_value)
# new item
weechat.bar_item_new('bandwidth', 'bandwidth_item_cb', '')
weechat.hook_timer(int(weechat.config_get_plugin("refresh_rate")) * 1000, 0, 0, "bandwidth_timer_cb", "")