public AgentContext Load(AgentContext context)
        {
            context = LoadFromConfigXml(context);

            if (context != null)
            {
                context = LoadFromCloud(context);
            }

            return context;
        }
        private AgentContext LoadFromCloud(AgentContext context)
        {
            Log.Debug("Loading agent context from cloud");

            // load base config
            try
            {
                var baseConfig = _cloudService.GetBaseConfig(context.ServerKey);
                if (baseConfig != null)
                {
                    var settings = baseConfig.Settings;
                    if (settings != null)
                    {
                        LoadAgentContextSettings(context, settings);
                    }

                    var pluginResources = baseConfig.PluginResources;
                    if (pluginResources != null)
                    {
                        foreach (var pluginResource in pluginResources)
                        {
                            context.AddPluginResource(pluginResource);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WarnFormat("Loading agent context from cloud failed with message: {0}", ex.Message);
                Log.Error(ex);
            }

            // load schedules
            try
            {
                var schedules = _cloudService.GetSchedules(context.ServerKey);
                if (schedules != null)
                {
                    context.PluginResourceSchedules = schedules.Where(s => context.PluginResources.Any(r => r.TextKey == s.PluginResourceTextKey)).ToList();
                }
            }
            catch (Exception ex)
            {
                Log.WarnFormat("Loading agent context schedules from cloud failed with message: {0}", ex.Message);
                Log.Error(ex);
            }

            return context;
        }
        private AgentContext LoadFromConfigXml(AgentContext context)
        {
            Log.DebugFormat("Loading agent context from config file: {0}", AgentContext.ConfigFilePath);

            // extract configuration from xml file
            var configXml = AgentContext.ConfigXml;
            if (configXml == null)
            {
                Log.DebugFormat("No agent context config file found");
                return null;
            }

            // <service>
            LoadAgentContextSettings(context, configXml.XPathSelectElements("//service/add").ToDictionary(e => e.Attribute<string>("key"), v => v.Attribute<string>("value")));

            // <plugins>
            foreach (var resourceNode in configXml.XPathSelectElements("//plugins/plugin/resource"))
            {
                if (context.PluginBlacklist == null || !context.PluginBlacklist.Contains(resourceNode.Attribute<string>("category"), StringComparer.InvariantCultureIgnoreCase))
                {
                    context.AddPluginResource(new PluginResource
                    {
                        PluginName = resourceNode.Parent.Attribute<string>("name"),
                        Category = resourceNode.Attribute<string>("category"),
                        CategoryTextKey = resourceNode.Attribute<string>("categoryTextKey"),
                        Label = resourceNode.Attribute<string>("label"),
                        ResourceTextKey = resourceNode.Attribute<string>("textKey"),
                        Unit = resourceNode.Attribute<string>("unit"),
                        RequiresOptionTextField = resourceNode.Attribute<bool>("requiresOptionTextField"),
                        Script = !string.IsNullOrWhiteSpace(resourceNode.Attribute<string>("psFile")) ? File.ReadAllText(Path.Combine(AgentContext.CustomPowerShellPluginsDirectoryPath, resourceNode.Attribute<string>("psFile"))) : null,
                        ConfigTextKey = resourceNode.Attribute<string>("configTextKey"),
                    });
                }
            }

            // <config>
            var configNode = configXml.XPathSelectElement("//config");
            if (configNode != null)
            {
                foreach (var node in configNode.Elements())
                {
                    context.Config[node.Name.LocalName] = node.Elements("add").ToDictionary(e => e.Attribute<string>("key"), v => v.Attribute<string>("value"));
                }
            }

            return context;
        }
 private void LoadAgentContextSettings(AgentContext context, System.Collections.Generic.IDictionary<string, string> settings)
 {
     if (settings.ContainsKey("ServerKey")) 
     {
         context.ServerKey = settings["ServerKey"];
     }
     if (settings.ContainsKey("DiscoveryCheckInterval"))
     {
         context.DiscoveryCheckInterval = (int)TimeSpan.FromSeconds(Math.Abs(int.Parse(settings["DiscoveryCheckInterval"]))).TotalMilliseconds;
     }
     if (settings.ContainsKey("MetricResultsQueueMax"))
     {
         context.MetricResultsQueueMax = Math.Abs(int.Parse(settings["MetricResultsQueueMax"]));
     }
     if (settings.ContainsKey("MetricResultsQueueBackupThreshold"))
     {
         context.MetricResultsQueueBackupThreshold = Math.Abs(int.Parse(settings["MetricResultsQueueBackupThreshold"]));
     }
     if (settings.ContainsKey("MaxMetricResultsPerSync"))
     {
         context.MaxMetricResultsPerSync = Math.Abs(int.Parse(settings["MaxMetricResultsPerSync"]));
     }
     if (settings.ContainsKey("EnableAlertCheckDelay"))
     {
         context.EnableAlertCheckDelay = bool.Parse(settings["EnableAlertCheckDelay"]);
     }
     if (settings.ContainsKey("PluginBlacklist"))
     {
         context.PluginBlacklist = settings["PluginBlacklist"].Split(",".ToCharArray()).Select(s => s.Trim()).ToArray();
     }
 }
        /// <summary>
        /// Seeds class properties with data from the agent config file.
        /// <para>The location of the agent config file is read from the Registry</para>
        /// <p
        /// </summary>
        public static void Load()
        {
            Current = DependencyRegistry.Resolve<IAgentContextLoader>().Load(new AgentContext());

            // queue metric result backups
            try
            {
                foreach (var resultFile in Directory.GetFiles(ResultsBackupDirectory, "*.json"))
                {
                    Current._metricAnalyzerResultQueue.Enqueue(JsonConvert.DeserializeObject<AnalyzerResult>(File.ReadAllText(resultFile)));
                    File.Delete(resultFile);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
 public AgentContext Load(AgentContext context)
 {
     context.ServerKey = "test";
     return context;
 }