// add a counter to the counter list public void AddCounter(CounterData counter) { if (counters.Count < MAX_COUNTERS) { counters.Add(counter); } }
// fetches a counter - used as a callback // if the counter is a big multi-instance counter it doesn't sleep the sampleTime // because it take a lot of time and it won't be able to send data faster than once per second anyway void FetchCounter(object o) { List <PerformanceCounter> entry = (List <PerformanceCounter>)o; while (true) { DateTime time = DateTime.Now; PerformanceCounter pc0 = entry[0]; // obtain event.id and event.Description var query = from CounterData cd in counters where cd.CategoryName == pc0.CategoryName && cd.CounterName == pc0.CounterName select cd; CounterData qcd = query.FirstOrDefault(); int id = qcd.id; string description = qcd.description; if (entry.Count == 1) { // single-instance int val = (int)Math.Round(pc0.NextValue()); if (val < 0) { val = 0; } ParseData(new SEvent(id, time, description, "global", EventType.SINGLE, new string[] { "value" }, new string[] { val.ToString() })); Thread.Sleep(samplingTime); } if (entry.Count > 1) { // multi-instance SEvent[] events = new SEvent[entry.Count]; int i = 0; foreach (PerformanceCounter pc in entry) { string instance = pc.InstanceName; int val; try { val = (int)Math.Round(pc.NextValue()); if (val < 0) { val = 0; } } catch { val = -1; } events[i++] = new SEvent(id, time, description, instance, EventType.SINGLE, new string[] { "value" }, new string[] { val.ToString() }); } ParseData(new MEvent(id, time, description, EventType.MULTIPLE, events.Length, events)); // if it's a big multi-instance counter skip the sleeping part if (entry.Count < 10) { Thread.Sleep(samplingTime); } } } }
// remove a counter from the counter list public void RemoveCounter(CounterData counter) { counters.Remove(counter); }