private static void EnumerateCountersFor(string category, string instance) { var sb = new StringBuilder(); var counterCategory = new PerformanceCounterCategory(category); foreach (var counter in counterCategory.GetCounters(instance)) { sb.AppendLine(string.Format("{0}:{1}:{2}", instance, category, counter.CounterName)); } Console.WriteLine(sb.ToString()); }
private static void EnumerateCountersFor(string category) { var sb = new StringBuilder(); var counterCategory = new PerformanceCounterCategory(category); if(counterCategory.CategoryType == PerformanceCounterCategoryType.SingleInstance) { foreach (var counter in counterCategory.GetCounters()) { sb.AppendLine(string.Format("{0}:{1}", category, counter.CounterName)); } } else { foreach (var counterInstance in counterCategory.GetInstanceNames()) { foreach (var counter in counterCategory.GetCounters(counterInstance)) { sb.AppendLine(string.Format("{0}:{1}:{2}", counterInstance, category, counter.CounterName)); } } } Console.WriteLine(sb.ToString()); }
private static void EnumerateCounters() { var categories = PerformanceCounterCategory.GetCategories().Select(c => c.CategoryName).OrderBy(s => s).ToArray(); var sb = new StringBuilder(); foreach (var category in categories) { var counterCategory = new PerformanceCounterCategory(category); foreach (var counterInstance in counterCategory.GetInstanceNames()) { try { foreach (var counter in counterCategory.GetCounters(counterInstance)) { sb.AppendLine(string.Format("{0}:{1}:{2}", counterInstance, category, counter.CounterName)); } } catch { // Drop it on the floor } } } Console.WriteLine(sb.ToString()); }
private void cboCategory_SelectedIndexChanged(object sender, EventArgs e) { if (cboCategory.Text.Length > 0) { try { PerformanceCounter[] pcs; cboCounter.Items.Clear(); cboCounter.Text = ""; cboInstance.Items.Clear(); cboInstance.Text = ""; PerformanceCounterCategory pcCat = new PerformanceCounterCategory(cboCategory.Text, txtComputer.Text); string instanceName = ""; if (pcCat.CategoryType == PerformanceCounterCategoryType.MultiInstance) { string[] instances = pcCat.GetInstanceNames(); foreach (string instanceNameItem in instances) { cboInstance.Items.Add(instanceNameItem); } if (instances.Length > 0) { instanceName = pcCat.GetInstanceNames()[0]; } pcs = pcCat.GetCounters(instanceName); } else { pcs = pcCat.GetCounters(); } foreach (PerformanceCounter pc in pcs) { cboCounter.Items.Add(pc.CounterName); } if (InitialCounter != null && InitialCounter.Length > 0) { for (int i = 0; i < cboCounter.Items.Count; i++) { if (cboCounter.Items[i].ToString().ToLower() == InitialCounter.ToLower()) { cboCounter.SelectedIndex = i; break; } } } if (InitialInstance != null && InitialInstance.Length > 0) { for (int i = 0; i < cboInstance.Items.Count; i++) { if (cboInstance.Items[i].ToString().ToLower() == InitialInstance.ToLower()) { cboInstance.SelectedIndex = i; break; } } } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } CheckForValidCounter(); }
private List<PerformanceCounter> getCounterlist(string counterName) { if (!counters.ContainsKey(counterName)) { List<System.Diagnostics.PerformanceCounter> counterlist = new List<PerformanceCounter>(); var counterData = DefaultPerfCounterRegEx.split(counterName); try { PerformanceCounterCategory mycat = new PerformanceCounterCategory(counterData.Category); var foo = PerformanceCounterCategory.GetCategories(); var foolist = new List<string>(); foreach (var f in foo) { foolist.Add(f.CategoryName); } foolist.Sort(); switch (mycat.CategoryType) { case PerformanceCounterCategoryType.SingleInstance: foreach (var counter in mycat.GetCounters()) { if (!counter.CounterName.Equals(counterData.Counter, StringComparison.InvariantCultureIgnoreCase)) continue; counterlist.Add(counter); counter.NextValue(); // Initialize performance counters in order to avoid them to return 0. } break; case PerformanceCounterCategoryType.MultiInstance: if (counterData.Instance == null || counterData.Instance.Equals("*")) { foreach (var instance in mycat.GetInstanceNames()) { foreach (var counter in mycat.GetCounters(instance)) { if (!counter.CounterName.Equals(counterData.Counter, StringComparison.InvariantCultureIgnoreCase)) continue; counterlist.Add(counter); counter.NextValue(); // Initialize performance counters in order to avoid them to return 0. } } } else { foreach (var counter in mycat.GetCounters(counterData.Instance)) { if (!counter.CounterName.Equals(counterData.Counter, StringComparison.InvariantCultureIgnoreCase)) continue; counterlist.Add(counter); counter.NextValue(); // Initialize performance counters in order to avoid them to return 0. } } break; default: break; } } catch (Exception e) { Log.Error(String.Format("Counter {0} will be ignored due to errors: {1}", counterName, e)); Log.Error(e); } finally { counters.Add(counterName, counterlist); } } return counters[counterName]; }
private IEnumerable <PerformanceCounter> GetPerformanceCounters(string categoryName, string counterName, string instanceName) { var response = new List <PerformanceCounter>(); try { if ((counterName.Contains("*") || (instanceName != null && instanceName.Contains("*")))) { var cat = new PerformanceCounterCategory(categoryName); string[] instances; if (instanceName.Contains("*")) { instances = cat.GetInstanceNames().Where(x => Match(x, instanceName)).ToArray(); //TODO: If this response gives no instances, this means that this counter should use null, for instance if (!instances.Any()) { instances = new[] { (string)null }; } } else { instances = new[] { instanceName }; } var counterNames = new[] { counterName }; if (counterName.Contains("*")) { foreach (var instance in instances) { if (string.IsNullOrEmpty(instance)) { counterNames = cat.GetCounters().Where(x => Match(x.CounterName, counterName)).Select(x => x.CounterName).ToArray(); } else { counterNames = cat.GetCounters(instance).Where(x => Match(x.CounterName, counterName)).Select(x => x.CounterName).ToArray(); } } } foreach (var counter in counterNames) { foreach (var instance in instances) { var processorCounter = new PerformanceCounter(categoryName, counter, instance); processorCounter.NextValue(); response.Add(processorCounter); } } } else { var processorCounter = new PerformanceCounter(categoryName, counterName, instanceName); processorCounter.NextValue(); response.Add(processorCounter); } } catch (Exception exception) { OnGetPerformanceCounters(exception, categoryName, counterName, instanceName); } return(response); }
static void init() { try { if (PerformanceCounterCategory.Exists("Processor Information") && PerformanceCounterCategory.CounterExists("% Processor Time", "Processor Information")) { cpu = new PerformanceCounter("Processor Information", "% Processor Time", "_Total"); } if (PerformanceCounterCategory.Exists("Memory") && PerformanceCounterCategory.CounterExists("Available Mbytes", "Memory")) { memory = new PerformanceCounter("Memory", "Available Mbytes"); } if (PerformanceCounterCategory.Exists("LogicalDisk") && PerformanceCounterCategory.CounterExists("% Free Space", "LogicalDisk")) { cFree = new PerformanceCounter("LogicalDisk", "% Free Space", "C:"); } if (PerformanceCounterCategory.Exists("PhysicalDisk") && PerformanceCounterCategory.CounterExists("Avg. Disk Queue Length", "PhysicalDisk")) { AvgDiskQueueLength = new PerformanceCounter("PhysicalDisk", "Avg. Disk Queue Length", "_Total"); } if (PerformanceCounterCategory.Exists("PhysicalDisk") && PerformanceCounterCategory.CounterExists("Avg. Disk sec/Read", "PhysicalDisk")) { AvgDiskSecRead = new PerformanceCounter("PhysicalDisk", "Avg. Disk sec/Read", "_Total"); } if (PerformanceCounterCategory.Exists("PhysicalDisk") && PerformanceCounterCategory.CounterExists("Avg. Disk sec/Write", "PhysicalDisk")) { AvgDiskSecWrite = new PerformanceCounter("PhysicalDisk", "Avg. Disk sec/Write", "_Total"); } if (PerformanceCounterCategory.Exists("PhysicalDisk") && PerformanceCounterCategory.CounterExists("Current Disk Queue Length", "PhysicalDisk")) { CurrentDiskQueueLength = new PerformanceCounter("PhysicalDisk", "Current Disk Queue Length", "_Total"); } if (PerformanceCounterCategory.Exists("PhysicalDisk") && PerformanceCounterCategory.CounterExists("Avg. Disk sec/Transfer", "PhysicalDisk")) { AvgDiskSecTransfer = new PerformanceCounter("PhysicalDisk", "Avg. Disk sec/Transfer", "_Total"); } if (PerformanceCounterCategory.Exists("SQLServer:Buffer Manager") && PerformanceCounterCategory.CounterExists("Buffer cache hit ratio", "SQLServer:Buffer Manager")) { BufferCacheHitRatio = new PerformanceCounter("SQLServer:Buffer Manager", "Buffer cache hit ratio"); } if (PerformanceCounterCategory.Exists("SQLServer:Buffer Manager") && PerformanceCounterCategory.CounterExists("Page life expectancy", "SQLServer:Buffer Manager")) { PageLifeExpectancy = new PerformanceCounter("SQLServer:Buffer Manager", "Page life expectancy"); } if (PerformanceCounterCategory.Exists("SQLServer:SQL Statistics") && PerformanceCounterCategory.CounterExists("Batch Requests/sec", "SQLServer:SQL Statistics")) { BatchRequestsSec = new PerformanceCounter("SQLServer:SQL Statistics", "Batch Requests/sec"); } if (PerformanceCounterCategory.Exists("SQLServer:SQL Statistics") && PerformanceCounterCategory.CounterExists("SQL Compilations/sec", "SQLServer:SQL Statistics")) { SQLCompilationsSec = new PerformanceCounter("SQLServer:SQL Statistics", "SQL Compilations/sec"); } if (PerformanceCounterCategory.Exists("SQLServer:SQL Statistics") && PerformanceCounterCategory.CounterExists("Re-Compilations/Sec", "SQLServer:SQL Statistics")) { ReCompilationsSec = new PerformanceCounter("SQLServer:SQL Statistics", "Re-Compilations/Sec"); } if (PerformanceCounterCategory.Exists("SQLServer:Access Methods") && PerformanceCounterCategory.CounterExists("Full Scans/sec", "SQLServer:Access Methods")) { FullScansSec = new PerformanceCounter("SQLServer:Access Methods", "Full Scans/sec"); } if (PerformanceCounterCategory.Exists("SQLServer:General Statistics") && PerformanceCounterCategory.CounterExists("User Connections", "SQLServer:General Statistics")) { UserConnections = new PerformanceCounter("SQLServer:General Statistics", "User Connections"); } if (PerformanceCounterCategory.Exists("SQLServer:Locks") && PerformanceCounterCategory.CounterExists("Lock Waits/sec", "SQLServer:Locks")) { if (PerformanceCounterCategory.InstanceExists("_Total", "SQLServer:Locks")) { LockWaitsSec = new PerformanceCounter("SQLServer:Locks", "Lock Waits/sec", "_Total"); } } if (PerformanceCounterCategory.Exists("SQLServer:General Statistics") && PerformanceCounterCategory.CounterExists("Processes blocked", "SQLServer:General Statistics")) { ProcessesBlocked = new PerformanceCounter("SQLServer:General Statistics", "Processes blocked"); } if (PerformanceCounterCategory.Exists("SQLServer:Buffer Manager") && PerformanceCounterCategory.CounterExists("Checkpoint Pages/Sec", "SQLServer:Buffer Manager")) { CheckpointPagesSec = new PerformanceCounter("SQLServer:Buffer Manager", "Checkpoint Pages/Sec"); } #region IIS if (PerformanceCounterCategory.Exists("Web Service") && PerformanceCounterCategory.CounterExists("Current Connections", "Web Service")) { iisCurrentConnections = new PerformanceCounter("Web Service", "Current Connections", "_Total"); } if (PerformanceCounterCategory.Exists("ASP.NET") && PerformanceCounterCategory.CounterExists("Requests Current", "ASP.NET")) { aspnetRequestsCurrent = new PerformanceCounter("ASP.NET", "Requests Current"); } if (PerformanceCounterCategory.Exists("ASP.NET") && PerformanceCounterCategory.CounterExists("Requests Queued", "ASP.NET")) { aspnetRequestsQueued = new PerformanceCounter("ASP.NET", "Requests Queued"); } #endregion if (PerformanceCounterCategory.Exists("TCPv4") && PerformanceCounterCategory.CounterExists("Connections Established", "TCPv4")) { tcpConnectionsEstablished = new PerformanceCounter("TCPv4", "Connections Established"); } if (PerformanceCounterCategory.Exists("TCPv4") && PerformanceCounterCategory.CounterExists("Connections Active", "TCPv4")) { ConnectionsActive = new PerformanceCounter("TCPv4", "Connections Active"); } if (PerformanceCounterCategory.Exists("TCPv4") && PerformanceCounterCategory.CounterExists("Connections Passive", "TCPv4")) { ConnectionsPassive = new PerformanceCounter("TCPv4", "Connections Passive"); } if (PerformanceCounterCategory.Exists("TCPv4") && PerformanceCounterCategory.CounterExists("Connections Reset", "TCPv4")) { ConnectionsReset = new PerformanceCounter("TCPv4", "Connections Reset"); } if (PerformanceCounterCategory.Exists("TCPv4") && PerformanceCounterCategory.CounterExists("Connection Failures", "TCPv4")) { ConnectionFailures = new PerformanceCounter("TCPv4", "Connection Failures"); } if (PerformanceCounterCategory.Exists("Network Interface")) { List <string> nis = new List <string>(); NetworkInterface[] allNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); for (int i = 0; i < allNetworkInterfaces.Length; i++) { NetworkInterface networkInterface = allNetworkInterfaces[i]; if (networkInterface.OperationalStatus == OperationalStatus.Up && (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet || networkInterface.NetworkInterfaceType == NetworkInterfaceType.GigabitEthernet ) ) { nis.Add(networkInterface.Description); } } PerformanceCounterCategory nicCategory = new PerformanceCounterCategory("Network Interface"); if (nicCategory != null) { if (nis.Count > 0) { var ins = nis[0].Replace("(", "[").Replace(")", "]").Replace("/", "_"); if (PerformanceCounterCategory.InstanceExists(ins, "Network Interface")) { nics = nicCategory.GetCounters(ins); } } } if (nis.Count > 1) { var ins = nis[0].Replace("(", "[").Replace(")", "]").Replace("/", "_"); if (PerformanceCounterCategory.InstanceExists(ins, "Network Interface")) { nics2 = nicCategory.GetCounters(ins); } } } } catch (Exception ex) { Console.WriteLine("init失败" + ex.Message + ex.StackTrace); } }