Example #1
0
 private static void SingleSearch()
 {
     try
     {
         var splunk = new SplunkApi(hostName, "admin", "admin");
         int numberOfConnections = 0;
         // Do X number of searches
         for (int i = 0; i < searches; i++)
         {
             try
             {
                 splunk.Connect();
                 numberOfConnections++;
                 if (i % 50 == 0)
                 {
                     Auxilary.WriteLineColor(ConsoleColor.Gray, "{0} {1} connections made so far, {2} remaining", DateTime.Now, numberOfConnections, searches - i);
                 }
             }
             catch (Exception e)
             {
                 ConsoleColor c = Console.ForegroundColor;
                 Console.ForegroundColor = ConsoleColor.Yellow;
                 Console.WriteLine("--- One of the threads caught an exception {0}, {1} searches remaining", e.Message, searches - i);
                 Console.ForegroundColor = c;
             }
         }
     }
     catch (Exception e)
     {
         ConsoleColor c = Console.ForegroundColor;
         Console.ForegroundColor = ConsoleColor.Red;
         Console.WriteLine("--- One of the threads caught an exception {0}.Exiting thread", e.Message);
         Console.ForegroundColor = c;
     }
 }
Example #2
0
        static SplunkApi getConnectedSplunk(string hostName, int durationInSec = 120)
        {
            bool useSSL     = true;
            int  startIndex = 0;

            if (hostName.StartsWith("http://"))
            {
                useSSL     = false;
                startIndex = hostName.IndexOf(':') + 3;
            }
            else if (hostName.StartsWith("https://"))
            {
                startIndex = hostName.IndexOf(':') + 3;
            }
            hostName = hostName.Substring(startIndex);
            var splunk = new SplunkApi(hostName, "admin", "changeme", useSSL: useSSL);

            var tStart = DateTime.Now;

            while ((DateTime.Now - tStart).TotalSeconds < durationInSec)
            {
                try
                {
                    splunk.Connect();
                    return(splunk);
                }
                catch (Exception)
                {
                    Thread.Sleep(100);
                }
            }
            Console.WriteLine("Failed to connect to {0} in {1} seconds. Exiting ...", hostName, durationInSec);
            Environment.Exit(1);
            return(null);
        }
Example #3
0
        private static void SendDataViaHttpInput(SplunkApi splunk, string hostName, string token, int durationInSec, int dataSize)
        {
            long          addedEvents = 0, failedEvents = 0;
            var           tStart       = DateTime.Now;
            int           i            = 0;
            string        messageStart = "{ \"event\": { \"data\": \"Thread: " + Thread.CurrentThread.Name + ", event: ";
            string        filer        = new string('*', dataSize - messageStart.Length - 19);
            StringBuilder sb           = new StringBuilder();

            while ((DateTime.Now - tStart).TotalSeconds < durationInSec)
            {
                sb.Clear();
                sb.Append(messageStart);
                sb.Append(" ");
                sb.Append(i);
                sb.Append(", filler:'");
                sb.Append(filer);
                sb.Append("' \" } }");
                try
                {
                    bool result = splunk.SendDataViaHttp(token, sb.ToString());
                    if (result)
                    {
                        addedEvents++;
                    }
                    i++;
                }
                catch (Exception)
                {
                    failedEvents++;
                }
            }
            Interlocked.Add(ref totalAddedEvents, addedEvents);
            Interlocked.Add(ref totalFailedEvents, failedEvents);
        }
Example #4
0
 static void DeleteToken(SplunkApi splunk, string tokenName)
 {
     try
     {
         splunk.HttpDelete(string.Format("{0}/servicesNS/admin/system/data/inputs/http/{1}", splunk.BaseUrl, tokenName));
     }
     catch (System.Net.WebException)
     {
     }
 }
Example #5
0
 static string CreateToken(SplunkApi splunk, string tokenName)
 {
     try
     {
         var     xmlDoc = splunk.HttpPost(splunk.BaseUrl + "/servicesNS/admin/system/data/inputs/http", new[] { "name", "description", "index", "indexes" }, new[] { tokenName, string.Format("description from thread '{0}'", Thread.CurrentThread.Name), "main", "main" });
         XmlNode node   = xmlDoc.LastChild;
         while (node.LastChild != null)
         {
             node = node.LastChild;
         }
         return(node.InnerText);
     }
     catch (System.Net.WebException)
     {
     }
     return("");
 }
