コード例 #1
0
        private static void GetBytesSentReceivedCounters(string[] args, string[] hosts, int nThreads, int nReads, int sleep)
        {
            DateTime startTime = DateTime.Now;
            Dictionary <string, float> dtSent     = new Dictionary <string, float>();
            Dictionary <string, float> dtReceived = new Dictionary <string, float>();

            MySpace.DataMining.Threading.ThreadTools <string> .Parallel(
                new Action <string>(
                    delegate(string slave)
            {
                if (string.Compare(slave, "localhost", true) == 0)
                {
                    slave = System.Net.Dns.GetHostName();
                }

                List <System.Diagnostics.PerformanceCounter> received = new List <System.Diagnostics.PerformanceCounter>();
                List <System.Diagnostics.PerformanceCounter> sent = new List <System.Diagnostics.PerformanceCounter>();

                lock (dtSent)
                {
                    Console.WriteLine();
                    Console.WriteLine("Waiting to connect: {0}", slave);
                }

                System.Diagnostics.PerformanceCounterCategory cat = new System.Diagnostics.PerformanceCounterCategory("Network Interface", slave);
                string[] instances = cat.GetInstanceNames();

                try
                {
                    foreach (string s in instances)
                    {
                        if (s.ToLower().IndexOf("loopback") == -1)
                        {
                            received.Add(new System.Diagnostics.PerformanceCounter("Network Interface", "Bytes Received/sec", s, slave));
                            sent.Add(new System.Diagnostics.PerformanceCounter("Network Interface", "Bytes Sent/sec", s, slave));
                        }
                    }

                    lock (dtSent)
                    {
                        Console.WriteLine();
                        Console.WriteLine("Connected: {0}", slave);
                    }

                    //Initial reading.
                    foreach (System.Diagnostics.PerformanceCounter pc in received)
                    {
                        pc.NextValue();
                    }

                    foreach (System.Diagnostics.PerformanceCounter pc in sent)
                    {
                        pc.NextValue();
                    }

                    float br = 0;
                    float bs = 0;

                    for (int i = 0; i < nReads; i++)
                    {
                        System.Threading.Thread.Sleep(sleep);

                        foreach (System.Diagnostics.PerformanceCounter pc in received)
                        {
                            br += pc.NextValue();
                        }
                        foreach (System.Diagnostics.PerformanceCounter pc in sent)
                        {
                            bs += pc.NextValue();
                        }
                    }

                    if (nReads > 1)
                    {
                        br = br / (float)nReads;
                        bs = bs / (float)nReads;
                    }

                    lock (dtSent)
                    {
                        Console.WriteLine();
                        Console.WriteLine("Received {0}: {1}/s", slave, AELight.GetFriendlyByteSize((long)br));
                        Console.WriteLine("Sent {0}: {1}/s", slave, AELight.GetFriendlyByteSize((long)bs));
                        dtSent.Add(slave, bs);
                    }

                    lock (dtReceived)
                    {
                        dtReceived.Add(slave, br);
                    }
                }
                catch (Exception e)
                {
                    lock (dtSent)
                    {
                        Console.Error.WriteLine("Error while reading counter: {0}: {1}", slave, e.Message);
                    }
                }
            }
                    ), hosts, nThreads);

            //Write out total sent, received.
            double totalSent     = 0;
            double totalReceived = 0;

            foreach (float f in dtSent.Values)
            {
                totalSent += f;
            }

            foreach (float f in dtReceived.Values)
            {
                totalReceived += f;
            }

            Console.WriteLine();
            Console.WriteLine("Total Received: {0}/s", AELight.GetFriendlyByteSize((long)totalReceived));
            Console.WriteLine("Total Sent: {0}/s", AELight.GetFriendlyByteSize((long)totalSent));

            //Sort
            List <KeyValuePair <string, float> > sSent = new List <KeyValuePair <string, float> >(dtSent);

            sSent.Sort(CompareFloat);

            List <KeyValuePair <string, float> > sReceived = new List <KeyValuePair <string, float> >(dtReceived);

            sReceived.Sort(CompareFloat);

            //Max, min
            Console.WriteLine();
            Console.WriteLine("Min Received: {0} {1}/s", sReceived[0].Key, AELight.GetFriendlyByteSize((long)sReceived[0].Value));
            Console.WriteLine("Min Sent: {0} {1}/s", sSent[0].Key, AELight.GetFriendlyByteSize((long)sSent[0].Value));

            Console.WriteLine("Max Received: {0} {1}/s", sReceived[sReceived.Count - 1].Key, AELight.GetFriendlyByteSize((long)sReceived[sReceived.Count - 1].Value));
            Console.WriteLine("Max Sent: {0} {1}/s", sSent[sSent.Count - 1].Key, AELight.GetFriendlyByteSize((long)sSent[sSent.Count - 1].Value));

            //Avg
            double avgSent     = totalSent / (double)sSent.Count;
            double avgReceived = totalReceived / (double)sReceived.Count;

            Console.WriteLine();
            Console.WriteLine("Avg Received: {0}/s", AELight.GetFriendlyByteSize((long)avgReceived));
            Console.WriteLine("Avg Sent: {0}/s", AELight.GetFriendlyByteSize((long)avgSent));

            //Dt
            Console.WriteLine();
            Console.WriteLine("Perfmon Request Time: {0}", startTime.ToString());
            Console.WriteLine("Perfmon End Time: {0}", DateTime.Now.ToString());
        }
コード例 #2
0
        public static void SafeGetCounters(string[] args, string[] hosts)
        {
            if (args.Length == 0)
            {
                Console.Error.WriteLine("Action is not provided for perfmon.");
                AELight.SetFailure();
                return;
            }

            string act              = args[0].ToLower();
            int    nReads           = 1;
            int    sleep            = 5000;
            string category         = "";
            string counter          = "";
            string instance         = "";
            bool   friendlyByteSize = false;
            int    nThreads         = -1;

            if (args.Length > 1)
            {
                for (int i = 1; i < args.Length; i++)
                {
                    string s   = args[i];
                    int    del = s.IndexOf('=');
                    string nm  = s.ToLower();
                    string val = "";
                    if (del > -1)
                    {
                        nm  = s.Substring(0, del);
                        val = s.Substring(del + 1);
                    }
                    if (nm.StartsWith("-"))
                    {
                        nm = nm.Substring(1);
                    }

                    switch (nm)
                    {
                    case "a":
                        try
                        {
                            nReads = Int32.Parse(val);
                        }
                        catch
                        {
                            Console.Error.WriteLine("Reading count provided for 'a' is not a valid number.");
                            AELight.SetFailure();
                            return;
                        }

                        if (nReads <= 0)
                        {
                            nReads = 1;
                        }
                        break;

                    case "t":
                        try
                        {
                            nThreads = Int32.Parse(val);
                        }
                        catch
                        {
                            Console.Error.WriteLine("Thread count provided for 't' is not a valid number.");
                            AELight.SetFailure();
                            return;
                        }
                        break;

                    case "s":
                        try
                        {
                            sleep = Int32.Parse(val);
                        }
                        catch
                        {
                            Console.Error.WriteLine("Sleep provided for 's' is not a valid number.");
                            AELight.SetFailure();
                            return;
                        }

                        if (sleep <= 0)
                        {
                            sleep = 5000;
                        }
                        break;

                    case "o":
                        category = val;
                        break;

                    case "c":
                        counter = val;
                        break;

                    case "i":
                        instance = val;
                        break;

                    case "f":
                        friendlyByteSize = true;
                        break;

                    default:
                        if (i == args.Length - 1)
                        {
                            string shosts = args[i];
                            if (shosts.StartsWith("@"))
                            {
                                shosts = System.IO.File.ReadAllText(shosts.Substring(1));
                                hosts  = shosts.Split(new string[] { ";", ",", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                            }
                            else
                            {
                                hosts = shosts.Split(';', ',');
                            }
                        }
                        else
                        {
                            Console.Error.WriteLine("Unexpected: {0}", args[i]);
                        }
                        break;
                    }
                }
            }

            if (null == hosts)
            {
                dfs dc = AELight.LoadDfsConfig();
                hosts = dc.Slaves.SlaveList.Split(';');
            }

            if (nThreads <= 0)
            {
                nThreads = hosts.Length;
            }

#if DEBUG
            //System.Threading.Thread.Sleep(1000 * 8);
#endif

            switch (act)
            {
            case "network":
                GetBytesSentReceivedCounters(args, hosts, nThreads, nReads, sleep);
                break;

            case "cputime":
                GetGenericCounters("Processor", "% Processor Time", "_Total", hosts, nThreads, nReads, sleep, null);
                break;

            case "diskio":
                GetGenericCounters("PhysicalDisk", "Disk Bytes/sec", "_Total", hosts, nThreads, nReads, sleep, GetFriendlyByteSize);
                break;

            case "availablememory":
                GetGenericCounters("Memory", "Available Bytes", "", hosts, nThreads, nReads, sleep, GetFriendlyByteSize);
                break;

            case "generic":
                if (category.Length == 0 || counter.Length == 0)
                {
                    Console.Error.WriteLine("Argument(s) missing: [o=<ObjectName>] [c=<CounterName>]");
                    AELight.SetFailure();
                    return;
                }

                if (friendlyByteSize)
                {
                    GetGenericCounters(category, counter, instance, hosts, nThreads, nReads, sleep, GetFriendlyByteSize);
                }
                else
                {
                    GetGenericCounters(category, counter, instance, hosts, nThreads, nReads, sleep, null);
                }

                break;

            default:
                Console.Error.WriteLine("Action is invalid: {0}", act);
                AELight.SetFailure();
                return;
            }
        }
コード例 #3
0
 private static string GetFriendlyByteSize(double size)
 {
     return(AELight.GetFriendlyByteSize((long)size));
 }