Ejemplo n.º 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;
     }
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
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;
            }
        }
Ejemplo n.º 5
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);
            }
        }