Example #6
0
        static void Main(string[] args)
        {
            string hostName      = args.Length > 0 ? args[0] : "127.0.0.1";
            string user          = args.Length > 1 ? args[1] : "admin";
            string password      = args.Length > 2 ? args[2] : "notchangeme";
            int    threadCount   = args.Length > 3 ? Convert.ToInt32(args[3]) : 50;
            int    durationInSec = args.Length > 4 ? Convert.ToInt32(args[4]) : 300;

            var splunk = new SplunkApi(hostName, user, password, 8089);

            Console.WriteLine("{0}\tStarted", DateTime.Now);
            string tokenName = "testtoken";

            splunk.Connect();
            Console.WriteLine("{0}\tConnected", DateTime.Now);
            // Delete token if it existed
            DeleteToken(splunk, tokenName);
            Console.WriteLine("{0}\tToken deleted", DateTime.Now);
            List <Thread> bgThreads = new List <Thread>();

            bgThreads.Add(new Thread(new ThreadStart(delegate { tokenAddRemove(splunk, tokenName, durationInSec); })));

            for (int i = 0; i < threadCount; i++)
            {
                string tname = string.Format("token{0}", i);
                var    t     = new Thread(new ThreadStart(delegate() { tokenAddRemove(splunk, tname, durationInSec); }));

                t.Name = string.Format("addremove thread {0}", i);
                bgThreads.Add(t);
            }
            for (int i = 0; i < threadCount / 5; i++)
            {
                var t = new Thread(new ThreadStart(delegate { GenerateData(splunk, durationInSec); }));
                t.Name = string.Format("Thread {0}", i);
                bgThreads.Add(t);
            }
            foreach (var t in bgThreads)
            {
                t.Start();
            }
            foreach (var t in bgThreads)
            {
                t.Join();
            }
            Console.WriteLine("{0}\tAll data inserted", DateTime.Now);
        }
        private static void SingleSearch(string hostName, int searches, int port, string defaultQuery, bool saveResult)
        {
            try
            {
                var splunk = new SplunkApi(hostName, "admin", "notchangeme", port);
                splunk.Connect();

                int searchTimeOutInMinutes = (defaultQuery == null) ? 10 : 5;
                // Do X number of searches
                string query = GetQuery(defaultQuery);
                while (query != null)
                {
                    try
                    {
                        DateTime tStart = DateTime.Now;
                        splunk.Search(query, saveResult, searchTimeOutInMinutes);
                        DateTime tEnd = DateTime.Now;
                        lock (_searchCounterLock)
                        {
                            _numberOfSearches++;
                            TimeSpan ts = tEnd - tStart;
                            _searchTimeSummarySec += ts.TotalSeconds;
                        }
                    }
                    catch (Exception e)
                    {
                        ConsoleColor c = Console.ForegroundColor;
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("--- One of the threads '{2}' caught an exception {0}, {1} searches remaining", e.Message, searchQueries.Count, Thread.CurrentThread.Name);
                        Console.ForegroundColor = c;
                    }
                    query = GetQuery(defaultQuery);
                }
            }
            catch (Exception e)
            {
                ConsoleColor c = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("--- One of the threads caught an exception {0}.Exiting thread '{1}'", e.Message, Thread.CurrentThread.Name);
                Console.ForegroundColor = c;
            }
        }
Example #8
0
        static void GenerateData(SplunkApi splunk, int durationInSec)
        {
            var tStart = DateTime.Now;
            int i      = 0;

            while ((DateTime.Now - tStart).TotalSeconds < durationInSec)
            {
                bool result = SendData(splunk, token, "{}");

                if (result)
                {
                    i++;
                    if (i % 500 == 0)
                    {
                        Console.WriteLine("{0}\t{1} events were sent in thread '{2}'", DateTime.Now, i, Thread.CurrentThread.Name);
                    }
                }
            }
            Console.WriteLine("{0}\t{1} Data generation completed in thread '{2}'. Events were sent total.", DateTime.Now, i, Thread.CurrentThread.Name);
        }
Example #9
0
        static void tokenAddRemove(SplunkApi splunk, string tokenName, int durationInSec)
        {
            var tStart = DateTime.Now;
            int i      = 0;

            while ((DateTime.Now - tStart).TotalSeconds < durationInSec)
            {
                // Sleep 0.5 - 5 sec
                Thread.Sleep(rnd.Next(50, 500));
                token = CreateToken(splunk, tokenName);
                // Sleep 0.5 - 5 sec
                Thread.Sleep(rnd.Next(50, 500));
                DeleteToken(splunk, tokenName);
                i++;
                if (i % 5 == 0)
                {
                    Console.WriteLine("{0}\t{1} tokens were created in thread '{2}'", DateTime.Now, i, Thread.CurrentThread.Name);
                }
            }
            Console.WriteLine("{0}\tTokens creation completed, total {1} were created in thread '{2}'", DateTime.Now, i, Thread.CurrentThread.Name);
        }
Example #10
0
        private static void ConnectionTest(object info)
        {
            object[] arguments        = info as object[];
            string   hostName         = (string)arguments[0];
            int      numberOfAttempts = (int)arguments[1];
            int      threadId         = (int)arguments[2];

            try
            {
                var  splunk = new SplunkApi(hostName, "admin", "admin");
                long connectionMade = 0, connectionFailed = 0;

                // Do X number of connections
                for (int i = 0; i < numberOfAttempts; i++)
                {
                    // Batch status update to avoid spinlocks
                    if (i % 50 == 0)
                    {
                        Interlocked.Add(ref _connectionSucceeded, connectionMade);
                        Interlocked.Add(ref _connectionFailed, connectionFailed);
                        connectionMade = connectionFailed = 0;
                    }
                    DateTime tStart = DateTime.Now;
                    try
                    {
                        splunk.Connect();
                        connectionMade++;
                    }
                    catch (Exception)
                    {
                        connectionFailed++;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine();
                Auxilary.WriteLineColor(ConsoleColor.Red, "{0} [Thread {1:D4}] caught an exception '{2}'. Exiting thread", DateTime.Now, threadId, e.Message);
            }
        }
Example #11
0
        static bool SendData(SplunkApi splunk, string token, string postData)
        {
            WebRequest request = WebRequest.Create(splunk.BaseUrl + "/services/logging");

            request.Method = "POST";
            request.Headers.Add("Authorization", "Splunk " + token);
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentType   = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
            using (Stream dataStream = request.GetRequestStream())
            {
                dataStream.Write(byteArray, 0, byteArray.Length);
            }

            try
            {
                using (WebResponse response = request.GetResponse())
                {
                    string status = ((HttpWebResponse)response).StatusDescription;
                    if (status != "OK")
                    {
                        Console.WriteLine("{1}\tFailed to insert data. Status is {0}", status, DateTime.Now);
                        System.Environment.Exit(-1);
                    }
                    using (Stream dataStream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(dataStream))
                        {
                            string responseFromServer = reader.ReadToEnd();
                        }
                    }
                }
            }
            catch (System.Net.WebException)
            {
                return(false);
            }
            return(true);
        }