public IGenericPlugin CreateInstance(string entry, IPlugInContext context)
        {
            IConfiguration config = context.Configuration;
            ILogger        logger = context.Logger;

            switch (entry.ToLower())
            {
            case PACKAGE_UPDATE:
                return(new PackageUpdater(context, new AutoUpdateServiceHttpClient()));

            case CONFIG_UPDATE:
                return(new ConfigurationFileUpdater(context));

            default:
                throw new ArgumentException($"Source {entry} not recognized.");
            }
        }
        /// <summary>
        /// EventSource constructor that takes a context
        /// </summary>
        /// <param name="context">The context object providing access to config, logging and metrics</param>
        public EventSource(IPlugInContext context)
        {
            this._context = context;
            this._config  = context.Configuration;
            this._logger  = context.Logger;
            this._metrics = context.Metrics;
            string required = _config?[ConfigConstants.REQUIRED];

            if (string.IsNullOrWhiteSpace(required))
            {
                _required = true;
            }
            else
            {
                _required = bool.Parse(required);
            }
        }
Пример #3
0
        private const int FLUSH_QUEUE_DELAY = 100; //Throttle at about 10 TPS

        public CloudWatchSink(int defaultInterval, IPlugInContext context, IAmazonCloudWatch cloudWatchClient) : base(defaultInterval, context)
        {
            _cloudWatchClient   = cloudWatchClient;
            _defaultRetryPolicy = new DefaultRetryPolicy(_cloudWatchClient.Config);

            //StorageResolution is used to specify standard or high-resolution metrics. Valid values are 1 and 60
            //It is different to interval.
            //See https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_MetricDatum.html for full details
            _storageResolution = base._interval < 60 ? 1 : 60;

            string dimensionsConfig = null;

            if (_config != null)
            {
                dimensionsConfig = _config["dimensions"];
                _namespace       = _config["namespace"];
            }
            if (!string.IsNullOrEmpty(dimensionsConfig))
            {
                List <Dimension> dimensions     = new List <Dimension>();
                string[]         dimensionPairs = dimensionsConfig.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var dimensionPair in dimensionPairs)
                {
                    string[] keyValue = dimensionPair.Split('=');
                    string   value    = ResolveVariables(keyValue[1]);
                    dimensions.Add(new Dimension()
                    {
                        Name = keyValue[0], Value = value
                    });
                }
                _dimensions = dimensions.ToArray();
            }
            else
            {
                _dimensions = DefaultDimensions;
            }

            if (string.IsNullOrEmpty(_namespace))
            {
                _namespace = "KinesisTap";
            }
            else
            {
                _namespace = ResolveVariables(_namespace);
            }
        }
Пример #4
0
        public ISource CreateInstance(string entry, IPlugInContext context)
        {
            IConfiguration config = context.Configuration;
            ILogger        logger = context.Logger;

            switch (entry.ToLower())
            {
            case ULSSOURCE:
                UlsLogParser ulsParser = new UlsLogParser();
                return(DirectorySourceFactory.CreateEventSource(
                           context,
                           ulsParser,
                           DirectorySourceFactory.CreateDelimitedLogSourceInfo));

            default:
                throw new ArgumentException($"Source {entry} not recognized.");
            }
        }
        public IPipe CreateInstance(string entry, IPlugInContext context)
        {
            IConfiguration config = context.Configuration;
            ILogger        logger = context.Logger;

            switch (entry.ToLower())
            {
            case REGEX_FILTER_PIPE:
                Type sourceType          = (Type)context.ContextData[PluginContext.SOURCE_TYPE];
                Type sinkType            = (Type)context.ContextData[PluginContext.SINK_TYPE];
                Type sourceDataType      = sourceType.GenericTypeArguments[0];
                Type regexFilterPipeType = typeof(RegexFilterPipe <>).MakeGenericType(sourceDataType);
                return((IPipe)Activator.CreateInstance(regexFilterPipeType, context));

            default:
                throw new ArgumentException($"Source {entry} not recognized.");
            }
        }
Пример #6
0
        public IPipe CreateInstance(string entry, IPlugInContext context)
        {
            Type sourceOutputType = (Type)context.ContextData[PluginContext.SOURCE_OUTPUT_TYPE];

            switch (entry.ToLower())
            {
            case REGEX_FILTER_PIPE:
                Type regexFilterPipeType = typeof(RegexFilterPipe <>).MakeGenericType(sourceOutputType);
                return((IPipe)Activator.CreateInstance(regexFilterPipeType, context));

            case EMF_PIPE:
                Type emfPipeType = typeof(EMFPipe <>).MakeGenericType(sourceOutputType);
                return((IPipe)Activator.CreateInstance(emfPipeType, context));

            default:
                throw new ArgumentException($"Source {entry} not recognized.");
            }
        }
