Пример #1
0
    def cmd_monitor_get(self, *argv, **kwargs):
        """
        Runs monitor commands from monitor path
        """

        config = None
        b64_config = kwargs.get('config', None)

        # Use Pool on windows systems
        if ecm.is_win():
            thread_pool = Pool(20)

        try:
            if isinstance(b64_config, str):
                config = json.loads(b64decode(b64_config))
        except:
            pass

        retval = []
        to_execute = []

        # Create dirs if necessary
        self._check_path(MPLUGIN_PATH)
        self._check_path(CPLUGIN_PATH)
        self._check_path(CACHE_PATH)

        # Clean old cache files
        self._cache_clean()

        if not os.path.isdir(MPLUGIN_PATH):
            return retval

        # Foreach plugin inside mplugins and custom
        for plugin_path in [MPLUGIN_PATH, CPLUGIN_PATH]:
            if not os.path.isdir(plugin_path):
                continue

            for p_path in os.listdir(plugin_path):
                p_path = os.path.join(plugin_path, p_path)

                # Skip disabled plugins
                if not os.path.isdir(p_path):
                    continue

                # Skip disabled plugins
                if os.path.basename(p_path).startswith('.'):
                    continue

                runas = None
                scripts = []
                interval = COMMAND_INTERVAL

                # Search for plugin files
                if plugin_path == MPLUGIN_PATH:
                    mplugin = MPlugin(p_path)

                    # Update config and re-read
                    if config:
                        mplugin.write_config(config.get(mplugin.id))
                        mplugin = MPlugin(p_path)

                    runas = mplugin.data.get('runas', None)
                    interval = mplugin.data.get('interval', COMMAND_INTERVAL)

                    script = os.path.join(plugin_path, p_path, mplugin.id)

                    if not os.path.exists(script):
                        script += '.py'

                    if not os.path.exists(script):
                        continue

                    # Add as valid mplugin script
                    scripts = [script]

                    # Executable plugin base
                    if not os.access(script, os.X_OK):
                        os.chmod(script, 0755)

                # Custom plugins path
                else:
                    # Set default interval or read from path name
                    interval = self._interval_from_path(p_path)

                    for filename in os.listdir(p_path):
                        _tmp = os.path.join(plugin_path, p_path, filename)
                        if os.access(_tmp, os.X_OK):
                            scripts.append(_tmp)

                for script in scripts:
                    # Read last result from cache (even if void)
                    from_cache = self._cache_read(script, interval + CACHE_SOFT_TIME)
                    retval.append(self._parse_script_name(script) + GLUE + str(interval) + GLUE + from_cache)

                    # Execute script if cache wont be valid on next command_get execution
                    if not self._cache_read(script, interval - COMMAND_INTERVAL - CACHE_SOFT_TIME):
                        to_execute.append({'script': script, 'runas': runas})

        for data in to_execute:
            if ecm.is_win():
                p = Process(target=_run_background_file, args=(data['script'], data['runas'],))
                p.start()
                p.join()
            else:
                _run_background_file(data['script'], data['runas'])
        return retval
Пример #2
0
            raise ecm.InvalidParameters(self.cmd_service_exists.__doc__)

        scmhandle = ws.OpenSCManager(None, None, ws.SC_MANAGER_ALL_ACCESS)
        sserv, lserv = self._svc_getname(scmhandle, service)

        return bool(lserv)

    def _svc_getname(self, scmhandle, service):
        snames = ws.EnumServicesStatus(scmhandle)
        for i in snames:
            if i[0].lower() == service.lower():
                return i[0], i[1]

            if i[1].lower() == service.lower():
                return i[0], i[1]

        raise Exception("The %s service doesn't seem to exist." % service)


# Load class based on platform()
if ecm.is_win():
    ws = __import__("win32service")
    ECMWindows().run()

else:
    if(DBUS):
        ECMLinuxSystemD().run()
    else:
        # Do our best with init.d
        ECMLinuxInitD().run()
Пример #3
0
    def _boot_time(self):
        """ Returns server boot time """
        if ecm.is_win():
            return self._boot_time_windows()

        return self._boot_time_linux()