-
Notifications
You must be signed in to change notification settings - Fork 1
/
mpanel
executable file
·436 lines (391 loc) · 13.7 KB
/
mpanel
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/bin/python
# mpanel - Watches client status information from an MQTT broker
#
# By Dennis Sell
#
#
__author__ = "Dennis Sell"
__copyright__ = "Copyright (C) Dennis Sell"
#BUGS
# resizing terminal crashes. Possibly some curses/mosquitto interaction.
import os
import sys
import getopt
import mosquitto
import curses
from curses.ascii import isprint
import time
import datetime
version_number = 0.8
topiclist = []
numclients = 0
rxcount = 0
pos = 0
col = 0
topic_col = 0
topic_size = 60
retained_col = topic_size
qos_col = topic_size
count_col = topic_size
timestamp_col = topic_size
message_col = topic_size
items = []
class item:
def __init__(self, topic_tail, x, length, rl = 'l'):
self.topic_tail = topic_tail
self.x = x
self.length = length
self.rl = rl
def usage():
print "usage: mpanel [options]" #[hostname[:port]] [topics]"
print "Watch topics change from an MQTT broker."
print
print " -u --hostname HOSTNAME set hostname for broker (default localhost)"
print " -p --port PORT set port for mqtt broker (default 1883)"
print " -t --topics TOPICS set topics to watch (default /#)"
print " -h --help show this help information"
print " -v --version show version information"
print "By Dennis Sell -- 2013"
def version():
print "version: " + str(version_number)
def show_help():
help_cols = curses.COLS/2
help_lines = curses.LINES/2
help_win = curses.newwin(24, 70, help_lines-12, help_cols-35)
help_win.box()
help_win.addstr(1, 1, "Key Commands", curses.A_UNDERLINE)
help_win.addstr(3, 1, "[Home] - Jump to top left")
help_win.addstr(4, 1, "b - Jump to begining of line")
help_win.addstr(5, 1, "e - Jump to end of line")
help_win.addstr(6, 1, "[Right Arrow] - Scroll to right")
help_win.addstr(7, 1, "[Left Arrow] - Scroll to left")
help_win.addstr(8, 1, "[Up Arrow] - Scroll up")
help_win.addstr(9, 1, "[Down Arrow] - Scroll down")
help_win.addstr(10, 1, "[Page Up] - Page up")
help_win.addstr(11, 1, "[Page Down] - Page down")
help_win.addstr(13, 1, "p - Send a ping request globally")
help_win.addstr(14, 1, "i - Send an identify request globally")
help_win.addstr(15, 1, "R - Reload client settings for a specific client")
help_win.addstr(17, 1, "q - To exit help.")
help_win.noutrefresh()
curses.doupdate()
while True:
key = stdscr.getch()
if key == ord("q"):
break
help_win.erase()
del help_win
stats_txt.refresh(pos, col, 3,2, page,curses.COLS-3)
header_txt.refresh(0, 0, 2, 1, 2, curses.COLS-3)
stdscr.nooutrefresh()
curses.doupdate()
def get_client(prompt):
client_cols = curses.COLS/2
client_lines = curses.LINES/2
client_win = curses.newwin(7, 50, client_lines-12, client_cols-25)
client_win.box()
client_win.addstr(1, 1, prompt)
client_win.addstr(3, 1, "Enter client number:")
client_win.addstr(5, 1, "0 - For none")
client_win.noutrefresh()
stdscr.nooutrefresh()
curses.doupdate()
curses.echo()
client_win.addstr(3, 22, " ", curses.A_UNDERLINE)
client_num = client_win.getstr(3, 22)
curses.noecho()
client_win.erase()
del client_win
stats_txt.refresh(pos, col, 3,2, page,curses.COLS-3)
header_txt.refresh(0, 0, 2, 1, 2, curses.COLS-3)
stdscr.nooutrefresh()
curses.doupdate()
if client_num == '0':
ret = 99999
elif client_num.isdigit():
ret = int(client_num) - 1
if ret < 0:
ret = 99999
else:
ret = 99999
return ret
def on_connect(mosq, userdata, rc):
stdscr.addstr(curses.LINES-1,0,"Connected to %s" % broker)
stdscr.addstr(0,15,"Sunscribed to %s" % topics, curses.A_REVERSE)
stdscr.noutrefresh()
curses.doupdate()
def on_message(mosq, userdata, msg):
global numclients
global rxcount
global retained_flag
global timestamp_flag
rxcount = rxcount + 1
stdscr.addstr(0,80,"Msg count %s" % rxcount, curses.A_REVERSE)
t = msg.topic.split("/")
if len(t) > 3:
if t[2] != "global":
if t[2] not in topiclist:
topiclist.append(t[2])
numclients = numclients + 1
offset = topiclist.index(t[2])
stats_txt.addstr(offset, num_col-1, str(numclients).rjust(num_len))
stats_txt.addstr(offset, name_col-1, t[2].ljust(name_len))
stdscr.addstr(0,60,"Client count %s" % numclients, curses.A_REVERSE)
else:
offset = topiclist.index(t[2])
for i in items:
if i.topic_tail == t[3]:
if i.length + 1 > len(msg.payload) and i.rl == 'r':
stats_txt.addstr(offset, i.x-1, str(msg.payload).rjust(i.length))
else:
stats_txt.addstr(offset, i.x-1, str(msg.payload).ljust(i.length))
stats_txt.refresh(pos, col, 3,2, page,curses.COLS-3)
curses.doupdate()
def getTerminalSize():
"""
returns (lines:int, cols:int)
"""
import os, struct
def ioctl_GWINSZ(fd):
import fcntl, termios
return struct.unpack("hh", fcntl.ioctl(fd, termios.TIOCGWINSZ, "1234"))
# try stdin, stdout, stderr
for fd in (0, 1, 2):
try:
return ioctl_GWINSZ(fd)
except:
pass
# try os.ctermid()
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
try:
return ioctl_GWINSZ(fd)
finally:
os.close(fd)
except:
pass
# try `stty size`
try:
return tuple(int(x) for x in os.popen("stty size", "r").read().split())
except:
pass
# try environment variables
try:
return tuple(int(os.getenv(var)) for var in ("LINES", "COLUMNS"))
except:
pass
# i give up. return default.
return (25, 80)
try:
opts, args = getopt.getopt(sys.argv[1:], 'u:p:t:hv', ['hostname=', 'port=', 'topics=', 'help', 'version'])
except getopt.GetoptError as err:
print str(err)
usage()
sys.exit(2)
# defaults
broker = "127.0.0.1"
port = 1883
topics = "/clients/#"
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
sys.exit(2)
if opt in ('-v', '--version'):
version()
sys.exit(2)
elif opt in ('-u', '--hostname'):
broker = arg
elif opt in ('-p', '--port'):
port = int(arg)
elif opt in ('-t', '--topics'):
topics = arg
else:
usage()
sys.exit(2)
# minitialize mosquitto, connect, and subscribe
client = mosquitto.Mosquitto()
client.on_message = on_message
client.on_connect = on_connect
client.connect(broker, port)
client.subscribe(topics)
# initialize curses
stdscr = curses.initscr()
if curses.COLS < 95:
curses.endwin()
print "mpanel requires a minimum terminal width of 95 columns."
sys.exit(2)
if curses.LINES < 7:
curses.endwin()
print "mpanel requires a minimum terminal heigth of 7 rows."
sys.exit(2)
stdscr.nodelay(1)
curses.curs_set(0)
if curses.has_colors():
curses.start_color()
curses.cbreak()
curses.noecho()
stdscr.keypad(1)
# draw basic screen
stdscr.addstr("MQTT Panel ", curses.A_REVERSE)
stdscr.chgat(-1,curses.A_REVERSE)
stdscr.addstr(curses.LINES-1,curses.COLS-57,"page-up, page-dn, cursor arrows, ? for help, q to Quit")
page = curses.LINES - 3
stats = curses.newwin(curses.LINES-2,curses.COLS,1,0)
stats.box()
header_txt = curses.newpad(1,2000)
stats_txt = curses.newpad(2000,2000)
stats_txt.refresh(pos, col, 3,2, page,curses.COLS-3)
stdscr.noutrefresh()
stats.noutrefresh()
num_col = 1
num_len = 3
name_col = num_col+num_len+1
name_len = 34
ver_col = name_col+name_len+1
ver_len = 14
status_col = ver_col+ver_len+1
status_len = 13
class_col = status_col+status_len+1
class_len = 6
ping_col = class_col+class_len+1
ping_len = 8
extip_col = ping_col+ping_len+1
extip_len = 15
locip_col = extip_col+extip_len+1
locip_len = 15
pid_col = locip_col+locip_len+1
pid_len = 6
startts_col = pid_col+pid_len+1
startts_len = 26
con_col = startts_col+startts_len+1
con_len = 4
conts_col = con_col+con_len+1
conts_len = 26
dists_col = conts_col+conts_len+1
dists_len = 26
header_txt.addstr(0, num_col, "#".ljust(num_len), curses.A_UNDERLINE)
header_txt.addstr(0, name_col, "Client Name".ljust(name_len), curses.A_UNDERLINE)
header_txt.addstr(0, ver_col, "Ver. (a:c:d)".ljust(ver_len), curses.A_UNDERLINE)
header_txt.addstr(0, status_col, "Status".ljust(status_len), curses.A_UNDERLINE)
header_txt.addstr(0, class_col, "Class".ljust(class_len), curses.A_UNDERLINE)
header_txt.addstr(0, ping_col, "Ping".ljust(ping_len), curses.A_UNDERLINE)
header_txt.addstr(0, extip_col, "External Ip".ljust(extip_len), curses.A_UNDERLINE)
header_txt.addstr(0, locip_col, "Local IP".ljust(locip_len), curses.A_UNDERLINE)
header_txt.addstr(0, pid_col, "PID".ljust(pid_len), curses.A_UNDERLINE)
header_txt.addstr(0, startts_col, "Start Timestamp".ljust(startts_len), curses.A_UNDERLINE)
header_txt.addstr(0, con_col, "Con.".ljust(con_len), curses.A_UNDERLINE)
header_txt.addstr(0, conts_col, "Connect Timestamp".ljust(conts_len), curses.A_UNDERLINE)
header_txt.addstr(0, dists_col, "Disconnect Timestamp".ljust(dists_len), curses.A_UNDERLINE)
header_txt.refresh(0, 0, 2, 1, 2, curses.COLS-3)
stats.noutrefresh()
curses.doupdate()
items.append(item("status", status_col, status_len, 'l'))
items.append(item("ping", ping_col, ping_len, 'l'))
items.append(item("version", ver_col, 4, 'r'))
items.append(item("class", class_col, class_len, 'l'))
items.append(item("core-version", ver_col+5, 4, 'r'))
items.append(item("daemon-version", ver_col+10, 4, 'r'))
items.append(item("extip", extip_col, extip_len, 'r'))
items.append(item("locip", locip_col, locip_len, 'r'))
items.append(item("pid", pid_col, pid_len, 'r'))
items.append(item("start", startts_col, startts_len, 'r'))
items.append(item("count", con_col, con_len, 'r'))
items.append(item("connecttime", conts_col, conts_len, 'r'))
items.append(item("disconnecttime", dists_col, dists_len, 'r'))
# main loop
while client.loop(0) == 0:
key = stdscr.getch()
if key == ord("q"):
break
if key == ord("?"):
show_help()
if key == ord("p"):
client.publish("/clients/global/ping", "request")
if key == ord("P"):
n = get_client("Ping a Client")
if n != 99999 and n < len(topiclist):
clienttopic = topiclist[n]
client.publish("/clients/" + clienttopic + "/ping", "request")
if key == ord("i"):
client.publish("/clients/global/identify", "request")
if key == ord("I"):
n = get_client("Identify a Client")
if n != 99999 and n < len(topiclist):
clienttopic = topiclist[n]
client.publish("/clients/" + clienttopic + "/identify", "request")
if key == ord("R"):
n = get_client("Reset a Client")
if n != 99999 and n < len(topiclist):
clienttopic = topiclist[n]
client.publish("/clients/" + clienttopic + "/command", "reload")
if key == curses.KEY_DOWN:
pos = pos + 1
if pos > numclients:
pos = numclients
stats_txt.refresh(pos, col, 3,2, page,curses.COLS-3)
header_txt.refresh(0, col, 2, 1, 2, curses.COLS-3)
curses.doupdate()
if key == curses.KEY_UP:
pos = pos - 1
if pos < 0:
pos = 0
stats_txt.refresh(pos, col, 3,2, page,curses.COLS-3)
header_txt.refresh(0, col, 2, 1, 2, curses.COLS-3)
curses.doupdate()
if key == curses.KEY_NPAGE:
pos = pos + page - 2
if pos > numclients:
pos = numclients
stats_txt.refresh(pos, col, 3,2, page,curses.COLS-3)
header_txt.refresh(0, col, 2, 1, 2, curses.COLS-3)
curses.doupdate()
if key == curses.KEY_PPAGE:
pos = pos - page + 2
if pos < 0:
pos = 0
stats_txt.refresh(pos, col, 3,2, page,curses.COLS-3)
header_txt.refresh(0, col, 2, 1, 2, curses.COLS-3)
curses.doupdate()
if key == curses.KEY_LEFT:
col = col - 1
if col < 0:
col = 0
stats_txt.refresh(pos, col, 3,2, page,curses.COLS-3)
header_txt.refresh(0, col, 2, 1, 2, curses.COLS-3)
curses.doupdate()
if key == curses.KEY_RIGHT:
col = col + 1
if col > 200: #3-curses.COLS:
col = 200 #3-curses.COLS
stats_txt.refresh(pos, col, 3,2, page,curses.COLS-3)
header_txt.refresh(0, col, 2, 1, 2, curses.COLS-3)
curses.doupdate()
if key == curses.KEY_HOME:
pos = 0
col = 0
stats_txt.refresh(pos, col, 3,2, page,curses.COLS-3)
header_txt.refresh(0, col, 2, 1, 2, curses.COLS-3)
curses.doupdate()
if key == ord("b"):
col = 0
stats_txt.refresh(pos, col, 3,2, page,curses.COLS-3)
header_txt.refresh(0, col, 2, 1, 2, curses.COLS-3)
curses.doupdate()
if key == ord("e"):
col = 200 #3 - curses.COLS
stats_txt.refresh(pos, col, 3,2, page,curses.COLS-3)
header_txt.refresh(0, col, 2, 1, 2, curses.COLS-3)
curses.doupdate()
if key == curses.KEY_RESIZE: #Resizing the terminal screen crashes this program.
l, c = getTerminalSize() #It seems to be something to do with calling the mosquitto library in the above while loop.
if c > 50: #Redrawing belongs here if the problem gets solved.
stdscr.addstr(0, curses.COLS-20, "Col: " + str(c) + " Lines: " + str(l))
l, c = getTerminalSize()
if c > 120:
stdscr.addstr(0,c - 27,"%s" % str(datetime.datetime.now()), curses.A_REVERSE)
pass
# clean up terminal settings
curses.nocbreak()
stdscr.keypad(0)
curses.echo()
curses.endwin()