Пример #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="S3Sink"/> class.
        /// Note that the default batch interval is set to five minutes,
        /// the default number of allowed records is set to ten thousand,
        /// and the maximum batch size is 160 GB (the max size of a singe-file
        /// S3 upload.  These values can be configured at the agent level.
        /// </summary>
        /// <param name="context">The <see cref="IPlugInContext"/> that contains configuration info, logger, metrics etc.</param>
        public S3Sink(IPlugInContext context) : base(context, 5 * 60, 10000, OneHundredSixtyGigabytes)
        {
            _bucketName = ResolveVariables(_config[AWSConstants.BucketName]).ToLower();
            if (string.IsNullOrWhiteSpace(_bucketName) || _bucketName.Equals(AWSConstants.BucketName))
            {
                throw new ArgumentException("'BucketName' setting in config file cannot be null, whitespace, or 'BucketName'");
            }

            _filePath = ParseStaticConfig(ResolveVariables(_config[AWSConstants.FilePath]));
            _fileName = ParseStaticConfig(ResolveVariables(_config[AWSConstants.FileName]));

            if (string.IsNullOrWhiteSpace(_fileName) || _bucketName.Equals(AWSConstants.FileName))
            {
                throw new ArgumentException("'FileName' setting in config file cannot be null, whitespace, or 'FileName'");
            }

            _throttle = new AdaptiveThrottle(new TokenBucket(1, 5), _backoffFactor, _recoveryFactor, _minRateAdjustmentFactor);
        }
        public ISource CreateInstance(string entry, IPlugInContext context)
        {
            IConfiguration config = context.Configuration;
            ILogger        logger = context.Logger;

            switch (entry.ToLower())
            {
            case "exchangelogsource":
                ExchangeLogParser exchangeLogParser = new ExchangeLogParser();
                exchangeLogParser.TimeStampField = config["TimeStampField"];
                return(DirectorySourceFactory.CreateEventSource(
                           context,
                           exchangeLogParser));

            default:
                throw new ArgumentException($"Source {entry} not recognized.");
            }
        }
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="directory">Path of the directory to monitor</param>
        /// <param name="filterSpec">File name filter</param>
        /// <param name="logger">Logger</param>
        public DirectorySource(
            string directory,
            string filterSpec,
            int interval,
            IPlugInContext context,
            IRecordParser <TData, TContext> recordParser
            ) : base(new DirectoryDependency(directory), context)
        {
            Guard.ArgumentNotNullOrEmpty(directory, nameof(directory));
            _directory   = directory;
            _filterSpec  = filterSpec;
            _fileFilters = ParseFilterSpec(filterSpec);
            if (_fileFilters.Length > 1)
            {
                _fileFilterRegexs = _fileFilters.Select(ff => new Regex(Utility.WildcardToRegex(ff, true)))
                                    .ToArray();
            }
            _interval     = interval;
            _recordParser = recordParser;
            if (_config != null)
            {
                _skipLines = Utility.ParseInteger(_config["SkipLines"], 0);
                _encoding  = _config["Encoding"];
                bool.TryParse(this._config["IncludeSubdirectories"], out bool result);
                this.includeSubdirectories = result ? bool.Parse(this._config["IncludeSubdirectories"]) : false;
                if (this.includeSubdirectories)
                {
                    this.includeDirectoryFilter = _config["IncludeDirectoryFilter"]?.Split(';');
                }
                if (bool.TryParse(_config["BookmarkOnBufferFlush"] ?? "false", out bool bookmarkOnBufferFlush))
                {
                    this.bookmarkOnBufferFlush = bookmarkOnBufferFlush;
                }
            }

            this.bookmarkDir = Path.GetDirectoryName(GetBookmarkFilePath());
            if (!Directory.Exists(this.bookmarkDir))
            {
                Directory.CreateDirectory(this.bookmarkDir);
            }

            _timer = new Timer(OnTimer, null, Timeout.Infinite, Timeout.Infinite);
            DelayBetweenDependencyPoll = TimeSpan.FromSeconds(5);
        }
