The apsaradb for Mongodb data service module of the Adhesive framework provides a storage function for large amounts of data. Sometimes, we want to monitor the data volume or a certain value of the data, and trigger an alarm when a threshold value is reached. In this case, you can use the alarm service module to send email and SMS alarms. The implementation of the alarm service is actually very simple. It regularly checks the data volume or data value, and then determines whether to trigger an alarm based on the configuration. If so, obtain the Alarm Receiver Based on the configuration, and then generate the corresponding alarm. Now let's take a look at the configuration of the alarm service. Similarly, you can find the configuration node of the alarm service in the global configuration on the configuration Background:
Click in:
Here we will introduce each configuration in sequence:
1. Alarm configuration of status data: the alarm configuration based on status data is configured here. The so-called status information means that only one latest data item represents a status, and an alarm is used to view the value of a column. Based on this value, an alarm is required.
2. Statistical data alarm configuration: the statistical data-based alarm configuration is configured here. The so-called statistical data is the number of data records written during a time interval. The alarm depends on the data volume, rather than the value of a column of a certain piece of data.
3. Receiver configuration: The alarm data receiver is divided into groups and managed using the group concept.
4. SMS service category ID: This is the unique category ID of our company's SMS channel and is not universal.
5. email server address and account: the Common module provides the JMail mail sending function.
6. Mail title and body, as well as text message and log message templates: A template is configured here for the mail title and body, the message content, and the log record content. There are many placeholders:
1) configuration name
2) column name
3) Alert Interval
4) data volume
5) Condition Type
6) Threshold Value
A typical data is as follows:
Adhesive. Test. Mvc error log monitoring ItemCount 60 seconds 1400 MoreThan 100
7. The time interval between sending emails and short messages, and the time interval after an error occurs in sending emails and Short Messages: here we still use the memory queue service module to send emails and short messages, the configuration here is how many milliseconds a message or short message is sent under normal circumstances, and how long it takes to try sending a short message or email again after an error occurs.
First, let's take a look at the configuration of the Alarm Receiver group:
Here we add a group named test:
Each group has the following configurations:
1. Group Name
2. Whether to enable email messages
3. Whether to enable SMS messages
4. The interval between sending mail messages, that is, only one message will be sent within a certain period of time.
5. The interval between sending mobile phone messages. Sometimes, we may send the same alarm information to two groups. The first group allows one message to be received every second, and the second group is the leading group, so we do not want to have a lot of harassment, the configuration takes a little longer.
6. Receiver details in the group:
A user is configured here, and then click to View Details:
Configure the account, real name, mobile phone number, and email address for each user.
Next, let's take a look at the configuration of the two alarm modes. The first is the data volume-based alarm:
After clicking the data volume statistics configuration node, we can see that an alarm called error log monitoring is configured here. That is, to monitor the data volume of error logs in the Adhesive. Test. Mvc table, let's take a look at how to implement this function through flexible configuration:
First, we know that the error Log exists in the Aic _ Log database, so we specify the database name Aic _ Log. Then we want to generate an alarm for the Adhesive. Test. Mvc table. We also need to configure the corresponding table name. Then, if we want to configure more than 100 data records in one minute to trigger an alarm, we need to set the time interval to 1 minute, set the data volume threshold to 100, and set the condition type to MoreThan, this is an enumeration:
internal enum AlarmConditionType { LessThan = 1, LessThanAndEqualTo = 2, MoreThan = 3, MoreThanAndEqualTo = 4, }
Configure a 10-second check to check whether the threshold value is reached in the previous minute. There is another problem here, that is, if we want to trigger an alarm only for errors, we need to further configure data filtering:
In this example, set the speed to 4. You can compare the definitions of LogInfo:
[Export dbpersistenceitem (export dbindexoption = export dbindexoption. ascending, ColumnName = "lev")] [MongodbPresentationItem (MongodbFilterOption = MongodbFilterOption. dropDownListFilter, DisplayName = "log level", ShowInTableView = true)] public LogLevel {get; set ;}
The column name at the log level is "column" and the level with the LogLevel Value of "4" is "Error:
public enum LogLevel { None = 99, Debug = 1, Info = 2, Warning = 3, Error = 4 }
Finally, let's take a look at the status-based Configuration:
Here we want to monitor the current number of items in the memory queue of the Mongodb server. If the number exceeds 100, an alarm is triggered (100 pieces of remaining data in the memory cannot be submitted to the database ).
The basic configuration is similar to the statistic configuration, but note that the column name here is fuzzy match:
Compare the definition of State _ MongodbServer, that is, an alarm is triggered when the CurrentItemConut of any memory queue exceeds 100. This is because our Mongodb storage dictionary is in a tree structure.
After introducing the alarm service configuration, let's take a look at one of the most critical implementations, that is, the Service initialization process:
internal static void Init() { mailService.Init(AlarmConfiguration.GetConfig().MailSmtp, AlarmConfiguration.GetConfig().MailUsername, AlarmConfiguration.GetConfig().MailPassword); mobileService.Init(AlarmConfiguration.GetConfig().MobileCategoryId); mailMemoryQueueService.Init(new MemoryQueueServiceConfiguration("AlarmService_MailQueue", mailService.Send) { ConsumeItemCountInOneBatch = 1, ConsumeIntervalMilliseconds = AlarmConfiguration.GetConfig().MailMessageInerval, ConsumeIntervalWhenErrorMilliseconds = AlarmConfiguration.GetConfig().MailMessageErrorInerval, ConsumeErrorAction = MemoryQueueServiceConsumeErrorAction.EnqueueTwiceAndLogException }); mobileMemoryQueueService.Init(new MemoryQueueServiceConfiguration("AlarmService_MobileQueue", mobileService.Send) { ConsumeItemCountInOneBatch = 1, ConsumeIntervalMilliseconds = AlarmConfiguration.GetConfig().MobileMessageInerval, ConsumeIntervalWhenErrorMilliseconds = AlarmConfiguration.GetConfig().MobileMessageErrorInerval, ConsumeErrorAction = MemoryQueueServiceConsumeErrorAction.EnqueueTwiceAndLogException }); var config = AlarmConfiguration.GetConfig(); foreach (var item in config.AlarmConfigurationByStatistics) { var alarmServiceState = new AlarmServiceState { AlarmConfigurationItemName = item.Key, AlarmServiceStateItems = new Dictionary<string, AlarmServiceStateItem>(), }; item.Value.AlarmReceiverGroupNames.Values.Each(groupName => { alarmServiceState.AlarmServiceStateItems.Add(groupName, new AlarmServiceStateItem { ReceiverGroupName = groupName, AlarmReceiverGroupLastMailMessageTime = DateTime.MinValue, AlarmReceiverGroupLastMobileMessageTime = DateTime.MinValue, }); }); var interval = item.Value.CheckTimeSpan; var timer = new System.Threading.Timer(CheckActionForStatistics, item.Key, interval, interval); alarmServiceState.CheckTimer = timer; alarmServiceStates.Add(alarmServiceState); } foreach (var item in config.AlarmConfigurationByStates) { var alarmServiceState = new AlarmServiceState { AlarmConfigurationItemName = item.Key, AlarmServiceStateItems = new Dictionary<string, AlarmServiceStateItem>(), }; item.Value.AlarmReceiverGroupNames.Values.Each(groupName => { alarmServiceState.AlarmServiceStateItems.Add(groupName, new AlarmServiceStateItem { ReceiverGroupName = groupName, AlarmReceiverGroupLastMailMessageTime = DateTime.MinValue, AlarmReceiverGroupLastMobileMessageTime = DateTime.MinValue, }); }); var interval = item.Value.CheckTimeSpan; var timer = new System.Threading.Timer(CheckActionForState, item.Key, interval, interval); alarmServiceState.CheckTimer = timer; alarmServiceStates.Add(alarmServiceState); } } }
We know that in the alarm service module, we also use the memory queue service to send mail and SMS data, so we first initialize two memory queues.
Then, we read the configuration, which is a statistical-data-based alarm and a status-based alarm initialization background timer. Each type is configured with a timer. The timer execution interval is the previously mentioned CheckTimeSpan. Our Code also defines the alarmServiceStates field as the root:
private static List<AlarmServiceState> alarmServiceStates = new List<AlarmServiceState>();