Exemple #1
0
 def handle(self, devices, *args, **options):
     devs = set()
     for d in devices:
         try:
             devs |= set(ManagedObjectSelector.resolve_expression(d))
         except ManagedObjectSelector.DoesNotExist:
             self.die("Invalid object '%s'" % d)
     self.stdout.write("profile,platform,oid,value\n")
     for o in sorted(devs, key=lambda x: x.name):
         if "get_snmp_get" not in o.scripts:
             continue
         if o.platform:
             platform = o.platform.full_name
         else:
             try:
                 v = o.scripts.get_version()
             except AttributeError:
                 v = {
                     "platform": "Unknown"
                 }
             platform = v["platform"]
         # sysObjectID
         try:
             v = o.scripts.get_snmp_get(oid=mib["SNMPv2-MIB::sysObjectID.0"])
         except NOCError:
             continue
         self.stdout.write(
             "%s,%s,%s,%s\n" % (
                 o.profile.name,
                 platform,
                 "SNMPv2-MIB::sysObjectID.0",
                 v
             )
         )
Exemple #2
0
 def iter_objects(self, objects):
     r = set()
     for x in objects:
         for o in ManagedObjectSelector.resolve_expression(x):
             r.add(o)
     for o in r:
         yield o
Exemple #3
0
 def handle(self, *args, **options):
     clean = set()
     for expr in args:
         for obj in ManagedObjectSelector.resolve_expression(expr):
             if obj.id in clean:
                 continue  # Already cleaned
             self.clean_managed_object(obj)
             clean.add(obj.id)
Exemple #4
0
 def handle_collect(self, storage, path, spec, force, objects, *args, **options):
     # Get spec data
     sp = Spec.get_by_name(spec)
     if not sp:
         self.die("Invalid spec: '%s'" % spec)
     # Spec data
     req = sp.get_spec_request()
     # Get effective list of managed objects
     mos = set()
     for ox in objects:
         for mo in ManagedObjectSelector.resolve_expression(ox):
             mos.add(mo)
     # Collect beefs
     for mo in mos:
         self.print("Collecting beef from %s" % mo.name)
         if mo.profile.name != sp.profile.name:
             self.print("  Profile mismatch. Skipping")
             continue
         if mo.object_profile.beef_policy == "D" and not force:
             self.print("  Collection disabled by policy. Skipping")
             continue
         if not mo.object_profile.beef_storage and not storage:
             self.print("  Beef storage is not configured. Skipping")
             continue
         if not mo.object_profile.beef_path_template and not force:
             self.print("  Beef path template is not configured. Skipping")
             continue
         elif not mo.object_profile.beef_path_template and force:
             self.print("  Beef path template is not configured. But force set. Generate path")
             path = DEFAULT_BEEF_PATH_TEMPLATE.format(mo)
         else:
             path = mo.object_profile.beef_path_template.render_subject(
                 object=mo,
                 spec=sp,
                 datetime=datetime
             )
         storage = mo.object_profile.beef_storage or self.get_storage(storage, beef=True)
         if not path:
             self.print("  Beef path is empty. Skipping")
             continue
         try:
             bdata = mo.scripts.get_beef(spec=req)
         except Exception as e:
             self.print("Failed collect beef on %s: %s" % (mo.name, e))
             continue
         beef = Beef.from_json(bdata)
         self.print("  Saving to %s:%s" % (storage.name, path))
         try:
             cdata, udata = beef.save(storage, path)
             if cdata == udata:
                 self.print("  %d bytes written" % cdata)
             else:
                 self.print("  %d bytes written (%d uncompressed. Ratio %.2f/1)" % (
                     cdata, udata, float(udata) / float(cdata)))
         except IOError as e:
             self.print("  Failed to save: %s" % e)
         self.print("  Done")
Exemple #5
0
 def handle_run(self, job, managed_objects, check=None, trace=False, *args, **options):
     self.trace = trace
     job = job[0]
     mos = []
     for x in managed_objects:
         for mo in ManagedObjectSelector.resolve_expression(x):
             if mo not in mos:
                 mos += [mo]
     checks = set()
     for c in check:
         checks.update(c.split(","))
     for c in checks:
         if c not in self.checks[job]:
             self.die(
                 "Unknown check '%s' for job '%s'. Available checks are: %s\n" % (
                     c, job, ", ".join(self.checks[job])
                 )
             )
     for mo in mos:
         self.run_job(job, mo, checks)