Пример #10
0
        public KinesisTapProfileRefreshingAWSCredentials(IPlugInContext context) : base(GetProfileConfiguration(context))
        {
            _context = context;
            var config = context?.Configuration;

            string refreshInterval = config?["refreshinterval"];

            if (!string.IsNullOrWhiteSpace(refreshInterval))
            {
                RefreshInterval = int.Parse(refreshInterval);
            }

            string warningIntervalSeconds = config?["warninginterval"];

            if (!string.IsNullOrWhiteSpace(warningIntervalSeconds))
            {
                _warningIntervalSeconds = int.Parse(warningIntervalSeconds);
            }
        }
Пример #11
0
        public TextDecorationExEvaluator(string objectDecoration,
                                         Func <string, string> evaluateVariable,
                                         Func <string, IEnvelope, object> evaluateRecordVariable,
                                         IPlugInContext context
                                         )
        {
            _tree = TextDecorationParserFacade.ParseTextDecoration(objectDecoration);
            FunctionBinder binder = new FunctionBinder(new Type[] { typeof(BuiltInFunctions) });
            ExpressionEvaluationContext <IEnvelope> evalContext = new ExpressionEvaluationContext <IEnvelope>(
                evaluateVariable,
                evaluateRecordVariable,
                binder,
                context?.Logger
                );
            var validator = new TextDecorationValidator <IEnvelope>(evalContext);

            validator.Visit(_tree, null); //Should throw if cannot resolve function
            _interpreter = new TextDecorationInterpreter <IEnvelope>(evalContext);
        }
        /// <summary>
        /// Create AWS Client from plug-in context
        /// </summary>
        /// <typeparam name="TAWSClient">The type of AWS Client</typeparam>
        /// <param name="context">Plug-in context</param>
        /// <returns>AWS Client</returns>
        public static TAWSClient CreateAWSClient <TAWSClient>(IPlugInContext context, RegionEndpoint regionOverride = null) where TAWSClient : AmazonServiceClient
        {
            (AWSCredentials credential, RegionEndpoint region) = GetAWSCredentialsRegion(context);
            if (regionOverride != null)
            {
                region = regionOverride;
            }

            var clientConfig = CreateAWSClientConfig <TAWSClient>(context, region);
            var awsClient    = (TAWSClient)Activator.CreateInstance(typeof(TAWSClient), credential, clientConfig);

            var headers = new Dictionary <string, string> {
                [AWSSDKUtils.UserAgentHeader] = UserAgent
            };

            if (context.Configuration[ConfigConstants.SINK_TYPE]?.ToLower() == AWSEventSinkFactory.CLOUD_WATCH_LOG_EMF)
            {
                headers.Add("x-amzn-logs-format", "Structured");
            }

            var customHeaderSection = context.Configuration.GetSection(ConfigConstants.CUSTOM_AWS_CLIENT_HEADERS);

            if (customHeaderSection != null)
            {
                foreach (var customHeader in customHeaderSection.GetChildren())
                {
                    headers.Add(customHeader.Key, ResolveConfigVariable(customHeader.Value));
                }
            }

            awsClient.BeforeRequestEvent += (sender, args) =>
            {
                if (args is WebServiceRequestEventArgs wsArgs)
                {
                    foreach (var kvp in headers)
                    {
                        wsArgs.Headers[kvp.Key] = kvp.Value;
                    }
                }
            };

            return(awsClient);
        }
Пример #13
0
        private static (string profile, string filePath) GetProfileConfiguration(IPlugInContext context)
        {
            var    config  = context?.Configuration;
            string profile = config?["profile"];

            if (string.IsNullOrWhiteSpace(profile))
            {
                profile = SharedCredentialsFile.DefaultProfileName;
            }

            string filePath = config?["filepath"];

            if (string.IsNullOrWhiteSpace(filePath))
            {
                filePath = SharedCredentialsFile.DefaultFilePath;
            }

            return(profile, filePath);
        }
