diff --git a/flowspec/snmpstats.py b/flowspec/snmpstats.py index 0fe7c639ee3aba203c4b0684da7ff6c83bc56bca..75fbb2b76e565f98d6b8c1765c0c889c64f61eee 100644 --- a/flowspec/snmpstats.py +++ b/flowspec/snmpstats.py @@ -167,11 +167,22 @@ def load_history(): try: with open(settings.SNMP_TEMP_FILE, "r") as f: history = json.load(f) + f.close() except: logger.info("There is no file with SNMP historical data.") pass return history +# TODO: need locking for ro access? +def get_last_msrm_delay_time(): + last_msrm_delay_time = "" + try: + history = load_history() + last_msrm_delay_time = history['_last_msrm_delay_time'] + except Exception as e: + logger.info("get_last_msrm_delay_time(): got exception: "+str(e)) + return last_msrm_delay_time + def save_history(history, nowstr): # store updated history tf = settings.SNMP_TEMP_FILE + "." + nowstr @@ -218,6 +229,9 @@ def poll_snmp_statistics(): # load history history = load_history() + + now2 = datetime.now() + msrm_delay_time = now2 - now zero_measurement = { "bytes" : 0, "packets" : 0 } null_measurement = 0 @@ -228,8 +242,10 @@ def poll_snmp_statistics(): except Exception as e: logger.info("poll_snmp_statistics(): got exception while trying to access history[_last_poll_time]: "+str(e)) last_poll_no_time=None + logger.info("poll_snmp_statistics(): snmpstats: msrm_delay_time="+str(msrm_delay_time)) logger.info("poll_snmp_statistics(): snmpstats: last_poll_no_time="+str(last_poll_no_time)) history['_last_poll_no_time']=nowstr + history['_last_msrm_delay_time']=str(msrm_delay_time) try: history_per_rule = history['_per_rule'] @@ -255,7 +271,7 @@ def poll_snmp_statistics(): toremove = [] for rule in history: try: - if rule!='_last_poll_no_time' and rule!="_per_rule": + if len(rule)>0 and rule[0]!='_': #ts = datetime.strptime(history[rule][0]["ts"], '%Y-%m-%dT%H:%M:%S.%f') ts = helper_stats_store_parse_ts(history[rule][0]["ts"]) if ts!=None and (now - ts).total_seconds() >= settings.SNMP_REMOVE_RULES_AFTER: @@ -268,7 +284,7 @@ def poll_snmp_statistics(): if settings.STATISTICS_PER_MATCHACTION_ADD_FINAL_ZERO == True: # for now workaround for low-level rules (by match params, not FoD rule id) no longer have data, typically because of haveing been deactivated for rule in history: - if rule!='_last_poll_no_time' and rule!="_per_rule": + if len(rule)>0 and rule[0]!='_': ts = history[rule][0]["ts"] if ts!=nowstr and ts==last_poll_no_time: counter = {"ts": nowstr, "value": null_measurement } diff --git a/flowspec/views.py b/flowspec/views.py index fe224efefaba3d7beb27440f8f373284bc7aeb69..78df4d6be783403aa94a494a4d0f18c1198ea1a0 100644 --- a/flowspec/views.py +++ b/flowspec/views.py @@ -52,6 +52,8 @@ from flowspec.helpers import send_new_mail, get_peer_techc_mails import datetime import os +from flowspec.snmpstats import load_history, get_last_msrm_delay_time + LOG_FILENAME = os.path.join(settings.LOG_FILE_LOCATION, 'celery_jobs.log') # FORMAT = '%(asctime)s %(levelname)s: %(message)s' # logging.basicConfig(format=FORMAT) @@ -833,9 +835,11 @@ def routedetails(request, route_slug): route = get_object_or_404(Route, name=route_slug) #return render(request, 'flowspy/route_details.html', {'route': route}) now = datetime.datetime.now() + last_msrm_delay_time = get_last_msrm_delay_time() return render(request, 'flowspy/route_details.html', { 'route': route, 'mytime': now, + 'last_msrm_delay_time': last_msrm_delay_time, 'tz' : settings.TIME_ZONE, 'route_comments_len' : len(str(route.comments)) }) @@ -847,9 +851,10 @@ def routestats(request, route_slug): import time res = {} try: - with open(settings.SNMP_TEMP_FILE, "r") as f: - res = json.load(f) - f.close() + #with open(settings.SNMP_TEMP_FILE, "r") as f: + # res = json.load(f) + #f.close() + res = load_history() routename = create_junos_name(route) route_id = str(route.id) if not res: diff --git a/templates/flowspy/route_details.html b/templates/flowspy/route_details.html index dc24a79203a14de1cc9c2767631e73c051130b1b..3558476b111190d2f8acf478c47d7dd35a9eec27 100644 --- a/templates/flowspy/route_details.html +++ b/templates/flowspy/route_details.html @@ -84,7 +84,7 @@ function myreloadPage() { </div> <div> <h2>Statistics</h2> - <div>(all times are in {{ tz }}; current System time: {{ mytime|date:'Y-m-d H:i' }}, active rules will be updated every 5 minutes)</div> + <div>(all times are in {{ tz }}; current System time: {{ mytime|date:'Y-m-d H:i' }}, active rules will be updated every 5 minutes, last duration for measurement was {{ last_msrm_delay_time }})</div> <div><span id="traffic-plot-loading">(Loading data...)</span> <h3>Number of packets (absolute)</h3> <div><canvas id="traffic-plot-pkts-abs" width=200 height=200></canvas></div>