Exemple #6
0
 def handle_portmap(self, portmap_objects=[]):
     for po in portmap_objects:
         for o in ManagedObjectSelector.resolve_expression(po):
             if not o.remote_system:
                 self.stdout.write(
                     "%s (%s, %s) NRI: N/A\n" % (o.name, o.address, o.platform)
                 )
                 continue
             portmapper = loader.get_loader(o.remote_system.name)(o)
             nri = o.remote_system.name
             self.stdout.write("%s (%s, %s) NRI: %s\n" % (
                 o.name, o.address, o.platform, nri))
             r = []
             for i in Interface._get_collection().find({
                 "managed_object": o.id,
                 "type": "physical"
             }, {
                 "_id": 1,
                 "name": 1,
                 "nri_name": 1
             }):
                 rn = portmapper.to_remote(i["name"]) or self.PORT_ERROR
                 if rn == self.PORT_ERROR:
                     ln = self.PORT_ERROR
                 else:
                     ln = portmapper.to_local(rn) or self.PORT_ERROR
                 if i.get("nri_name") == rn and ln != self.PORT_ERROR:
                     status = "OK"
                 elif not i.get("nri_name") and ln != self.PORT_ERROR:
                     status = "Not in database"
                 elif rn == self.PORT_ERROR:
                     status = "Failed to convert to remote name"
                 else:
                     print(ln, rn, i.get("nri_name"))
                     status = "Failed to convert to local name"
                 r += [(i["name"], rn, i.get("nri_name", "--"), status)]
             r = [("Local", "Remote", "Interface NRI", "Status")] + sorted(r, key=lambda x: split_alnum(x[0]))
             self.stdout.write(
                 "%s\n" % format_table([0, 0, 0, 0], r, sep=" | ", hsep="-+-")
             )
Exemple #7
0
class ReportFilterApplication(SimpleReport):
    title = _("Discovery Problem")
    form = ReportForm
    try:
        default_selector = ManagedObjectSelector.resolve_expression(
            "@Problem Discovery Report")
    except ManagedObjectSelector.DoesNotExist:
        default_selector = None
    predefined_reports = {
        "default":
        PredefinedReport(_("Problem Discovery 2(default)"),
                         {"selector": default_selector})
    }

    def get_data(self,
                 request,
                 pool=None,
                 obj_profile=None,
                 selector=None,
                 avail_status=None,
                 profile_check_only=None,
                 failed_scripts_only=None,
                 filter_pending_links=None,
                 filter_none_objects=None,
                 filter_view_other=None,
                 **kwargs):
        data = []
        match = None
        code_map = {
            "1": "Unknown error",
            "10000": "Unspecified CLI error",
            "10005": "Connection refused",
            "10001": "Authentication failed",
            "10002": "No super command defined",
            "10003": "No super privileges",
            "10004": "SSH Protocol error"
        }

        if not pool:
            pool = Pool.objects.filter()[0]
        data += [SectionRow(name="Report by %s" % pool.name)]
        if selector:
            mos = ManagedObject.objects.filter(selector.Q)
        else:
            mos = ManagedObject.objects.filter(pool=pool, is_managed=True)

        if not request.user.is_superuser:
            mos = mos.filter(
                administrative_domain__in=UserAccess.get_domains(request.user))
        if obj_profile:
            mos = mos.filter(object_profile=obj_profile)
        if filter_view_other:
            mnp_in = list(
                ManagedObjectProfile.objects.filter(enable_ping=False))
            mos = mos.filter(profile=Profile.objects.get(
                name=GENERIC_PROFILE)).exclude(object_profile__in=mnp_in)
        discovery = "noc.services.discovery.jobs.box.job.BoxDiscoveryJob"
        mos_id = list(mos.values_list("id", flat=True))
        if avail_status:
            avail = ObjectStatus.get_statuses(mos_id)

        if profile_check_only:
            match = {
                "$or": [{
                    "job.problems.suggest_cli": {
                        "$exists": True
                    }
                }, {
                    "job.problems.suggest_snmp": {
                        "$exists": True
                    }
                }, {
                    "job.problems.profile.": {
                        "$regex": "Cannot detect profile"
                    }
                }, {
                    "job.problems.version.": {
                        "$regex": "Remote error code 1000[1234]"
                    }
                }]
            }

        elif failed_scripts_only:
            match = {
                "$and": [{
                    "job.problems": {
                        "$exists": "true",
                        "$ne": {}
                    }
                }, {
                    "job.problems.suggest_snmp": {
                        "$exists": False
                    }
                }, {
                    "job.problems.suggest_cli": {
                        "$exists": False
                    }
                }]
            }
        elif filter_view_other:
            match = {"job.problems.suggest_snmp": {"$exists": False}}

        rdp = ReportDiscoveryProblem(mos, avail_only=avail_status, match=match)
        exclude_method = []
        if filter_pending_links:
            exclude_method += ["lldp", "lacp", "cdp", "huawei_ndp"]

        for discovery in rdp:
            mo = ManagedObject.get_by_id(discovery["key"])
            for method in ifilterfalse(lambda x: x in exclude_method,
                                       discovery["job"][0]["problems"]):
                problem = discovery["job"][0]["problems"][method]
                if filter_none_objects and not problem:
                    continue
                if isinstance(problem, dict) and "" in problem:
                    problem = problem.get("", "")
                if "Remote error code" in problem:
                    problem = code_map.get(problem.split(" ")[-1], problem)
                if isinstance(problem, six.string_types):
                    problem = problem.replace("\n", " ").replace("\r", " ")

                data += [(mo.name, mo.address,
                          mo.profile.name, mo.administrative_domain.name,
                          _("Yes") if mo.get_status() else _("No"),
                          discovery["st"].strftime("%d.%m.%Y %H:%M")
                          if "st" in discovery else "", method, problem)]

        return self.from_dataset(title=self.title,
                                 columns=[
                                     _("Managed Object"),
                                     _("Address"),
                                     _("Profile"),
                                     _("Administrative Domain"),
                                     _("Avail"),
                                     _("Last successful discovery"),
                                     _("Discovery"),
                                     _("Error")
                                 ],
                                 data=data)