Пример #14
0
        /// <summary>
        /// 读取入口点,执行入口点方法
        /// </summary>
        private void StartFromCode()
        {
            if (EntryPoint != null)
            {
                EntryPoint.Start(Context);
                return;
            }
            string entryPointTypeName = PlugInInfo.PlugInData.EntryPoint.Type;
            int    runtimeDllCount    = PlugInInfo.PlugInData.Runtime.Assemblies.Count;

            for (int i = runtimeDllCount - 1; i >= 0; i--)
            {
                string runtimeAssemblyPath = PlugInInfo.PlugInData.Runtime.Assemblies[i].Path;

                System.Reflection.Assembly dllAssemblly = System.Reflection.Assembly.LoadFrom(runtimeAssemblyPath);
                Type type = dllAssemblly.GetType(entryPointTypeName);
                if (type != null)
                {
                    object obj = Activator.CreateInstance(type);
                    entryPoint = obj as IPlugInEntryPoint;
                    if (EntryPoint != null)
                    {
                        PlugInContext context = new PlugInContext();
                        context.Framework = Framework.Inistace;
                        context.PlugIn    = this;

                        this.Context = context;
                        EntryPoint.Start(context);
                    }
                    //System.Reflection.MethodInfo method = type.GetMethod(ConstantString.EntryPointStartMethod);
                    //if (method != null)
                    //{
                    //    PlugInContext context = new PlugInContext();
                    //    context.Framework = Framework.Inistace;
                    //    context.PlugIn = this;
                    //    this.Context = context;
                    //    method.Invoke(obj, new object[] { context });
                    //}
                }
            }
        }
Пример #15
0
        public EventLogSource(string logName, string query, IPlugInContext context) : base(new ServiceDependency("EventLog"), context)
        {
            Guard.ArgumentNotNullOrEmpty(logName, nameof(logName));
            this.logName           = logName;
            this.query             = query;
            this.recordSubject     = new Subject <IEnvelope <EventInfo> >();
            this.bookmarkDirectory = Path.GetDirectoryName(this.GetBookmarkFilePath());
            if (!Directory.Exists(this.bookmarkDirectory))
            {
                Directory.CreateDirectory(this.bookmarkDirectory);
            }

            if (bool.TryParse(context?.Configuration?["BookmarkOnBufferFlush"] ?? "false", out bool bookmarkOnBufferFlush))
            {
                this.bookmarkOnBufferFlush = bookmarkOnBufferFlush;
            }

            if (string.IsNullOrWhiteSpace(query))
            {
                this.eventLogQuery = new EventLogQuery(this.logName, PathType.LogName);
            }
            else
            {
                this.eventLogQuery = new EventLogQuery(this.logName, PathType.LogName, this.query);
            }

            if (bool.TryParse(context?.Configuration?["IncludeEventData"], out bool incEventData))
            {
                this.includeEventData = incEventData;
            }

            this.customFilters = context?.Configuration?["CustomFilters"]?.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries) ?? new string[0];
            if (this.customFilters.Length > 0)
            {
                var badFilters = string.Join(",", this.customFilters.Where(i => EventInfoFilters.GetFilter(i) == null));
                if (!string.IsNullOrWhiteSpace(badFilters))
                {
                    throw new ConfigurationException($"Custom filter/s '{badFilters}' do not exist. Please check the filter names.");
                }
            }
        }
        public PackageInstaller(IPlugInContext context,
                                IAppDataFileProvider appDataFileProvider,
                                ISet <string> allowedPublishers,
                                bool skipSignatureVerification)
        {
            _context                   = context;
            _logger                    = context.Logger;
            _allowedPublishers         = allowedPublishers;
            _skipSignatureVerification = skipSignatureVerification;
            _logger                    = context.Logger;
            _metrics                   = context.Metrics;

            _metrics?.InitializeCounters(string.Empty, MetricsConstants.CATEGORY_PLUGIN, CounterTypeEnum.Increment, new Dictionary <string, MetricValue>
            {
                { $"{UpdateMetricsConstants.Prefix}{UpdateMetricsConstants.PackagesDownloaded}", MetricValue.ZeroCount },
                { $"{UpdateMetricsConstants.Prefix}{UpdateMetricsConstants.PackageSignaturesValid}", MetricValue.ZeroCount },
                { $"{UpdateMetricsConstants.Prefix}{UpdateMetricsConstants.PackageSignaturesInvalid}", MetricValue.ZeroCount },
                { $"{UpdateMetricsConstants.Prefix}{UpdateMetricsConstants.PowershellExecutions}", MetricValue.ZeroCount }
            });
            _appDataFileProvider = appDataFileProvider;
        }
        //http://docs.aws.amazon.com/firehose/latest/dev/limits.html
        public KinesisFirehoseSink(
            IPlugInContext context
            ) : base(context, 1, 500, 4 * 1024 * 1024)
        {
            if (_count > 500)
            {
                throw new ArgumentException("The maximum buffer size for firehose is 500");
            }

            //Set Defaults
            if (!int.TryParse(_config[ConfigConstants.RECORDS_PER_SECOND], out _maxRecordsPerSecond))
            {
                _maxRecordsPerSecond = 5000;
            }

            if (!long.TryParse(_config[ConfigConstants.BYTES_PER_SECOND], out _maxBytesPerSecond))
            {
                _maxBytesPerSecond = 5 * 1024 * 1024;
            }

            string combineRecords = _config["CombineRecords"];

            if (!string.IsNullOrWhiteSpace(combineRecords) && bool.TryParse(combineRecords, out bool canCombineRecords))
            {
                CanCombineRecords = canCombineRecords;
            }

            _deliveryStreamName = ResolveVariables(_config["StreamName"]);

            _throttle = new AdaptiveThrottle(
                new TokenBucket[]
            {
                new TokenBucket(1, _maxRecordsPerSecond / 200),   //Number of API calls per second. For simplicity, we tie to _maxRecordsPerSecond
                new TokenBucket(_count, _maxRecordsPerSecond),
                new TokenBucket(_maxBatchSize, _maxBytesPerSecond)
            },
                _backoffFactor,
                _recoveryFactor,
                _minRateAdjustmentFactor);
        }
        public EventLogSource(string logName, string query, IPlugInContext context) : base(context)
        {
            Guard.ArgumentNotNullOrEmpty(logName, nameof(logName));
            _logName = logName;
            _query   = query;

            if (string.IsNullOrEmpty(query))
            {
                _eventLogQuery = new EventLogQuery(logName, PathType.LogName);
            }
            else
            {
                _eventLogQuery = new EventLogQuery(logName, PathType.LogName, query);
            }

            string includeEventData = context?.Configuration?["IncludeEventData"];

            if (!string.IsNullOrWhiteSpace(includeEventData))
            {
                _includeEventData = Convert.ToBoolean(includeEventData);
            }
        }
        /// <summary>
        /// A generic version of the Factory method to create directory source
        /// </summary>
        /// <typeparam name="TData">Parser output type</typeparam>
        /// <typeparam name="TLogContext">Log context</typeparam>
        /// <param name="context">Plug-in Context</param>
        /// <param name="recordParser">Record Parser</param>
        /// <returns>Event Source</returns>
        public static IEventSource <TData> CreateEventSource <TData, TLogContext>(
            IPlugInContext context,
            IRecordParser <TData, TLogContext> recordParser
            ) where TLogContext : LogContext, new()
        {
            IConfiguration config = context.Configuration;

            GetDirectorySourceParameters(config, out string directory, out string filter, out int interval);
            DirectorySource <TData, TLogContext> source = new DirectorySource <TData, TLogContext>(
                directory,
                filter,
                interval * 1000, //milliseconds
                context,
                recordParser);

            source.NumberOfConsecutiveIOExceptionsToLogError = 3;

            EventSource <TData> .LoadCommonSourceConfig(config, source);

            source.Id = config[ConfigConstants.ID] ?? Guid.NewGuid().ToString();
            return(source);
        }
