def get_interface_counter_graph_url(interface, timeframe='day', kind='Octets'): """Returns a Graphite graph render URL for an interface traffic graph""" def _get_target(direction): assert direction.lower() in ('in', 'out') path = metric_path_for_interface( interface.netbox.sysname, interface.ifname, 'if{0}{1}'.format(direction.capitalize(), kind) ) meta = get_metric_meta(path) return meta['target'], meta.get('unit', None) (out_series, unit), (in_series, unit) = [ _get_target(d) for d in ('out', 'in')] in_series = 'alias(color(stacked({0}),"00ff00cc"),"In")'.format(in_series) out_series = 'alias({0},"Out")'.format(out_series) titlemap = dict(octets='Traffic on {shortname}:{ifname} {ifalias}', errors='Errors on {shortname}:{ifname} {ifalias}', ucastpkts='Unicast packets on {shortname}:{ifname}', discards='Discarded packets on {shortname}:{ifname}') title = titlemap.get(kind.lower(), '{ifname}').format( ifname=interface.ifname, ifalias=(u"(%s)" % interface.ifalias) if interface.ifalias else u'', sysname=interface.netbox.sysname, shortname=interface.netbox.get_short_sysname(), ) return get_simple_graph_url( [in_series, out_series], "1" + timeframe, title=title, width=597, height=251, vtitle=unit or '')
def get_context_data(self, *args, **kwargs): context = super(AlertWidget, self).get_context_data(**kwargs) self.title = self.preferences.get('title', 'Alert') metric = self.preferences.get('metric') try: sensorid = int(self.preferences.get('sensor', 0)) except ValueError: sensorid = None if not metric and sensorid: metric = Sensor.objects.get(pk=sensorid).get_metric_name() self.preferences['metric'] = metric if self.mode == NAVLET_MODE_EDIT: if metric: form = AlertWidgetForm(self.preferences) else: form = AlertWidgetForm() context['form'] = form else: if not metric: return context context['data_url'] = get_simple_graph_url(metric, time_frame='10minutes', format='json') context.update(self.preferences) return context
def get_vlan_graph_url(vlanid, family=4, timeframe="day"): """Returns a Graphite graph render URL for a VLAN""" vlan = get_object_or_404(Vlan, pk=vlanid) try: family = int(family) except ValueError: family = 4 extra = {'where': ['family(netaddr) = %s' % family]} prefixes = sorted( vlan.prefix_set.all().extra(**extra), key=methodcaller('get_prefix_size'), reverse=True, ) if not prefixes: return None metrics = _vlan_metrics_from_prefixes(prefixes, family) return get_simple_graph_url( metrics, "1" + timeframe, title="Total IPv{0} addresses on VLAN {1}".format(family, vlan), width=597, height=251, )
def create_prefix_graph(request, prefixid): """Returns a Graphite graph render URL for this prefix""" prefix = get_object_or_404(Prefix, pk=prefixid) path = partial(metric_path_for_prefix, prefix.net_address) ip_count = ( 'alpha(' 'color(cactiStyle(alias(stacked({0}), "IP addresses ")), "green"),' '0.8)' ).format(path('ip_count')) ip_range = 'color(cactiStyle(alias({0}, "Max addresses")), "red")'.format( path('ip_range') ) mac_count = 'color(cactiStyle(alias({0}, "MAC addresses")), "blue")'.format( path('mac_count') ) metrics = [ip_count, mac_count] if IP(prefix.net_address).version() == 4: metrics.append(ip_range) timeframe = "1" + request.GET.get('timeframe', 'day') url = get_simple_graph_url( metrics, timeframe, title=prefix.net_address, width=397, height=201 ) if url: return redirect(url) else: return HttpResponse(status=500)
def create_prefix_graph(request, prefixid): """Returns a Graphite graph render URL for this prefix""" prefix = get_object_or_404(Prefix, pk=prefixid) path = partial(metric_path_for_prefix, prefix.net_address) ip_count = ( 'alpha(' 'color(cactiStyle(alias(stacked({0}), "IP addresses ")), "green"),' '0.8)' ).format(path('ip_count')) ip_range = 'color(cactiStyle(alias({0}, "Max addresses")), "red")'.format( path('ip_range')) mac_count = 'color(cactiStyle(alias({0}, "MAC addresses")), "blue")'.format( path('mac_count')) metrics = [ip_count, mac_count] if IP(prefix.net_address).version() == 4: metrics.append(ip_range) timeframe = "1" + request.GET.get('timeframe', 'day') url = get_simple_graph_url(metrics, timeframe, title=prefix.net_address, width=397, height=201) if url: return redirect(url) else: return HttpResponse(status=500)
def get_graphite_render_url(request, metric=None): """Redirect to graphite graph based on request data""" if metric: return redirect(get_simple_graph_url( metric, time_frame='1' + request.REQUEST.get('timeframe', 'w'))) else: return HttpResponse(status=400)
def get_interface_counter_graph_url(interface, timeframe='day', kind='Octets'): """Returns a Graphite graph render URL for an interface traffic graph""" def _get_target(direction): assert direction.lower() in ('in', 'out') path = metric_path_for_interface( interface.netbox.sysname, interface.ifname, 'if{0}{1}'.format(direction.capitalize(), kind)) meta = get_metric_meta(path) return meta['target'], meta.get('unit', None) (out_series, unit), (in_series, unit) = [_get_target(d) for d in ('out', 'in')] in_series = 'alias(color(stacked({0}),"00ff00cc"),"In")'.format(in_series) out_series = 'alias({0},"Out")'.format(out_series) titlemap = dict(octets='Traffic on {shortname}:{ifname} {ifalias}', errors='Errors on {shortname}:{ifname} {ifalias}', ucastpkts='Unicast packets on {shortname}:{ifname}', discards='Discarded packets on {shortname}:{ifname}') title = titlemap.get(kind.lower(), '{ifname}').format( ifname=interface.ifname, ifalias=(u"(%s)" % interface.ifalias) if interface.ifalias else u'', sysname=interface.netbox.sysname, shortname=interface.netbox.get_short_sysname(), ) return get_simple_graph_url([in_series, out_series], "1" + timeframe, title=title, width=597, height=251, vtitle=unit or '')
def sensor_details(request, identifier): """Controller for getting sensor info""" sensor = get_object_or_404(Sensor, pk=identifier) if request.method == 'POST': if sensor.unit_of_measurement == sensor.UNIT_TRUTHVALUE: form = BooleanSensorForm(request.POST) if form.is_valid(): data = form.cleaned_data sensor.on_message_user = data['on_message'] sensor.off_message_user = data['off_message'] sensor.on_state_user = data['on_state'] sensor.alert_type = data['alert_type'] sensor.save() return redirect(request.path) else: form = SensorRangesForm(request.POST) if form.is_valid(): sensor.display_minimum_user = form.cleaned_data['minimum'] sensor.display_maximum_user = form.cleaned_data['maximum'] sensor.save() return redirect(request.path) netbox_sysname = sensor.netbox.sysname navpath = NAVPATH + [ (netbox_sysname, reverse('ipdevinfo-details-by-name', kwargs={'name': netbox_sysname})), ('Sensor Details',)] heading = title = 'Sensor details: ' + six.text_type(sensor) metric = dict(id=sensor.get_metric_name()) find_rules([metric]) if sensor.unit_of_measurement == sensor.UNIT_TRUTHVALUE: form = BooleanSensorForm(initial={ 'on_message': sensor.on_message, 'off_message': sensor.off_message, 'on_state': sensor.on_state, 'alert_type': sensor.alert_type, }) else: form = SensorRangesForm(initial={ 'minimum': sensor.get_display_range()[0], 'maximum': sensor.get_display_range()[1], }) return render(request, 'ipdevinfo/sensor-details.html', { 'data_url': get_simple_graph_url( sensor.get_metric_name(), time_frame='10minutes', format='json'), 'sensor': sensor, 'navpath': navpath, 'heading': heading, 'title': title, 'metric': metric, 'form': form, 'graphite_data_url': Graph(magic_targets=[sensor.get_metric_name()], format='json') })
def get_graphite_render_url(request, metric=None): """Redirect to graphite graph based on request data""" if metric: return redirect( get_simple_graph_url(metric, time_frame='1' + request.REQUEST.get('timeframe', 'w'))) else: return HttpResponse(status=400)
def get_graph_url(request): """Get graph url based on metric""" graph_args = { 'title': 'Latest values for this metric', 'width': 600, 'height': 400 } if 'metric' in request.GET: metric = request.GET['metric'] if 'raw' in request.GET: graph = Graph(targets=[metric], **graph_args) return redirect(unicode(graph)) else: return redirect(get_simple_graph_url([metric], **graph_args)) return HttpResponse()
def get_vlan_graph_url(vlanid, family=4, timeframe="day"): """Returns a Graphite graph render URL for a VLAN""" vlan = get_object_or_404(Vlan, pk=vlanid) try: family = int(family) except ValueError: family = 4 extra = {'where': ['family(netaddr) = %s' % family]} prefixes = sorted(vlan.prefix_set.all().extra(**extra), key=methodcaller('get_prefix_size'), reverse=True) if not prefixes: return None metrics = _vlan_metrics_from_prefixes(prefixes, family) return get_simple_graph_url( metrics, "1" + timeframe, title="Total IPv{0} addresses on VLAN {1}".format(family, vlan), width=597, height=251)
def _get_prefix_url(prefix): return get_simple_graph_url( [metric_path_for_prefix(prefix.strCompressed(), 'ip_count')], format='json')