示例#1
0
 private void RaiseNotifierAgentAlertRaised(INotifier notifierAgent, AlertRaised alertRaised)
 {
     try
     {
         NotifierAgentAlertRaised?.Invoke(notifierAgent, alertRaised);
     }
     catch (Exception ex)
     {
         System.Diagnostics.Trace.WriteLine(string.Format("Error in RaiseRaiseMonitorPackError: {0}", ex.Message));
     }
 }
示例#2
0
        private void SendNotifierAlert(AlertRaised alertRaised)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            if (alertRaised != null && alertRaised.RaisedFor != null && alertRaised.RaisedFor.CurrentState != null)
            {
                alertRaised.RaisedFor.CurrentState.AlertsRaised = new List <string>();
                if (alertRaised.MessageRaw == null)
                {
                    alertRaised.MessageRaw = "";
                }
                if (alertRaised.MessageHTML == null)
                {
                    alertRaised.MessageHTML = "";
                }
                if (alertRaised.MessageRaw.Length == 0)
                {
                    alertRaised.MessageRaw = alertRaised.RaisedFor.CurrentState.ReadAllRawDetails();
                }
                if (alertRaised.MessageHTML.Length == 0)
                {
                    alertRaised.MessageHTML = alertRaised.RaisedFor.CurrentState.ReadAllHtmlDetails();
                }

                /*
                 * if (alertRaised.Level != AlertLevel.Debug &&
                 *      (
                 *          (alertRaised.RaisedFor.AlertHeaderText != null && alertRaised.RaisedFor.AlertHeaderText.Length > 0) ||
                 *          (alertRaised.RaisedFor.AlertFooterText != null && alertRaised.RaisedFor.AlertFooterText.Length > 0) ||
                 *          (alertRaised.Level == AlertLevel.Error && alertRaised.RaisedFor.ErrorAlertText != null && alertRaised.RaisedFor.ErrorAlertText.Length > 0) ||
                 *          (alertRaised.Level == AlertLevel.Warning && alertRaised.RaisedFor.WarningAlertText != null && alertRaised.RaisedFor.WarningAlertText.Length > 0) ||
                 *          (alertRaised.Level == AlertLevel.Info && alertRaised.RaisedFor.GoodAlertText != null && alertRaised.RaisedFor.GoodAlertText.Length > 0)
                 *      )
                 *  )
                 * {
                 *  string additionalHeaderText = "";
                 *  string additionalFooterText = "";
                 *  string additionalLevelText = "";
                 *
                 *  if (alertRaised.RaisedFor.AlertHeaderText != null && alertRaised.RaisedFor.AlertHeaderText.Length > 0)
                 *  {
                 *      additionalHeaderText = alertRaised.RaisedFor.AlertHeaderText;
                 *  }
                 *  if (alertRaised.RaisedFor.AlertFooterText != null && alertRaised.RaisedFor.AlertFooterText.Length > 0)
                 *  {
                 *      additionalFooterText = alertRaised.RaisedFor.AlertFooterText;
                 *  }
                 *
                 *  if (alertRaised.Level == AlertLevel.Error && alertRaised.RaisedFor.ErrorAlertText != null && alertRaised.RaisedFor.ErrorAlertText.Length > 0)
                 *  {
                 *      additionalLevelText = alertRaised.RaisedFor.ErrorAlertText;
                 *  }
                 *  if (alertRaised.Level == AlertLevel.Warning && alertRaised.RaisedFor.WarningAlertText != null && alertRaised.RaisedFor.WarningAlertText.Length > 0)
                 *  {
                 *      additionalLevelText = alertRaised.RaisedFor.WarningAlertText;
                 *  }
                 *  if (alertRaised.Level == AlertLevel.Info && alertRaised.RaisedFor.GoodAlertText != null && alertRaised.RaisedFor.GoodAlertText.Length > 0)
                 *  {
                 *      additionalLevelText = alertRaised.RaisedFor.GoodAlertText;
                 *  }
                 *
                 *  if (additionalLevelText.Length > 0)
                 *  {
                 *      alertRaised.MessageRaw = additionalLevelText + "\r\n" + alertRaised.MessageRaw;
                 *      alertRaised.MessageHTML = "<p>" + additionalLevelText + "</p>" + alertRaised.MessageHTML;
                 *  }
                 *  if (additionalHeaderText.Length > 0)
                 *  {
                 *      alertRaised.MessageRaw = additionalHeaderText + "\r\n" + alertRaised.MessageRaw;
                 *      alertRaised.MessageHTML = "<p>" + additionalHeaderText + "</p>" + alertRaised.MessageHTML;
                 *  }
                 *  if (additionalFooterText.Length > 0)
                 *  {
                 *      alertRaised.MessageRaw = alertRaised.MessageRaw.TrimEnd('\r', '\n') + "\r\n" + additionalFooterText;
                 *      alertRaised.MessageHTML += "<p>" + additionalFooterText + "</p>";
                 *  }
                 * }
                 */
            }

            //If alerts are paused for CollectorHost...
            if (alertRaised != null && alertRaised.RaisedFor != null && alertRaised.RaisedFor.AlertsPaused)
            {
            }
            else
            {
                //First check each NotifierHost if it is enabled and should apply to the alert
                foreach (NotifierHost notifierEntry in (from n in NotifierHosts
                                                        where
                                                        n.IsEnabledNow() &&
                                                        (int)n.AlertLevel <= (int)alertRaised.Level &&
                                                        (alertRaised.DetailLevel == DetailLevel.All || alertRaised.DetailLevel == n.DetailLevel) &&
                                                        (n.OnlyRecordAlertOnHosts == null || n.OnlyRecordAlertOnHosts.Count == 0 || n.OnlyRecordAlertOnHosts.Any(h => h.ToLower() == System.Net.Dns.GetHostName().ToLower())) &&
                                                        (alertRaised.RaisedFor == null || n.AlertForCollectors.Count == 0 || n.AlertForCollectors.Contains(alertRaised.RaisedFor.Name)) &&
                                                        (alertRaised.RaisedFor == null || n.Categories == null || n.IsCollectorInCategory(alertRaised.RaisedFor))

                                                        select n))
                {
                    try
                    {
                        List <string> alertsRecorded = new List <string>();

                        notifierEntry.ApplyConfigVarsNow();
                        foreach (INotifier notifierAgent in notifierEntry.NotifierAgents)
                        {
                            bool allowedToRun = true;
                            if (RunningAttended == AttendedOption.AttendedAndUnAttended) //no Attended option set on MonitorPack
                            {
                                allowedToRun = true;
                            }
                            else if (RunningAttended == AttendedOption.OnlyAttended) //Running in Attended mode
                            {
                                if (notifierAgent.AttendedRunOption == AttendedOption.OnlyUnAttended || notifierEntry.AttendedOptionOverride == AttendedOption.OnlyUnAttended)
                                {
                                    allowedToRun = false;
                                }
                            }
                            else //unattended mode
                            {
                                if (notifierAgent.AttendedRunOption == AttendedOption.OnlyAttended || notifierEntry.AttendedOptionOverride == AttendedOption.OnlyAttended)
                                {
                                    allowedToRun = false;
                                }
                            }
                            if (!notifierAgent.Enabled)
                            {
                                allowedToRun = false;
                            }

                            if (allowedToRun)
                            {
                                PCRaiseNotifierAlertSend();

                                notifierAgent.RecordMessage(alertRaised);

                                if (notifierAgent.AgentConfig != null)
                                {
                                    string configSummary = ((INotifierConfig)notifierAgent.AgentConfig).ConfigSummary;
                                    alertsRecorded.Add(string.Format("{0} ({1})", notifierAgent.AgentClassDisplayName, configSummary));
                                    RaiseNotifierAgentAlertRaised(notifierAgent, alertRaised);
                                    LoggingAlertsRaisedEvent(string.Format("Alert raised for Collector '{0}'\r\nNotifier: '{1}'\r\n{2}", alertRaised.RaisedFor.Name, notifierAgent.Name, alertRaised.MessageRaw));
                                }
                                else
                                {
                                    LoggingAlertsRaisedEvent(string.Format("Alert raised notifier: '{0}'\r\n{1}", notifierAgent.Name, alertRaised.MessageRaw));
                                }
                            }
                        }
                        if (alertsRecorded.Count > 0 && alertRaised.RaisedFor != null && alertRaised.RaisedFor.CurrentState != null)
                        {
                            StringBuilder sbAlertsRaisedSummary = new StringBuilder();
                            sbAlertsRaisedSummary.AppendLine(notifierEntry.Name);
                            alertsRecorded.ForEach(araised => sbAlertsRaisedSummary.AppendLine("  " + araised));
                            alertRaised.RaisedFor.CurrentState.AlertsRaised.Add(sbAlertsRaisedSummary.ToString());
                        }
                        LoggingNotifierEvent(string.Format("Notifier '{0}' called for {1} alert on '{2}'", notifierEntry.Name, alertRaised.Level, (alertRaised.RaisedFor == null ? "Unknown Collector" : alertRaised.RaisedFor.Name)));
                    }
                    catch (Exception ex)
                    {
                        RaiseNotifierError(notifierEntry, ex.ToString());
                        WriteLogging(string.Format("Error in SendNotifierAlert: {0}\r\nNotifier: {1}", ex.Message, notifierEntry.Name));
                    }
                }
            }
            sw.Stop();
            PCSetNotifiersSendTime(sw.ElapsedMilliseconds);
            PCRaiseNotifiersCalled();
        }
示例#3
0
 public abstract void RecordMessage(AlertRaised alertRaised);