예제 #1
0
파일: Warden.cs 프로젝트: weiplanet/Warden
        /// <summary>
        /// Start the Warden.
        /// It will be running iterations in a loop (infinite by default but can be changed) and executing all of the configured hooks.
        /// </summary>
        /// <returns></returns>
        public async Task StartAsync()
        {
            _logger.Info($"Starting Warden: {Name}");
            _running = true;
            _logger.Trace("Executing Warden hooks OnStart.");
            _configuration.Hooks.OnStart.Execute();
            _logger.Trace("Executing Warden hooks OnStartAsync.");
            await _configuration.Hooks.OnStartAsync.ExecuteAsync();

            await TryExecuteIterationsAsync();
        }
예제 #2
0
        /// <summary>
        ///     Creates the Warden service which monitors processes on the computer.
        /// </summary>
        /// <param name="options"></param>
        public static void Initialize(WardenOptions options)
        {
            if (!Privileges.IsUserAnAdministrator())
            {
                throw new WardenManageException(Resources.Exception_No_Admin);
            }
            WardenImpersonator.Initialize();
            Stop();
            Options = options ?? throw new WardenManageException(Resources.Exception_No_Options);
            try
            {
                ShutdownUtils.RegisterEvents();


                _wmiOptions = new ConnectionOptions
                {
                    Authentication   = AuthenticationLevel.Default,
                    EnablePrivileges = true,
                    Impersonation    = ImpersonationLevel.Impersonate,
                    Timeout          = TimeSpan.MaxValue
                };

                _connectionScope = new ManagementScope($@"\\{Environment.MachineName}\root\cimv2", _wmiOptions);
                _connectionScope.Connect();

                var creationThreadStarted = new ManualResetEvent(false);
                CreationThread = new Thread(StartCreationListener)
                {
                    IsBackground = true
                };
                CreationThread.Start(creationThreadStarted);

                var destructionThreadStarted = new ManualResetEvent(false);
                DestructionThread = new Thread(StartDestructionListener)
                {
                    IsBackground = true
                };
                DestructionThread.Start(destructionThreadStarted);


                creationThreadStarted.WaitOne();
                destructionThreadStarted.WaitOne();
                Initialized = true;
                Logger?.Info("Initialized");
            }
            catch (Exception ex)
            {
                throw new WardenException(ex.Message, ex);
            }
        }
예제 #3
0
        private async Task <IList <WatcherExecutionResult> > TryExecuteWatcherCheckAndHooksAsync(
            WatcherConfiguration watcherConfiguration)
        {
            var startedAt = _configuration.DateTimeProvider();
            var watcher   = watcherConfiguration.Watcher;
            IWardenCheckResult wardenCheckResult = null;
            var results = new List <WatcherExecutionResult>();

            try
            {
                await InvokeOnStartHooksAsync(watcherConfiguration, WatcherCheck.Create(watcher));

                _logger.Info($"Executing Watcher: {watcher.Name}.");
                var watcherCheckResult = await watcher.ExecuteAsync();

                _logger.Info($"Completed executing Watcher: {watcher.Name}. " +
                             $"Is valid: {watcherCheckResult.IsValid}. " +
                             $"Description: {watcherCheckResult.Description}");
                var completedAt = _configuration.DateTimeProvider();
                wardenCheckResult = WardenCheckResult.Create(watcherCheckResult, startedAt, completedAt);
                var watcherResults = await ExecuteWatcherCheckAsync(watcher, watcherCheckResult, wardenCheckResult,
                                                                    watcherConfiguration);

                results.AddRange(watcherResults);
            }
            catch (Exception exception)
            {
                _logger.Error($"There was an error while executing Watcher: {watcher.Name}.", exception);
                var completedAt = _configuration.DateTimeProvider();
                wardenCheckResult = WardenCheckResult.Create(WatcherCheckResult.Create(watcher, false, exception.Message),
                                                             startedAt, completedAt, exception);
                results.Add(new WatcherExecutionResult(watcher, WatcherResultState.Error,
                                                       GetPreviousWatcherState(watcher), wardenCheckResult, exception));
                await UpdateWatcherResultStateAndExecuteHooksPossibleAsync(watcher, WatcherResultState.Error,
                                                                           () => InvokeOnFirstErrorHooksAsync(watcherConfiguration, exception));

                var wardenException = new WardenException("There was an error while executing Warden " +
                                                          $"caused by watcher: '{watcher.Name}'.", exception);

                await InvokeOnErrorHooksAsync(watcherConfiguration, wardenException);
            }
            finally
            {
                await InvokeOnCompletedHooksAsync(watcherConfiguration, wardenCheckResult);
            }

            return(results);
        }
예제 #4
0
        /// <summary>
        /// Start the Warden Manager using the underlying IWarden instance.
        /// </summary>
        /// <returns></returns>
        public async Task StartAsync()
        {
            _logger.Info("Starting Warden Manager.");
            _isManagerRunning = true;
            _isWardenRunning  = true;
            await _warden.StartAsync();

            while (_isManagerRunning)
            {
                await Task.Delay(1000);

                if (_isWardenRunning)
                {
                    continue;
                }

                _logger.Trace("Warden has been stopped, awaiting for the start command...");
            }
        }