コード例 #1
0
 public CompositeRoot()
 {
     ClientConsole       = new ClientConsole();
     InfluxDbAgentLoader = new InfluxDbAgentLoader();
     FileLoaderAgent     = new FileLoaderAgent();
     ConfigBusiness      = new ConfigBusiness(FileLoaderAgent);
     ConfigBusiness.InvalidConfigEvent += InvalidConfigEvent;
     CounterBusiness   = new CounterBusiness();
     PublisherBusiness = new PublisherBusiness();
     MetaDataBusiness  = new MetaDataBusiness();
     SendBusiness      = new SendBusiness(ConfigBusiness, new ConsoleQueueEvents(ClientConsole));
     TagLoader         = new TagLoader(ConfigBusiness);
     SocketClient      = new SocketClient();
 }
コード例 #2
0
        public override async Task <int> CollectRegisterCounterValuesAsync()
        {
            lock (_syncRoot)
            {
                try
                {
                    StopTimer();

                    var swMain   = new StopwatchHighPrecision();
                    var timeInfo = new Dictionary <string, long>();

                    if (_timestamp == null)
                    {
                        _timestamp = DateTime.UtcNow;
                        var nw = Floor(_timestamp.Value, new TimeSpan(0, 0, 0, 1));
                        _timestamp = nw;
                    }

                    var timestamp = DateTime.UtcNow;

                    var precision = TimeUnit.Seconds;
                    //No synchronization when running the safe collector engine
                    //timeInfo.Add(TimerConstants.Synchronize, swMain.ElapsedSegment);

                    //TODO: Create a mutex lock here (So that two counters canno read the same signature at the same time, since the content of the _performanceCounterGroup might change during this process.

                    //Prepare read
                    var performanceCounterInfos = PrepareCounters();
                    timeInfo.Add(TimerConstants.Prepare, swMain.ElapsedSegment);

                    //Perform Read (This should be as fast and short as possible)
                    var values = ReadValues(performanceCounterInfos);
                    timeInfo.Add(TimerConstants.Read, swMain.ElapsedSegment);

                    //Prepare result
                    var points = FormatResult(performanceCounterInfos, values, precision, timestamp).ToArray();
                    timeInfo.Add(TimerConstants.Format, swMain.ElapsedSegment);

                    //Queue result
                    Enqueue(points);
                    timeInfo.Add(TimerConstants.Enque, swMain.ElapsedSegment);

                    //Cleanup
                    RemoveObsoleteCounters(values, performanceCounterInfos);
                    timeInfo.Add(TimerConstants.Cleanup, swMain.ElapsedSegment);

                    if (_metadata)
                    {
                        Enqueue(MetaDataBusiness.GetCollectorPoint(EngineName, Name, points.Length, timeInfo, null).ToArray());
                    }

                    OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(Name, points.Length, timeInfo, 0, OutputLevel.Default));

                    //TODO: Release mutex

                    //TOOD: Send metadata about the read to influx, (this should be configurable)

                    return(points.Length);
                }
                catch (Exception exception)
                {
                    OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(Name, exception));
                    return(-1);
                }
                finally
                {
                    ResumeTimer();
                }
            }
        }
