Beispiel #1
0
    def execute_snmp(self):
        interfaces = []
        try:
            ifindex = self.snmp.get("1.3.6.1.2.1.2.2.1.1.1")
            name = self.snmp.get(mib["IF-MIB::ifDescr", ifindex])
            mac = self.snmp.get(mib["IF-MIB::ifPhysAddress", ifindex])
            a_status = self.snmp.get(mib["IF-MIB::ifAdminStatus", ifindex])
            o_status = self.snmp.get(mib["IF-MIB::ifOperStatus", ifindex])
            interfaces += [
                {
                    "type": "physical",
                    "name": name,
                    "mac": mac,
                    "admin_status": True if a_status == 7 else False,
                    "oper_status": True if o_status == 1 else False,
                    "subinterfaces": [],
                }
            ]
        except Exception:
            mac = self.scripts.get_chassis_id()[0].get("first_chassis_mac")
            interfaces += [
                {
                    "type": "physical",
                    "name": "st",
                    "mac": mac if mac else None,
                    "admin_status": True,
                    "oper_status": True,
                    "subinterfaces": [],
                }
            ]

        for index in self.profile.PORT_TYPE.keys():
            s_status = 0
            status = self.snmp.get("1.3.6.1.4.1.41752.5.15.1.%s.0" % index)
            if index == 1 and int(status) == 0:
                s_status = 1
            elif index == 2:
                if is_float(status) and (-55 < float(status) < 600):
                    s_status = 1
            elif index in [4, 6] and float(status) > 0:
                s_status = 1
            elif index == 9 and int(status) != 2:
                s_status = 1
            interfaces += [
                {
                    "type": "physical",
                    "name": self.profile.IFACE_NAME.get(index),
                    "admin_status": s_status,
                    "oper_status": s_status,
                    "snmp_ifindex": index,
                    "description": self.profile.PORT_TYPE.get(index),
                    "subinterfaces": [],
                }
            ]

        return [{"interfaces": interfaces}]
Beispiel #2
0
 def execute(self):
     if self.has_snmp():
         try:
             su = self.snmp.get("1.3.6.1.4.1.27514.%s" % self.check_oid())
             # DES-1210-28/ME/B3 fw 10.04.B020 return 'VLAN-1002'
             if is_float(su):
                 return float(su) // 100.0
         except (self.snmp.TimeOutError, self.snmp.SNMPError):
             pass
     return None
Beispiel #3
0
 def execute(self):
     if self.has_snmp():
         try:
             su = self.snmp.get(mib["SNMPv2-MIB::sysUpTime", 0])
             # DES-1210-28/ME/B3 fw 10.04.B020 return 'VLAN-1002'
             if is_float(su):
                 return float(su) / 100.0
         except (self.snmp.TimeOutError, self.snmp.SNMPError):
             pass
     return None
Beispiel #4
0
 def get_current_input(self, metrics):
     for metric in metrics:
         if self.is_lite:
             value = self.snmp.get("1.3.6.1.4.1.27514.103.0.28.0")
         else:
             value = self.snmp.get("1.3.6.1.4.1.27514.102.0.21")
         if is_float(value) or is_int(value):
             self.set_metric(
                 id=("Environment | Electric Current", metric.labels),
                 value=value,
                 scale=scale(0.01),
             )
Beispiel #5
0
def handler_object(mo, event):
    try:
        event["threshold_interval"] = th_interval(mo, event)
        event["ts_from_date"] = grafana_date()
        res = event["path"].split("|")
        if len(res) > 2:
            event["name"] = res[-1::][0].split("::")[-1]
        if is_float(event["value"]):
            event["value"] = round(float(event["value"]), 2)
        return event
    except Exception as e:
        logger.info("Error: %s \n %s", e, event["path"].split("|")[-1::][0].split("::")[-1].strip())
        return event
Beispiel #6
0
 def get_temperature(self, metrics):
     for metric in metrics:
         port = metric.labels[0].rsplit("::", 1)[-1]
         if "temp" in port:
             value = self.snmp.get("1.3.6.1.4.1.41752.5.15.1.%s.0" %
                                   metric.ifindex)
             if is_float(value):
                 self.set_metric(
                     id=("Environment | Temperature", metric.labels),
                     labels=[f"noc::module::{port}", f"noc::name::{port}"],
                     value=value,
                     multi=True,
                 )
