-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_monitor.py
executable file
·299 lines (212 loc) · 7.55 KB
/
mqtt_monitor.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from datetime import datetime
from time import sleep
import ssl
import paho.mqtt.client as mqtt
from os import system, name, path
import argparse
from configparser import ConfigParser
from tabulate import tabulate
import signal
import sys
import pandas as pd
pd.set_option('display.max_columns', None)
pd.set_option('max_colwidth', 30)
pd.options.display.width = 0
from colors import *
maxlen = 5
topicDict = {}
messages = 0
def signal_handler(sig, frame):
print("\n\n\nYou pressed Ctrl+C!\n\n\n")
gotoxy(0, 80)
sys.exit(0)
def gotoxy(x, y):
print("%c[%d;%df" % (0x1B, y, x), end='')
def cls():
# for windows
if name == 'nt':
_ = system('cls')
# for mac and linux(here, os.name is 'posix')
else:
_ = system('clear')
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
for topic in topics:
# print ("subscribing to", topic)
client.subscribe(topic)
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
global maxlen
global topicDict
global ts
global messages
data = "{:%Y-%m-%d %H:%M:%S}".format(datetime.now())
ts = datetime.now().timestamp()
messages += 1
topicSplit = msg.topic.split("/")
if len(topicSplit) > maxlen:
maxlen = len(topicSplit)
topicSplit.extend([' '] * (maxlen - len(topicSplit)))
payload = colorString(changeString(str(msg.payload.decode("utf-8"))))
topicSplit.append(payload)
topicDict[msg.topic] = [ts] + [data] + [
colorString(changeString(s)) for s in topicSplit
]
# final calculations on Pandas DataFrame
toDisplayDf = pd.DataFrame.from_dict(topicDict, orient='index')
toDisplayDf.insert(1, "ago", 0)
toDisplayDf["ago"] = toDisplayDf[0].apply(pretty_date)
toDisplayDf = toDisplayDf.drop([0, 1], axis=1)
toDisplayDf = toDisplayDf.sort_index() # .drop([1], axis=1)
toDisplay = headerFileText + tabulate(toDisplayDf, showindex=showTopic, tablefmt=tableStyle) + footerFileText
if saveTo:
# we are writing to file
saveToFile(toDisplay, saveTo)
else:
# we are displaying
cls()
displayStatus()
gotoxy(0, 3)
print(toDisplay)
def displayStatus():
uptime = ts - timeStart
# messages per second
if messages > 0:
pckgs = messages / uptime
else:
pckgs = 0
gotoxy(0, 0)
print(
"Now is: {:%Y-%m-%d %H:%M:%S}\t Last update: {}\tMessages received: {} ({:.2f} msg/s)| Connected to: {}:{}"
.format(datetime.now(), pretty_date(ts), messages, pckgs, host, port))
# console window title
consoleTitle = "{}:{} | {} | {} msgs |".format(host, port, pretty_date(ts),
messages)
print('\33]0;%s\a' % (consoleTitle), end='', flush=True)
def changeString(string):
if string in wordDict:
string = wordDict[string]
return string
def file_read(filename):
f = open(filename, 'r')
content = f.read()
f.close()
return content
def colorString(string):
if string in colorDict:
string = colorDict[string] + string + resetColor
return string
def parseArguments():
parser = argparse.ArgumentParser(
description='''Program for clustering data''',
add_help=True,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
conflict_handler='resolve')
optional_arguments = parser.add_argument_group('Optional arguments')
optional_arguments.add_argument('--conf',
help='config file',
dest="configFile",
default='')
optional_arguments.add_argument('--test',
help='perform tests',
dest="performTest",
action='store_true')
return parser.parse_args()
if __name__ == "__main__":
ts = datetime.now().timestamp()
timeStart = ts
#
# ---------------------------------- arguments ------------------------- #
#
args = parseArguments()
configFile = args.configFile
performTest = args.performTest
#
# ---------------------------------- CONFIG ------------------------- #
#
if configFile == "":
# config file path not provided
configFile = path.join(path.dirname(__file__), 'mqtt_monitor.conf')
print("configFile", configFile)
config = ConfigParser()
try:
config.read_file(open(configFile, "r"))
except:
print("Can't find config file in %s" % (configFile))
exit(2)
host = config['server']['host']
port = int(config['server']['port'])
username = config['server']['username']
password = config['server']['password']
usessl = config.getboolean('server', 'usessl')
showTopic = config.getboolean('display', 'showTopic')
tableStyle = config['display']['tableStyle']
addResetColor = config.getboolean('display', 'addResetColor')
if addResetColor:
resetColor = colors.reset
else:
resetColor = ""
if config.has_option('display', 'saveTo'):
saveTo = config['display']['saveTo']
else:
saveTo = False
# header and footer files
if config.has_option('display', 'headerFile'):
headerFileText = file_read(config['display']['headerFile'])
else:
headerFileText = ""
if config.has_option('display', 'footerFile'):
footerFileText = file_read(config['display']['footerFile'])
else:
footerFileText = ""
topics = [x.strip() for x in config['topics']['topics'].split(',')]
print(topics)
wordDict = config._sections['replacements']
colorDict = {}
if not saveTo:
for key in config._sections['coloring']:
colorDict[eval(key)] = eval(config._sections['coloring'][key])
else:
for key in config._sections['coloring']:
colorDict[eval(key)] = removeAnsii(
eval(config._sections['coloring'][key]))
#
# ---------------------------------- MAIN ------------------------- #
#
cls()
gotoxy(0, 0)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(username, password=password)
if usessl:
client.tls_set(cert_reqs=ssl.CERT_NONE)
client.tls_insecure_set(True)
client.connect(host, port, 60)
signal.signal(signal.SIGINT, signal_handler)
print("Press Ctrl+C")
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
if saveTo:
# no need for the interface
client.loop_forever()
else:
# GUI console interface
client.loop_start()
continueLoop = True
while continueLoop:
displayStatus()
# check if we are in a test mode:
if performTest:
# if received at least 3 messages or it took longer than 30 seconds
if messages > 4 or (datetime.now().timestamp() - ts > 30):
print(colors.fg.green + "Test passed!" + resetColor)
exit(0)
sleep(1)