Exemple #8
0
 def get_objects(exprs):
     objects = set()
     for s in exprs:
         objects.update(ManagedObjectSelector.resolve_expression(s))
     return sorted(objects, key=lambda x: x.name)
Exemple #9
0
 def handle(self, *args, **options):
     self.debug = bool(options.get("debug", True))
     # Build list of comands either from input file or from argument
     commands = []
     if options.get("command"):
         # Argument
         commands += options["command"].split("\\n")
     if options.get("input"):
         # Input file
         if not os.path.exists(options["input"]):
             raise CommandError("Input file '%s' is not found" %
                                options["input"])
         with open(options["input"]) as f:
             commands += f.read().split("\n")
     if not commands:
         raise CommandError("No commands to perform")
     # Build list of targets
     targets = []
     if args:
         targets += list(args)
     if options.get("target"):
         # Input file
         if not os.path.exists(options["target"]):
             raise CommandError("Target file '%s' is not found" %
                                options["target"])
         with open(options["target"]) as f:
             targets += f.read().split("\n")
     targets = [x.strip() for x in targets if x.strip()]
     if not targets:
         raise CommandError("No managed objects found")
     objects = []
     try:
         for x in targets:
             objects += ManagedObjectSelector.resolve_expression(x)
     except ManagedObject.DoesNotExist:
         raise CommandError("Managed object not found: '%s'" % x)
     except ManagedObjectSelector:
         raise CommandError("Managed object selector not found: '%s'" % x)
     # Run commands
     tasks = set()
     limit = options.get("limit")
     timeout = options.get("timeout")
     retries = options.get("retries")
     while objects or tasks:
         # Spool next party
         while len(tasks) < limit and objects:
             o = objects.pop(0)
             mt = MapTask(managed_object=o,
                          map_script="%s.commands" % o.profile_name,
                          script_params={"commands": commands},
                          next_try=datetime.datetime.now(),
                          script_timeout=timeout,
                          retries_left=retries)
             mt.save()
             tasks.add(mt.id)
         # Check complete tasks
         complete = False
         for ct in MapTask.objects.filter(id__in=tasks,
                                          status__in=["C", "F"]):
             self.report_result(ct)
             tasks.remove(ct.id)
             ct.delete()
             complete = True
         if not complete:
             time.sleep(1)