forked from karpov-sv/ccdlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.py
executable file
·567 lines (432 loc) · 20.6 KB
/
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
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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#!/usr/bin/env python3
from __future__ import absolute_import, division, print_function, unicode_literals
from twisted.internet import stdio
from twisted.protocols.basic import LineReceiver
from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.web.static import File
from twisted.internet.endpoints import TCP4ServerEndpoint
try:
from txsockjs.factory import SockJSResource
_HAVE_TXSOCKJS = True
except:
_HAVE_TXSOCKJS = False
from twistedauth import wrap_with_auth as Auth
import os
import sys
import posixpath
import datetime
import re
try:
# Python2
from urlparse import urlparse, parse_qs
from StringIO import StringIO
from StringIO import StringIO as BytesIO
except:
# Python3
from urllib.parse import urlparse, parse_qs
from io import BytesIO, StringIO
import json
import numpy as np
from collections import OrderedDict
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.dates import DateFormatter
from matplotlib.ticker import ScalarFormatter, LogLocator, LinearLocator, MaxNLocator, NullLocator
from daemon import SimpleFactory, SimpleProtocol
from command import Command
from daemon import catch
from db import DB
def kwargsToString(kwargs, prefix=''):
return " ".join([prefix + _ + '=' + kwargs[_] for _ in kwargs])
class MonitorProtocol(SimpleProtocol):
_debug = False
def __init__(self):
SimpleProtocol.__init__(self)
self.name = None
self.status = {}
@catch
def connectionMade(self):
SimpleProtocol.connectionMade(self)
self.message('id name=monitor') # Send our identity to the peer
self.message('get_id') # Request peer identity
@catch
def connectionLost(self, reason):
if self.name in self.object['clients']:
self.log("%s disconnected" % self.name, type='info')
# print "Disconnected:", self.name
SimpleProtocol.connectionLost(self, reason)
@catch
def processMessage(self, string):
if self._debug:
print("%s:%d > %s" % (self._peer.host, self._peer.port, string))
cmd = Command(string)
if cmd.name == 'id':
self.name = cmd.get('name', None)
self.type = cmd.get('type', None)
if self.name in self.object['clients']:
self.log("%s connected" % self.name, type='info')
# print "Connected:", self.name
elif cmd.name == 'status':
# We keep var=value pairs from the status to report it to clients
self.status = cmd.kwargs
# We have to keep the history of values for some variables for plots
if self.name in self.object['values']:
for name in self.object['values'][self.name]:
if name == 'time':
value = datetime.datetime.utcnow()
else:
value = self.status.get(name, None)
# Now we should try to convert the value to numerical form, if possible
try:
value = float(value)
except:
pass
self.object['values'][self.name][name].append(value)
# Keep the maximal length of data arrays limited
# TODO: make it configurable, probably for every plot
if len(self.object['values'][self.name][name]) > 1000:
self.object['values'][self.name][name] = self.object['values'][self.name][name][100:]
# Broadcast new values to all CCDs, if the client itself is not CCD
if self.type != 'ccd':
self.factory.messageAll("set_keywords " + " ".join([self.name+'.'+_+'=\"' +
self.status[_]+'\"' for _ in self.status.keys()]), type="ccd")
# Store the values to database, if necessary
if 'db' in self.object and self.object['db'] is not None:
if (datetime.datetime.utcnow() - self.object['db_status_timestamp']).total_seconds() > self.object['db_status_interval']:
# FIXME: should we also store the status if no peer is reporting at all?
# print "Storing the state to DB"
time = datetime.datetime.utcnow()
status = self.factory.getStatus(as_dict=True)
self.object['db'].query('INSERT INTO monitor_status (time, status) VALUES (%s,%s)', (time, status))
self.object['db_status_timestamp'] = datetime.datetime.utcnow()
pass
elif cmd.name == 'get_status':
if cmd.kwargs.get('format', 'plain') == 'json':
self.message('status_json ' + json.dumps(self.factory.getStatus(as_dict=True)))
else:
self.message(self.factory.getStatus())
elif cmd.name == 'send' and cmd.chunks[1]:
c = self.factory.findConnection(name=cmd.chunks[1])
if c:
c.message(" ".join(cmd.chunks[2:]))
elif cmd.name in ['debug', 'info', 'message', 'error', 'warning', 'success']:
msg = " ".join(cmd.chunks[1:])
self.log(msg, source=self.name, type=cmd.name)
elif cmd.name == 'reset_plots':
self.factory.reset_plots()
def log(self, msg, time=None, source=None, type='message'):
if source is None:
source = self.name
self.factory.log(msg, time=time, source=source, type=type)
def update(self):
if self.name or self.type:
self.message('get_status')
class WSProtocol(SimpleProtocol):
def message(self, string):
"""Sending outgoing message with no newline"""
self.transport.write(string.encode('ascii'))
class MonitorFactory(SimpleFactory):
@catch
def getStatus(self, as_dict=False):
if as_dict:
status = {'nconnected': len(self.connections), 'db_status_interval': self.object['db_status_interval']}
else:
status = 'status nconnected=%d db_status_interval=%g' % (len(self.connections), self.object['db_status_interval'])
# Monitor only specified connections
for name in self.object['clients']:
c = self.findConnection(name=name)
if c:
if as_dict:
status[c.name] = c.status
else:
status += ' ' + c.name + '=1 ' + kwargsToString(c.status, prefix=c.name + '.')
else:
if as_dict:
status[name] = {}
else:
status += ' ' + name + '=0'
# Monitor all connections instead
# for c in self.connections:
# if c.name:
# status += ' ' + c.name + '=1 ' + kwargsToString(c.status, prefix=c.name + '_')
return status
@catch
def log(self, msg, time=None, source=None, type='message'):
"""Log the message to both console, web-interface and database, if connected"""
if time is None:
time = datetime.datetime.utcnow()
if source is None:
source = 'monitor'
print("%s: %s > %s > %s" % (time, source, type, msg))
# DB
if 'db' in self.object and self.object['db'] is not None:
self.object['db'].log(msg, time=time, source=source, type=type)
# WebSockets
if 'ws' in self.object:
self.object['ws'].messageAll(json.dumps({'msg': msg, 'time': str(time), 'source': source, 'type': type}))
@catch
def reset_plots(self):
values = self.object['values']
for client in values.keys():
for param in values[client].keys():
values[client][param] = []
self.log('Resetting plots', source='monitor', type='info')
pass
class CmdlineProtocol(LineReceiver):
delimiter = os.linesep.encode('ascii')
def __init__(self, factory=None, object=None):
self.factory = factory
self.object = object
def connectionMade(self):
self.transport.write(b'### ')
def message(self, string=''):
self.transport.write(string.encode('ascii'))
self.transport.write('\n'.encode('ascii'))
@catch
def lineReceived(self, line):
cmd = Command(line.decode('ascii'))
if cmd.name == 'exit':
self.factory._reactor.stop()
elif cmd.name == 'connections':
self.message("Number of connections: %d" % len(self.factory.connections))
for c in self.factory.connections:
self.message(" %s:%s name:%s type:%s\n" % (c._peer.host, c._peer.port, c.name, c.type))
if 'ws' in self.object:
self.message("Number of WS connections: %d" % len(self.object['ws'].connections))
for c in self.object['ws'].connections:
self.message(" %s:%s name:%s type:%s\n" % (c._peer.host, c._peer.port, c.name, c.type))
elif cmd.name == 'clients' or not cmd.name:
self.message("Number of registered clients: %d" % len(self.object['clients']))
for name, c in self.object['clients'].items():
conn = self.factory.findConnection(name=c['name'])
self.message(" %s:%s name:%s connected:%s" % (c['host'], c['port'], c['name'], conn != None))
self.message()
elif cmd.name == 'send' and cmd.chunks[1]:
c = self.factory.findConnection(name=cmd.chunks[1])
if c:
c.message(" ".join(cmd.chunks[2:]))
elif cmd.name == 'get_status':
self.message(self.factory.getStatus())
elif cmd.name in ['debug', 'info', 'message', 'error', 'warning']:
msg = " ".join(cmd.chunks[1:])
time = datetime.datetime.utcnow()
self.factory.log(msg, time=time, source='web', type=cmd.name)
elif cmd.name == 'reset_plots':
self.factory.reset_plots()
self.transport.write(b'### ')
def serve_json(request, **kwargs):
request.responseHeaders.setRawHeaders("Content-Type", ['application/json'])
return json.dumps(kwargs)
def make_plot(file, obj, client_name, plot_name, size=800):
plot = obj['clients'][client_name]['plots'][plot_name]
values = obj['values'][client_name]
has_data = False
fig = Figure(facecolor='white', dpi=72, figsize=(plot['width']/72, plot['height']/72), tight_layout=True)
ax = fig.add_subplot(111)
for _ in plot['values'][1:]:
# Check whether we have at least one data point to plot
if np.any(np.array(values[_]) != None):
has_data = True
ax.plot(values[plot['values'][0]], values[_], '-', label=_)
if plot['values'][0] == 'time' and len(values[plot['values'][0]]) > 1 and has_data:
ax.xaxis.set_major_formatter(DateFormatter('%H:%M:%S'))
fig.autofmt_xdate()
if plot['xlabel']:
ax.set_xlabel(plot['xlabel'])
else:
ax.set_xlabel(plot['values'][0])
if plot['ylabel']:
ax.set_ylabel(plot['ylabel'])
elif len(plot['values']) == 1:
ax.set_ylabel(plot['values'][1])
if has_data:
if plot['xscale'] != 'linear':
ax.set_xscale(plot['xscale'], nonposx='clip')
if plot['yscale'] != 'linear':
ax.set_yscale(plot['yscale'], nonpositive='clip')
if plot['yscale'] == 'log':
# Try to fix the ticks if the data span is too small
axis = ax.get_yaxis()
if np.ptp(np.log10(axis.get_data_interval())) < 1:
axis.set_major_locator(MaxNLocator())
axis.set_minor_locator(NullLocator())
if len(plot['values']) > 4:
ax.legend(frameon=True, loc=2, framealpha=0.99)
elif len(plot['values']) > 2:
ax.legend(frameon=False)
if plot['name']:
ax.set_title(plot['name'])
ax.margins(0.01, 0.1)
# FIXME: make it configurable
ax.grid(True)
# Return the image
canvas = FigureCanvas(fig)
canvas.print_png(file)#, bbox_inches='tight')
class WebMonitor(Resource):
isLeaf = True
def __init__(self, factory=None, object=None):
self.factory = factory
self.object = object
@catch
def render_GET(self, request):
q = urlparse(request.uri)
args = parse_qs(q.query)
path = q.path.decode('ascii')
qs = path.split('/')
if q.path == b'/monitor/status':
return serve_json(request,
clients=self.object['clients'],
status=self.factory.getStatus(as_dict=True)).encode('ascii')
# /monitor/plots/{client}/{name}
elif qs[1] == 'monitor' and qs[2] == 'plot' and len(qs) > 4:
s = BytesIO()
make_plot(s, self.object, qs[3], qs[4])
request.responseHeaders.setRawHeaders("Content-Type", ['image/png'])
request.responseHeaders.setRawHeaders("Content-Length", [str(len(s.getvalue()))])
request.responseHeaders.setRawHeaders("Cache-Control", ['no-store, no-cache, must-revalidate, max-age=0'])
return s.getvalue()
elif path == '/monitor/command' and b'string' in args:
cmd = Command(args[b'string'][0].decode('ascii'))
# TODO: re-use the command processing code from TCP server part
if cmd.name == 'exit':
self.factory._reactor.stop()
elif cmd.name == 'send' and cmd.chunks[1]:
c = self.factory.findConnection(name=cmd.chunks[1])
if c:
c.message(" ".join(cmd.chunks[2:]))
elif (cmd.name == 'broadcast' or cmd.name == 'send_all'):
self.factory.messageAll(" ".join(cmd.chunks[1:]))
elif (cmd.name == 'set'):
if 'interval' in cmd:
self.object['db_status_interval'] = float(cmd.get('interval'))
self.factory.log('DB status interval set to %g' % self.object['db_status_interval'], type='info')
elif cmd.name in ['debug', 'info', 'message', 'error', 'warning']:
msg = " ".join(cmd.chunks[1:])
time = datetime.datetime.utcnow()
self.factory.log(msg, time=time, source='web', type=cmd.name)
elif cmd.name == 'reset_plots':
self.factory.reset_plots()
return serve_json(request).encode('ascii')
else:
return q.path
def loadINI(filename, obj):
# We use ConfigObj library, docs: http://configobj.readthedocs.io/en/latest/index.html
from configobj import ConfigObj, Section # apt-get install python-configobj
from validate import Validator
# Schema to validate and transform the values from config file
schema = ConfigObj(StringIO('''
port = integer(min=0,max=65535,default=%d)
http_port = integer(min=0,max=65535,default=%d)
name = string(default=%s)
db_host = string(default=%s)
db_status_interval = float(min=0, max=3600, default=%g)
[__many__]
enabled = boolean(default=True)
port = integer(min=0,max=65535,default=0)
host = string(default=localhost)
description = string(default=None)
template = string(default=default.html)
[[plots]]
[[[__many__]]]
name = string(default=None)
values = list(default=,)
xlabel = string(default=None)
ylabel = string(default=None)
width = integer(min=0,max=2048,default=800)
height = integer(min=0,max=2048,default=300)
xscale = string(default=linear)
yscale = string(default=linear)
''' % (obj['port'], obj['http_port'], obj['name'], obj['db_host'], obj['db_status_interval'])), list_values=False)
confname = '%s.ini' % posixpath.splitext(__file__)[0]
conf = ConfigObj(confname, configspec=schema)
if len(conf):
result = conf.validate(Validator())
if result != True:
print("Config file failed validation: %s" % confname)
print(result)
raise RuntimeError
for sname in conf:
section = conf[sname]
# Skip leafs and branches with enabled=False
if type(section) != Section or not section['enabled']:
continue
client = section.dict()
client['name'] = sname
obj['values'][sname] = {}
if 'plots' in section:
values = []
# Parse parameters of plots
for plot in section['plots']:
client['plots'][plot] = section['plots'][plot]
values += section['plots'][plot]['values']
obj['values'][sname] = {_: [] for _ in set(values)} # Unique values
obj['clients'][sname] = client
for key in ['port', 'http_port', 'name', 'db_host', 'db_status_interval']:
obj[key] = conf.get(key)
# print obj
# sys.exit(1)
return True
if __name__ == '__main__':
from optparse import OptionParser
# Object holding actual state and work logic.
obj = {'clients': OrderedDict(), 'values': {}, 'port': 7100, 'http_port': 8888, 'db_host': None,
'db_status_interval': 60.0, 'name': 'monitor', 'db': None}
# First read client config from INI file
loadINI('%s.ini' % posixpath.splitext(__file__)[0], obj)
# Now parse command-line arguments using values read from config as defaults
# so that they may be changed at startup time
parser = OptionParser(usage="usage: %prog [options] name1=host1:port1 name2=host2:port2 ...")
parser.add_option('-p', '--port', help='Daemon port', action='store', dest='port', type='int', default=obj['port'])
parser.add_option('-H', '--http-port', help='HTTP server port', action='store', dest='http_port', type='int', default=obj['http_port'])
parser.add_option('-d', '--db-host', help='Database server host', action='store', dest='db_host', type='string', default=obj['db_host'])
parser.add_option('-n', '--name', help='Daemon name', action='store', dest='name', type='string', default=obj['name'])
parser.add_option('-D', '--debug', help='Debug output', action='store_true', dest='debug', default=False)
parser.add_option('-s', '--server', help='Act as a TCP and HTTP server', action='store_true', dest='server', default=False)
parser.add_option('-i', '--interval', help='DB logging status inteval', dest='interval', type='float', default=obj['db_status_interval'])
parser.add_option('-a', '--auth-file', help='passwords file', action='store', dest='passwd_file', type='string') # htpasswd -c -d passwdfile user
(options, args) = parser.parse_args()
obj['db_status_interval'] = options.interval
# Next parse command line positional args as name=host:port tokens
for arg in args:
m = re.match('(([a-zA-Z0-9-_]+)=)?(.*):(\d+)', arg)
if m:
name, host, port = m.group(2, 3, 4)
if name in obj['clients']:
obj['clients'][name]['host'] = host
obj['clients'][name]['port'] = int(port)
else:
obj['clients'][name] = {'host': host, 'port': int(port), 'name': name, 'description': name, 'template': 'default.html', 'plots': None}
# Now we have everything to construct and run the daemon
daemon = MonitorFactory(MonitorProtocol, obj, name=options.name)
for name, c in obj['clients'].items():
daemon.connect(c['host'], c['port'])
# Simple stdio interface
stdio.StandardIO(CmdlineProtocol(factory=daemon, object=obj), reactor=daemon._reactor)
# Web interface
if options.debug:
from twisted.python import log
log.startLogging(sys.stdout)
if options.server:
# Listen for incoming TCP connections
print("Listening for incoming TCP connections on port %d" % options.port)
daemon.listen(options.port)
# Serve files from web
root = File(r"web")
root.putChild(b"", File('web/main.html'))
root.putChild(b"monitor", WebMonitor(factory=daemon, object=obj))
if options.passwd_file and os.path.exists(options.passwd_file):
site = Site(Auth(root, options.passwd_file))
else:
site = Site(root)
# WebSockets
if _HAVE_TXSOCKJS:
ws = SimpleFactory(WSProtocol, obj)
obj['ws'] = ws
root.putChild(b"ws", SockJSResource(ws))
# Database connection
obj['db'] = DB(dbhost=options.db_host)
obj['db_status_timestamp'] = datetime.datetime.utcfromtimestamp(0)
print("Listening for incoming HTTP connections on port %d" % options.http_port)
TCP4ServerEndpoint(daemon._reactor, options.http_port).listen(site)
daemon._reactor.run()