From 744879ce16f89ca959216d499e6edd9af28adeab Mon Sep 17 00:00:00 2001 From: Dmitry Volodin Date: Wed, 26 Feb 2020 10:52:22 +0300 Subject: [PATCH] #1263 bi managedobject: full link methods detalisation --- bi/models/managedobjects.py | 15 +++++-- core/etl/bi/extractor/managedobject.py | 54 ++++++++++++++++++-------- 2 files changed, 49 insertions(+), 20 deletions(-) diff --git a/bi/models/managedobjects.py b/bi/models/managedobjects.py index 696d4496c3..8f241d1a91 100644 --- a/bi/models/managedobjects.py +++ b/bi/models/managedobjects.py @@ -74,11 +74,20 @@ class ManagedObject(Model): # Topology n_neighbors = Int32Field(description=_("Neighbors")) n_links = Int32Field(description=_("Links count")) - nri_links = Int32Field(description=_("Links (NRI)")) + bfd_links = Int32Field(description=_("Links (BFD)")) + cdp_links = Int32Field(description=_("Links (CDP)")) + fdp_links = Int32Field(description=_("Links (FDP)")) + huawei_ndp_links = Int32Field(description=_("Links (Huawei NDP)")) + lacp_links = Int32Field(description=_("Links (LACP)")) + lldp_links = Int32Field(description=_("Links (LLDP)")) mac_links = Int32Field(description=_("Links (MAC)")) + nri_links = Int32Field(description=_("Links (NRI)")) + oam_links = Int32Field(description=_("Links (OAM)")) + rep_links = Int32Field(description=_("Links (REP)")) stp_links = Int32Field(description=_("Links (STP)")) - lldp_links = Int32Field(description=_("Links (LLDP)")) - cdp_links = Int32Field(description=_("Links (CDP)")) + udld_links = Int32Field(description=_("Links (UDLD)")) + xmac_links = Int32Field(description=_("Links (xMAC)")) + other_links = Int32Field(description=_("Links (xMAC)")) # Capabilities has_stp = BooleanField(description=_("Has STP")) has_lldp = BooleanField(description=_("Has LLDP")) diff --git a/core/etl/bi/extractor/managedobject.py b/core/etl/bi/extractor/managedobject.py index d25ceb9b16..39d169827f 100644 --- a/core/etl/bi/extractor/managedobject.py +++ b/core/etl/bi/extractor/managedobject.py @@ -2,7 +2,7 @@ # ---------------------------------------------------------------------- # Managed Object Extractor # ---------------------------------------------------------------------- -# Copyright (C) 2007-2019 The NOC Project +# Copyright (C) 2007-2020 The NOC Project # See LICENSE for details # ---------------------------------------------------------------------- @@ -14,6 +14,7 @@ from collections import defaultdict # Third-party modules from pymongo import ReadPreference from mongoengine.queryset.visitor import Q +import six # NOC modules from .base import BaseExtractor @@ -47,6 +48,23 @@ class ManagedObjectsExtractor(BaseExtractor): "SNMP | v2c": "has_snmp_v2c", } + # Link discovery method to field mapping + LD_MAP = { + "bfd": "bfd_links", + "cdp": "cdp_links", + "fdp": "fdp_links", + "huawei_ndp": "huawei_ndp_links", + "lacp": "lacp_links", + "lldp": "lldp_links", + "mac": "mac_links", + "nri": "nri_links", + "oam": "oam_links", + "rep": "rep_links", + "stp": "stp_links", + "udld": "udld_links", + "xmac": "xmac_links", + } + def __init__(self, prefix, start, stop): super(ManagedObjectsExtractor, self).__init__(prefix, start, stop) self.mo_stream = Stream(ManagedObjectBI, prefix) @@ -122,31 +140,33 @@ class ManagedObjectsExtractor(BaseExtractor): Build discovery method summary :return: """ + + def link_data(mo): + links_left = t[mo] + ld = { + "n_neighbors": len(neighbors[mo]) - 1, + "n_links": links_left, + } + for lm, field in six.iteritems(self.LD_MAP): + n = r.get((mo, lm), 0) + ld[field] = n + links_left -= n + ld["other_links"] = links_left + return ld + t = defaultdict(int) # object -> count r = defaultdict(int) # object_id, method -> count neighbors = defaultdict(set) # object_id -> {objects} - for d in Link._get_collection().find(): + for d in Link._get_collection().find( + {}, {"_id": 0, "discovery_method": 1, "linked_objects": 1} + ): method = d.get("discovery_method") linked = d.get("linked_objects", []) for o in linked: r[o, method] += 1 t[o] += 1 neighbors[o].update(linked) - return dict( - ( - o, - { - "n_neighbors": len(neighbors[o]), - "n_links": t[o], - "nri_links": r[o, "nri"], - "mac_links": r[o, "mac"], - "stp_links": r[o, "stp"], - "lldp_links": r[o, "lldp"], - "cdp_links": r[o, "cdp"], - }, - ) - for o in t - ) + return {o: link_data(o) for o in t} def get_interfaces(self): """ -- GitLab