Пример #1
0
        public async Task <IActionResult> EditMonitor(MonitorJobRequest request)
        {
            string vaild = _monitorService.VaildMonitorJob(request);

            if (!vaild.IsEmpty())
            {
                return(Json(new HttpResultEntity(-1, vaild, null)));
            }

            IMonitorJob model = _monitorService.GetMonitorJob(request);

            if (request.Id.IsEmpty() || request.Id == "0")
            {
                await _storage.AddMonitorJob(model).ConfigureAwait(false);
            }

            else
            {
                await _storage.UpdateMonitorJob(model).ConfigureAwait(false);
            }

            await _scheduleService.UpdateMonitorJobAsync();

            return(Json(new HttpResultEntity(1, "ok", null)));
        }
Пример #2
0
        /// <summary>
        /// 检查请求错误监控
        /// </summary>
        /// <param name="job"></param>
        /// <param name="payload"></param>
        /// <returns></returns>
        private async Task <AlarmOption> CheckErrorResponseMonitor(IMonitorJob job, MonitorJobPayload payload)
        {
            if (payload.ErrorResponseMonitor == null)
            {
                return(null);
            }

            var(now, start, end) = GetNowTimes(_monitorService.ParseJobCron(job.CronLike));

            _logger.LogInformation("CheckErrorResponseMonitor Start " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            var errorCount = await _storage.GetRequestCountAsync(new RequestCountFilterOption()
            {
                Nodes       = job.Nodes.Split(','),
                StartTime   = start,
                EndTime     = end,
                StatusCodes = payload.ErrorResponseMonitor.HttpCodeStatus.Split(',').Select(x => x.ToInt()).ToArray()
            });

            var count = await _storage.GetRequestCountAsync(new RequestCountFilterOption()
            {
                Nodes     = job.Nodes.Split(','),
                StartTime = start,
                EndTime   = end,
            });

            if (count == 0)
            {
                return(null);
            }
            var percent = errorCount * 100.0 / count;

            _logger.LogInformation("CheckErrorResponseMonitor End  " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + $"  Result {(percent > payload.ErrorResponseMonitor.Percentage ? "预警" : "正常")}");

            if (percent > payload.ErrorResponseMonitor.Percentage)
            {
                return(new AlarmOption()
                {
                    IsHtml = true,
                    Content = $@"

                          <br>
                          <b>【{_lang.Monitor_Type_RequestError}】 </b>

                          <p>{_lang.Warning_Threshold}:{payload.ErrorResponseMonitor.Percentage.ToString("F2")}%  {_lang.Warning_Current}:{percent.ToString("F2")}% </p>

                          <p>{_lang.Warning_Title}:{job.Title}</p>

                          <p>{_lang.Monitor_ServiceNode}:{job.Nodes}</p>

                          <p>{_lang.Monitor_Frequency}:{_monitorService.ParseJobCronString(job.CronLike)} </p>

                          <p>{_lang.Monitor_HttpStatusCode}:{payload.ErrorResponseMonitor.HttpCodeStatus}</p>

                          <p>{_lang.Warning_TimeRange}:{start.ToStandardTime()}-{end.ToStandardTime()} </p>"
                });
            }
            return(null);
        }
Пример #3
0
        public MonitorJobRequest GetMonitorJobRequest(IMonitorJob job)
        {
            MonitorJobPayload payload = JsonConvert.DeserializeObject <MonitorJobPayload>(job.Payload);

            MonitorJobRequest request = new MonitorJobRequest()
            {
                Id          = job.Id,
                Title       = job.Title,
                Description = job.Description,
                Emails      = job.Emails,
                Interval    = ParseJobCron(job.CronLike),
                Status      = job.Status,
                Mobiles     = job.Mobiles,
                Service     = job.Service,
                Instance    = job.Instance,
                WebHook     = job.WebHook
            };

            if (payload.ResponseTimeOutMonitor != null)
            {
                request.ResponseTimeOutMonitor = new ResponseTimeOutMonitorViewModel
                {
                    Status     = payload.ResponseTimeOutMonitor.Status,
                    TimeOutMs  = payload.ResponseTimeOutMonitor.TimeOutMs.ToString(),
                    Percentage = payload.ResponseTimeOutMonitor.Percentage + "%"
                };
            }

            if (payload.ErrorResponseMonitor != null)
            {
                request.ErrorResponseMonitor = new ErrorResponseMonitorViewModel
                {
                    Status         = payload.ErrorResponseMonitor.Status,
                    HttpCodeStatus = payload.ErrorResponseMonitor.HttpCodeStatus,
                    Percentage     = payload.ErrorResponseMonitor.Percentage + "%"
                };
            }

            if (payload.IPMonitor != null)
            {
                request.IPMonitor = new IPMonitorViewModel
                {
                    Status     = payload.IPMonitor.Status,
                    WhiteList  = payload.IPMonitor.WhiteList,
                    Percentage = payload.IPMonitor.Percentage + "%"
                };
            }

            if (payload.RequestCountMonitor != null)
            {
                request.RequestCountMonitor = new RequestCountMonitorViewModel
                {
                    Status = payload.RequestCountMonitor.Status,
                    Max    = payload.RequestCountMonitor.Max.ToString()
                };
            }

            return(request);
        }
Пример #4
0
        /// <summary>
        /// 检查请求错误监控
        /// </summary>
        /// <param name="job"></param>
        /// <param name="payload"></param>
        /// <returns></returns>
        private async Task <AlarmOption> CheckErrorResponseMonitor(IMonitorJob job, MonitorJobPayload payload)
        {
            if (payload.ErrorResponseMonitor == null)
            {
                return(null);
            }

            var(now, start, end) = GetNowTimes(_monitorService.ParseJobCron(job.CronLike));

            _logger.LogInformation("检查请求错误监控开始 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            var errorCount = await _storage.GetRequestCountAsync(new RequestCountFilterOption()
            {
                Nodes       = job.Nodes.Split(','),
                StartTime   = start,
                EndTime     = end,
                StatusCodes = payload.ErrorResponseMonitor.HttpCodeStatus.Split(',').Select(x => x.ToInt()).ToArray()
            }).ConfigureAwait(false);

            var count = await _storage.GetRequestCountAsync(new RequestCountFilterOption()
            {
                Nodes     = job.Nodes.Split(','),
                StartTime = start,
                EndTime   = end,
            }).ConfigureAwait(false);

            if (count == 0)
            {
                return(null);
            }
            var percent = errorCount * 100.0 / count;

            _logger.LogInformation("检查请求错误监控结束  " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + $"  检查结果 {(percent > payload.ErrorResponseMonitor.Percentage ? "预警" : "正常")}");

            if (percent > payload.ErrorResponseMonitor.Percentage)
            {
                return(new AlarmOption()
                {
                    IsHtml = true,
                    Content = $@"

                          <br>
                          <b>【请求错误】触发预警 </b>

                          <p>命中率预警值:{payload.ErrorResponseMonitor.Percentage.ToString("F2")}%  当前值:{percent.ToString("F2")}% </p>

                          <p>任务标题:{job.Title}</p>

                          <p>监控节点:{job.Nodes}</p>

                          <p>监控频率:{_monitorService.ParseJobCron(job.CronLike)} 分钟</p>

                          <p>设定Http状态码:{payload.ErrorResponseMonitor.HttpCodeStatus}</p>

                          <p>时间段:{start.ToStandardTime()}-{end.ToStandardTime()} </p>"
                });
            }
            return(null);
        }
Пример #5
0
        /// <summary>
        /// 检查超时监控
        /// </summary>
        /// <returns></returns>
        private async Task <AlarmOption> CheckResponseTimeOutMonitor(IMonitorJob job, MonitorJobPayload payload)
        {
            if (payload.ResponseTimeOutMonitor == null)
            {
                return(null);
            }

            var(now, start, end) = GetNowTimes(_monitorService.ParseJobCron(job.CronLike));

            _logger.LogInformation("检查超时监控开始 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            var timeoutCount = await _storage.GetTimeoutResponeCountAsync(new RequestCountFilterOption()
            {
                Nodes     = job.Nodes.Split(','),
                StartTime = start,
                EndTime   = end,
            }, payload.ResponseTimeOutMonitor.TimeOutMs).ConfigureAwait(false);

            var count = await _storage.GetRequestCountAsync(new RequestCountFilterOption()
            {
                Nodes     = job.Nodes.Split(','),
                StartTime = start,
                EndTime   = end,
            }).ConfigureAwait(false);

            if (count == 0)
            {
                return(null);
            }

            var percent = timeoutCount * 100.0 / count;

            _logger.LogInformation("检查超时监控结束  " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + $"  检查结果 {(percent > payload.ResponseTimeOutMonitor.Percentage ? "预警":"正常")}");

            if (percent > payload.ResponseTimeOutMonitor.Percentage)
            {
                return(new AlarmOption()
                {
                    IsHtml = true,
                    Content = $@"

                          <br>
                          <b>【响应超时】触发预警 </b>

                          <p>超时率预警值:{payload.ResponseTimeOutMonitor.Percentage.ToString("F2")}%  当前值:{percent.ToString("F2")}% </p>

                          <p>任务标题:{job.Title}</p>

                          <p>监控节点:{job.Nodes}</p>

                          <p>监控频率:{_monitorService.ParseJobCron(job.CronLike)} 分钟</p>

                          <p>时间段:{start.ToStandardTime()}-{end.ToStandardTime()} </p>"
                });
            }

            return(null);
        }
Пример #6
0
        /// <summary>
        /// 检查超时监控
        /// </summary>
        /// <returns></returns>
        private async Task <AlarmOption> CheckResponseTimeOutMonitor(IMonitorJob job, MonitorJobPayload payload)
        {
            if (payload.ResponseTimeOutMonitor == null)
            {
                return(null);
            }

            var(now, start, end) = GetNowTimes(_monitorService.ParseJobCron(job.CronLike));

            _logger.LogInformation("CheckResponseTimeOutMonitor Start " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            var timeoutCount = await _storage.GetTimeoutResponeCountAsync(new RequestCountFilterOption()
            {
                StartTime = start,
                EndTime   = end,
            }, payload.ResponseTimeOutMonitor.TimeOutMs);

            var count = await _storage.GetRequestCountAsync(new RequestCountFilterOption()
            {
                StartTime = start,
                EndTime   = end,
            });

            if (count == 0)
            {
                return(null);
            }

            var percent = timeoutCount * 100.0 / count;

            _logger.LogInformation("CheckResponseTimeOutMonitor End  " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + $"  Result {(percent > payload.ResponseTimeOutMonitor.Percentage ? "预警" : "正常")}");

            if (percent > payload.ResponseTimeOutMonitor.Percentage)
            {
                return(new AlarmOption()
                {
                    IsHtml = true,
                    Content = $@"

                          <br>
                          <b>【{Localize.Monitor_Type_Timeout}】 </b>

                          <p> {Localize.Warning_Threshold}:{payload.ResponseTimeOutMonitor.Percentage.ToString("F2")}%  {Localize.Warning_Current}:{percent.ToString("F2")}% </p>

                          <p>{Localize.Monitor_Title}:{job.Title}</p>

                          <p>{Localize.Monitor_ServiceNode}:{job.Service}</p>

                          <p>{Localize.Monitor_Frequency}:{_monitorService.ParseJobCronString(job.CronLike)} </p>

                          <p>{Localize.Warning_TimeRange}:{start.ToStandardTime()}-{end.ToStandardTime()} </p>"
                });
            }

            return(null);
        }
Пример #7
0
        public async Task <bool> AddMonitorJob(IMonitorJob job)
        {
            var model = job as MonitorJob;

            model.Id = MD5_16(Guid.NewGuid().ToString());

            var response = await Client.IndexAsync <MonitorJob>(model, x => x.Index(GetIndexName <MonitorJob>()));

            return(response.IsValid);
        }
Пример #8
0
        public async Task <bool> AddMonitorJob(IMonitorJob job)
        {
            string sql = $@"Insert Into MonitorJob 
            (Id,Title,Description,CronLike,Emails,Mobiles,Status,Nodes,PayLoad,CreateTime)
             Values (monitorjob_seq_id.nextval,'{job.Title}','{job.Description}','{job.CronLike}','{job.Emails}','{job.Mobiles}',{job.Status},'{job.Nodes}','{job.Payload}',to_date('{job.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")}','YYYY-MM-DD HH24:MI:SS'))";

            TraceLogSql(sql);

            return(await LoggingSqlOperation(async connection => (await connection.ExecuteAsync(sql, job).ConfigureAwait(false)) > 0).ConfigureAwait(false));
        }
Пример #9
0
        public async Task <bool> UpdateMonitorJob(IMonitorJob job)
        {
            var response = await Client.IndexAsync <MonitorJob>(job as MonitorJob, a => a.Index(GetIndexName <MonitorJob>()).Id(job.Id));

            if (response != null && response.IsValid)
            {
                return(true);
            }

            return(false);
        }
Пример #10
0
 private async Task AlarmAsync(IList <AlarmOption> alarmOption, IMonitorJob job)
 {
     foreach (var item in alarmOption)
     {
         if (alarmOption != null)
         {
             item.Emails = job.Emails?.Split(',').AsEnumerable();
             item.Phones = job.Mobiles?.Split(',').AsEnumerable();
             await _alarmService.AlarmAsync(item);
         }
     }
 }
Пример #11
0
        /// <summary>
        /// 检查IP异常监控
        /// </summary>
        /// <param name="job"></param>
        /// <param name="payload"></param>
        /// <returns></returns>
        private async Task <AlarmOption> CheckIPMonitor(IMonitorJob job, MonitorJobPayload payload)
        {
            if (payload.IPMonitor == null)
            {
                return(null);
            }

            _logger.LogInformation("检查IP异常监控开始 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            var(now, start, end) = GetNowTimes(_monitorService.ParseJobCron(job.CronLike));

            var(max, count) = await _storage.GetRequestCountWithWhiteListAsync(new RequestCountWithListFilterOption()
            {
                Nodes     = job.Nodes.Split(','),
                StartTime = start,
                EndTime   = end,
                InList    = false,
                List      = payload.IPMonitor.WhileList.Split(',').ToArray(),
            }).ConfigureAwait(false);

            if (count == 0)
            {
                return(null);
            }
            var percent = max * 100.0 / count;

            _logger.LogInformation("检查IP异常监控结束  " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + $"  检查结果 {(percent > payload.IPMonitor.Percentage ? "预警" : "正常")}");

            if (percent > payload.IPMonitor.Percentage)
            {
                return(new AlarmOption()
                {
                    IsHtml = true,
                    Content = $@"

                          <br>
                          <b>【IP异常】触发预警 </b>

                          <p>IP重复率预警值:{payload.IPMonitor.Percentage.ToString("F2")}%  当前值:{percent.ToString("F2")}% </p>

                          <p>任务标题:{job.Title}</p>

                          <p>监控节点:{job.Nodes}</p>

                          <p>监控频率:{_monitorService.ParseJobCron(job.CronLike)} 分钟</p>

                          <p>IP白名单:{payload.IPMonitor.WhileList}</p>

                          <p>时间段:{start.ToStandardTime()}-{end.ToStandardTime()} </p>"
                });
            }
            return(null);
        }
Пример #12
0
        public async Task <bool> AddMonitorJob(IMonitorJob job)
        {
            job.Id = MD5_16(Guid.NewGuid().ToString());

            string sql = $@"Insert Into MonitorJob 
            (Id,Title,Description,CronLike,Emails,WebHook,Mobiles,Status,Nodes,PayLoad,CreateTime)
             Values ('{Guid.NewGuid().ToString()}','{job.Title}','{job.Description}','{job.CronLike}','{job.Emails}', '{job.WebHook}', '{job.Mobiles}',{job.Status},'{job.Nodes}','{job.Payload}',to_date('{job.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")}','YYYY-MM-DD HH24:MI:SS'))";

            TraceLogSql(sql);

            return(await LoggingSqlOperation(async connection => (await connection.ExecuteAsync(sql, job)) > 0));
        }
Пример #13
0
        public async Task <bool> AddMonitorJob(IMonitorJob job)
        {
            job.Id = MD5_16(Guid.NewGuid().ToString());

            string sql = $@"Insert Into MonitorJob 
            (Id,Title,Description,CronLike,Emails,WebHook,Mobiles,Status,Service,Instance,PayLoad,CreateTime)
             Values ('{Guid.NewGuid().ToString()}',:Title,:Description,:CronLike,:Emails,:WebHook,:Mobiles,:Status,:Service,:Instance,:Payload,to_date('{job.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")}','YYYY-MM-DD HH24:MI:SS'))";

            TraceLogSql(sql);

            return(await LoggingSqlOperation(async _ => (await _.ExecuteAsync(sql, job)) > 0));
        }
Пример #14
0
        private async Task <AlarmOption> CheckIPMonitor(IMonitorJob job, MonitorJobPayload payload)
        {
            if (payload.IPMonitor == null)
            {
                return(null);
            }

            _logger.LogInformation("CheckIPMonitor Start " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            var(now, start, end) = GetNowTimes(_monitorService.ParseJobCron(job.CronLike));

            var(max, count) = await _storage.GetRequestCountWithWhiteListAsync(new RequestCountWithListFilterOption()
            {
                Nodes     = job.Nodes.Split(','),
                StartTime = start,
                EndTime   = end,
                InList    = false,
                List      = payload.IPMonitor.WhiteList.Split(',').ToArray(),
            });

            if (count == 0)
            {
                return(null);
            }
            var percent = max * 100.0 / count;

            _logger.LogInformation("CheckIPMonitor End  " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + $"  Result {(percent > payload.IPMonitor.Percentage ? "预警" : "正常")}");

            if (percent > payload.IPMonitor.Percentage)
            {
                return(new AlarmOption()
                {
                    IsHtml = true,
                    Content = $@"

                          <br>
                          <b>【{_lang.Monitor_Type_IP}】 </b>

                          <p>{_lang.Warning_Threshold}:{payload.IPMonitor.Percentage.ToString("F2")}% {_lang.Warning_Current}:{percent.ToString("F2")}% </p>

                          <p>{_lang.Warning_Title}:{job.Title}</p>

                          <p>{_lang.Monitor_ServiceNode}:{job.Nodes}</p>

                          <p>{_lang.Monitor_Frequency}:{_monitorService.ParseJobCronString(job.CronLike)} </p>

                          <p>{_lang.Monitor_IPWhiteList}:{payload.IPMonitor.WhiteList}</p>

                          <p>{_lang.Warning_TimeRange}:{start.ToStandardTime()}-{end.ToStandardTime()} </p>"
                });
            }
            return(null);
        }
Пример #15
0
        private async Task ScheduleJobAsync(IMonitorJob model)
        {
            var job = JobBuilder.Create <MonitorBackendJob>().
                      WithIdentity(SchedulerTag + model.Id, SchedulerGroup)
                      .SetJobData(new JobDataMap {
                { "job", model }
            }).Build();

            var trigger = TriggerBuilder.Create().WithCronSchedule(model.CronLike).Build();

            await scheduler.ScheduleJob(job, trigger);
        }
Пример #16
0
        public async Task <bool> AddMonitorJob(IMonitorJob job)
        {
            string sql = $@"Insert Into MonitorJob 
            (Title,Description,CronLike,Emails,Mobiles,Status,Nodes,PayLoad,CreateTime)
             Values (@Title,@Description,@CronLike,@Emails,@Mobiles,@Status,@Nodes,@PayLoad,@CreateTime)";

            TraceLogSql(sql);

            return(await LoggingSqlOperation(async connection => (

                                                 await connection.ExecuteAsync(sql, job).ConfigureAwait(false)

                                                 ) > 0).ConfigureAwait(false));
        }
Пример #17
0
        public async Task <bool> UpdateMonitorJob(IMonitorJob job)
        {
            string sql = $@"Update MonitorJob 

                Set Title = '{job.Title}' ,Description = '{job.Description}',CronLike = '{job.CronLike}',Emails = '{job.Emails}',Mobiles = '{job.Mobiles}', WebHook = '{job.WebHook}', Status= {job.Status} ,Nodes = '{job.Nodes}',PayLoad = '{job.Payload}' 

                Where Id = '{job.Id}' ";

            TraceLogSql(sql);

            return(await LoggingSqlOperation(async connection => (

                                                 await connection.ExecuteAsync(sql, job).ConfigureAwait(false)

                                                 ) > 0).ConfigureAwait(false));
        }
Пример #18
0
        public async Task <bool> UpdateMonitorJob(IMonitorJob job)
        {
            string sql = $@"Update MonitorJob 

                Set Title = :Title ,Description = :Description,CronLike = :CronLike ,Emails = :Emails ,Mobiles = :Mobiles , WebHook = :WebHook, Status = :Status , Service =:Service,Instance = :Instance ,PayLoad = :Payload' 

                Where Id = :Id ";

            TraceLogSql(sql);

            return(await LoggingSqlOperation(async _ => (

                                                 await _.ExecuteAsync(sql, job)

                                                 ) > 0));
        }
Пример #19
0
        public async Task Execute(IJobExecutionContext context)
        {
            IMonitorJob job = context.JobDetail.JobDataMap.Get("job") as IMonitorJob;

            MonitorJobPayload payload = JsonConvert.DeserializeObject <MonitorJobPayload>(job.Payload);

            //开始调用任务
            var response = GetCheckResponse(new List <Func <IMonitorJob, MonitorJobPayload, Task <AlarmOption> > > {
                CheckResponseTimeOutMonitor,
                CheckErrorResponseMonitor,
                CheckIPMonitor,
                CheckRequestCountMonitor
            }, job, payload);

            await AlarmAsync(response.Select(x => x.Result).ToList(), job);
        }
Пример #20
0
        public async Task <bool> UpdateMonitorJob(IMonitorJob job)
        {
            string sql = $@"Update MonitorJob 

                Set Title = @Title,Description = @Description,CronLike = @CronLike,Emails = @Emails,WebHook = @WebHook, Mobiles = @Mobiles,Status= @Status,Nodes = @Nodes,PayLoad = @PayLoad 

                Where Id = @Id ";

            TraceLogSql(sql);

            return(await LoggingSqlOperation(async connection => (

                                                 await connection.ExecuteAsync(sql, job).ConfigureAwait(false)

                                                 ) > 0).ConfigureAwait(false));
        }
Пример #21
0
        public async Task <bool> UpdateMonitorJob(IMonitorJob job)
        {
            string sql = $@"Update ""{Prefix}MonitorJob""

                Set Title = @Title,Description = @Description,CronLike = @CronLike,Emails = @Emails,WebHook = @WebHook, Mobiles = @Mobiles,Status= @Status,Service = @Service, Instance = @Instance, PayLoad = @PayLoad 

                Where Id = @Id ";

            TraceLogSql(sql);

            return(await LoggingSqlOperation(async connection => (

                                                 await connection.ExecuteAsync(sql, job)

                                                 ) > 0));
        }
Пример #22
0
        public async Task <bool> AddMonitorJob(IMonitorJob job)
        {
            job.Id = MD5_16(Guid.NewGuid().ToString());

            string sql = $@"Insert Into ""{Prefix}MonitorJob"" 
            (Id,Title,Description,CronLike,Emails,WebHook,Mobiles,Status,Service,Instance,PayLoad,CreateTime)
             Values (@Id,@Title,@Description,@CronLike,@Emails,@WebHook, @Mobiles,@Status,@Service,@Instance,@PayLoad,@CreateTime)";

            TraceLogSql(sql);

            return(await LoggingSqlOperation(async connection => (

                                                 await connection.ExecuteAsync(sql, job)

                                                 ) > 0));
        }
Пример #23
0
        /// <summary>
        /// 请求量监控
        /// </summary>
        /// <param name="job"></param>
        /// <param name="payload"></param>
        /// <returns></returns>
        private async Task <AlarmOption> CheckRequestCountMonitor(IMonitorJob job, MonitorJobPayload payload)
        {
            if (payload.RequestCountMonitor == null)
            {
                return(null);
            }

            _logger.LogInformation("检查请求量监控开始 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            var(now, start, end) = GetNowTimes(_monitorService.ParseJobCron(job.CronLike));
            var count = await _storage.GetRequestCountAsync(new RequestCountFilterOption()
            {
                Nodes     = job.Nodes.Split(','),
                StartTime = start,
                EndTime   = end,
            }).ConfigureAwait(false);

            _logger.LogInformation("检查请求量监控结束  " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + $"  检查结果 {(count > payload.RequestCountMonitor.Max ? "预警" : "正常")}");

            if (count > payload.RequestCountMonitor.Max)
            {
                return(new AlarmOption()
                {
                    IsHtml = true,
                    Content = $@"

                          <br>
                          <b>【请求量监控】触发预警 </b>

                          <p>请求量最大预警值:{payload.RequestCountMonitor.Max}  当前值:{count} </p>

                          <p>任务标题:{job.Title}</p>

                          <p>监控节点:{job.Nodes}</p>

                          <p>监控频率:{_monitorService.ParseJobCron(job.CronLike)} 分钟</p>

                          <p>时间段:{start.ToStandardTime()}-{end.ToStandardTime()} </p>"
                });
            }

            return(null);
        }
Пример #24
0
        private async Task <AlarmOption> CheckRequestCountMonitor(IMonitorJob job, MonitorJobPayload payload)
        {
            if (payload.RequestCountMonitor == null)
            {
                return(null);
            }

            _logger.LogInformation("CheckRequestCountMonitor Start " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            var(now, start, end) = GetNowTimes(_monitorService.ParseJobCron(job.CronLike));
            var count = await _storage.GetRequestCountAsync(new RequestCountFilterOption()
            {
                Nodes     = job.Nodes.Split(','),
                StartTime = start,
                EndTime   = end,
            });

            _logger.LogInformation("CheckRequestCountMonitor End  " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + $"  Result {(count > payload.RequestCountMonitor.Max ? "预警" : "正常")}");

            if (count > payload.RequestCountMonitor.Max)
            {
                return(new AlarmOption()
                {
                    IsHtml = true,
                    Content = $@"

                          <br>
                          <b>【{_lang.Monitor_Type_RequestCount}】 </b>

                          <p>{_lang.Warning_Threshold}:{payload.RequestCountMonitor.Max}  {_lang.Warning_Current}:{count} </p>

                          <p>{_lang.Warning_Title}:{job.Title}</p>

                          <p>{_lang.Monitor_ServiceNode}:{job.Nodes}</p>

                          <p>{_lang.Monitor_Frequency}:{_monitorService.ParseJobCronString(job.CronLike)} </p>

                          <p>{_lang.Warning_TimeRange}:{start.ToStandardTime()}-{end.ToStandardTime()} </p>"
                });
            }

            return(null);
        }
Пример #25
0
        public async Task UpdateMonitorJobAsync()
        {
            List <IMonitorJob> list = await _storage.GetMonitorJobs().ConfigureAwait(false);

            if (list == null || list.Count == 0)
            {
                return;
            }

            foreach (var k in list)
            {
                var job = await scheduler.GetJobDetail(new JobKey(SchedulerTag + k.Id, SchedulerGroup));

                if (job == null)
                {
                    if (k.Status == 1)
                    {
                        await ScheduleJobAsync(k);
                    }
                }
                else
                {
                    if (k.Status == 0)
                    {
                        await DeleteJobAsync(job);
                    }
                    else
                    {
                        IMonitorJob monitorJob = job.JobDataMap.Get("job") as IMonitorJob;

                        // 判断是否有修改,如果修改后,重置Job
                        if (JsonConvert.SerializeObject(k) != JsonConvert.SerializeObject(monitorJob))
                        {
                            await DeleteJobAsync(job);
                            await ScheduleJobAsync(k);
                        }
                    }
                }
            }
        }
Пример #26
0
        public async Task Execute(IJobExecutionContext context)
        {
            _storage        = _storage ?? ServiceContainer.provider.GetService(typeof(IHttpReportsStorage)) as IHttpReportsStorage;
            _alarmService   = _alarmService ?? ServiceContainer.provider.GetService(typeof(IAlarmService)) as IAlarmService;
            _monitorService = _monitorService ?? ServiceContainer.provider.GetService(typeof(MonitorService)) as MonitorService;
            _logger         = _logger ?? ServiceContainer.provider.GetService(typeof(ILogger <MonitorBackendJob>)) as ILogger <MonitorBackendJob>;


            IMonitorJob job = context.JobDetail.JobDataMap.Get("job") as IMonitorJob;

            MonitorJobPayload payload = JsonConvert.DeserializeObject <MonitorJobPayload>(job.Payload);


            //开始调用任务
            var response = GetCheckResponse(new List <Func <IMonitorJob, MonitorJobPayload, Task <AlarmOption> > > {
                CheckResponseTimeOutMonitor,
                CheckErrorResponseMonitor,
                CheckIPMonitor,
                CheckRequestCountMonitor
            }, job, payload);

            await AlarmAsync(response.Select(x => x.Result).ToList(), job);
        }
Пример #27
0
        /// <summary>
        /// 检查超时监控
        /// </summary>
        /// <returns></returns>
        private async Task <AlarmOption> CheckResponseTimeOutMonitor(IMonitorJob job, MonitorJobPayload payload)
        {
            if (payload.ResponseTimeOutMonitor == null)
            {
                return(null);
            }

            var(now, start, end) = GetNowTimes(_monitorService.ParseJobCron(job.CronLike));

            _logger.LogInformation("CheckResponseTimeOutMonitor Start " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));


            #region BuildService

            string LocalIP   = "";
            int    LocalPort = 0;

            if (job.Instance.IsEmpty() || job.Instance == "ALL")
            {
                LocalIP   = "";
                LocalPort = 0;
            }
            else
            {
                LocalIP   = job.Instance.Substring(0, job.Instance.LastIndexOf(':'));
                LocalPort = job.Instance.Substring(job.Instance.LastIndexOf(':') + 1).ToInt();
            }

            #endregion


            var timeoutCount = await _storage.GetTimeoutResponeCountAsync(new RequestCountFilterOption()
            {
                Service   = job.Service,
                LocalIP   = LocalIP,
                LocalPort = LocalPort,
                StartTime = start,
                EndTime   = end,
            }, payload.ResponseTimeOutMonitor.TimeOutMs);

            var count = await _storage.GetRequestCountAsync(new RequestCountFilterOption()
            {
                Service   = job.Service,
                LocalIP   = LocalIP,
                LocalPort = LocalPort,
                StartTime = start,
                EndTime   = end,
            });

            if (count == 0)
            {
                return(null);
            }

            var percent = timeoutCount * 100.0 / count;

            _logger.LogInformation("CheckResponseTimeOutMonitor End  " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + $"   {(percent > payload.ResponseTimeOutMonitor.Percentage ? "Alert notification trigger" : "Pass")}");

            if (percent > payload.ResponseTimeOutMonitor.Percentage)
            {
                return(new AlarmOption()
                {
                    IsHtml = true,
                    Content = $@"

                          <br>
                          <b>【{_lang.Monitor_Type_Timeout}】 </b>

                          <p> {_lang.Warning_Threshold}:{payload.ResponseTimeOutMonitor.Percentage.ToString("F2")}%  {_lang.Warning_Current}:{percent.ToString("F2")}% </p>

                          <p>{_lang.Monitor_Title}:{job.Title}</p>

                          <p>{_lang.Monitor_ServiceNode}:{job.Service}</p>

                          <p>{_lang.Monitor_InstanceName}:{(job.Instance.IsEmpty() ? BasicConfig.ALLTag : job.Instance)}</p>

                          <p>{_lang.Monitor_Frequency}:{_monitorService.ParseJobCronString(job.CronLike)} </p>

                          <p>{_lang.Warning_TimeRange}:{start.ToStandardTime()}-{end.ToStandardTime()} </p>"
                });
            }

            return(null);
        }
Пример #28
0
        private IEnumerable <Task <AlarmOption> > GetCheckResponse(List <Func <IMonitorJob, MonitorJobPayload, Task <AlarmOption> > > funcs, IMonitorJob job, MonitorJobPayload payload)
        {
            List <Task <AlarmOption> > alarmOptions = new List <Task <AlarmOption> >();

            foreach (var func in funcs)
            {
                alarmOptions.Add(Task.Run(() => func.Invoke(job, payload)));
            }

            return(alarmOptions);
        }