def check(self, agentConfig):

        self.logger.debug('getPlugins: start')

        plugin_directory = agentConfig.get('plugin_directory', None)
        if plugin_directory:

            self.logger.info(
                'getPlugins: plugin_directory %s', plugin_directory)

            if not os.access(plugin_directory, os.R_OK):
                self.logger.warning(
                    'getPlugins: Plugin path %s is set but not readable by ' +
                    'agent. Skipping plugins.', plugin_directory)
                return False
        else:
            self.logger.debug('getPlugins: plugin_directory not set')

            return False

        # Have we already imported the plugins?
        # Only load the plugins once
        if self.plugins is None:
            self.logger.debug(
                'getPlugins: initial load from %s', plugin_directory)

            sys.path.append(plugin_directory)

            self.plugins = []
            plugins = []

            # Loop through all the plugin files
            for root, dirs, files in os.walk(plugin_directory):
                for name in files:
                    self.logger.debug('getPlugins: considering: %s', name)

                    name = name.split('.', 1)

                    # Only pull in .py files (ignores others, inc .pyc files)
                    try:
                        if name[1] == 'py':

                            self.logger.debug(
                                'getPlugins: ' + name[0] + '.' + name[1] +
                                ' is a plugin')

                            plugins.append(name[0])
                    except IndexError:
                        continue

            # Loop through all the found plugins, import them then create new
            # objects
            for plugin_name in plugins:
                self.logger.debug('getPlugins: loading %s', plugin_name)

                plugin_path = os.path.join(
                    plugin_directory, '%s.py' % plugin_name)

                if not os.access(plugin_path, os.R_OK):
                    self.logger.error(
                        'getPlugins: Unable to read %s so skipping this '
                        'plugin.', plugin_path)
                    continue

                try:
                    # Import the plugin, but only from the plugin directory
                    # (ensures no conflicts with other module names elsewhere
                    # in the sys.path
                    import imp
                    imported_plugin = imp.load_source(plugin_name, plugin_path)

                    self.logger.debug('getPlugins: imported %s', plugin_name)

                    # Find out the class name and then instantiate it
                    plugin_class = getattr(imported_plugin, plugin_name, None)
                    if plugin_class is None:
                        self.logger.info(
                            'getPlugins: Unable to locate class %s in %s, '
                            'skipping', plugin_name, plugin_path)
                        continue

                    try:
                        plugin_obj = plugin_class(
                            agentConfig, self.logger, self.raw_config)
                    except TypeError:

                        try:
                            plugin_obj = plugin_class(
                                agentConfig, self.logger)
                        except TypeError:
                            # Support older plugins.
                            plugin_obj = plugin_class()

                    self.logger.debug('getPlugins: instantiated %s', plugin_name)

                    # Store in class var so we can execute it again on the
                    # next cycle
                    self.plugins.append(plugin_obj)

                except Exception:
                    self.logger.error(
                        'getPlugins (%s): exception = %s', plugin_name,
                        traceback.format_exc())

        # Now execute the objects previously created
        if self.plugins is not None:
            self.logger.debug('getPlugins: executing plugins')

            # Execute the plugins
            output = {}

            for plugin in self.plugins:
                self.logger.info(
                    'getPlugins: executing  %s', plugin.__class__.__name__)

                try:
                    value = plugin.run()
                    if value:
                        output[plugin.__class__.__name__] = value
                        self.logger.debug(
                            'getPlugins: %s output: %s',
                            plugin.__class__.__name__,
                            output[plugin.__class__.__name__])
                        self.logger.info(
                            'getPlugins: executed %s',
                            plugin.__class__.__name__)
                    else:
                        self.logger.info(
                            'getPlugins: executed %s but returned no data',
                            plugin.__class__.__name__)
                except Exception:
                    self.logger.error(
                        'getPlugins: exception = %s', traceback.format_exc())

            self.logger.debug('getPlugins: returning')
            # Each plugin should output a dictionary so we can convert it to
            # JSON later
            return output

        else:
            self.logger.debug('getPlugins: no plugins, returning false')

            return False
dataset/ETHPy150Open serverdensity/sd-agent/checks/server_density/plugins.py/Plugins.check/original