Example #1
0
        static void VerifyErrorsAreRaised()
        {
            string           indexName     = "errorindex";
            string           tokenName     = "errortoken";
            double           testStartTime = SplunkCliWrapper.GetEpochTime();
            SplunkCliWrapper splunk        = new SplunkCliWrapper();
            string           token         = CreateIndexAndToken(splunk, tokenName, indexName);

            var trace = new TraceSource("HttpEventCollectorLogger");

            trace.Switch.Level = SourceLevels.All;

            var validMetaData   = new HttpEventCollectorEventInfo.Metadata(index: indexName, source: "host", sourceType: "log", host: "customhostname");
            var invalidMetaData = new HttpEventCollectorEventInfo.Metadata(index: "notexistingindex", source: "host", sourceType: "log", host: "customhostname");

            var listenerWithWrongToken = new HttpEventCollectorTraceListener(
                uri: new Uri("https://127.0.0.1:8088"),
                token: "notexistingtoken",
                metadata: validMetaData);
            var listenerWithWrongUri = new HttpEventCollectorTraceListener(
                uri: new Uri("https://127.0.0.1:8087"),
                token: token,
                metadata: validMetaData);
            var listenerWithWrongMetadata = new HttpEventCollectorTraceListener(
                uri: new Uri("https://127.0.0.1:8088"),
                token: token,
                metadata: invalidMetaData);

            bool wrongTokenWasRaised = false;

            listenerWithWrongToken.AddLoggingFailureHandler((HttpEventCollectorException e) =>
            {
                wrongTokenWasRaised = true;
            });
            bool wrongUriWasRaised = false;

            listenerWithWrongUri.AddLoggingFailureHandler((HttpEventCollectorException e) =>
            {
                wrongUriWasRaised = true;
            });
            bool wrongMetaDataWasRaised = false;

            listenerWithWrongMetadata.AddLoggingFailureHandler((HttpEventCollectorException e) =>
            {
                wrongMetaDataWasRaised = true;
            });

            trace.Listeners.Add(listenerWithWrongToken);
            trace.Listeners.Add(listenerWithWrongUri);
            trace.Listeners.Add(listenerWithWrongMetadata);
            // Generate data
            int eventCounter = GenerateData(trace);

            Console.WriteLine("{0} events were created, waiting for errors to be raised.", eventCounter);
            Thread.Sleep(30 * 1000);
            trace.Close();
            Assert.True(wrongTokenWasRaised);
            Assert.True(wrongUriWasRaised);
            Assert.True(wrongMetaDataWasRaised);
        }
Example #2
0
        public static void StallWrite()
        {
            string           tokenName     = "stallwritetoken";
            string           indexName     = "stallwriteindex";
            SplunkCliWrapper splunk        = new SplunkCliWrapper();
            double           testStartTime = SplunkCliWrapper.GetEpochTime();
            string           token         = CreateIndexAndToken(splunk, tokenName, indexName);

            const string baseUrl  = "https://127.0.0.1:8088";
            string       postData = "{ \"event\": { \"data\": \"test event\" } }";

            List <HttpWebRequest> requests = new List <HttpWebRequest>();
            List <Stream>         streams  = new List <Stream>();

            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            for (int i = 0; i < 10000; i++)
            {
                var request = WebRequest.Create(baseUrl + "/services/collector") as HttpWebRequest;
                requests.Add(request);
                request.Timeout   = 60 * 1000;
                request.KeepAlive = true;
                request.Method    = "POST";
                request.Headers.Add("Authorization", "Splunk " + token);
                request.ContentType   = "application/x-www-form-urlencoded";
                request.ContentLength = byteArray.Length * 2;
                Stream dataStream = request.GetRequestStream();
                streams.Add(dataStream);
                dataStream.Write(byteArray, 0, byteArray.Length);
            }
        }
Example #3
0
        static void VerifyEventsAreInOrder()
        {
            string           tokenName     = "verifyeventsareinordertoken";
            string           indexName     = "verifyeventsareinorderindex";
            SplunkCliWrapper splunk        = new SplunkCliWrapper();
            double           testStartTime = SplunkCliWrapper.GetEpochTime();
            string           token         = CreateIndexAndToken(splunk, tokenName, indexName);

            var trace = new TraceSource("HttpEventCollectorLogger");

            trace.Switch.Level = SourceLevels.All;
            var meta     = new HttpEventCollectorEventInfo.Metadata(index: indexName, source: "host", sourceType: "log", host: "customhostname");
            var listener = new HttpEventCollectorTraceListener(
                uri: new Uri("https://127.0.0.1:8088"),
                token: token,
                metadata: meta);

            trace.Listeners.Add(listener);

            // Generate data
            int totalEvents = 1000;

            string[] filer = new string[2];
            filer[0] = new string('s', 1);
            filer[1] = new string('l', 100000);
            for (int i = 0; i < totalEvents; i++)
            {
                trace.TraceInformation(string.Format("TraceInformation. This is event {0}. {1}", i, filer[i % 2]));
            }

            string searchQuery = "index=" + indexName;

            Console.WriteLine("{0} events were created, waiting for indexing to complete.", totalEvents);
            splunk.WaitForIndexingToComplete(indexName);
            int eventsFound = splunk.GetSearchCount(searchQuery);

            Console.WriteLine("Indexing completed, {0} events were found. Elapsed time {1:F2} seconds", eventsFound, SplunkCliWrapper.GetEpochTime() - testStartTime);
            // Verify all events were indexed correctly and in order
            Assert.Equal(totalEvents, eventsFound);
            List <string> searchResults = splunk.GetSearchResults(searchQuery);

            Assert.Equal(searchResults.Count, eventsFound);
            for (int i = 0; i < totalEvents; i++)
            {
                int    id       = totalEvents - i - 1;
                string expected = string.Format("TraceInformation. This is event {0}", id);
                Assert.True(searchResults[i].Contains(expected));
            }
            trace.Close();
        }