Пример #20
0
        /// <inheriteddoc />
        public void Initialize(IPlugInContext ctx)
        {
            lock (this._SYNC)
            {
                if (ctx == null)
                {
                    throw new ArgumentNullException("ctx");
                }

                if (this.IsInitialized)
                {
                    throw new InvalidOperationException();
                }

                this.OnInitialize(ref ctx);

                this.Context       = ctx;
                this.IsInitialized = true;

                this.RaiseEventHandler(this.Initialized);
            }
        }
Пример #21
0
        public EventSink(IPlugInContext context)
        {
            _context = context;
            _logger  = context.Logger;
            _config  = context.Configuration;
            _metrics = context.Metrics;
            Id       = _config?[ConfigConstants.ID];
            _format  = _config?[ConfigConstants.FORMAT];

            string textDecoration = _config?[ConfigConstants.TEXT_DECORATION];

            if (!string.IsNullOrWhiteSpace(textDecoration))
            {
                _textDecorationEvaluator = new TextDecorationEvaluator(textDecoration, ResolveRecordVariables);
            }

            string textDecorationEx = _config?[ConfigConstants.TEXT_DECORATION_EX];

            if (!string.IsNullOrWhiteSpace(textDecorationEx))
            {
                _textDecorationEvaluator = new TextDecorationExEvaluator(textDecorationEx, EvaluateVariable, ResolveRecordVariable, context);
            }

            string objectDecoration = _config?[ConfigConstants.OBJECT_DECORATION];

            if (!string.IsNullOrWhiteSpace(objectDecoration))
            {
                _objectDecorationEvaluator = new ObjectDecorationEvaluator(objectDecoration, ResolveRecordVariables);
            }

            string objectDecorationEx = _config?[ConfigConstants.OBJECT_DECORATION_EX];

            if (!string.IsNullOrWhiteSpace(objectDecorationEx))
            {
                _objectDecorationEvaluator = new ObjectDecorationExEvaluator(objectDecorationEx, EvaluateVariable, ResolveRecordVariable, context);
            }
            ValidateConfig();
        }
        public AWSBufferedEventSink(
            IPlugInContext context,
            int defaultInterval,
            int defaultRecordCount,
            long maxBatchSize
            ) : base(context, defaultInterval, defaultRecordCount, maxBatchSize)
        {
            if (!int.TryParse(_config["MaxAttempts"], out _maxAttempts))
            {
                _maxAttempts = ConfigConstants.DEFAULT_MAX_ATTEMPTS;
            }

            if (!double.TryParse(_config["JittingFactor"], out _jittingFactor))
            {
                _jittingFactor = ConfigConstants.DEFAULT_JITTING_FACTOR;
            }

            if (!double.TryParse(_config["BackoffFactor"], out _backoffFactor))
            {
                _backoffFactor = ConfigConstants.DEFAULT_BACKOFF_FACTOR;
            }

            if (!double.TryParse(_config["RecoveryFactor"], out _recoveryFactor))
            {
                _recoveryFactor = ConfigConstants.DEFAULT_RECOVERY_FACTOR;
            }

            if (!double.TryParse(_config["MinRateAdjustmentFactor"], out _minRateAdjustmentFactor))
            {
                _minRateAdjustmentFactor = ConfigConstants.DEFAULT_MIN_RATE_ADJUSTMENT_FACTOR;
            }

            if (!int.TryParse(_config[ConfigConstants.UPLOAD_NETWORK_PRIORITY], out _uploadNetworkPriority))
            {
                _uploadNetworkPriority = ConfigConstants.DEFAULT_NETWORK_PRIORITY;
            }
        }
        /// <summary>
        /// This is the non-generic version of the CreateEventSource relying on reflection to instantiate generic methods.
        /// </summary>
        /// <param name="context">Plug-in context</param>
        /// <param name="recordParser">Record Parser. Must implement IRecordParser<TData,LogContext></param>
        /// <param name="logSourceInfoFactory">Factory method for generating LogContext</param>
        /// <returns>Generated Directory Source</returns>
        internal static ISource CreateEventSource(IPlugInContext context,
                                                  IRecordParser recordParser)
        {
            Guard.ArgumentNotNull(recordParser, "recordParser");

            IConfiguration config = context.Configuration;

            GetDirectorySourceParameters(config, out string directory, out string filter, out int interval);

            var recordParserType = recordParser.GetType().GetTypeInfo().ImplementedInterfaces
                                   .FirstOrDefault(t => t.GetTypeInfo().IsGenericType&& t.GetGenericTypeDefinition() == typeof(IRecordParser <,>));

            if (recordParserType == null)
            {
                throw new ConfigurationException("recordParser must implement generic interface IRecordParser<,>");
            }

            var directorySourceType        = typeof(DirectorySource <,>);
            var genericDirectorySourceType = directorySourceType.MakeGenericType(recordParserType.GenericTypeArguments);
            var source = (ISource)Activator.CreateInstance(genericDirectorySourceType,
                                                           directory,
                                                           filter,
                                                           interval * 1000, //milliseconds
                                                           context,
                                                           recordParser);

            ((dynamic)source).NumberOfConsecutiveIOExceptionsToLogError = 3;

            //The following is the translation of EventSource<TData>.LoadCommonSourceConfig(config, source);
            typeof(EventSource <>)
            .MakeGenericType(recordParserType.GenericTypeArguments[0])
            .GetMethod("LoadCommonSourceConfig", BindingFlags.Static | BindingFlags.Public)
            .Invoke(null, new object[] { config, source });

            source.Id = config[ConfigConstants.ID] ?? Guid.NewGuid().ToString();
            return(source);
        }
        public static IEventSource <TData> CreateEventSource <TData, TContext>(
            IPlugInContext context,
            IRecordParser <TData, TContext> recordParser,
            Func <string, long, TContext> logSourceInfoFactory
            ) where TContext : LogContext
        {
            IConfiguration config          = context.Configuration;
            string         directory       = config["Directory"];
            string         filter          = config["FileNameFilter"];
            string         intervalSetting = config["Interval"];
            int            interval        = 0; //Seconds

            if (!string.IsNullOrEmpty(intervalSetting))
            {
                int.TryParse(intervalSetting, out interval);
            }
            if (interval == 0)
            {
                interval = 1;
            }

            DirectorySource <TData, TContext> source = new DirectorySource <TData, TContext>(
                directory,
                filter,
                interval * 1000, //milliseconds
                context,
                recordParser,
                logSourceInfoFactory);

            source.NumberOfConsecutiveIOExceptionsToLogError = 3;

            EventSource <TData> .LoadCommonSourceConfig(config, source);

            source.Id = config[ConfigConstants.ID] ?? Guid.NewGuid().ToString();
            return(source);
        }
