/// <summary> /// Creates CPU counter /// </summary> public static SimpleCounter CreateCPUCounter() { // Créer un compteur par CPU System.Diagnostics.PerformanceCounterCategory category = new System.Diagnostics.PerformanceCounterCategory("Processor"); SimpleCounter mainCounter = null; List <SimpleCounter> counters = new List <SimpleCounter>(); foreach (string instance in category.GetInstanceNames().OrderBy(s => s)) { System.Diagnostics.PerformanceCounter pc = new System.Diagnostics.PerformanceCounter("Processor", "% Processor Time", instance, true); SimpleCounter counter = new KnownMaxPerformanceCounter(new PerformanceCounter(pc), new StaticPerformanceCounter(100)); if (instance == "_Total") { mainCounter = counter; } else { counters.Add(counter); } } return(new SubPerformanceCounter(mainCounter, counters)); }
/// <summary> /// Creates counters for each physical disk connected /// </summary> /// <returns>enumeration of counters</returns> public static IEnumerable<SimpleCounter> CreatePhysicalDiskCounters() { List<SimpleCounter> list = new List<SimpleCounter>(); System.Diagnostics.PerformanceCounterCategory category = new System.Diagnostics.PerformanceCounterCategory("PhysicalDisk"); foreach (string instance in category.GetInstanceNames().OrderBy(s => s)) { if (instance == "_Total") { continue; } List<SimpleCounter> subCounterslist = new List<SimpleCounter>(); System.Diagnostics.PerformanceCounterCategory subCounterCategory = new System.Diagnostics.PerformanceCounterCategory("LogicalDisk"); foreach (string subCounterInstance in subCounterCategory .GetInstanceNames().OrderBy(s => s)) { if (subCounterInstance == "_Total") { continue; } if (!instance.Contains(subCounterInstance)) { continue; } System.Diagnostics.PerformanceCounter subPc = new System.Diagnostics.PerformanceCounter("LogicalDisk", "% Idle Time", subCounterInstance , true); subCounterslist.Add(new ReversePerformanceCounter(new PerformanceCounter(subPc), new StaticPerformanceCounter(100))); } System.Diagnostics.PerformanceCounter pc = new System.Diagnostics.PerformanceCounter("PhysicalDisk", "% Idle Time", instance, true); // list.Add(new ReversePerformanceCounter(new PerformanceCounter(pc), new StaticPerformanceCounter(100))); list.Add( new SubPerformanceCounter( new ReversePerformanceCounter( new PerformanceCounter(pc), new StaticPerformanceCounter(100)), subCounterslist)); } return list; }
private int GetCounters(string[] hosts) { dtSent.Clear(); dtReceived.Clear(); int error = 0; QueryAnalyzer_ADOTrafficMonitor.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>(); try { System.Diagnostics.PerformanceCounterCategory cat = new System.Diagnostics.PerformanceCounterCategory("Network Interface", slave); string[] instances = cat.GetInstanceNames(); 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)); } } //Initial reading. foreach (System.Diagnostics.PerformanceCounter pc in received) { pc.NextValue(); } foreach (System.Diagnostics.PerformanceCounter pc in sent) { pc.NextValue(); } lock (dtSent) { dtSent.Add(slave, sent); dtReceived.Add(slave, received); } } catch (Exception e) { AppendStatusText(txtStatus, "Text", "Error while getting counter from " + slave + ". Error: " + e.ToString()); System.Threading.Interlocked.Increment(ref error); } } ), hosts, hosts.Length); return(error); }
/// <summary> /// Create counter list from string /// </summary> /// <param name="counterString">string which represents one or more counters: "\\<machineName>\<categoryName>(<instanceName>)\<counterName>" /// counterName and/or instanceName can have the special value "#ALL#", meaning we want to get all of them</param> /// <returns>list of counters</returns> public static IEnumerable <System.Diagnostics.PerformanceCounter> CreateCountersFromString(string counterString) { string machineName, categoryName, instanceName, counterName; ParseCounterString(counterString, out machineName, out categoryName, out instanceName, out counterName); System.Diagnostics.PerformanceCounterCategory category = new System.Diagnostics.PerformanceCounterCategory(categoryName, machineName); IEnumerable <System.Diagnostics.PerformanceCounter> counters = new System.Diagnostics.PerformanceCounter[] { }; if (counterName == "#ALL#" && instanceName == "#ALL#") { foreach (string instance in category.GetInstanceNames().OrderBy(s => s)) { counters = counters.Concat(category.GetCounters(instance)); } } else if (counterName == "#ALL#") { if (string.IsNullOrEmpty(instanceName)) { counters = category.GetCounters(); } else { counters = category.GetCounters(instanceName); } } else if (instanceName == "#ALL#") { foreach (string instance in category.GetInstanceNames().OrderBy(s => s)) { counters = counters.Concat(new System.Diagnostics.PerformanceCounter[] { new System.Diagnostics.PerformanceCounter(categoryName, counterName, instance, machineName) }); } } else { counters = new System.Diagnostics.PerformanceCounter[] { new System.Diagnostics.PerformanceCounter(categoryName, counterName, instanceName, machineName) }; } // Création des contrôles return(counters); }
public static System.Collections.Generic.Dictionary <int, int> GetAllProcessParentPids() { var childPidToParentPid = new System.Collections.Generic.Dictionary <int, int>(); var processCounters = new System.Collections.Generic.SortedDictionary <string, System.Diagnostics.PerformanceCounter[]>(); var category = new System.Diagnostics.PerformanceCounterCategory("Process"); // As the base system always has more than one process running, // don't special case a single instance return. var instanceNames = category.GetInstanceNames(); foreach (string t in instanceNames) { try { processCounters[t] = category.GetCounters(t); } catch (System.InvalidOperationException) { // Transient processes may no longer exist between // GetInstanceNames and when the counters are queried. } } foreach (var kvp in processCounters) { int childPid = -1; int parentPid = -1; foreach (var counter in kvp.Value) { if ("ID Process".CompareTo(counter.CounterName) == 0) { childPid = (int)(counter.NextValue()); } else if ("Creating Process ID".CompareTo(counter.CounterName) == 0) { parentPid = (int)(counter.NextValue()); } } if (childPid != -1 && parentPid != -1) { childPidToParentPid[childPid] = parentPid; } } return(childPidToParentPid); } // End Function GetAllProcessParentPids
/// <summary> /// Creates network counter /// </summary> public static SimpleCounter CreateNetworkCounter() { // Créer les contrôles pour chaque interface réseau System.Diagnostics.PerformanceCounterCategory category = new System.Diagnostics.PerformanceCounterCategory("Network Interface"); List <SimpleCounter> counters = new List <SimpleCounter>(); foreach (string instance in category.GetInstanceNames().OrderBy(s => s)) { System.Diagnostics.PerformanceCounter pc = new System.Diagnostics.PerformanceCounter("Network Interface", "Bytes Total/sec", instance, true); PerformanceCounter counter = new PerformanceCounter(pc); counters.Add(new PerformanceCounter(pc)); } return(new SumPerformanceCounter(counters)); }
/// <summary> /// Creates CPU percentage of max frequency counter /// </summary> public static SimpleCounter CreateCPUFrequencyCounter() { // Créer un compteur par CPU System.Diagnostics.PerformanceCounterCategory category = new System.Diagnostics.PerformanceCounterCategory("ProcessorPerformance"); SimpleCounter mainCounter = null; List <SimpleCounter> counters = new List <SimpleCounter>(); foreach (string instance in category.GetInstanceNames().OrderBy(s => s)) { System.Diagnostics.PerformanceCounter pc = new System.Diagnostics.PerformanceCounter("ProcessorPerformance", "percentage", instance, true); SimpleCounter counter = new KnownMaxPerformanceCounter(new PerformanceCounter(pc), new StaticPerformanceCounter(100)); counters.Add(counter); } mainCounter = new SumPerformanceCounter(counters); return(new SubPerformanceCounter(mainCounter, counters)); }
private void categoryCb_SelectedIndexChanged(object sender, EventArgs e) { string[] instanceNames; var category = new System.Diagnostics.PerformanceCounterCategory(categoryCb.SelectedItem.ToString()); instanceCb.Items.Clear(); nameCb.Items.Clear(); instanceCb.Text = ""; nameCb.Text = ""; instanceCb.Enabled = true; nameCb.Enabled = false; try { instanceNames = category.GetInstanceNames(); if (instanceNames.Length == 0) { instanceCb.Enabled = false; nameCb.Enabled = true; System.Collections.ArrayList counters = new System.Collections.ArrayList(); counters.AddRange(category.GetCounters()); foreach (System.Diagnostics.PerformanceCounter counter in counters) { nameCb.Items.Add(counter.CounterName); } } else { for (int i = 0; i < instanceNames.Length; i++) { instanceCb.Items.Add(instanceNames[i]); } } } catch (System.Exception ex) { MessageBox.Show("Unable to list the counters for this category:\n" + ex.Message); } }
/// <summary> /// Creates counters for each physical disk connected /// </summary> /// <returns>enumeration of counters</returns> public static IEnumerable <SimpleCounter> CreatePhysicalDiskCounters() { List <SimpleCounter> list = new List <SimpleCounter>(); System.Diagnostics.PerformanceCounterCategory category = new System.Diagnostics.PerformanceCounterCategory("PhysicalDisk"); foreach (string instance in category.GetInstanceNames().OrderBy(s => s)) { if (instance == "_Total") { continue; } List <SimpleCounter> subCounterslist = new List <SimpleCounter>(); System.Diagnostics.PerformanceCounterCategory subCounterCategory = new System.Diagnostics.PerformanceCounterCategory("LogicalDisk"); foreach (string subCounterInstance in subCounterCategory.GetInstanceNames().OrderBy(s => s)) { if (subCounterInstance == "_Total") { continue; } if (!instance.Contains(subCounterInstance)) { continue; } System.Diagnostics.PerformanceCounter subPc = new System.Diagnostics.PerformanceCounter("LogicalDisk", "% Idle Time", subCounterInstance, true); subCounterslist.Add(new ReversePerformanceCounter(new PerformanceCounter(subPc), new StaticPerformanceCounter(100))); } System.Diagnostics.PerformanceCounter pc = new System.Diagnostics.PerformanceCounter("PhysicalDisk", "% Idle Time", instance, true); // list.Add(new ReversePerformanceCounter(new PerformanceCounter(pc), new StaticPerformanceCounter(100))); list.Add( new SubPerformanceCounter( new ReversePerformanceCounter( new PerformanceCounter(pc), new StaticPerformanceCounter(100)), subCounterslist)); } return(list); }
/// <summary> /// Creates CPU counter /// </summary> public static SimpleCounter CreateCPUCounter() { // Créer un compteur par CPU System.Diagnostics.PerformanceCounterCategory category = new System.Diagnostics.PerformanceCounterCategory("Processor"); SimpleCounter mainCounter = null; List<SimpleCounter> counters = new List<SimpleCounter>(); foreach (string instance in category.GetInstanceNames().OrderBy(s => s)) { System.Diagnostics.PerformanceCounter pc = new System.Diagnostics.PerformanceCounter("Processor", "% Processor Time", instance, true); SimpleCounter counter = new KnownMaxPerformanceCounter(new PerformanceCounter(pc), new StaticPerformanceCounter(100)); if (instance == "_Total") { mainCounter = counter; } else { counters.Add(counter); } } return new SubPerformanceCounter(mainCounter, counters); }
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()); }
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { string[] objectNames; if (this.listBox1.SelectedIndex != -1) { System.Diagnostics.PerformanceCounterCategory selectedPerfCat = new System.Diagnostics.PerformanceCounterCategory(this.listBox1.SelectedItem.ToString()); this.listBox2.Items.Clear(); try { objectNames = selectedPerfCat.GetInstanceNames(); Array.Sort(objectNames); for (int i = 0; i < objectNames.Length; i++) { this.listBox2.Items.Add(objectNames[i]); } } catch (System.Exception ex) { MessageBox.Show("Unable to list the counters for this category:\n" + ex.Message); } } }
private List <IISAppPoolStateInfo> AppPoolStatesIIS7Plus(string hostName) { List <IISAppPoolStateInfo> appPools = new List <IISAppPoolStateInfo>(); try { if (UsePerfCounter && System.Diagnostics.PerformanceCounterCategory.Exists("APP_POOL_WAS", hostName)) { //Try performance counters System.Diagnostics.PerformanceCounterCategory pcCat = new System.Diagnostics.PerformanceCounterCategory("APP_POOL_WAS", hostName); //getting instance names string[] instances = pcCat.GetInstanceNames(); foreach (string instanceName in instances) { if (instanceName != "_Total") { System.Diagnostics.PerformanceCounter pc = (from pcCacheEntry in pcCatCache where pcCacheEntry.InstanceName == instanceName select pcCacheEntry).FirstOrDefault(); if (pc == null) { pc = new System.Diagnostics.PerformanceCounter("APP_POOL_WAS", "Current Application Pool State", instanceName, hostName); pcCatCache.Add(pc); } float value = pc.NextValue(); IISAppPoolStateInfo appPool = new IISAppPoolStateInfo() { DisplayName = instanceName }; appPool.Status = ReadAppPoolStateFromPCValue(value); appPools.Add(appPool); } } } else { using (WebAdmin.ServerManager serverManager = WebAdmin.ServerManager.OpenRemote(hostName)) { foreach (var pool in serverManager.ApplicationPools) { IISAppPoolStateInfo appPool = new IISAppPoolStateInfo() { DisplayName = pool.Name }; appPool.Status = (AppPoolStatus)pool.State; appPools.Add(appPool); } } } } //catch (UnauthorizedAccessException unauthEx) //{ // appPools = new List<IISAppPoolStateInfo>(); // //Try performance counters // System.Diagnostics.PerformanceCounterCategory pcCat = new System.Diagnostics.PerformanceCounterCategory("APP_POOL_WAS", hostName); // //getting instance names // string[] instances = pcCat.GetInstanceNames(); // foreach (string instanceName in instances) // { // System.Diagnostics.PerformanceCounter pc = new System.Diagnostics.PerformanceCounter("APP_POOL_WAS", "Current Application Pool State", instanceName, hostName); // float value = pc.NextValue(); // IISAppPoolStateInfo appPool = new IISAppPoolStateInfo() { DisplayName = instanceName }; // appPool.Status = ReadAppPoolStateFromPCValue(value); // appPools.Add(appPool); // } //} catch (Exception ex) { if (ex is UnauthorizedAccessException) { throw new Exception("Using ServiceManager class on local machine requires elevated rights. Please run this collector in a process that runs in 'Admin mode'"); } else if (ex.Message.Contains("80040154")) { throw new Exception(string.Format("ServiceManager class not supported on {0}", hostName)); } else if (ex.Message.Contains("800706ba")) { throw new Exception("Firewall blocking ServiceManager call. Please add OAB exception"); } else { throw; } } return(appPools); }
/// <summary> /// Create counter list from string /// </summary> /// <param name="counterString">string which represents one or more counters: "\\<machineName>\<categoryName>(<instanceName>)\<counterName>" /// counterName and/or instanceName can have the special value "#ALL#", meaning we want to get all of them</param> /// <returns>list of counters</returns> public static IEnumerable<System.Diagnostics.PerformanceCounter> CreateCountersFromString(string counterString) { string machineName, categoryName, instanceName, counterName; ParseCounterString(counterString, out machineName, out categoryName, out instanceName, out counterName); System.Diagnostics.PerformanceCounterCategory category = new System.Diagnostics.PerformanceCounterCategory(categoryName, machineName); IEnumerable<System.Diagnostics.PerformanceCounter> counters = new System.Diagnostics.PerformanceCounter[] { }; if (counterName == "#ALL#" && instanceName == "#ALL#") { foreach (string instance in category.GetInstanceNames().OrderBy(s => s)) { counters = counters.Concat(category.GetCounters(instance)); } } else if (counterName == "#ALL#") { if (string.IsNullOrEmpty(instanceName)) { counters = category.GetCounters(); } else { counters = category.GetCounters(instanceName); } } else if (instanceName == "#ALL#") { foreach (string instance in category.GetInstanceNames().OrderBy(s => s)) { counters = counters.Concat(new System.Diagnostics.PerformanceCounter[] { new System.Diagnostics.PerformanceCounter(categoryName, counterName, instance, machineName) }); } } else { counters = new System.Diagnostics.PerformanceCounter[] { new System.Diagnostics.PerformanceCounter(categoryName, counterName, instanceName, machineName) }; } // Création des contrôles return counters; }
/// <summary> /// Creates CPU percentage of max frequency counter /// </summary> public static SimpleCounter CreateCPUFrequencyCounter() { // Créer un compteur par CPU System.Diagnostics.PerformanceCounterCategory category = new System.Diagnostics.PerformanceCounterCategory("ProcessorPerformance"); SimpleCounter mainCounter = null; List<SimpleCounter> counters = new List<SimpleCounter>(); foreach (string instance in category.GetInstanceNames().OrderBy(s => s)) { System.Diagnostics.PerformanceCounter pc = new System.Diagnostics.PerformanceCounter("ProcessorPerformance", "percentage", instance, true); SimpleCounter counter = new KnownMaxPerformanceCounter(new PerformanceCounter(pc), new StaticPerformanceCounter(100)); counters.Add(counter); } mainCounter = new SumPerformanceCounter(counters); return new SubPerformanceCounter(mainCounter, counters); }
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()); }
/// <summary> /// Creates network counter /// </summary> public static SimpleCounter CreateNetworkCounter() { // Créer les contrôles pour chaque interface réseau System.Diagnostics.PerformanceCounterCategory category = new System.Diagnostics.PerformanceCounterCategory("Network Interface"); List<SimpleCounter> counters = new List<SimpleCounter>(); foreach (string instance in category.GetInstanceNames().OrderBy(s => s)) { System.Diagnostics.PerformanceCounter pc = new System.Diagnostics.PerformanceCounter("Network Interface", "Bytes Total/sec", instance, true); PerformanceCounter counter = new PerformanceCounter(pc); counters.Add(new PerformanceCounter(pc)); } return new SumPerformanceCounter(counters); }
private int GetCounters(string[] hosts) { dtSent.Clear(); dtReceived.Clear(); int error = 0; QueryAnalyzer_ADOTrafficMonitor.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>(); try { System.Diagnostics.PerformanceCounterCategory cat = new System.Diagnostics.PerformanceCounterCategory("Network Interface", slave); string[] instances = cat.GetInstanceNames(); 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)); } } //Initial reading. foreach (System.Diagnostics.PerformanceCounter pc in received) { pc.NextValue(); } foreach (System.Diagnostics.PerformanceCounter pc in sent) { pc.NextValue(); } lock (dtSent) { dtSent.Add(slave, sent); dtReceived.Add(slave, received); } } catch (Exception e) { AppendStatusText(txtStatus, "Text", "Error while getting counter from " + slave + ". Error: " + e.ToString()); System.Threading.Interlocked.Increment(ref error); } } ), hosts, hosts.Length); return error; }