Class to POST the agent payload data to the Server Density servers.
Exemple #1
0
        public void Execute(IJobExecutionContext context)
        {
            var retransmitRootPath = Path.Combine(basePath, "_retransmit");

            // var zeroPath = Path.Combine(retransmitRootPath, "0");

            if (!Directory.Exists(retransmitRootPath))
            {
                return;
            }


            foreach (var file in Directory.GetFiles(retransmitRootPath, "*.json", SearchOption.AllDirectories))
            {
                var values = File.ReadAllText(file);

                var transmitValues = PayloadPoster.TransmitValues(values, bypassSave: true);

                if (transmitValues)
                {
                    File.Delete(file);
                }
                else
                {
                    var partOfPath = file.Replace(retransmitRootPath, "");

                    if (partOfPath.StartsWith("\\"))
                    {
                        partOfPath = partOfPath.Substring(1);
                    }

                    string[] splitPath = partOfPath.Split('\\');

                    int Number = Convert.ToInt32(splitPath[0]);

                    if (Number == Config.ConfigurationSingleton.Instance.Config.RetriesCount - 1)
                    {
                        File.Move(file, Path.Combine(retransmitRootPath, (Number).ToString(), Path.GetFileNameWithoutExtension(file) + ".failed"));
                    }
                    else
                    {
                        File.Move(file, Path.Combine(retransmitRootPath, (Number + 1).ToString(), Path.GetFileName(file)));
                    }
                }
            }
        }
Exemple #2
0
        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            var results = new Dictionary <string, object>();
            var objList = new List <dynamic>();

            if (systemStatsSent)
            {
                sdCoreChecks.RemoveAll(f => f.Key == "systemStats");
            }

            systemStatsSent = true;
            logger.Info("Polling Checks");
            Parallel.ForEach(
                sdCoreChecks,
                check =>
            {
                logger.Debug($"{check.GetType()}: start");

                try
                {
                    var result = check.DoCheck();

                    if (result == null)
                    {
                        return;
                    }

                    results.Add(check.Key, result);

                    logger.Debug("{0}: end", check.GetType());
                }
                catch (Exception ex)
                {
                    logger.Error(ex, $"Error on {check.GetType()}");
                }
            });

            var pluginCollection = new List <Metric>();

            logger.Info("Polling Core Metrics");
            Parallel.ForEach(sdCoreMetrics, check =>
            {
                logger.Trace($"{check.GetType(): start}");

                try
                {
                    var result = check.GetMetrics();

                    if (result == null || !result.Any())
                    {
                        return;
                    }

                    pluginCollection.AddRange(result);
                }
                catch (Exception ex)
                {
                    logger.Error(ex, $"Error on {check.GetType()}");
                }

                logger.Trace($"{check.GetType(): end}");
            });

            logger.Info("Polling long checks");

            try
            {
                var longRunningChecks = instanceProxy.GetLongRunningChecks();

                if (longRunningChecks.Any())
                {
                    foreach (var lrc in longRunningChecks)
                    {
                        var list = JsonConvert.DeserializeObject <List <Metric> >(lrc.Value);
                        list.ForEach(f => f.Timestamp = DateTime.UtcNow);
                        pluginCollection.Add(list);
                    }
                }
            }
            catch (RemotingException remoting)
            {
                logger.Warn("Remoting exception, will reload plugins", remoting);
                pluginChangeDetected = true;
            }
            catch (Exception exception)
            {
                logger.Warn(exception, "Error on polling for long checks");
            }



            Parallel.ForEach(
                ICheckTypeList,
                check =>
            {
                logger.Debug("{0}: start", check);
                try
                {
                    var result = instanceProxy.PerformCheck(check);

                    if (result == null || !result.Any())
                    {
                        return;
                    }

                    pluginCollection.Add(result);

                    logger.Debug($"{check}: end");
                }
                catch (Exception ex)
                {
                    logger.Error(ex, $"Error on {check.GetType()}");
                }
            });


            var serializeObject = JsonConvert.SerializeObject(pluginCollection, Formatting.None, new MetricJsonConverter());

            results.Add("metrics", JsonConvert.DeserializeObject(serializeObject));

            try
            {
                var poster = new PayloadPoster(results);
                poster.Post();
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Error with sending data to SD servers");
            }

            CheckIfNeedToReloadPlugins();
        }