Example #4
0
        static void SendEventsUnBatched()
        {
            string           tokenName     = "unbatchedtoken";
            string           indexName     = "unbatchedindex";
            SplunkCliWrapper splunk        = new SplunkCliWrapper();
            double           testStartTime = SplunkCliWrapper.GetEpochTime();
            string           token         = CreateIndexAndToken(splunk, tokenName, indexName);

            var trace = new TraceSource("HttpEventCollectorLogger");

            trace.Switch.Level = SourceLevels.All;
            var meta     = new HttpEventCollectorEventInfo.Metadata(index: indexName, source: "host", sourceType: "log", host: "customhostname");
            var listener = new HttpEventCollectorTraceListener(
                uri: new Uri("https://127.0.0.1:8088"),
                token: token,
                metadata: meta);

            trace.Listeners.Add(listener);

            GenerateDataWaitForIndexingCompletion(splunk, indexName, testStartTime, trace);
            trace.Close();
        }
Example #5
0
        public void VerifyFlushEvents()
        {
            string           tokenName     = "flusheventtoken";
            string           indexName     = "flusheventdindex";
            SplunkCliWrapper splunk        = new SplunkCliWrapper();
            double           testStartTime = SplunkCliWrapper.GetEpochTime();

            string token = CreateIndexAndToken(splunk, tokenName, indexName);

            splunk.StopServer();
            Thread.Sleep(5 * 1000);

            var trace = new TraceSource("HttpEventCollectorLogger");

            trace.Switch.Level = SourceLevels.All;
            var meta     = new HttpEventCollectorEventInfo.Metadata(index: indexName, source: "host", sourceType: "log", host: "customhostname");
            var listener = new HttpEventCollectorTraceListener(
                uri: new Uri("https://127.0.0.1:8088"),
                token: token,
                retriesOnError: int.MaxValue,
                metadata: meta);

            trace.Listeners.Add(listener);

            // Generate data, wait a little bit so retries are happenning and start Splunk. Expecting to see all data make it
            const int eventsToGeneratePerLoop = 250;
            const int expectedCount           = eventsToGeneratePerLoop * 7;
            int       eventCounter            = GenerateData(trace, eventsPerLoop: eventsToGeneratePerLoop);

            splunk.StartServer();
            trace.Close();

            // Verify every event made to Splunk
            splunk.WaitForIndexingToComplete(indexName, stabilityPeriod: 30);
            int eventsFound = splunk.GetSearchCount("index=" + indexName);

            Assert.Equal(expectedCount, eventsFound);
        }
Example #6
0
        private static void GenerateDataWaitForIndexingCompletion(SplunkCliWrapper splunk, string indexName, double testStartTime, TraceSource trace)
        {
            // Generate data
            int    eventCounter = GenerateData(trace);
            string searchQuery  = "index=" + indexName;

            Console.WriteLine("{0} events were created, waiting for indexing to complete.", eventCounter);
            splunk.WaitForIndexingToComplete(indexName);
            int eventsFound = splunk.GetSearchCount(searchQuery);

            Console.WriteLine("Indexing completed, {0} events were found. Elapsed time {1:F2} seconds", eventsFound, SplunkCliWrapper.GetEpochTime() - testStartTime);
            // Verify all events were indexed correctly
            Assert.Equal(eventCounter, eventsFound);
            List <string> searchResults = splunk.GetSearchResults(searchQuery);

            Assert.Equal(searchResults.Count, eventsFound);
            for (int eventId = 0; eventId < eventCounter; eventId++)
            {
                string expected = string.Format("This is event {0}", eventId);
                Assert.NotNull(searchResults.FirstOrDefault(s => s.Contains(expected)));
            }
            // Verify metadata
            List <string> metaData = splunk.GetMetadataResults(searchQuery);

            Assert.Equal(metaData[0], "customhostname");
            Assert.Equal(metaData[1], "log");
            Assert.Equal(metaData[2], "host");
        }