Пример #1
0
        /// <summary>
        /// Create the querier options from the command line options.
        /// </summary>
        /// <param name="opts">The command line options.</param>
        /// <returns>The querier options according to the command line options.</returns>
        private static IQuerierOptions CreateQuerierOptions(GlobalOptions opts)
        {
            QuerierOptions returnOptions = QuerierOptions.Default;

            if (opts.SnmpVersion.HasValue)
            {
                returnOptions = returnOptions.WithProtocolVersion(opts.SnmpVersion.Value.ToSnmpVersion()).WithCaching(opts.UseCaching);
            }

            return(returnOptions);
        }
        // TODO: Finalizer nur überschreiben, wenn Dispose(bool disposing) weiter oben Code für die Freigabe nicht verwalteter Ressourcen enthält.
        // ~DataFetchingService()
        // {
        //   // Ändern Sie diesen Code nicht. Fügen Sie Bereinigungscode in Dispose(bool disposing) weiter oben ein.
        //   Dispose(false);
        // }
        /// <inheritdoc />
        public Task StartAsync(CancellationToken cancellationToken)
        {
            IConfigurationSection aquisisionServiceSection = this.configuration.GetSection(Program.RssiAquisitionServiceSectionKey);

            this.usePenaltySystem = aquisisionServiceSection.GetValue<bool>(Program.PenaltySystemEnablingKey);

            this.logger.LogInformation($"Penality system for RSSI aquisition: {(this.usePenaltySystem ? "enabled" : "disabled")}");

            // configure thread pool for number of parallel queries
            this.maxParallelQueries = aquisisionServiceSection.GetValue<int>("MaximumParallelQueries");
            if (this.maxParallelQueries == 0)
            {
                this.maxParallelQueries = Environment.ProcessorCount;
            }

            ThreadPool.GetMinThreads(out int minWorkerThreads, out int minEaThreads);
            ThreadPool.GetMaxThreads(out int maxworkerThreads, out int maxEaThreads);

            this.logger.LogInformation($"Thread pool: Found: Min workers = {minWorkerThreads}, Max workers = {maxworkerThreads}, Min EA {minEaThreads}, Max EA {maxEaThreads}");

            this.refreshInterval = TimeSpan.Parse(aquisisionServiceSection.GetValue<string>("RefreshInterval"));

            var snmpVersion = aquisisionServiceSection.GetValue<int>("SnmpVersion");
            if (snmpVersion != 0)
            {
                // 0 is the default value set by config framework and doesn't make any sense here - so we can use it to identify a missing config
                this.snmpQuerierOptions = this.snmpQuerierOptions.WithProtocolVersion(snmpVersion.ToSnmpVersion());
            }

            var snmpTimeoutConfig = this.configuration.GetSection(Program.RssiAquisitionServiceSectionKey).GetValue<int>("SnmpTimeoutSeconds");
            if (snmpTimeoutConfig != 0)
            {
                // 0 is the default value set by config framework and doesn't make any sense here - so we can use it to identify a missing config
                this.snmpQuerierOptions = this.snmpQuerierOptions.WithTimeout(TimeSpan.FromSeconds(snmpTimeoutConfig));
            }

            var snmpRetriesConfig = aquisisionServiceSection.GetValue<int>("SnmpRetries");
            if (snmpRetriesConfig != 0)
            {
                // 0 is the default value set by config framework and doesn't make any sense here - so we can use it to identify a missing config
                this.snmpQuerierOptions = this.snmpQuerierOptions.WithRetries(snmpRetriesConfig);
            }

            this.snmpQuerierOptions = this.snmpQuerierOptions.WithCaching(aquisisionServiceSection.GetValue<bool>("UseQueryCaching"));

            var hamnetDbConfig = this.configuration.GetSection(HamnetDbProvider.HamnetDbSectionName);

            // by default waiting a couple of secs before first Hamnet scan
            TimeSpan timeToFirstAquisition = TimeSpan.FromSeconds(11);

            this.NewDatabaseContext();

            var status = this.resultDatabaseContext.Status;
            var nowItIs = DateTime.UtcNow;
            var timeSinceLastAquisitionStart = (nowItIs - status.LastRssiQueryStart);
            if (status.LastRssiQueryStart > status.LastRssiQueryEnd)
            {
                this.logger.LogInformation($"STARTING first RSSI aquisition immediately: Last aquisition started {status.LastRssiQueryStart} seems not to have ended successfully (last end time {status.LastRssiQueryEnd})");
            }
            else if (timeSinceLastAquisitionStart < this.refreshInterval)
            {
                // no aquisition required yet (e.g. service restart)
                timeToFirstAquisition = this.refreshInterval - timeSinceLastAquisitionStart;
            }

            this.logger.LogInformation($"STARTING first RSSI aquisition after restart in {timeToFirstAquisition}: Last aquisition started {status.LastRssiQueryStart}, configured interval {this.refreshInterval}");

            this.timer = new Timer(DoFetchData, null, timeToFirstAquisition, this.refreshInterval);

            return Task.CompletedTask;
        }
        // TODO: Finalizer nur überschreiben, wenn Dispose(bool disposing) weiter oben Code für die Freigabe nicht verwalteter Ressourcen enthält.
        // ~DataFetchingService()
        // {
        //   // Ändern Sie diesen Code nicht. Fügen Sie Bereinigungscode in Dispose(bool disposing) weiter oben ein.
        //   Dispose(false);
        // }

        /// <inheritdoc />
        public Task StartAsync(CancellationToken cancellationToken)
        {
            IConfigurationSection aquisisionServiceSection = this.configuration.GetSection(Program.BgpAquisitionServiceSectionKey);

            this.usePenaltySystem = aquisisionServiceSection.GetValue <bool>(Program.PenaltySystemEnablingKey);

            this.logger.LogInformation($"Penality system for BGP aquisition: {(this.usePenaltySystem ? "enabled" : "disabled")}");

            // configure thread pool for number of parallel queries
            this.maxParallelQueries = aquisisionServiceSection.GetValue <int>("MaximumParallelQueries");
            if (this.maxParallelQueries == 0)
            {
                this.maxParallelQueries = Environment.ProcessorCount;
            }

            ThreadPool.GetMinThreads(out int minWorkerThreads, out int minEaThreads);
            ThreadPool.GetMaxThreads(out int maxworkerThreads, out int maxEaThreads);

            this.logger.LogInformation($"Thread pool: Found: Min workers = {minWorkerThreads}, Max workers = {maxworkerThreads}, Min EA {minEaThreads}, Max EA {maxEaThreads}");

            this.refreshInterval = TimeSpan.Parse(aquisisionServiceSection.GetValue <string>("RefreshInterval"));

            var monitoringAccountsSection = this.configuration.GetSection(Program.MonitoringAccountsSectionKey).GetSection(Program.BgpAccountSectionKey);

            // we poll BGP solely using the vendor-specific APIs (SNMP is too fragile and slow for it)
            this.snmpQuerierOptions = this.snmpQuerierOptions.WithAllowedApis(QueryApis.VendorSpecific);

            var loginUserName = monitoringAccountsSection.GetValue <string>("User");

            if (!string.IsNullOrWhiteSpace(loginUserName))
            {
                this.snmpQuerierOptions = this.snmpQuerierOptions.WithUser(loginUserName);
            }

            var loginPassword = monitoringAccountsSection.GetValue <string>("Password");

            if (!string.IsNullOrWhiteSpace(loginPassword))
            {
                this.snmpQuerierOptions = this.snmpQuerierOptions.WithPassword(loginPassword);
            }

            this.snmpQuerierOptions = this.snmpQuerierOptions
                                      .WithCaching(aquisisionServiceSection.GetValue <bool>("UseQueryCaching"))
                                      .WithTimeout(TimeSpan.FromSeconds(2));

            var hamnetDbConfig = this.configuration.GetSection(HamnetDbProvider.HamnetDbSectionName);

            // get filter regex
            var filterRegexConfig = aquisisionServiceSection.GetSection("WhitelistFilterRegex").GetChildren();

            this.filterRegexList = filterRegexConfig.Select(c => new Regex(c.Value, RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)).ToList();
            if (this.filterRegexList.Count == 0)
            {
                this.filterRegexList.Add(new Regex(@".*", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase));
            }

            // by default waiting a couple of secs before first Hamnet scan
            TimeSpan timeToFirstAquisition = TimeSpan.FromSeconds(17);

            this.NewDatabaseContext();

            var status  = this.resultDatabaseContext.Status;
            var nowItIs = DateTime.UtcNow;
            var timeSinceLastAquisitionStart = (nowItIs - status.LastBgpQueryStart);

            if (status.LastBgpQueryStart > status.LastBgpQueryEnd)
            {
                this.logger.LogInformation($"STARTING first BGP aquisition immediately: Last aquisition started {status.LastBgpQueryStart} seems not to have ended successfully (last end time {status.LastBgpQueryEnd})");
            }
            else if (timeSinceLastAquisitionStart < this.refreshInterval)
            {
                // no aquisition required yet (e.g. service restart)
                timeToFirstAquisition = this.refreshInterval - timeSinceLastAquisitionStart;
            }

            this.logger.LogInformation($"STARTING first BGP aquisition after restart in {timeToFirstAquisition}: Last aquisition started {status.LastBgpQueryStart}, configured interval {this.refreshInterval}");

            this.timer = new Timer(DoFetchData, null, timeToFirstAquisition, this.refreshInterval);

            return(Task.CompletedTask);
        }