Ejemplo n.º 1
0
Archivo: base.py Proyecto: nbashev/noc
    async def send_telemetry(self):
        """
        Publish telemetry data

        :return:
        """
        spans = get_spans()
        if spans:
            self.register_metrics("span", [span_to_dict(s) for s in spans])
Ejemplo n.º 2
0
 def run_job(self, job, mo, checks):
     if job == "segment":
         scheduler = Scheduler("scheduler",
                               pool=None,
                               service=ServiceStub())
     else:
         scheduler = Scheduler("discovery",
                               pool=mo.pool.name,
                               service=ServiceStub())
     jcls = self.jcls[job]
     # Try to dereference job
     job_args = scheduler.get_collection().find_one({
         Job.ATTR_CLASS: jcls,
         Job.ATTR_KEY: mo.id
     })
     if job_args:
         self.print("Job ID: %s" % job_args["_id"])
     else:
         job_args = {Job.ATTR_ID: "fakeid", Job.ATTR_KEY: mo.id}
     job_args["_checks"] = checks
     job = get_handler(jcls)(scheduler, job_args)
     if job.context_version:
         ctx_key = job.get_context_cache_key()
         self.print("Loading job context from %s" % ctx_key)
         ctx = cache.get(ctx_key, version=job.context_version)
         if not ctx:
             self.print("Job context is empty")
         job.load_context(ctx)
     sample = 1 if self.trace else 0
     with Span(sample=sample):
         job.dereference()
         job.handler()
     if sample:
         spans = get_spans()
         self.print("Spans:")
         self.print("\n".join(str(s) for s in spans))
     if scheduler.service.metrics:
         self.print("Collected CH data:")
         for t in scheduler.service.metrics:
             self.print("Table: %s" % t)
             self.print("\n".join(
                 str(x) for x in scheduler.service.metrics[t]))
     if job.context_version and job.context:
         self.print("Saving job context to %s" % ctx_key)
         scheduler.cache_set(key=ctx_key,
                             value=job.context,
                             version=job.context_version)
         scheduler.apply_cache_ops()
         time.sleep(3)
Ejemplo n.º 3
0
 def send_metrics(self):
     # Inject spans
     spans = get_spans()
     if spans:
         self.register_metrics(SPAN_FIELDS, spans)
     #
     if not self._metrics:
         return
     w = self.get_nsq_writer()
     with self.metrics_lock:
         data = self._metrics
         self._metrics = defaultdict(list)
     for channel, fields in data:
         to_send = data[channel, fields]
         while to_send:
             chunk, to_send = to_send[:config.nsqd.ch_chunk_size], to_send[
                 config.nsqd.ch_chunk_size:]
             w.pub(channel, "%s\n%s\n" % (fields, "\n".join(chunk)))
Ejemplo n.º 4
0
    def update_spec(self, name, script):
        """
        Update named spec
        :param name: Spec name
        :param script: BaseScript instance
        :return:
        """
        from noc.dev.models.quiz import Quiz
        from noc.dev.models.spec import Spec, SpecAnswer
        from noc.sa.models.profile import Profile

        self.print("Updating spec: %s" % name)
        spec = Spec.get_by_name(name)
        changed = False
        if not spec:
            self.print("   Spec not found. Creating")
            # Get Ad-Hoc quiz
            quiz = Quiz.get_by_name("Ad-Hoc")
            if not quiz:
                self.print("   'Ad-Hoc' quiz not found. Skipping")
                return
            # Create Ad-Hoc spec for profile
            spec = Spec(
                name,
                description="Auto-generated Ad-Hoc spec for %s profile" %
                script.profile.name,
                revision=1,
                quiz=quiz,
                author="NOC",
                profile=Profile.get_by_name(script.profile.name),
                changes=[],
                answers=[])
            changed = True
        # Fetch commands from spans
        cli_svc = {"beef_cli", "cli", "telnet", "ssh"}
        commands = set()
        for sd in get_spans():
            row = sd.split("\t")
            if row[6] not in cli_svc:
                continue
            commands.add(row[12].decode("string_escape").strip())
        # Update specs
        s_name = "cli_%s" % script.name.rsplit(".", 1)[-1]
        names = set()
        for ans in spec.answers:
            if ((ans.name == s_name or ans.name.startswith(s_name + "."))
                    and ans.type == "cli"):
                names.add(ans.name)
                if ans.value in commands:
                    # Already recorded
                    commands.remove(ans.value)
        if commands:
            # New commands left
            max_n = 0
            for n in names:
                if "." in n:
                    nn = int(n.rsplit(".", 1)[-1])
                    if nn > max_n:
                        max_n = nn
            #
            ntpl = "%s.%%d" % s_name
            for nn, cmd in enumerate(sorted(commands)):
                spec.answers += [
                    SpecAnswer(name=ntpl % (nn + 1), type="cli", value=cmd)
                ]
            changed = True
        if changed:
            spec.save()