Example #1
0
File: span.py Project: 0pt1on/noc
    def __exit__(self, exc_type, exc_val, exc_tb):
        def q_tsv(s):
            if not s:
                return ""
            else:
                return str(s).encode("string_escape")

        global spans
        if self.is_sampled or self.hist or self.quantile:
            self.duration = int((perf_counter() - self.ts0) * US)
        if self.hist:
            self.hist.register(self.duration)
        if self.quantile:
            self.quantile.register(self.duration)
        if config.features.forensic and hasattr(self, "forensic_id"):
            # N.B. config.features.forensic may be changed during span
            forensic_logger.info("[<%s]", self.forensic_id)
        if not self.is_sampled:
            return
        if exc_type and not self.error_text and not self.is_ignorable_error(exc_type):
            self.error_code = ERR_UNKNOWN
            self.error_text = str(exc_val).strip("\t").replace("\t", " ").replace("\n", " ")
        lt = time.localtime(self.start)
        row = "\t".join(
            str(x)
            for x in [
                time.strftime("%Y-%m-%d", lt),
                time.strftime("%Y-%m-%d %H:%M:%S", lt),
                self.span_context,
                self.span_id,
                self.parent,
                q_tsv(self.server),
                q_tsv(self.service),
                q_tsv(self.client),
                self.duration,
                self.error_code or 0,
                q_tsv(self.error_text),
                self.sample,
                ch_escape(q_tsv(self.in_label)),
                ch_escape(q_tsv(self.out_label)),
            ]
        )
        with span_lock:
            spans += [row]
        if self.span_parent == DEFAULT_ID:
            del tls.span_parent
            del tls.span_context
        else:
            tls.span_parent = self.span_parent
        metrics["spans"] += 1
Example #2
0
    def register_message(self, cfg, timestamp, message, facility, severity):
        """
        Spool message to be sent
        """
        for item in self.ignorept:
            re_events = re.compile(self.ignorept[item])
            if self.ignorept[item].match(str(message)):
                return

        if cfg.process_events:
            # Send to classifier
            metrics["events_out"] += 1
            self.messages += [{
                "ts": timestamp,
                "object": cfg.id,
                "data": {
                    "source": "syslog",
                    "collector": config.pool,
                    "message": message
                },
            }]
        if cfg.archive_events and cfg.bi_id:
            # Archive message
            metrics["events_archived"] += 1
            now = datetime.datetime.now()
            date = now.strftime("%Y-%m-%d")
            ts = now.strftime("%Y-%m-%d %H:%M:%S")
            msg = ch_escape(message)
            self.register_metrics(
                "syslog.date.ts.managed_object.facility.severity.message",
                [
                    str("%s\t%s\t%s\t%d\t%d\t%s" %
                        (date, ts, cfg.bi_id, facility, severity, msg))
                ],
            )
Example #3
0
 def clean(self, row):
     s = "\t".join(str(x) for x in row)
     if "\n" in s or "\\" in s or s.count("\t") >= len(row):
         metrics["error", ("type", "rogue_chars")] += 1
         self.logger.error("Rogue chars in row %s", row)
         row = map(
             lambda x: ch_escape(x)
             if isinstance(x, six.string_types) else x, list(row))
     return row
Example #4
0
 def extract(self):
     nr = 0
     ts = datetime.datetime.now()
     # External data
     x_data = [self.get_interfaces(), self.get_links(), self.get_caps()]
     sn = self.get_mo_sn()
     # Extract managed objects
     for mo in ManagedObject.objects.all():
         did = DiscoveryID.objects.filter(object=mo).first()
         uptime = Uptime.objects.filter(object=mo.id, stop=None).first()
         serials = sn.get(mo.id, [])
         inventory = mo.get_inventory()
         if inventory:
             serials += inventory[0].get_object_serials(chassis_only=False)
         r = {
             "ts": ts,
             "managed_object": mo,
             "profile": mo.profile,
             "administrative_domain": mo.administrative_domain,
             "segment": mo.segment,
             "container": mo.container,
             "level": mo.object_profile.level,
             "x": mo.x,
             "y": mo.y,
             "pool": mo.pool,
             # "object_profile": mo.object_profile,
             "vendor": mo.vendor,
             "platform": mo.platform,
             "version": mo.version,
             "name": ch_escape(mo.name),
             "hostname": did.hostname if did else "",
             "ip": mo.address,
             "is_managed": mo.is_managed,
             "location":
             mo.container.get_address_text() if mo.container else "",
             "uptime": uptime.last_value if uptime else 0.0,
             "tags": [str(t) for t in mo.tags] if mo.tags else [],
             "serials": list(set(serials))
             # subscribers
             # services
         }
         # Apply external data
         for data in x_data:
             d = data.get(mo.id)
             if d:
                 r.update(d)
         # Submit
         self.mo_stream.push(**r)
         nr += 1
     self.mo_stream.finish()
     return nr