Пример #25
0
        public RegexFilterPipe(IPlugInContext context) : base(context)
        {
            var config        = context.Configuration;
            var filterPattern = config[ConfigConstants.FILTER_PATTERN];

            if (string.IsNullOrWhiteSpace(filterPattern))
            {
                throw new ArgumentNullException("'FilterPattern' property of RegexFilterPipe cannot be null or whitespace.");
            }

            var options = RegexOptions.Compiled | RegexOptions.ExplicitCapture;

            if (bool.TryParse(config[ConfigConstants.MULTILINE], out var multiLine) && multiLine)
            {
                options |= RegexOptions.Multiline;
            }

            if (bool.TryParse(config[ConfigConstants.IGNORE_CASE], out var ignoreCase) && ignoreCase)
            {
                options |= RegexOptions.IgnoreCase;
            }

            if (bool.TryParse(config[ConfigConstants.RIGHT_TO_LEFT], out var rightToLeft) && rightToLeft)
            {
                options |= RegexOptions.RightToLeft;
            }

            _filter = new Regex(filterPattern.Trim(), options);

            // Negating matches in a regex is very expensive, so to make it cheaper, we'll add
            // in a flag that allows you to drop events that match the expression rather than allow.
            if (bool.TryParse(config[ConfigConstants.NEGATE], out var neg))
            {
                _negate = neg;
            }
        }
