def getips(request): """ Return the IPs and interfaces """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') try: get_ips = get_ipaddress() except Exception: get_ips = None data = json.dumps(get_ips['itfip']) response = HttpResponse() response['Content-Type'] = "text/javascript" response.write(data) return response
def gettraffic(request): """ Return the traffic for the interface """ if not request.user.is_authenticated(): return HttpResponseRedirect('/login') datasets_in = [] datasets_in_i = [] datasets_out = [] datasets_out_o = [] cookie_traffic = {} try: intf = get_ipaddress() intf = intf['interface'][0] traffic = get_traffic(intf) except Exception: traffic = 0 try: cookies = request._cookies['traffic'] except Exception: cookies = None if not cookies: datasets_in.append(0) datasets_in_i.append(0) datasets_out.append(0) datasets_out_o.append(0) else: datasets = eval(cookies) datasets_in = datasets[0] datasets_out = datasets[1] datasets_in_i = datasets[2] datasets_out_o = datasets[3] if len(datasets_in) > 10: while datasets_in: del datasets_in[0] if len(datasets_in) == 10: break if len(datasets_in_i) > 2: while datasets_in_i: del datasets_in_i[0] if len(datasets_in_i) == 2: break if len(datasets_out) > 10: while datasets_out: del datasets_out[0] if len(datasets_out) == 10: break if len(datasets_out_o) > 2: while datasets_out_o: del datasets_out_o[0] if len(datasets_out_o) == 2: break if len(datasets_in_i) <= 1: datasets_in_i.append(float(traffic['traffic_in'])) if len(datasets_in_i) == 2: datasets_in_i.append(float(traffic['traffic_in'])) del datasets_in_i[0] if len(datasets_out_o) <= 1: datasets_out_o.append(float(traffic['traffic_out'])) if len(datasets_out_o) == 2: datasets_out_o.append(float(traffic['traffic_out'])) del datasets_out_o[0] if len(datasets_in) <= 9: datasets_in.append( float(((datasets_in_i[1] - datasets_in_i[0]) / 1024) / (time_refresh_net / 1000))) if len(datasets_in) == 10: datasets_in.append( float(((datasets_in_i[1] - datasets_in_i[0]) / 1024) / (time_refresh_net / 1000))) del datasets_in[0] if len(datasets_out) <= 9: datasets_out.append( float(((datasets_out_o[1] - datasets_out_o[0]) / 1024) / (time_refresh_net / 1000))) if len(datasets_out) == 10: datasets_out.append( float(((datasets_out_o[1] - datasets_out_o[0]) / 1024) / (time_refresh_net / 1000))) del datasets_out[0] # Some fix division by 0 Chart.js if len(datasets_in) == 10: if sum(datasets_in) == 0: datasets_in[9] += 0.1 if sum(datasets_in) / 10 == datasets_in[0]: datasets_in[9] += 0.1 traff = { 'labels': ["KBps"] * 10, 'datasets': [ { "fillColor": "rgba(105,210,231,0.5)", "strokeColor": "rgba(105,210,231,1)", "pointColor": "rgba(105,210,231,1)", "pointStrokeColor": "#fff", "data": datasets_in }, { "fillColor": "rgba(227,48,81,0.5)", "strokeColor": "rgba(227,48,81,1)", "pointColor": "rgba(227,48,81,1)", "pointStrokeColor": "#fff", "data": datasets_out } ] } cookie_traffic = [datasets_in, datasets_out, datasets_in_i, datasets_out_o] data = json.dumps(traff) response = HttpResponse() response['Content-Type'] = "text/javascript" response.cookies['traffic'] = cookie_traffic response.write(data) return response