private int CheckPerformanceCounter(PeformanceCounterSetting setting) { try { // var ruleValidator = new RuleValidator(); SettingWithPerfomanceCounter settingWithCounter = GetPerformanceCounter(setting); //if (_logPerformanceCounterDetails) //{ // LogPerformanceCounterDetails(settingWithCounter); //} var nextValue = (int)settingWithCounter.Counter.NextValue(); return(nextValue); //if (ruleValidator.ValidateValue(nextValue, settingWithCounter.Setting.ValidationExpression)) //{ // SetAlarm(settingWithCounter.Setting, GetAlarmMessage(setting, settingWithCounter)); //} //else //{ // ResetAlarm(settingWithCounter.Setting); //} } catch (Exception ex) { var message = $"Failure when trying to read performance counter '{GetPerformanceCounterDisplayString(setting)}' on machine {setting.MachineName}: {ex.GetNestedMessages()}"; //SetAlarm(setting, message); Log.To.Main.Add(message, LogLevel.Error); return(-1); } }
//private void ResetAlarm(PeformanceCounterSetting setting) //{ // var counterKey = GetCounterKey(setting); // _alarmThresholds.TryRemove(counterKey, out PendingAlarm oldValue); // Debug.WriteLine(oldValue); //} //private void SetAlarm(PeformanceCounterSetting setting, string text) //{ // var counterKey = GetCounterKey(setting); // var now = DateTimeProvider.Singleton.Time(); // Func<string, PendingAlarm> addAlarmFunc = s => new PendingAlarm(now.Add(setting.Duration)); // _hasTriggerdState = true; // PendingAlarm alarm = _alarmThresholds.GetOrAdd(counterKey, addAlarmFunc); // if(alarm.StringBuilder.Length < 1) // alarm.StringBuilder.AppendLine($"{text}"); // if (alarm.ThresholdTime <= now) // { // // sound the alarm // string message = alarm.StringBuilder.ToString(); // Log.To.Main.Add("ALARM - " + message); // _hasFiredState = true; // _notify(message); // // reset the alarm // ResetAlarm(setting); // // re-add the alarm; this performance counter measurement acts as new trigger // _alarmThresholds.TryAdd(counterKey, addAlarmFunc(counterKey)); // } //} private PeformanceCounterSetting GetPeformanceCounterSettings(string machineName, string key) { //var keys = Settings.GetSetting($"PerformanceMonitor.{machineName}.PerfCounters").Split(','); //var values = keys.Select(key => new PeformanceCounterSetting( var item = new PeformanceCounterSetting( machineName: machineName, category: Settings.GetSetting($"PerformanceMonitor.{machineName}.{key}.Category"), name: Settings.GetSetting($"PerformanceMonitor.{machineName}.{key}.Name"), instance: Settings.GetSetting($"PerformanceMonitor.{machineName}.{key}.Instance"), validationExpression: Settings.GetSetting($"PerformanceMonitor.{machineName}.{key}.ValidationExpression"), duration: Settings.GetTimeSpan($"PerformanceMonitor.{machineName}.{key}.DurationInSeconds", TimeSpanSerializedUnit.Seconds, TimeSpan.FromSeconds(10))); if ( string.IsNullOrWhiteSpace(item.Category) || string.IsNullOrWhiteSpace(item.Name) || string.IsNullOrWhiteSpace(item.ValidationExpression) || string.IsNullOrWhiteSpace(item.Instance) ) { Log.To.Main.Add("Failed validation of PerformanceMonitor agent settings", LogLevel.Error); return(null); } return(item); }
private bool Equals(PeformanceCounterSetting other) { return(Duration.Equals(other.Duration) && string.Equals(ValidationExpression, other.ValidationExpression) && string.Equals(Instance, other.Instance) && string.Equals(Name, other.Name) && string.Equals(Category, other.Category) && string.Equals(MachineName, other.MachineName)); }
private void RemoveSettingWithCounter(PeformanceCounterSetting setting) { var counterKey = GetCounterKey(setting); if (_performanceCounters.TryRemove(counterKey, out var oldSettingWithCounter)) { oldSettingWithCounter.Counter.Dispose(); Log.To.Main.Add($"Removed performance counter {GetPerformanceCounterDisplayString(oldSettingWithCounter.Setting)} on machine {oldSettingWithCounter.Setting.MachineName}. Validation expression: '{oldSettingWithCounter.Setting.ValidationExpression}', Duration: {oldSettingWithCounter.Setting.Duration}"); } }
//private string GetAlarmMessage(PeformanceCounterSetting setting, SettingWithPerfomanceCounter settingWithCounter) //{ // return string.Format("The test '{0}' has triggered for performance counter '{1}' on machine {2} has failed the test '{2}'.", // settingWithCounter.Setting.ValidationExpression, GetPerformanceCounterDisplayString(setting), setting.MachineName); //} //private void LogPerformanceCounterDetails(SettingWithPerfomanceCounter settingWithCounter) //{ // Log.To.Main.Add($"Checking performance counter '{GetPerformanceCounterDisplayString(settingWithCounter.Setting)}' on machine {settingWithCounter.Setting.MachineName}. Validation expression: '{settingWithCounter.Setting.ValidationExpression}', Duration: {settingWithCounter.Setting.Duration}",LogLevel.Verbose); //} private SettingWithPerfomanceCounter GetPerformanceCounter(PeformanceCounterSetting setting) { var counterKey = GetCounterKey(setting); var storedSettingWithCounter = _performanceCounters.GetOrAdd(counterKey, key => CreateSettingWithCounter(setting)); if (!storedSettingWithCounter.Setting.Equals(setting)) { RemoveSettingWithCounter(setting); storedSettingWithCounter = GetPerformanceCounter(setting); } return(storedSettingWithCounter); }
public SettingWithPerfomanceCounter(PeformanceCounterSetting setting, PerformanceCounter counter) { Setting = setting; Counter = counter; }
private static string GetCounterKey(PeformanceCounterSetting setting) { return(setting.MachineName + setting.Category + setting.Name + setting.Instance); }
private SettingWithPerfomanceCounter CreateSettingWithCounter(PeformanceCounterSetting setting) { Log.To.Main.Add($"Adding performance counter {GetPerformanceCounterDisplayString(setting)} on machine {setting.MachineName}. Validation expression: '{setting.ValidationExpression}', Duration: {setting.Duration}"); return(new SettingWithPerfomanceCounter(setting, new PerformanceCounter(setting.Category, setting.Name, setting.Instance, setting.MachineName))); }
private static string GetPerformanceCounterDisplayString(PeformanceCounterSetting setting) { return($"{setting.Category}\\{setting.Name}{(!string.IsNullOrWhiteSpace(setting.Instance) ? "(" + setting.Instance + ")" : string.Empty)}"); }