コード例 #3
0
        public override async Task <int> CollectRegisterCounterValuesAsync()
        {
            lock (_syncRoot)
            {
                try
                {
                    var swMain   = new StopwatchHighPrecision();
                    var timeInfo = new Dictionary <string, long>();

                    double elapseOffsetSeconds = 0;
                    if (_timestamp == null)
                    {
                        _sw        = new StopwatchHighPrecision();
                        _timestamp = DateTime.UtcNow;
                        var nw = Floor(_timestamp.Value, new TimeSpan(0, 0, 0, 1));
                        _timestamp = nw;
                    }
                    else
                    {
                        var elapsedTotal = _sw.ElapsedTotal;

                        elapseOffsetSeconds = new TimeSpan(elapsedTotal).TotalSeconds - SecondsInterval * _counter;

                        if (_missCounter >= 6)
                        {
                            //Reset everything and start over.
                            OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(Name, string.Format("Missed {0} cycles. Resetting and starting over.", _missCounter), OutputLevel.Warning));

                            _timestamp   = null;
                            _counter     = 0;
                            _missCounter = 0;
                            return(-4);
                        }

                        if (elapseOffsetSeconds > SecondsInterval)
                        {
                            _missCounter++;
                            _counter = _counter + 1 + (int)(elapseOffsetSeconds / SecondsInterval);
                            OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(Name, string.Format("Dropping {0} steps.", (int)elapseOffsetSeconds), OutputLevel.Warning));
                            return(-2);
                        }

                        if (elapseOffsetSeconds < SecondsInterval * -1)
                        {
                            _missCounter++;
                            OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(Name, string.Format("Jumping 1 step. ({0} = new TimeSpan({1}).TotalSeconds - {2} * {3})", (int)elapseOffsetSeconds, elapsedTotal, SecondsInterval, _counter), OutputLevel.Warning));
                            return(-3);
                        }

                        _missCounter = 0;

                        //Adjust interval
                        var next = 1000 * (SecondsInterval - elapseOffsetSeconds);
                        if (next > 0)
                        {
                            SetTimerInterval(next);
                        }
                    }

                    var timestamp = _timestamp.Value.AddSeconds(SecondsInterval * _counter);
                    _counter++;

                    var precision = TimeUnit.Seconds;
                    timeInfo.Add(TimerConstants.Synchronize, swMain.ElapsedSegment);

                    //TODO: Create a mutex lock here (So that two counters canno read the same signature at the same time, since the content of the _performanceCounterGroup might change during this process.

                    //Prepare read
                    var performanceCounterInfos = PrepareCounters();
                    timeInfo.Add(TimerConstants.Prepare, swMain.ElapsedSegment);

                    //Perform Read (This should be as fast and short as possible)
                    var values = ReadValues(performanceCounterInfos);
                    timeInfo.Add(TimerConstants.Read, swMain.ElapsedSegment);

                    //Prepare result
                    var points = FormatResult(performanceCounterInfos, values, precision, timestamp).ToArray();
                    timeInfo.Add(TimerConstants.Format, swMain.ElapsedSegment);

                    //Queue result
                    Enqueue(points);
                    timeInfo.Add(TimerConstants.Enque, swMain.ElapsedSegment);

                    //Cleanup
                    RemoveObsoleteCounters(values, performanceCounterInfos);
                    timeInfo.Add(TimerConstants.Cleanup, swMain.ElapsedSegment);

                    if (_metadata)
                    {
                        Enqueue(MetaDataBusiness.GetCollectorPoint(EngineName, Name, points.Length, timeInfo, elapseOffsetSeconds).ToArray());
                    }

                    OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(Name, points.Count(), timeInfo, elapseOffsetSeconds, OutputLevel.Default));

                    //TODO: Release mutex

                    //TOOD: Send metadata about the read to influx, (this should be configurable)

                    return(points.Length);
                }
                catch (Exception exception)
                {
                    OnCollectRegisterCounterValuesEvent(new CollectRegisterCounterValuesEventArgs(Name, exception));
                    return(-1);
                }
            }
        }
コード例 #4
0
        protected async Task <IDatabaseConfig> GetUsernameAsync(string paramList, int index, IDatabaseConfig config, string action)
        {
            var dataChanged = false;

            var url = config.Url;

            IInfluxDbAgent      client   = null;
            InfluxDbApiResponse response = null;

            try
            {
                if (!string.IsNullOrEmpty(config.Name) && !string.IsNullOrEmpty(config.Username) && !string.IsNullOrEmpty(config.Password))
                {
                    client   = _influxDbAgentLoader.GetAgent(config);
                    response = await MetaDataBusiness.TestWriteAccess(client, action);
                }
            }
            catch (Exception exception)
            {
                OutputError(exception.Message);
            }

            if (response == null || !response.Success)
            {
                OutputInformation("Enter the database, username and password for the InfluxDB.");
            }

            while (response == null || !response.Success)
            {
                var database = string.Empty;
                try
                {
                    database = QueryParam <string>("DatabaseName", GetParam(paramList, index++));
                    var user     = QueryParam <string>("Username", GetParam(paramList, index++));
                    var password = QueryParam <string>("Password", GetParam(paramList, index++));
                    config = new InfluxDatabaseConfig(url, user, password, database);

                    client   = _influxDbAgentLoader.GetAgent(config);
                    response = await MetaDataBusiness.TestWriteAccess(client, action);

                    dataChanged = true;
                }
                catch (CommandEscapeException)
                {
                    return(null);
                }
                catch (InfluxDbApiException exception)
                {
                    if (exception.StatusCode == HttpStatusCode.NotFound)
                    {
                        var create = QueryParam("Database does not exist, create?", GetParam(paramList, index++), new Dictionary <bool, string>()
                        {
                            { true, "Yes" }, { false, "No" }
                        });
                        if (create)
                        {
                            client.CreateDatabaseAsync(database);
                            response    = MetaDataBusiness.TestWriteAccess(client, action).Result;
                            dataChanged = true;
                        }
                    }
                }
                catch (Exception exception)
                {
                    OutputError("{0}", exception.Message);
                }
            }

            OutputInformation("Access to database {0} confirmed.", config.Name);

            if (dataChanged)
            {
                _configBusiness.SaveDatabaseConfig(config.Name, config.Username, config.Password);
            }

            return(config);
        }
コード例 #5
0
 public ExactCollectorEngine(IPerformanceCounterGroup performanceCounterGroup, ISendBusiness sendBusiness, ITagLoader tagLoader, bool metadata)
     : base(performanceCounterGroup, sendBusiness, tagLoader, metadata)
 {
     _metaDataBusiness = new MetaDataBusiness();
 }