Beispiel #7
0
 def get_current_input(self, metrics):
     for metric in metrics:
         if self.is_lite:
             value = self.snmp.get("1.3.6.1.4.1.27514.103.0.28.0")
         else:
             value = self.snmp.get("1.3.6.1.4.1.27514.102.0.21")
         port = metric.labels[0].rsplit("::", 1)[-1]
         if is_float(value) or is_int(value):
             self.set_metric(
                 id=("Environment | Electric Current", metric.labels),
                 labels=[f"noc::sensor::{port}"],
                 value=value,
                 scale=scale(0.01),
             )
Beispiel #8
0
 def get_sensor_status(self, metrics):
     for metric in metrics:
         port = metric.labels[0].rsplit("::", 1)[-1]
         if "st" in port:
             continue
         value = 1
         status = self.snmp.get("1.3.6.1.4.1.41752.5.15.1.%s.0" %
                                metric.ifindex)
         if metric.ifindex == 1 and int(status) == 0:
             value = 0
         elif metric.ifindex == 2:
             if is_float(status) and (-55 < float(status) < 600):
                 value = 0
         elif metric.ifindex in [4, 6] and float(status) > 0:
             value = 0
         elif metric.ifindex == 9 and int(status) != 2:
             value = 0
         self.set_metric(
             id=("Environment | Sensor Status", metric.labels),
             value=value,
         )
Beispiel #9
0
    def execute_snmp(self, interfaces=None):
        result = []
        try:
            ifindex = self.snmp.get("1.3.6.1.2.1.2.2.1.1.1")
            name = self.snmp.get(mib["IF-MIB::ifDescr", ifindex])
            a_status = self.snmp.get(mib["IF-MIB::ifAdminStatus", ifindex])
            o_status = self.snmp.get(mib["IF-MIB::ifOperStatus", ifindex])
            result += [{
                "interface": name,
                "admin_status": a_status,
                "oper_status": o_status
            }]
        except Exception:
            result += [{
                "interface": "st",
                "admin_status": True,
                "oper_status": True
            }]
        for index in self.profile.PORT_TYPE.keys():
            s_status = 0
            status = self.snmp.get("1.3.6.1.4.1.41752.5.15.1.%s.0" % index)
            if index == 1 and int(status) == 0:
                s_status = 1
            elif index == 2:
                if is_float(status) and (-55 < float(status) < 600):
                    s_status = 1
            elif index in [4, 6] and float(status) > 0:
                s_status = 1
            elif index == 9 and int(status) != 2:
                s_status = 1
            result += [{
                "interface": self.profile.IFACE_NAME.get(index),
                "admin_status": s_status,
                "oper_status": s_status,
            }]

        return result
Beispiel #10
0
def handler(mo, event):
    metric = MetricType.objects.get(name=event["metric"])
    iface_name = event["path"].split("|")[-1::][0].split("::")[-1].strip()
    iface = Interface.objects.get(name=iface_name, managed_object=mo.object)
    try:
        event["interface"] = iface_name
        if iface.description:
            event["description"] = str(iface.description)
        event["profile"] = str(iface.profile.name)
        event["threshold_interval"] = th_interval(mo, event)
        event["ts_from_date"] = grafana_date()
        if "Load" in event["metric"]:
            if iface.in_speed and iface.out_speed:
                if "In" in event["metric"]:
                    iface_speed = iface.in_speed
                else:
                    iface_speed = iface.out_speed
                event["percent"] = round((100.0 / int(iface_speed)) * ((event["value"]) / 1000))
            event["convert_value"] = humanize_speed(event["value"], metric.measure)
        if "Duplex" in event["metric"]:
            if event["value"] != 2:
                logger.debug("Value %s is not True" % event["value"])
                return None
        if "Status" in event["metric"]:
            if "Admin" in event["metric"]:
                if iface.is_linked:
                    linked_object = iface.link.interfaces
                    if linked_object and len(linked_object) == 2:
                        linked_object.remove(iface)
                        event["linked_object"] = linked_object[0].managed_object.name
                        event["linked_object_interface"] = linked_object[0].name
                        event["linked_object_status"] = linked_object[0].managed_object.get_status()
            else:
                ifaces_metrics, last_ts = get_interface_metrics(mo.object)
                im = ifaces_metrics[mo.object][iface_name]
                error_in = im.get("Interface | Errors | In")
                error_out = im.get("Interface | Errors | Out")
                if error_in is not None:
                    event["error_in"] = error_in
                if error_out is not None:
                    event["error_out"] = error_out
                if iface.is_linked:
                    linked_object = iface.link.interfaces
                    if linked_object and len(linked_object) == 2:
                        linked_object.remove(iface)
                        event["linked_object"] = linked_object[0].managed_object.name
                        event["linked_object_interface"] = linked_object[0].name
                        event["linked_object_status"] = linked_object[0].managed_object.get_status()
                        linked_ifaces_metrics, linked_last_ts = get_interface_metrics(
                            linked_object[0].managed_object
                        )
                        if linked_ifaces_metrics:
                            lim = linked_ifaces_metrics[linked_object[0].managed_object][
                                linked_object[0].name
                            ]
                            linked_error_in = lim.get("Interface | Errors | In")
                            linked_error_out = lim.get("Interface | Errors | Out")
                            if linked_error_in is not None:
                                event["linked_error_in"] = linked_error_in
                            if linked_error_out is not None:
                                event["linked_error_out"] = linked_error_out
        if "Errors" in event["metric"]:
            if iface.is_linked:
                linked_object = iface.link.interfaces
                if linked_object and len(linked_object) == 2:
                    linked_object.remove(iface)
                    event["linked_object"] = linked_object[0].managed_object.name
                    event["linked_object_interface"] = linked_object[0].name
        if is_float(event["value"]):
            event["value"] = round(float(event["value"]), 2)
        return event
    except Exception as e:
        logger.info("Error: %s \n %s", e, iface_name)
        return event
