-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit_verification.py
754 lines (604 loc) · 34.1 KB
/
init_verification.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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
"""Module containing functions to be called inside the verified service. This provides a function to set up the
consumption thread (initialising verification) and a function to insert events into the consumption queue. """
import datetime
import json
import os
import pickle
import threading
import flask
import requests
import base64
from Queue import Queue
import requests
from VyPR.SCFG.construction import CFGEdge, CFGVertex
from VyPR.QueryBuilding import *
from VyPR.monitor_synthesis import formula_tree
from VyPR.verdict_reports import VerdictReport
VERDICT_SERVER_URL = None
VYPR_OUTPUT_VERBOSE = True
PROJECT_ROOT = None
class MonitoringLog(object):
"""
Class to handle monitoring logging.
"""
def __init__(self, logs_to_stdout):
self.logs_to_stdout = logs_to_stdout
# check for log directory - create it if it doesn't exist
if not (os.path.isdir("vypr_monitoring_logs")):
os.mkdir("vypr_monitoring_logs")
self.log_file_name = "vypr_monitoring_logs/%s" \
% str(datetime.datetime.now()). \
replace(" ", "_").replace(":", "_").replace(".", "_").replace("-", "_")
self.handle = None
def start_logging(self):
# open the log file in append mode
self.handle = open(self.log_file_name, "a")
def end_logging(self):
self.handle.close()
def log(self, message):
if self.handle:
message = "[VyPR monitoring - %s] %s" % (str(datetime.datetime.now()), message)
self.handle.write("%s\n" % message)
# flush the contents of the file to disk - this way we get a log even with an unhandled exception
self.handle.flush()
if self.logs_to_stdout:
print(message)
def to_timestamp(obj):
if type(obj) is datetime.datetime:
return obj.isoformat()
elif type(obj) is datetime.timedelta:
return obj.total_seconds()
else:
return obj
# set up logging variable
vypr_logger = None
def vypr_output(string):
global vypr_logger
if VYPR_OUTPUT_VERBOSE:
vypr_logger.log(string)
def send_function_call_data(function_name, time_of_call, end_time_of_call, program_path, transaction_time):
"""
Send a function call to the verdict server.
"""
global VERDICT_SERVER_URL
vypr_output("Sending function call data to server...")
# first, send function call data - this will also insert program path data
vypr_output("Function start time was %s" % time_of_call)
vypr_output("Function end time was %s" % end_time_of_call)
call_data = {
"transaction_time": transaction_time.isoformat(),
"time_of_call": time_of_call.isoformat(),
"end_time_of_call": end_time_of_call.isoformat(),
"function_name": function_name,
"program_path": program_path
}
insertion_result = json.loads(requests.post(
os.path.join(VERDICT_SERVER_URL, "insert_function_call_data/"),
data=json.dumps(call_data)
).text)
vypr_output("Function call data sent.")
return insertion_result
def send_verdict_report(verdict_report, property_hash, function_id, function_call_id):
"""
Send verdict data for a given function call.
"""
global VERDICT_SERVER_URL
verdicts = verdict_report.get_final_verdict_report()
vypr_output("Sending verdicts to server...")
# second, send verdict data - all data in one request
# for this, we first build the structure
# that we'll send over HTTP
verdict_dict_list = []
for bind_space_index in verdicts.keys():
verdict_list = verdicts[bind_space_index]
for verdict in verdict_list:
vypr_output("Sending verdict")
vypr_output(verdict)
# remember to deal with datetime objects in json serialisation
verdict_dict = {
"bind_space_index": bind_space_index,
"verdict": [
verdict[0],
verdict[1].isoformat(),
verdict[2],
verdict[3],
verdict[4],
verdict[5],
verdict[6]
]
}
verdict_dict_list.append(verdict_dict)
request_body_dict = {
"function_call_id": function_call_id,
"function_id": function_id,
"verdicts": verdict_dict_list,
"property_hash": property_hash
}
# send request
try:
requests.post(os.path.join(VERDICT_SERVER_URL, "register_verdicts/"),
data=json.dumps(request_body_dict, default=to_timestamp))
except Exception as e:
vypr_output(
"Something went wrong when sending verdict information to the verdict server. The verdict information we "
"tried to send is now lost.")
import traceback
vypr_output(traceback.format_exc())
vypr_output("Verdicts sent.")
def consumption_thread_function(verification_obj):
# the web service has to be considered as running forever, so the monitoring loop for now should also run forever
# this needs to be changed for a clean exit
INACTIVE_MONITORING = False
continue_monitoring = True
while continue_monitoring:
# take top element from the queue
try:
top_pair = verification_obj.consumption_queue.get(timeout=1)
except:
continue
if top_pair[0] == "end-monitoring":
# return from the monitoring function to end the monitoring thread
vypr_output("Returning from monitoring thread.")
continue_monitoring = False
continue
# if monitoring is inactive, we do nothing with what we consume unless it's a resume message
if INACTIVE_MONITORING:
if top_pair[0] == "inactive-monitoring-stop":
# return from the monitoring function to end the monitoring thread
vypr_output("Restarting monitoring. Monitoring thread will still be alive.")
INACTIVE_MONITORING = False
continue
else:
if top_pair[0] == "inactive-monitoring-start":
# return from the monitoring function to end the monitoring thread
vypr_output("Pausing monitoring. Monitoring thread will still be alive.")
# turn on inactive monitoring
INACTIVE_MONITORING = True
# skip to the next iteration of the consumption loop
continue
# if inactive monitoring is off (so monitoring is running), process what we consumed
vypr_output("Consuming: %s" % str(top_pair))
first_element = top_pair[0]
if first_element == "function":
# we have a function start/end instrument
property_hash_list = top_pair[1]
function_name = top_pair[2]
scope_event = top_pair[3]
if scope_event == "end":
# first, send the function call data independently of any property
# the latest time of call and program path are the same everywhere
latest_time_of_call = verification_obj.function_to_maps[function_name][
verification_obj.function_to_maps[function_name].keys()[0]
].latest_time_of_call
program_path = verification_obj.function_to_maps[function_name][
verification_obj.function_to_maps[function_name].keys()[0]
].program_path
insertion_data = send_function_call_data(
function_name,
latest_time_of_call,
top_pair[-1],
program_path,
top_pair[4]
)
# now handle the verdict data we have for each property
for property_hash in property_hash_list:
maps = verification_obj.function_to_maps[function_name][property_hash]
static_qd_to_monitors = maps.static_qd_to_monitors
verdict_report = maps.verdict_report
vypr_output("*" * 50)
# before resetting the qd -> monitor map, go through it to find monitors
# that reached a verdict, and register those in the verdict report
for static_qd_index in static_qd_to_monitors:
for monitor in static_qd_to_monitors[static_qd_index]:
# check if the monitor has a collapsing atom - only then do we register a verdict
if monitor.collapsing_atom_index is not None:
verdict_report.add_verdict(
static_qd_index,
monitor._formula.verdict,
monitor.atom_to_observation,
monitor.atom_to_program_path,
monitor.collapsing_atom_index,
monitor.collapsing_atom_sub_index,
monitor.atom_to_state_dict
)
# reset the monitors
maps.static_qd_to_monitors = {}
# send the verdict data
send_verdict_report(
verdict_report,
property_hash,
insertion_data["function_id"],
insertion_data["function_call_id"]
)
# reset the verdict report
maps.verdict_report.reset()
# reset the function start time for the next time
maps.latest_time_of_call = None
elif scope_event == "start":
vypr_output("Function '%s' has started." % function_name)
for property_hash in property_hash_list:
# reset anything that might have been left over from the previous call,
# especially if an unhandled exception caused the function to end without
# vypr instruments sending an end signal
maps = verification_obj.function_to_maps[function_name][property_hash]
maps.static_qd_to_monitors = {}
maps.verdict_report.reset()
# remember when the function call started
maps.latest_time_of_call = top_pair[4]
vypr_output("Set start time to %s" % maps.latest_time_of_call)
# reset the program path
maps.program_path = []
vypr_output("*" * 50)
else:
# we have another kind of instrument that is specific to a property
property_hash = first_element
# remove the property hash and just deal with the rest of the data
top_pair = top_pair[1:]
instrument_type = top_pair[0]
function_name = top_pair[1]
# get the maps we need for this function
maps = verification_obj.function_to_maps[function_name][property_hash]
static_qd_to_monitors = maps.static_qd_to_monitors
formula_structure = maps.formula_structure
program_path = maps.program_path
verdict_report = maps.verdict_report
atoms = formula_structure._formula_atoms
if instrument_type == "trigger":
# we've received a trigger instrument
vypr_output("Processing trigger - dealing with monitor instantiation")
static_qd_index = top_pair[2]
bind_variable_index = top_pair[3]
vypr_output("Trigger is for bind variable %i" % bind_variable_index)
if bind_variable_index == 0:
vypr_output("Instantiating new, clean monitor")
# we've encountered a trigger for the first bind variable, so we simply instantiate a new monitor
new_monitor = formula_tree.new_monitor(formula_structure.get_formula_instance())
try:
static_qd_to_monitors[static_qd_index].append(new_monitor)
except:
static_qd_to_monitors[static_qd_index] = [new_monitor]
else:
vypr_output("Processing existing monitors")
# we look at the monitors' timestamps, and decide whether to generate a new monitor
# and copy over existing information, or update timestamps of existing monitors
new_monitors = []
subsequences_processed = []
for monitor in static_qd_to_monitors[static_qd_index]:
# check if the monitor's timestamp sequence includes a timestamp for this bind variable
vypr_output(
" Processing monitor with timestamp sequence %s" % str(monitor._monitor_instantiation_time))
if len(monitor._monitor_instantiation_time) == bind_variable_index + 1:
if monitor._monitor_instantiation_time[:bind_variable_index] in subsequences_processed:
# the same subsequence might have been copied and extended multiple times
# we only care about one
continue
else:
subsequences_processed.append(monitor._monitor_instantiation_time[:bind_variable_index])
# construct new monitor
vypr_output(" Instantiating new monitor with modified timestamp sequence")
# instantiate a new monitor using the timestamp subsequence excluding the current bind
# variable and copy over all observation, path and state information
old_instantiation_time = list(monitor._monitor_instantiation_time)
updated_instantiation_time = tuple(
old_instantiation_time[:bind_variable_index] + [datetime.datetime.now()])
new_monitor = formula_tree.new_monitor(formula_structure.get_formula_instance())
new_monitors.append(new_monitor)
# copy timestamp sequence, observation, path and state information
new_monitor._monitor_instantiation_time = updated_instantiation_time
# iterate through the observations stored by the previous monitor
# for bind variables before the current one and use them to update the new monitor
for atom_index in monitor._state._state:
atom = atoms[atom_index]
if not (type(atom) is formula_tree.LogicalNot):
# the copy we do for the information related to the atom
# depends on whether the atom is mixed or not
if formula_tree.is_mixed_atom(atom):
# for mixed atoms, the return value here is a list
base_variables = get_base_variable(atom)
# determine the base variables which are before the current bind variable
relevant_base_variables = filter(
lambda var : (
formula_structure._bind_variables.index(var) < bind_variable_index
),
base_variables
)
# determine the base variables' sub-indices in the current atom
relevant_sub_indices = map(
lambda var : base_variables.index(var),
relevant_base_variables
)
# copy over relevant information for the sub indices
# whose base variables had positions less than the current variable index
# relevant_sub_indices can contain at most 0 and 1.
for sub_index in relevant_sub_indices:
# set up keys in new monitor state if they aren't already there
if not(new_monitor.atom_to_observation.get(atom_index)):
new_monitor.atom_to_observation[atom_index] = {}
new_monitor.atom_to_program_path[atom_index] = {}
new_monitor.atom_to_state_dict[atom_index] = {}
# copy over observation, program path and state information
new_monitor.atom_to_observation[atom_index][sub_index] = \
monitor.atom_to_observation[atom_index][sub_index]
new_monitor.atom_to_program_path[atom_index][sub_index] = \
monitor.atom_to_program_path[atom_index][sub_index]
new_monitor.atom_to_state_dict[atom_index][sub_index] = \
monitor.atom_to_state_dict[atom_index][sub_index]
# update the state of the monitor
new_monitor.check_atom_truth_value(atom, atom_index, atom_sub_index)
else:
# the atom is not mixed, so copying over information is simpler
if (formula_structure._bind_variables.index(
get_base_variable(atom)) < bind_variable_index
and not (monitor._state._state[atom] is None)):
# decide how to update the new monitor based on the existing monitor's truth
# value for it
if monitor._state._state[atom_index] == True:
new_monitor.check_optimised(atom)
elif monitor._state._state[atom_index] == False:
new_monitor.check_optimised(formula_tree.lnot(atom))
# copy over observation, program path and state information
new_monitor.atom_to_observation[atom_index][0] = \
monitor.atom_to_observation[atom_index][0]
new_monitor.atom_to_program_path[atom_index][0] = \
monitor.atom_to_program_path[atom_index][0]
new_monitor.atom_to_state_dict[atom_index][0] = \
monitor.atom_to_state_dict[atom_index][0]
vypr_output(" New monitor construction finished.")
elif len(monitor._monitor_instantiation_time) == bind_variable_index:
vypr_output(" Updating existing monitor timestamp sequence")
# extend the monitor's timestamp sequence
tmp_sequence = list(monitor._monitor_instantiation_time)
tmp_sequence.append(datetime.datetime.now())
monitor._monitor_instantiation_time = tuple(tmp_sequence)
# add the new monitors
static_qd_to_monitors[static_qd_index] += new_monitors
if instrument_type == "path":
# we've received a path recording instrument
# append the branching condition to the program path encountered so far for this function.
program_path.append(top_pair[2])
if instrument_type == "instrument":
static_qd_indices = top_pair[2]
atom_index = top_pair[3]
atom_sub_index = top_pair[4]
instrumentation_point_db_ids = top_pair[5]
observation_time = top_pair[6]
observation_end_time = top_pair[7]
observed_value = top_pair[8]
thread_id = top_pair[9]
try:
state_dict = top_pair[10]
except:
# instrument isn't from a transition measurement
state_dict = None
vypr_output("Consuming data from an instrument in thread %i" % thread_id)
lists = zip(static_qd_indices, instrumentation_point_db_ids)
for values in lists:
static_qd_index = values[0]
instrumentation_point_db_id = values[1]
vypr_output("Binding space index : %i" % static_qd_index)
vypr_output("Atom index : %i" % atom_index)
vypr_output("Atom sub index : %i" % atom_sub_index)
vypr_output("Instrumentation point db id : %i" % instrumentation_point_db_id)
vypr_output("Observation time : %s" % str(observation_time))
vypr_output("Observation end time : %s" % str(observation_end_time))
vypr_output("Observed value : %s" % observed_value)
vypr_output("State dictionary : %s" % str(state_dict))
instrumentation_atom = atoms[atom_index]
# update all monitors associated with static_qd_index
if static_qd_to_monitors.get(static_qd_index):
for (n, monitor) in enumerate(static_qd_to_monitors[static_qd_index]):
# checking for previous observation of the atom is done by the monitor's internal logic
monitor.process_atom_and_value(instrumentation_atom, observation_time, observation_end_time,
observed_value, atom_index, atom_sub_index,
inst_point_id=instrumentation_point_db_id,
program_path=len(program_path), state_dict=state_dict)
# set the task as done
verification_obj.consumption_queue.task_done()
vypr_output("Consumption finished.")
vypr_output("=" * 100)
# temporarily stop closure of the logging file
# if we reach this point, the monitoring thread is ending
#vypr_logger.end_logging()
class PropertyMapGroup(object):
"""
Groups together all the maps needed for verification of a single run of a single function, wrt a single property.
"""
def __init__(self, module_name, function_name, property_hash):
self._function_name = function_name
self._property_hash = property_hash
try:
from VyPR_queries_with_imports import verification_conf
except ImportError:
print("Query file generated by instrumentation couldn't be found. Run VyPR instrumentation first.")
exit()
# to get the property structure,
property_data = json.loads(
requests.get(
os.path.join(VERDICT_SERVER_URL, "get_property_from_hash/%s/" % property_hash)
).text
)
property_index = property_data["index_in_specification_file"]
vypr_output("Queries imported.")
# store all the data we have
self.formula_structure = verification_conf[module_name][function_name.replace(":", ".")][property_index]
self.static_qd_to_monitors = {}
self.static_bindings_to_monitor_states = {}
self.verdict_report = VerdictReport()
self.latest_time_of_call = None
self.program_path = []
def read_configuration(file):
"""
Read in 'file', parse into an object and return.
"""
with open(file, "r") as h:
contents = h.read()
return json.loads(contents)
class Verification(object):
def __init__(self):
"""
Sets up the consumption thread for events from instruments.
"""
global vypr_logger
vypr_logger = MonitoringLog(logs_to_stdout=False)
vypr_logger.start_logging()
vypr_output("VyPR verification object instantiated...")
# read configuration file
inst_configuration = read_configuration("vypr.config")
global VERDICT_SERVER_URL, VYPR_OUTPUT_VERBOSE, PROJECT_ROOT, VYPR_MODULE
VERDICT_SERVER_URL = inst_configuration.get("verdict_server_url") if inst_configuration.get(
"verdict_server_url") else "http://localhost:9001/"
VYPR_OUTPUT_VERBOSE = inst_configuration.get("verbose") if inst_configuration.get("verbose") else True
PROJECT_ROOT = inst_configuration.get("project_root") if inst_configuration.get("project_root") else ""
VYPR_MODULE = inst_configuration.get("vypr_module") if inst_configuration.get("vypr_module") else ""
# try to connect to the verdict server before we set anything up
try:
requests.get(VERDICT_SERVER_URL)
self.initialisation_failure = False
except Exception:
vypr_output("Couldn't connect to the verdict server at '%s'. Initialisation failed." % VERDICT_SERVER_URL)
self.initialisation_failure = True
return
self.machine_id = ("%s-" % inst_configuration.get("machine_id")) if inst_configuration.get(
"machine_id") else ""
# check if there's an NTP server given that we should use for time
self.ntp_server = inst_configuration.get("ntp_server")
if self.ntp_server:
print("Setting time based on NTP server '%s'." % self.ntp_server)
# set two timestamps - the local time, and the ntp server time, from the same instant
import ntplib
client = ntplib.NTPClient()
try:
response = client.request(self.ntp_server)
# set the local start time
self.local_start_time = datetime.datetime.utcfromtimestamp(response.orig_time)
# compute the delay due to network latency to reach the ntp server
adjustment = (response.dest_time - response.orig_time) / 2
# set the ntp start time by subtracting the adjustment from the time given by the ntp server
# this works because 'adjustment' is approximately the time elapsed
# between the first time we measure local time and when the ntp server measures its own time
# so by subtracting this difference we adjust the ntp server time to the same instant
# as the local time
self.ntp_start_time = datetime.datetime.utcfromtimestamp(response.tx_time - adjustment)
except:
vypr_output("Couldn't set time based on NTP server '%s'." % self.ntp_server)
print("Couldn't set time based on NTP server '%s'." % self.ntp_server)
exit()
# set up the maps that the monitoring algorithm that the consumption thread runs
# first get the list of monitored functions and prepare it for iteration
function_to_properties_map = json.loads(
requests.get(
os.path.join(VERDICT_SERVER_URL, "get_function_property_pairs/")
).text
)
self.function_to_maps = {}
for monitored_function in function_to_properties_map:
tokens = monitored_function.split(".")
module = ".".join(tokens[:-1])
function = tokens[-1]
for property_hash in function_to_properties_map[monitored_function]:
vypr_output("Setting up monitoring state for module/function/property triple %s, %s, %s" % (
module, function, property_hash))
module_function_string = "%s%s" % (self.machine_id, monitored_function)
if not (self.function_to_maps.get(module_function_string)):
self.function_to_maps[module_function_string] = {}
self.function_to_maps[module_function_string][property_hash] = PropertyMapGroup(module, function,
property_hash)
vypr_output(self.function_to_maps)
def initialise(self, flask_object):
vypr_output("Initialising VyPR alongside service.")
if flask_object:
if VYPR_MODULE != "":
# if a VyPR module is given, this means VyPR will be running between requests
def prepare_vypr():
import datetime
from app import vypr
# this function runs inside a request, so flask.g exists
# we store just the request time
flask.g.request_time = vypr.get_time()
flask_object.before_request(prepare_vypr)
# add VyPR end points - we may use this for statistics collection on the server
# add the safe exist end point
@flask_object.route("/vypr/stop-monitoring/")
def endpoint_stop_monitoring():
from app import vypr
# send end-monitoring message
vypr.end_monitoring()
# wait for the thread to end
vypr.consumption_thread.join()
return "VyPR monitoring thread exited. The server must be restarted to turn monitoring back on.\n"
@flask_object.route("/vypr/pause-monitoring/")
def endpoint_pause_monitoring():
from app import vypr
vypr.pause_monitoring()
return "VyPR monitoring paused - thread is still running.\n"
@flask_object.route("/vypr/resume-monitoring/")
def endpoint_resume_monitoring():
from app import vypr
vypr.resume_monitoring()
return "VyPR monitoring resumed.\n"
vypr_output("Setting up monitoring thread.")
# setup consumption queue and store it globally across requests
self.consumption_queue = Queue()
# setup consumption thread
self.consumption_thread = threading.Thread(
target=consumption_thread_function,
args=[self]
)
self.consumption_thread.start()
else:
# if no VyPR module is given, this means VyPR will have to run per request
def start_vypr():
# setup consumption queue and store it within the request context
from flask import g
self.consumption_queue = Queue()
# setup consumption thread
self.consumption_thread = threading.Thread(
target=consumption_thread_function,
args=[self]
)
# store vypr object in request context
g.vypr = self
self.consumption_thread.start()
g.request_time = g.vypr.get_time()
flask_object.before_request(start_vypr)
# set up tear down function
def stop_vypr(e):
# send kill message to consumption thread
# for now, we don't wait for the thread to end
from flask import g
g.vypr.end_monitoring()
flask_object.teardown_request(stop_vypr)
vypr_output("VyPR monitoring initialisation finished.")
def get_time(self, callee=""):
"""
Returns either the machine local time, or the NTP time (using the initial NTP time
obtained when VyPR started up, so we don't query an NTP server everytime we want to measure time).
The result is in UTC.
:return: datetime.datetime object
"""
if self.ntp_server:
vypr_output("Getting time based on NTP.")
current_local_time = datetime.datetime.utcnow()
# compute the time elapsed since the start
difference = current_local_time - self.local_start_time
# add that time to the ntp time obtained at the start
current_ntp_time = self.ntp_start_time + difference
return current_ntp_time
else:
vypr_output("Getting time based on local machine - %s" % callee)
return datetime.datetime.utcnow()
def send_event(self, event_description):
if not (self.initialisation_failure):
self.consumption_queue.put(event_description)
def end_monitoring(self):
if not (self.initialisation_failure):
vypr_output("Ending VyPR monitoring thread.")
self.consumption_queue.put(("end-monitoring",))
def pause_monitoring(self):
if not (self.initialisation_failure):
vypr_output("Sending monitoring pause message.")
self.consumption_queue.put(("inactive-monitoring-start",))
def resume_monitoring(self):
if not (self.initialisation_failure):
vypr_output("Sending monitoring resume message.")
self.consumption_queue.put(("inactive-monitoring-stop",))