static void Main(string[] args) { try { MonitorManager mm = new MonitorManager(); mm.TriggerMointors(); } catch (Exception ex) { Log.WriteErrorLog(ex.ToString()); } finally { ExecutionInfoStore.SerializeStore(); if (Log.ErrorCount > 0) { EmailHelper.SendEmail(string.Format("InteropMonitoring project throws {0} exception{1}.", Log.ErrorCount, Log.ErrorCount > 1 ? "s" : string.Empty), string.Format("Log folder: {0}", Log.LogDirectory), string.Empty, string.Empty, Log.LogFilePath); } else { EmailHelper.SendEmail("InteropMonitoring project works fine.", string.Empty, string.Empty, string.Empty, Log.LogFilePath); } } }
/// <summary> /// Execute all Rules and send mail if need alert. /// </summary> public void ExecuteRules() { List <MonitorRule> rules = LoadRules(); foreach (MonitorRule rule in rules) { try { if (rule.IsEnable) { DateTime lastQueryTime = DateTime.UtcNow; Log.WriteInfoLog("Rule: {0} start to execute", rule.RuleUniqueIdentity); var needAlert = ExecuteRule(rule, out string result); // If the result is empty, the ExecuteRule throws exception. The error log is written in ExecuteRule method. if (string.IsNullOrWhiteSpace(result)) { continue; } Log.WriteInfoLog("Rule: {0} execute done, result is {1}", rule.RuleUniqueIdentity, result); if (needAlert) { Log.WriteInfoLog("Rule: {0} meet the condition.", rule.RuleUniqueIdentity); TakeAlertAction(rule, result); } else { Log.WriteInfoLog("Rule: {0} don't meet the condition and no need alert.", rule.RuleUniqueIdentity); } ExecutionInfoStore.SetLastQueryTime(rule.RuleUniqueIdentity, lastQueryTime); } else { Log.WriteInfoLog("Rule: {0} is disabled.", rule.RuleUniqueIdentity); } } catch (Exception ex) { Log.WriteErrorLog("{0} throw exception: {1}", rule.RuleUniqueIdentity, ex.ToString()); } } }
/// <summary> /// Take Alert action: Send Email /// </summary> /// <param name="rule">The specified MonitorRule instance.</param> /// <param name="result">The value returned by ExcuteRule method.</param> public virtual void TakeAlertAction(MonitorRule rule, string result) { string subject = string.Format("Interop Monitoring Alert Fired:{0}", rule.RuleId); //Write the common method to fire the alert action, and this can also be overwritten by derived classed if ((DateTime.UtcNow - ExecutionInfoStore.GetLastActionTime(rule.RuleUniqueIdentity)).TotalMinutes > rule.AlertSuppressionWindowInMinutes) { Log.WriteInfoLog("Rule: {0} compare with last alert time and need alert, start to send alert mail.", rule.RuleUniqueIdentity); string ccEmail = ConfigurationManager.AppSettings[ConstStrings.ActionEmailServerity + rule.AlertSeverity.ToString()]; var body = AlertMail.GenerateAlertMail(rule, result); EmailHelper.SendEmail(subject, body, rule.EmailNotificationAddress, ccEmail); ExecutionInfoStore.SetLastActionTime(rule.RuleUniqueIdentity, DateTime.UtcNow); Log.WriteInfoLog("Rule: {0} send rule alert mail done", rule.RuleUniqueIdentity); } else { Log.WriteInfoLog("Rule: {0} compare with last alert time and no need to alert", rule.RuleUniqueIdentity); } }
/// <summary> /// Replace AtLastQueryTime using the actual last query time in AlertQuery property. /// </summary> /// <param name="tempRule">The specified monitor rule.</param> /// <returns>The MonitorRule with the updated AlertQuery value.</returns> public static MonitorRule ReplaceAtLastQueryTime(MonitorRule tempRule) { tempRule.AlertQuery = tempRule.AlertQuery.Replace("@LastQueryTime", ExecutionInfoStore.GetLastQueryTime(tempRule.RuleUniqueIdentity).ToString()); return(tempRule); }