Пример #26
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FailoverSink{TAWSClient}"/> class.
        /// </summary>
        /// <param name="context">The <see cref="IPlugInContext"/> that contains configuration info, logger, metrics etc.</param>
        /// <param name="failoverSinkRegionStrategy">The <see cref="FailoverStrategy{TAWSClient}"/> that defines sinks region selection strategy.</param>
        public FailoverSink(
            IPlugInContext context,
            FailoverStrategy <TAWSClient> failoverSinkRegionStrategy)
            : base(context)
        {
            // Parse or default
            // Max errors count before failover
            if (!int.TryParse(_config[ConfigConstants.MAX_ERRORS_COUNT_BEFORE_FAILOVER], out _maxErrorsCountBeforeFailover))
            {
                _maxErrorsCountBeforeFailover = ConfigConstants.DEFAULT_MAX_CONSECUTIVE_ERRORS_COUNT;
            }
            else if (_maxErrorsCountBeforeFailover < 1)
            {
                throw new ArgumentException(String.Format("Invalid \"{0}\" value, please provide positive integer.",
                                                          ConfigConstants.MAX_ERRORS_COUNT_BEFORE_FAILOVER));
            }

            // Max wait interval before failover
            if (!int.TryParse(_config[ConfigConstants.MAX_FAILOVER_INTERVAL_IN_MINUTES], out _maxWaitIntervalBeforeFailover))
            {
                _maxWaitIntervalBeforeFailover = ConfigConstants.DEFAULT_MAX_WAIT_BEFORE_REGION_FAILOVER_IN_MINUTES;
            }
            else if (_maxWaitIntervalBeforeFailover < 1)
            {
                throw new ArgumentException(String.Format("Invalid \"{0}\" value, please provide positive integer.",
                                                          ConfigConstants.MAX_FAILOVER_INTERVAL_IN_MINUTES));
            }

            // Setup Failover Strategy
            _failoverSinkRegionStrategy = failoverSinkRegionStrategy;

            // Setup Secondary Region Failover Timer
            _secondaryRegionFailoverTimer           = new Timer(_maxWaitIntervalBeforeFailover * 60 * 1000);
            _secondaryRegionFailoverTimer.Elapsed  += new ElapsedEventHandler(FailOverToSecondaryRegion);
            _secondaryRegionFailoverTimer.AutoReset = false;
        }
        protected SimpleMetricsSink(int defaultInterval, IPlugInContext context) : base(context)
        {
            if (_config != null)
            {
                string interval = _config["interval"];
                if (!string.IsNullOrEmpty(interval))
                {
                    int.TryParse(interval, out _interval);
                }

                _metricsFilter = _config["metricsfilter"];
                if (!string.IsNullOrWhiteSpace(_metricsFilter))
                {
                    ConstructFilters();
                }
            }

            if (_interval == 0)
            {
                _interval = defaultInterval;
            }

            _flushTimer = new Timer(Flush, null, Timeout.Infinite, Timeout.Infinite);
        }
