/// <summary> /// Find the process counter name /// </summary> /// <returns></returns> public static string GetProcessCounterName() { Process process = Process.GetCurrentProcess(); int id = process.Id; PerformanceCounterCategory perfCounterCat = new PerformanceCounterCategory("Process"); foreach(DictionaryEntry entry in perfCounterCat.ReadCategory()["id process"]) { string processCounterName = (string)entry.Key; if (((InstanceData)entry.Value).RawValue == id) return processCounterName; } return ""; }
/// <summary> /// Harvest a performance category. /// </summary> /// <param name="category">The name of the performance category.</param> /// <returns>A harvested file.</returns> public Util.PerformanceCategory HarvestPerformanceCategory(string category) { if (null == category) { throw new ArgumentNullException("category"); } if (PerformanceCounterCategory.Exists(category)) { Util.PerformanceCategory perfCategory = new Util.PerformanceCategory(); // Get the performance counter category and set the appropriate WiX attributes PerformanceCounterCategory pcc = new PerformanceCounterCategory(category); perfCategory.Id = CompilerCore.GetIdentifierFromName(pcc.CategoryName); perfCategory.Name = pcc.CategoryName; perfCategory.Help = pcc.CategoryHelp; if (PerformanceCounterCategoryType.MultiInstance == pcc.CategoryType) { perfCategory.MultiInstance = Util.YesNoType.yes; } foreach (InstanceDataCollection counter in pcc.ReadCategory().Values) { Util.PerformanceCounter perfCounter = new Util.PerformanceCounter(); // Get the performance counter and set the appropriate WiX attributes PerformanceCounter pc = new PerformanceCounter(pcc.CategoryName, counter.CounterName); perfCounter.Name = pc.CounterName; perfCounter.Type = CounterTypeToWix(pc.CounterType); perfCounter.Help = pc.CounterHelp; perfCategory.AddChild(perfCounter); } return perfCategory; } else { throw new WixException(UtilErrors.PerformanceCategoryNotFound(category)); } }
private void tvCategories_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e) { // Clear the listview to prep it for new data this.lvCounters.Items.Clear(); // If a category was selected, that has multiple instances skip everything if(e.Node.Parent == null && e.Node.GetNodeCount(false) > 0) return; // If there are no instances just get the category name, otherwise get category and instance name String strCategory; String strInstance = null; if(e.Node.Parent == null) { strCategory = e.Node.Text; } else { strCategory = e.Node.Parent.Text; strInstance = e.Node.Text; } // Suspend drawing until we're done this.lvCounters.BeginUpdate(); // Get the selected category PerformanceCounterCategory perfcat = new PerformanceCounterCategory(strCategory, this.comboBox.Text); try { InstanceDataCollectionCollection datacollcoll = perfcat.ReadCategory(); foreach(InstanceDataCollection datacoll in datacollcoll.Values) { foreach(InstanceData data in datacoll.Values) { if(strInstance == null || data.InstanceName == strInstance) { ListViewItem item = new ListViewItem(datacoll.CounterName); item.SubItems.Add(data.RawValue.ToString()); this.lvCounters.Items.Add(item); break; } } } } catch(Exception ex) { Console.WriteLine(ex); } // Result drawing this.lvCounters.EndUpdate(); }
public void PerfCounterRepopulate(PfcMonitor pfc) { alreadyPopulated = true; perfCounterCategories = pfc.Server == "" ? PerformanceCounterCategory.GetCategories() : PerformanceCounterCategory.GetCategories(pfc.Server); for (int x = 0; x < perfCounterCategories.Length; x++) { perfCounterPCTypeDdl.Items.Add(perfCounterCategories[x].CategoryName); } PerformanceCounterCategory pcc = new PerformanceCounterCategory(pfc.Category, pfc.Server); InstanceDataCollectionCollection idcc = pcc.ReadCategory(); perfCounterNames = new List<String>(idcc.Count); foreach (DictionaryEntry idc in idcc) { perfCounterNames.Add(((InstanceDataCollection)idc.Value).CounterName); } perfCounterInstances = new List<String>(pcc.GetInstanceNames()); if (perfCounterNames.Count > 0) perfCounterCounterNameDdl.Items.AddRange(perfCounterNames.ToArray()); else { perfCounterCounterNameDdl.Items.Add("None"); perfCounterCounterNameDdl.SelectedItem = "None"; } if (perfCounterInstances.Count > 0) perfCounterInstanceNameDdl.Items.AddRange(perfCounterInstances.ToArray()); else { perfCounterInstanceNameDdl.Items.Add("None"); perfCounterInstanceNameDdl.SelectedItem = "None"; } try { perfCounterPCTypeDdl.SelectedIndexChanged -= PerfCounterPcTypeDdlSelectedIndexChanged; perfCounterPCTypeDdl.SelectedItem = pfc.Category; perfCounterPCTypeDdl.SelectedIndexChanged += PerfCounterPcTypeDdlSelectedIndexChanged; perfCounterCounterNameDdl.SelectedItem = pfc.Counter; perfCounterInstanceNameDdl.SelectedItem = pfc.Instance; perfCounterTestDataThresholdBreachTextBox.Text = pfc.ThresholdBreachCount.ToString(); perfCounterTestDataThresholdLessThanCheckBox.Checked = pfc.ThresholdLessThan; perfCounterTestDataThresholdPanicTextBox.Text = pfc.ThresholdPanic; perfCounterTestDataThresholdWarningTextBox.Text = pfc.ThresholdWarning; perfCounterTestDataUpdateFreqTextBox.Text = pfc.UpdateFrequency.ToString(); } catch(Exception ex) { Logger.Instance.LogException(this.GetType(), ex); } perfCounterCategoryWaitLabel.Visible = false; perfCounterPCTypeDdl.Visible = true; perfCounterCounterNameWaitLabel.Visible = false; perfCounterCounterNameDdl.Visible = true; perfCounterInstanceNameWaitLabel.Visible = false; perfCounterInstanceNameDdl.Visible = true; }
private void GetCounterNamesAndInstances(object selectedItemText) { PerformanceCounterCategory pcc = new PerformanceCounterCategory((String)selectedItemText, IpOrHostName); try { InstanceDataCollectionCollection idcc = pcc.ReadCategory(); perfCounterNames = new List<String>(idcc.Count); foreach (DictionaryEntry idc in idcc) { perfCounterNames.Add(((InstanceDataCollection) idc.Value).CounterName); } } catch (Exception ex) { Logger.Instance.LogException(this.GetType(), ex); ToLog = "Error: " + ex.Message; } try { perfCounterInstances = new List<String>(pcc.GetInstanceNames()); } catch (Exception ex) { Logger.Instance.LogException(this.GetType(), ex); ToLog = "Error: " + ex.Message; } Invoke(new PopulateCounterNamesDelegate(PopulatePerfCounterNamesAndInstancesDdl)); }