Пример #1
0
 private static string FormatMessage(string message, AlarmConfigurationItemBase configItem, int count, string columnName, Func<string> getDetail)
 {
     message = message.Replace("{ConfigName}", configItem.ConfigName);
     message = message.Replace("{Description}", configItem.Description);
     message = message.Replace("{ColumnName}", columnName);
     message = message.Replace("{DataTimeSpanSeconds}", configItem.DataTimeSpan.TotalSeconds.ToString());
     message = message.Replace("{ActualItemCount}", count.ToString());
     var conditionType = "";
     switch (configItem.ConditionType)
     {
         case AlarmConditionType.LessThan:
             conditionType = "<";
             break;
         case AlarmConditionType.LessThanAndEqualTo:
             conditionType = "<=";
             break;
         case AlarmConditionType.MoreThan:
             conditionType = ">";
             break;
         case AlarmConditionType.MoreThanAndEqualTo:
             conditionType = ">=";
             break;
     }
     message = message.Replace("{ConditionType}", conditionType);
     message = message.Replace("{ItemCount}", configItem.Value.ToString());
     if (message.Contains("{Detail}"))
     {
         if (configItem is AlarmConfigurationItemByStatistics && getDetail != null)
             message = message.Replace("{Detail}", getDetail());
         else
             message = message.Replace("{Detail}", "");
     }
     return message;
 }
Пример #2
0
        private static void IntervalCheckAction(AlarmConfigurationItemBase configItem, int count, string columnName, AlarmServiceState state, Func<string> getDetail)
        {
            var needAction = false;
            switch (configItem.ConditionType)
            {
                case AlarmConditionType.LessThan:
                    {
                        if (count < configItem.Value)
                            needAction = true;
                        break;
                    }
                case AlarmConditionType.LessThanAndEqualTo:
                    {
                        if (count <= configItem.Value)
                            needAction = true;
                        break;
                    }
                case AlarmConditionType.MoreThan:
                    {
                        if (count > configItem.Value)
                            needAction = true;
                        break;
                    }
                case AlarmConditionType.MoreThanAndEqualTo:
                    {
                        if (count >= configItem.Value)
                            needAction = true;
                        break;
                    }
            }
            if (!needAction)
            {
                var logMessage = FormatMessage(AlarmConfiguration.GetConfig().LogMessageTemlate, configItem, count, columnName, null);
                AppInfoCenterService.LoggingService.Debug(AlarmConfigurationBase.ModuleName, "AlarmService", "IntervalCheckAction",
                    string.Format("{0} 没达到报警条件", logMessage), new ExtraInfo
                {
                    DropDownListFilterItem1 = "没达到报警条件",
                    DropDownListFilterItem2 = configItem.ConfigName,
                });
                return;
            }

            foreach (var groupName in configItem.AlarmReceiverGroupNames.Values)
            {
                InternalHandleAlarm(configItem, state.AlarmServiceStateItems[groupName], groupName, count, columnName, getDetail);
            }
        }