Beispiel #11
0
def test_is_float(raw, expected):
    assert is_float(raw) is expected
Beispiel #12
0
def get_interface_metrics(
    managed_objects: Union[Iterable, int],
    meric_map: Optional[Dict[str, Any]] = None
) -> Tuple[Dict["ManagedObject", Dict[str, Dict[str, Union[float, int]]]],
           Dict["ManagedObject", datetime.datetime], ]:
    """

    :param managed_objects: ManagedObject list or bi_id list
    :param meric_map: For customization getting metrics
    :return: Dictionary ManagedObject -> Path -> MetricName -> value
    """

    # mo = self.object
    if not meric_map:
        meric_map = {
            "table_name": "interface",
            "map": {
                "load_in": "Interface | Load | In",
                "load_out": "Interface | Load | Out",
                "errors_in": "Interface | Errors | In",
                "errors_out": "Interface | Errors | Out",
            },
        }
    if not isinstance(managed_objects, Iterable):
        managed_objects = [managed_objects]
    bi_map: Dict[str, "ManagedObject"] = {
        str(getattr(mo, "bi_id", mo)): mo
        for mo in managed_objects
    }
    query_interval: float = (ManagedObjectProfile.get_max_metrics_interval(
        set(mo.object_profile.id
            for mo in ManagedObject.objects.filter(bi_id__in=list(bi_map)))) *
                             1.5)
    from_date = datetime.datetime.now() - datetime.timedelta(
        seconds=max(query_interval, 3600))
    from_date = from_date.replace(microsecond=0)
    SQL = """SELECT managed_object, argMax(ts, ts),  splitByString('::', arrayFirst(x -> startsWith(x, 'noc::interface::'), labels))[-1] as iface, labels, %s
            FROM %s
            WHERE
              date >= toDate('%s')
              AND ts >= toDateTime('%s')
              AND managed_object IN (%s)
              AND NOT arrayExists(x -> startsWith(x, 'noc::unit::'), labels)
            GROUP BY managed_object, labels
            """ % (
        ", ".join(
            ["argMax(%s, ts) as %s" % (f, f)
             for f in meric_map["map"].keys()]),
        meric_map["table_name"],
        from_date.date().isoformat(),
        from_date.isoformat(sep=" "),
        ", ".join(bi_map),
    )
    ch = ch_connection()
    metric_map: DefaultDict["ManagedObject",
                            Dict[str, Dict[str,
                                           Union[int,
                                                 float]]]] = defaultdict(dict)
    last_ts: Dict["ManagedObject", datetime.datetime] = {}  # mo -> ts
    metric_fields = list(meric_map["map"].keys())
    try:
        for result in ch.execute(post=SQL):
            mo_bi_id, ts, iface, labels = result[:4]
            labels = ast.literal_eval(labels)
            res = dict(zip(metric_fields, result[4:]))
            mo = bi_map.get(mo_bi_id)
            if len(labels) == 1 and metric_map[mo].get(iface):
                # If only interface metric
                continue
            metric_map[mo][iface] = defaultdict(dict)
            for field, value in res.items():
                metric_map[mo][iface][meric_map["map"].get(field)] = (
                    float(value) if is_float(value) else int(value))
                last_ts[mo] = max(ts, last_ts.get(mo, ts))
    except ClickhouseError:
        pass
    return metric_map, last_ts