Пример #28
0
        public EventLogSource(string logName, string query, IPlugInContext context) : base(new ServiceDependency("EventLog"), context)
        {
            Guard.ArgumentNotNullOrEmpty(logName, nameof(logName));
            _logName = logName;
            _query   = query;

            if (string.IsNullOrEmpty(query))
            {
                _eventLogQuery = new EventLogQuery(logName, PathType.LogName);
            }
            else
            {
                _eventLogQuery = new EventLogQuery(logName, PathType.LogName, query);
            }

            string includeEventData = context?.Configuration?["IncludeEventData"];

            if (!string.IsNullOrWhiteSpace(includeEventData))
            {
                _includeEventData = Convert.ToBoolean(includeEventData);
            }

            string customFilters = context?.Configuration?["CustomFilters"];

            if (!string.IsNullOrWhiteSpace(customFilters))
            {
                _customFilters = customFilters.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var filter in _customFilters)
                {
                    if (EventInfoFilters.GetFilter(filter) == null)
                    {
                        throw new ConfigurationException($"Custom filter {filter} does not exist. Please check the filter name.");
                    }
                }
            }
        }
        /// <summary>
        /// Create a downloader according to the url
        /// </summary>
        /// <param name="url">Could be https://, s3:// or file:// urls</param>
        /// <param name="context">Standard plugin context used by KinesisTap plugins</param>
        /// <returns>One of the File Downloaders</returns>
        public static IFileDownloader CreateDownloaderFromUrl(string url, IPlugInContext context)
        {
            if (Uri.TryCreate(url, UriKind.Absolute, out Uri uri))
            {
                switch (uri.Scheme)
                {
                case "https":
                    return(new HttpDownloader());

                case "file":
                    return(new FileDownloader());

                case "s3":
                    return(new S3Downloader(context));

                default:
                    throw new InvalidOperationException("Invalid PackageVersion. Only https://, file:// and s3// are supported");
                }
            }
            else
            {
                throw new InvalidOperationException("PackageVersion is not a valid Url");
            }
        }
Пример #30
0
 public W3SVCLogParser(IPlugInContext plugInContext, string defaultMapping)
     : base(plugInContext, " ", (data, context) => new W3SVCLogRecord(data, context), defaultMapping)
 {
 }
Пример #31
0
        private void StopFromCode()
        {
            if (EntryPoint != null)
            {
                EntryPoint.Stop(Context);
                return;
            }
            string entryPointTypeName = PlugInInfo.PlugInData.EntryPoint.Type;
            int runtimeDllCount = PlugInInfo.PlugInData.Runtime.Assemblies.Count;
            for (int i = runtimeDllCount - 1; i >= 0; i--)
            {
                string runtimeAssemblyPath = PlugInInfo.PlugInData.Runtime.Assemblies[i].Path;

                System.Reflection.Assembly dllAssemblly = System.Reflection.Assembly.LoadFrom(runtimeAssemblyPath);
                Type type = dllAssemblly.GetType(entryPointTypeName);
                if (type != null)
                {
                    object obj = Activator.CreateInstance(type);
                    entryPoint = obj as IPlugInEntryPoint;
                    if (EntryPoint != null)
                    {
                        PlugInContext context = new PlugInContext();
                        context.Framework = Framework.Inistace;
                        context.PlugIn = this;

                        this.Context = context;
                        EntryPoint.Stop(Context);
                    }
                    //if (type != null)
                    //{
                    //    System.Reflection.MethodInfo method = type.GetMethod(ConstantString.EntryPointStopMethod);
                    //    if (method != null)
                    //    {
                    //        PlugInContext context = new PlugInContext();
                    //        method.Invoke(obj, new object[] { context });
                    //    }
                    //}
                }
            }
        }