Пример #3
0
        private static void InternalHandleAlarm(AlarmConfigurationItemBase configItem, AlarmServiceStateItem state, string groupName, int count, string columnName, Func<string> getDetail)
        {
            var group = AlarmConfiguration.GetConfig().AlarmReceiverGroups.Values.FirstOrDefault(g => g.GroupName == groupName);
            if (group == null) return;
            var logMessgae = FormatMessage(AlarmConfiguration.GetConfig().LogMessageTemlate, configItem, count, columnName, null);

            CreateAlarmEvent(configItem);

            bool b = AlarmIsHandling(configItem.ConfigName);

            if (group.EnableMailMessage)
            {
                var mailPastTime = DateTime.Now - state.AlarmReceiverGroupLastMailMessageTime;
                if (b)
                {
                    AppInfoCenterService.LoggingService.Info(AlarmConfigurationBase.ModuleName, "AlarmService", "InternalHandleAlarm", "事件正在处理,暂停报警",
                        new ExtraInfo
                        {
                            DropDownListFilterItem1 = "事件正在处理,暂停邮件报警",
                            DropDownListFilterItem2 = configItem.ConfigName,
                        });
                }
                else if (state.AlarmReceiverGroupLastMailMessageTime == DateTime.MinValue || mailPastTime > group.MailMessageIntervalTimeSpan)
                {
                    var mailItems = group.AlarmReceivers.Select(r =>
                    {
                        var contact = GetMongodbAdminConfigurationItem(r.Value.Name);
                        if (contact != null && !string.IsNullOrEmpty(contact.MailAddress))
                        {
                            return new MailItem
                            {
                                MailAddress = contact.MailAddress,
                                MailTitle = FormatMessage(AlarmConfiguration.GetConfig().MailTitleTemplate, configItem, count, columnName, getDetail),
                                MailBody = FormatMessage(AlarmConfiguration.GetConfig().MailBodyTemplate, configItem, count, columnName, getDetail),
                            };
                        }
                        return null;
                    }).ToList();

                    mailItems.Where(item => item != null).ToList().ForEach(mailItem =>
                    {
                        mailMemoryQueueService.Enqueue(mailItem);
                        AppInfoCenterService.LoggingService.Info(AlarmConfigurationBase.ModuleName, "AlarmService", "InternalHandleAlarm",
                            string.Format("{0} {1} > {2} 达到邮件发送条件 -> {3}", logMessgae, mailPastTime.ToString(), group.MailMessageIntervalTimeSpan.ToString(), mailItem.MailAddress),
                            new ExtraInfo
                            {
                                DropDownListFilterItem1 = "达到邮件发送条件",
                                DropDownListFilterItem2 = configItem.ConfigName,
                            });
                    });
                    state.AlarmReceiverGroupLastMailMessageTime = DateTime.Now;
                }
                else
                {
                    AppInfoCenterService.LoggingService.Info(AlarmConfigurationBase.ModuleName, "AlarmService", "InternalHandleAlarm",
                        string.Format("{0} {1} <= {2} 没达到邮件发送条件", logMessgae, mailPastTime.ToString(), group.MailMessageIntervalTimeSpan.ToString()),
                        new ExtraInfo
                        {
                            DropDownListFilterItem1 = "没达到邮件发送条件",
                            DropDownListFilterItem2 = configItem.ConfigName,
                        });
                }
            }

            if (group.EnableMobileMessage)
            {
                var mobilePastTime = DateTime.Now - state.AlarmReceiverGroupLastMobileMessageTime;
                if (b)
                {
                    AppInfoCenterService.LoggingService.Info(AlarmConfigurationBase.ModuleName, "AlarmService", "InternalHandleAlarm", "事件正在处理,暂停报警",
                        new ExtraInfo
                        {
                            DropDownListFilterItem1 = "事件正在处理,暂停短信报警",
                            DropDownListFilterItem2 = configItem.ConfigName,
                        });
                }
                else if (state.AlarmReceiverGroupLastMobileMessageTime == DateTime.MinValue || mobilePastTime > group.MobileMessageIntervalTimeSpan)
                {
                    var mobileItems = group.AlarmReceivers.Select(r =>
                    {
                        var contact = GetMongodbAdminConfigurationItem(r.Value.Name);
                        if (contact != null && !string.IsNullOrEmpty(contact.MobileNumber))
                        {
                            return new MobileItem
                            {
                                MobileNumber = contact.MobileNumber,
                                MobileMessage = FormatMessage(AlarmConfiguration.GetConfig().MobileMessageTemlate, configItem, count, columnName, getDetail),
                            };
                        };
                        return null;
                    }).ToList();

                    mobileItems.Where(item => item != null).ToList().ForEach(mobileItem =>
                    {
                        mobileMemoryQueueService.Enqueue(mobileItem);
                        AppInfoCenterService.LoggingService.Info(AlarmConfigurationBase.ModuleName, "AlarmService", "InternalHandleAlarm",
                            string.Format("{0} {1} > {2} 达到短信发送条件 -> {3}", logMessgae, mobilePastTime.ToString(), group.MobileMessageIntervalTimeSpan.ToString(), mobileItem.MobileNumber),
                            new ExtraInfo
                            {
                                DropDownListFilterItem1 = "达到短信发送条件",
                                DropDownListFilterItem2 = configItem.ConfigName,
                            });
                    });
                    state.AlarmReceiverGroupLastMobileMessageTime = DateTime.Now;
                }
                else
                {
                    AppInfoCenterService.LoggingService.Info(AlarmConfigurationBase.ModuleName, "AlarmService", "InternalHandleAlarm",
                        string.Format("{0} {1} <= {2} 没达到短信发送条件", logMessgae, mobilePastTime.ToString(), group.MobileMessageIntervalTimeSpan.ToString()), new ExtraInfo
                        {
                            DropDownListFilterItem1 = "没达到短信发送条件",
                            DropDownListFilterItem2 = configItem.ConfigName,
                        });
                }
            }
        }
Пример #4
0
 private static void CreateAlarmEvent(AlarmConfigurationItemBase configItem)
 {
     try
     {
         var dbContextFactory = LocalServiceLocator.GetService<IDbContextFactory>();
         using (var context = dbContextFactory.CreateContext<AlarmDbContext>())
         {
             var item = context.AlarmItems.FirstOrDefault(a => a.AlarmStatusId != (int)AlarmStatus.Closed
                 && a.AlarmConfigName == configItem.ConfigName);
             if (item == null)
             {
                 item = new AlarmItem()
                 {
                     AlarmDatabaseName = configItem.DatabasePrefix,
                     AlarmTableName = configItem.TableName,
                     AlarmConfigName = configItem.ConfigName,
                     AlarmStatus = AlarmStatus.Open,
                     OpenTime = DateTime.Now,
                 };
                 context.AlarmItems.Add(item);
                 context.SaveChanges();
             }
         }
     }
     catch (Exception ex)
     {
         ex.Handle(AlarmConfigurationBase.ModuleName, "AlarmService", "CreateAlarmEvent");
     }
 }