Esempio n. 1
0
        private void RunModuleTask(QueuedModuleTask queuedTask)
        {
            var config = ServiceProvider.GetService<AgentConfig>();
            if (config == null) return;

            var resultStorage = ServiceProvider.GetService<IResultStorage>();
            if (resultStorage == null) return;

            var task = queuedTask.Task;
            try {
                var checkPath = _getModulePath(task.Module);
                var dll = Path.Combine(checkPath, config.Modules[task.Module].Dll);

                if (!File.Exists(dll))
                {
                    throw new FileNotFoundException($"Check DLL \"{dll}\" not found!");
                }

                switch(task.FunctionType)
                {
                    case ModuleFunctionType.Check: {
                        var functionResult = ModuleLoader.ExecuteCheckFunction(dll, checkPath, task.Function, (CheckSettings)task.Settings);

                        queuedTask.Completed = DateTime.Now;

                        foreach (var kvpResult in functionResult.CheckResults)
                        {
                            var result = kvpResult.Value;
                            var target = kvpResult.Key;
                            if (result.RanSuccessfully)
                                _log.Info($"Task {task}({result.Target}) returned the raw values {result.RawValues}");
                            else
                                _log.Warn($"Task {task}({result.Target}) executed with error: {result.ExecutionException.Message}");
                            _log.Info($"  -> {result.Message}");
                        }
                        resultStorage.StoreResult(queuedTask, functionResult);
                    } break;
                    case ModuleFunctionType.Info: {
                        var functionResult = ModuleLoader.ExecuteInfoFunction(dll, checkPath, task.Function, (InfoSettings)task.Settings);

                        queuedTask.Completed = DateTime.Now;

                        foreach (var kvpResult in functionResult.InfoResults)
                        {
                            var result = kvpResult.Value;
                            var target = kvpResult.Key;
                            if (result.RanSuccessfully || result.ExecutionException == null)
                            {
                                _log.Info($"Task {task}({result.Target}) executed successfully!");
                                foreach(var item in result.Items)
                                {
                                    _log.Debug($" - {item.Key} = {item.Value}");
                                }
                            }
                            else
                                _log.Warn($"Task {task}({result.Target}) executed with error: {result.ExecutionException.Message}");
                            _log.Info($"  -> {result.Message}");
                        }
                        resultStorage.StoreResult(queuedTask, functionResult);
                    } break;
                    case ModuleFunctionType.Action: {
                        var functionResult = ModuleLoader.ExecuteActionFunction(dll, checkPath, task.Function, (ActionSettings)task.Settings);

                        queuedTask.Completed = DateTime.Now;

                        foreach (var kvpResult in functionResult.ActionResults)
                        {
                            var result = kvpResult.Value;
                            var target = kvpResult.Key;
                            if (result.RanSuccessfully)
                                _log.Info($"Task {task}({result.Target}) executed successfully!");
                            else
                                _log.Warn($"Task {task}({result.Target}) executed with error: {result.ExecutionException.Message}");
                            _log.Info($"  -> {result.Message}");
                        }
                        resultStorage.StoreResult(queuedTask, functionResult);
                    } break;
                }

                _log.Info($"The task {task} completed in { (queuedTask.Completed - queuedTask.Added).TotalSeconds.ToString("F2")} second(s)");

            }
            catch (Exception x)
            {
                _log.Error($"Error running Task {task}({string.Join(",", task.Targets)}): {x.Message}");
            }
        }
Esempio n. 2
0
 private new void EnqueueTasks(List<TaskBase> tasks)
 {
     _log.Debug($"Enqueueing {tasks.Count} task(s).");
     foreach (var task in tasks)
     {
         QueuedTask queuedTask = null;
         if (task.GetType() == typeof(ModuleTask))
         {
             queuedTask = new QueuedModuleTask() { Task = (ModuleTask)task };
         }
         else if (task.GetType() == typeof(InternalTask))
         {
             queuedTask = new QueuedInternalTask() { Task = (InternalTask)task };
         }
         else
         {
             throw new Exception($"Internal scheduler error: Unknown Task Type {task.GetType().ToString()}.");
         }
         if (TaskQueue.Contains(queuedTask))
         {
             // Todo: Handle dead-locked tasks @todo -NM
             _log.Warn($"Skipping task {task}, previous task not completed.");
         }
         else
         {
             queuedTask.Added = DateTime.Now;
             TaskQueue.Enqueue(queuedTask);
         }
     }
 }