This repository was archived by the owner on Jun 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpygnad.py
More file actions
executable file
·250 lines (196 loc) · 6.18 KB
/
pygnad.py
File metadata and controls
executable file
·250 lines (196 loc) · 6.18 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python
'''
pygnad - shows bandwith utilisation in a SysTray Icon, eg. in lxpanel
Copyright (C) 2011 Florian Streibelt
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/>.
'''
import gtk, gobject, os, sys, pyinotify
from pyinotify import WatchManager, Notifier, ProcessEvent, EventsCodes
from collections import deque
import time
import random
import getopt
class StatusIcc():
w=18
h=18
pixbuf=None;
# activate callback
def activate( self, widget, data=None):
global nic
v=0
max=0
for i in range(self.w):
x=self.myqueue[i]
v+=x
if x>max:
max=x
b=max/ival
dialog = gtk.MessageDialog(
parent = None,
flags = gtk.DIALOG_DESTROY_WITH_PARENT,
type = gtk.MESSAGE_INFO,
buttons = gtk.BUTTONS_OK,
message_format = 'Status of nic %s:\n' % (nic)
+'Statistics of the last %i seconds:\n' % (self.w*ival)
+'Bytes transfered: %i (Rx and Tx)\n' % (v)
+'max. bandwidth: %d B/s\n' % (b)
+'avg. bandwidth: %s B/s' % (v/(self.w*ival))
)
dialog.set_title(nic+ ' status')
dialog.connect('response', self.show_hide)
dialog.show()
# Show_Hide callback
def show_hide(self, widget,response_id, data= None):
if response_id == gtk.RESPONSE_YES:
widget.hide()
else:
widget.hide()
# destroyer callback
def destroyer(self, widget,response_id, data= None):
if response_id == gtk.RESPONSE_YES:
gtk.main_quit()
#sys.exit(1)
else:
widget.hide()
# popup callback
def popup(self, button, widget, data=None):
dialog = gtk.MessageDialog(
parent = None,
flags = gtk.DIALOG_DESTROY_WITH_PARENT,
type = gtk.MESSAGE_INFO,
buttons = gtk.BUTTONS_YES_NO,
message_format = "Terminate the PyGNAD tray icon?")
dialog.set_title('Close PyGNAD?')
dialog.connect('response', self.destroyer)
dialog.show()
def appendValue(self,value=0):
sys.stderr.write("got new value to add: %s \n" %value)
self.myqueue.popleft()
self.myqueue.append(value)
self.draw_graph()
def draw_graph(self):
w=self.w
h=self.h
pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, w, h)
#w,h = pixbuf.get_width(), pixbuf.get_height()
drawable = gtk.gdk.Pixmap( None, w, h, 24)
gc = drawable.new_gc()
drawable.draw_pixbuf(gc, pixbuf, 0,0,0,0,-1,-1)
#---ACTUAL DRAWING CODE---
pixmap,mask = pixbuf.render_pixmap_and_mask() # Function call
cm = pixmap.get_colormap()
red = cm.alloc_color(self.fgcolor)
green = cm.alloc_color(self.bgcolor)
black = cm.alloc_color('black')
gc.set_foreground(black)
drawable.draw_rectangle(gc, True, 0, 0, w, h)
maxvalue=0
for i in range(w):
v=self.myqueue[i]
if (v>maxvalue):
maxvalue=v
sys.stderr.write("max value in queue: %i \n" %maxvalue)
#drawable.draw_line(gc, 0, 0, w,h)
for i in range(w):
if (maxvalue>0):
value =h-int( (self.myqueue[i] / float(maxvalue) )*h)
else:
value = h
#sys.stderr.write(" %i " % value)
gc.set_foreground(red)
drawable.draw_line(gc,i,h,i,value)
#gc.set_foreground(green)
#drawable.draw_line(gc,i,0,i,value)
sys.stderr.write("\n")
#-------------------------
pb=pixbuf.get_from_drawable(drawable,cm,0,0,0,0,w,h)
self.staticon.set_from_pixbuf(pb)
def timer_update(self):
sys.stderr.write("tick\n")
#/sys/class/net/wlan0/statistics/tx_bytes
#value_t1 = int(open("/sys/class/net/wlan0/statistics/tx_bytes","r").read())
#value_r1 = int(open("/sys/class/net/wlan0/statistics/rx_bytes","r").read())
#value_t2 = int(open("/sys/class/net/eth0/statistics/tx_bytes","r").read())
#value_r2 = int(open("/sys/class/net/eth0/statistics/rx_bytes","r").read())
#value=value_t1 + value_r1 + value_t2 + value_r2
try:
value_t = int(open("/sys/class/net/%s/statistics/tx_bytes" % self.nic ,"r").read())
value_r = int(open("/sys/class/net/%s/statistics/rx_bytes" % self.nic ,"r").read())
value=value_r + value_t
except IOError:
value=0
if (self.lastvalue==None):
self.lastvalue=value
delta=value - self.lastvalue
sys.stderr.write("delta = %i on nic %s \n" % (delta,self.nic))
self.appendValue(delta);
self.lastvalue=value
return True
def __init__(self,nic,fg,bg,ival):
gtk.gdk.threads_init()
self.myqueue=deque()
self.lastvalue=None
self.fgcolor=fg
self.bgcolor=bg
self.nic=nic
for i in range(self.w):
self.myqueue.append(20)
sys.stderr.write("icon init started\n")
# create a new Status Icon
self.staticon = gtk.StatusIcon()
#self.draw_graph()
gobject.timeout_add_seconds(ival, self.timer_update)
self.staticon.connect("activate", self.activate)
self.staticon.connect("popup_menu", self.popup)
self.staticon.set_visible(True)
sys.stderr.write("icon init finished\n")
# register a timer
#gobject.timeout_add_seconds(10, self.timer_callback)
#self.timer_callback()
def usage(name):
print("usage: %s -n nic -f fgcolor -b bgcolor [-h] \n" % name)
def main(argv=None):
if argv is None:
argv = sys.argv
global nic
global ival
rxbytes=0
txbytes=0
time=0
nic = "eth0"
fgcolor = "red"
bgcolor = "black"
ival = 5
try:
opts, args = getopt.getopt(argv[1:], "hn:f:b:i:", ["help","nic=", "fgcolor=","bgcolor=","interval="])
except getopt.GetoptError:
usage(argv[0])
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage(argv[0])
sys.exit()
elif opt in ("-n", "--nic"):
nic = arg
elif opt in ("-f", "--fgcolor"):
fgcolor = arg
elif opt in ("-b", "--bgcolor"):
bgcolor = arg
elif opt in ("-i", "--interval"):
ival = int(arg)
statusicon = StatusIcc(nic,fgcolor,bgcolor,ival)
try:
gtk.main()
except KeyboardInterrupt:
pass
if __name__ == "__main__":
sys.exit(main())