Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // just an example
- string categoryName = "Processor";
- string counterName = "% Processor Time";
- string host = "<name or IP>"; // PRTG's %host
- string instanceName = "_Total";
- if (PerformanceCounterCategory.Exists(categoryName, host) &&
- PerformanceCounterCategory.CounterExists(counterName, categoryName, host) &&
- PerformanceCounterCategory.InstanceExists(instanceName, categoryName, host))
- {
- // despite the above verifications, sometimes a "category doesn't exist", "counter doesn't exist" or "instance doesn't exist" exception still occurs!
- PerformanceCounter counter = string.IsNullOrEmpty(instanceName) ?
- new PerformanceCounter(categoryName, counterName, true) { MachineName = host } :
- new PerformanceCounter(categoryName, counterName, instanceName, host) { ReadOnly = true };
- counter.BeginInit();
- // some counters need the previous value
- counter.NextValue();
- Thread.Sleep(1000);
- // the needed value
- float value = counter.NextValue();
- counter.Close();
- counter.Dispose();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement