Пример #1
0
 def handle_import(self, storage, path=None, paths=None, *args, **options):
     """
     Importing yaml file to beef
     :param storage:
     :param path:
     :param paths:
     :return:
     """
     for import_path in paths:
         self.print("Importing %s ..." % import_path)
         with open(import_path, "r") as f:
             data = yaml.safe_load(f)
         for c in data["cli_fsm"]:
             c["reply"] = [
                 codecs.encode(smart_bytes(reply), self.CLI_ENCODING)
                 for reply in c["reply"]
             ]
         for c in data["cli"]:
             c["reply"] = [
                 codecs.encode(smart_bytes(reply), self.CLI_ENCODING)
                 for reply in c["reply"]
             ]
         for m in data["mib"]:
             m["value"] = codecs.encode(smart_bytes(m["value"]),
                                        self.CLI_ENCODING)
         try:
             beef = Beef.from_json(data)
         except ValueError:
             self.print("Error when importing beef file %s" % import_path)
             continue
         st = self.get_storage(storage, beef=True)
         if not path:
             path = smart_text(
                 self.DEFAULT_BEEF_IMPORT_PATH_TEMPLATE.format(beef))
         beef.save(st, smart_text(path))
Пример #2
0
Файл: beef.py Проект: 0pt1on/noc
 def handle_import(self, storage, path, paths=None, *args, **options):
     """
     Importing yaml file to beef
     :param storage:
     :param path:
     :param paths:
     :return:
     """
     for import_path in paths:
         self.print("Importing %s ..." % import_path)
         with open(import_path, "r") as f:
             data = yaml.safe_load(f)
         for c in data["cli_fsm"]:
             c["reply"] = [
                 reply.encode(self.CLI_ENCODING) for reply in c["reply"]
             ]
         for c in data["cli"]:
             c["reply"] = [
                 reply.encode(self.CLI_ENCODING) for reply in c["reply"]
             ]
         for m in data["mib"]:
             m["value"] = m["value"].encode(self.CLI_ENCODING)
         beef = Beef.from_json(data)
         st = self.get_storage(storage, beef=True)
         beef.save(st, unicode(path))
Пример #3
0
Файл: beef.py Проект: 0pt1on/noc
 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.get_objects_from_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 = self.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")
Пример #4
0
    def handle_collect(self,
                       storage,
                       path,
                       spec,
                       force,
                       objects,
                       description=None,
                       *args,
                       **options):
        connect()
        from noc.sa.models.managedobjectselector import ManagedObjectSelector
        from noc.dev.models.spec import Spec

        class FakeSpec(object):
            def __init__(self, name):
                self.name = name
                self.uuid = "4ec10fd8-3a33-4f23-b96e-91e3967c3b1b"

        # Get spec data
        if ":" in spec:
            path, file = smart_text(os.path.dirname(spec)), smart_text(
                os.path.basename(spec))
            spec_data = open_fs(path).open(file)
            sp = Spec.from_json(spec_data.read())
            sp.quiz = FakeSpec("Ad-Hoc")
            sp.profile = FakeSpec("Generic.Host")
        else:
            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.get_objects_from_expression(ox):
                mos.add(mo)
        # Collect beefs
        for mo in mos:
            self.print("Collecting beef from %s" % mo.name)
            if ":" in spec:
                sp.profile = mo.profile
            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 = smart_text(self.DEFAULT_BEEF_PATH_TEMPLATE.format(mo))
            else:
                path = smart_text(
                    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)
            if description:
                beef.description = description
            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")
Пример #5
0
    def handle(self, script, object_name, arguments, pretty, yaml_o, use_snmp,
               access_preference, update_spec, beef_output, *args, **options):
        # Get object
        obj = self.get_object(object_name[0])
        # Build credentials
        credentials = self.get_credentials(obj)
        # Parse arguments
        args = self.get_script_args(arguments)
        # Load script
        script = script[0]
        if "." not in script:
            script = "%s.%s" % (obj.profile.name, script)
        script_class = loader.get_script(script)
        if not script_class:
            self.die("Failed to load script %s" % script_class)
        # Get capabilities
        caps = obj.get_caps()
        #
        if not use_snmp:
            if "snmp_ro" in credentials:
                del credentials["snmp_ro"]
            if "SNMP" in caps:
                del caps["SNMP"]
        if access_preference:
            credentials["access_preference"] = access_preference
        # Get version info
        if obj.version:
            version = {
                "vendor": obj.vendor.name if obj.vendor else None,
                "platform": obj.platform.name if obj.platform else None,
                "version": obj.version.version if obj.version else None,
                "image": obj.software_image if obj.software_image else None,
            }
        else:
            version = None
        # Run script
        service = ServiceStub(pool=obj.pool.name)
        scr = script_class(
            service=service,
            credentials=credentials,
            capabilities=caps,
            args=args,
            version=version,
            timeout=3600,
            name=script,
        )
        span_sample = 1 if update_spec or beef_output else 0
        with Span(sample=span_sample):
            result = scr.run()
        if pretty:
            pprint.pprint(result)
        elif yaml_o:
            import sys

            yaml.dump(result, sys.stdout)
        else:
            self.stdout.write("%s\n" % result)
        if update_spec:
            self.update_spec(update_spec, scr)
        if beef_output:
            spec = self.update_spec(update_spec, scr, save=False)
            bef_script_class = loader.get_script(
                "%s.%s" % (obj.profile.name, "get_beef"))
            beef_scr = bef_script_class(
                service=service,
                credentials=credentials,
                capabilities=caps,
                args={"spec": spec.get_spec_request()},
                version=version,
                timeout=3600,
                name="%s.%s" % (obj.profile.name, "get_beef"),
            )
            bdata = beef_scr.run()
            beef = Beef.from_json(bdata)
            storage = StorageStub("osfs:///")
            sdata = beef.get_data(decode=True)
            with storage.open_fs() as fs:
                fs.writebytes(beef_output, bytes(yaml.dump(sdata)))