public async Task <bool> AddMonitorJob(MonitorJob job) { job.CreateTime = DateTime.Now; job.Id = MD5_16(Guid.NewGuid().ToString()); return(await freeSql.Insert <MonitorJob>(job).ExecuteAffrowsAsync() > 0); }
public void Process(MonitorJob monitorJob) { Log.DebugFormat("Stepped into process for monitorjob '{0}' with directory '{1}' and fileExtensionToWatch '{2}'", monitorJob.ID, monitorJob.Path, monitorJob.FileExtensionToWatch); if (!Directory.Exists(monitorJob.Path)) { Log.ErrorFormat("We could not find the directory '{0}'", monitorJob.Path); throw new DirectoryNotFoundException(monitorJob.Path); } DirectoryInfo directoryInfo = new DirectoryInfo(monitorJob.Path); var allTheFiles = directoryInfo.GetFiles(monitorJob.FileExtensionToWatch); Log.DebugFormat("We found '{0}' files with extension '{1}'to check the size on in the directory '{2}'", allTheFiles.Length, monitorJob.FileExtensionToWatch, monitorJob.Path); foreach (var fileInfo in allTheFiles) { Log.DebugFormat("FileSmallerThanThresholdMonitorer found '{0}' files to process", allTheFiles.Length); if (fileInfo.Length < monitorJob.MinFileSizeInBytes) { Log.DebugFormat("we are looking at a file of size '{0}'", fileInfo.Length); var message = string.Format("There is a file '{0}' of type '{1}' smaller than the min filesize'{2}' in the directory '{3}'", fileInfo.Name, monitorJob.FileExtensionToWatch, monitorJob.MinFileSizeInBytes, monitorJob.Path); Log.InfoFormat(message); _emailActions.SendAlert(message); return; } } Log.DebugFormat("FileSmallerThanThresholdMonitorer.Process did not find a file smaller than '{0}'bytes to process", monitorJob.MinFileSizeInBytes); }
public MonitorJob GetMonitorJob(MonitorJobRequest request) { MonitorJobPayload payload = new MonitorJobPayload(); if (request.ResponseTimeOutMonitor != null) { payload.ResponseTimeOutMonitor = new ResponseTimeOutMonitorJob { Status = request.ResponseTimeOutMonitor.Status, TimeOutMs = request.ResponseTimeOutMonitor.TimeOutMs.ToInt(), Percentage = request.ResponseTimeOutMonitor.Percentage.Replace("%", "").ToDouble(2) }; } if (request.ErrorResponseMonitor != null) { payload.ErrorResponseMonitor = new ErrorResponseMonitorJob { Status = request.ErrorResponseMonitor.Status, HttpCodeStatus = request.ErrorResponseMonitor.HttpCodeStatus.Replace(",", ","), Percentage = request.ErrorResponseMonitor.Percentage.Replace("%", "").ToDouble(2) }; } if (request.IPMonitor != null) { payload.IPMonitor = new IPMonitorJob { Status = request.IPMonitor.Status, WhiteList = (request.IPMonitor.WhiteList ?? string.Empty), Percentage = request.IPMonitor.Percentage.Replace("%", "").ToDouble(2) }; } if (request.RequestCountMonitor != null) { payload.RequestCountMonitor = new RequestCountMonitorJob { Status = request.RequestCountMonitor.Status, Max = request.RequestCountMonitor.Max.ToInt() }; } MonitorJob model = new MonitorJob() { Id = request.Id, Title = request.Title, Description = (request.Description ?? string.Empty), CronLike = ParseJobRate(request.Interval), Emails = request.Emails.Replace(",", ","), WebHook = request.WebHook, Mobiles = (request.Mobiles ?? string.Empty).Replace(",", ","), Status = request.Status, Nodes = request.Nodes, Payload = JsonConvert.SerializeObject(payload), CreateTime = DateTime.Now }; return(model); }
public async Task <bool> AddMonitorJob(MonitorJob job) { job.CreateTime = DateTime.Now; job.Id = _idWorker.NextId(); return(await freeSql.Insert <MonitorJob>(job).ExecuteAffrowsAsync() > 0); }
public DateTime NextTimeThisJobShouldRun(MonitorJob monitorJob) { var result = monitorJob.LastTimeThisJobRan.Value.Add(GetTimeSpanToUse(monitorJob.Threshold, monitorJob.ThresholdType)); Log.DebugFormat("NextTimeThisJobShouldRun is returning '{0}'", result); return(result); }
public void Process(MonitorJob monitorJob) { Log.DebugFormat("Stepped into process for monitorjob '{0}'", monitorJob.ID); if (!Directory.Exists(monitorJob.Path)) { Log.ErrorFormat("We could not find the directory '{0}'", monitorJob.Path); throw new DirectoryNotFoundException(monitorJob.Path); } var directoryInfo = new DirectoryInfo(monitorJob.Path); var allTheFiles = directoryInfo.GetFiles(monitorJob.FileExtensionToWatch); foreach (var fileInfo in allTheFiles) { if (fileInfo.CreationTime < _timeActions.Now().Subtract(GetTimeSpanToUse(monitorJob.Threshold, monitorJob.ThresholdType))) { var message = string.Format("There is a file '{0}' of type '{1}' older than the threshold '{2}' {3} in the directory '{4}'", fileInfo.Name, monitorJob.FileExtensionToWatch, monitorJob.Threshold, monitorJob.ThresholdType, monitorJob.Path); Log.InfoFormat(message); _emailActions.SendAlert(message); return; } } }
public MonitorJobRequest GetMonitorJobRequest(MonitorJob 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); }
public async Task <AlarmOption> RequestCountTask(MonitorJob job, MonitorJobPayload payload) { if (payload.CallMonitor == null) { return(null); } var(now, start, end) = GetNowTimes(ParseJobCron(job.CronLike)); if (payload.CallMonitor.Min == 0 && payload.CallMonitor.Max == 0) { return(null); } if (!job.StartTime.IsEmpty() && !job.EndTime.IsEmpty()) { var startTime = new DateTime(now.Year, now.Month, now.Day, job.StartTime.Split(':')[0].ToInt(), job.StartTime.Split(':')[1].ToInt(), 0, DateTimeKind.Local); var endTime = new DateTime(now.Year, now.Month, now.Day, job.EndTime.Split(':')[0].ToInt(), job.EndTime.Split(':')[1].ToInt(), 0, DateTimeKind.Local); if (now < startTime || now > endTime) { return(null); } } var total = await _storage.GetCallCountAsync(new CallCountTaskFilter { Service = job.Service, Instance = job.Instance, StartTime = start, EndTime = end }); if (total < payload.CallMonitor.Min || total > payload.CallMonitor.Max) { return(new AlarmOption() { IsHtml = true, Alarm = new MonitorAlarm { JobId = job.Id, Body = $"【{job.Title}】 {_lang.Monitor_Type_RequestCount} --- {_lang.Warning_Threshold}:{_lang.Min} {payload.CallMonitor.Min} {_lang.Max} {payload.CallMonitor.Max} {_lang.Warning_Current}:{total} ", CreateTime = DateTime.Now }, Content = $@" <br> <b>{_lang.Monitor_Type_RequestCount} </b> <p>{_lang.Monitor_Title}:{job.Title} </p> <p>{_lang.Warning_Threshold}:{_lang.Min}:{payload.CallMonitor.Min} {_lang.Max}:{payload.CallMonitor.Max} {_lang.Warning_Current}:{total} </p> <p>{_lang.Warning_Title}:{job.Title}</p> <p>{_lang.Monitor_ServiceNode}:{job.Service}</p> <p>{_lang.Monitor_InstanceName}:{(job.Instance.IsEmpty() ? "ALL" : job.Instance)} </p> <p>{_lang.Monitor_Frequency}:{ParseJobCronString(job.CronLike)} </p> <p>{_lang.Warning_TimeRange}:{start.ToStandardTime()} {_lang.To} {end.ToStandardTime()} </p>" }); } return(null); }
public void Setup() { _provider = TestUtility.InitDI(); _scope = _provider.CreateScope(); _testDbContext = new ApplicationDbContext(_scope.ServiceProvider.GetRequiredService <DbContextOptions <ApplicationDbContext> >()); _processService = Substitute.For <EndpointProcessService>(null, null, null, null); _service = new MonitorJob(_provider, _processService); SeedEndpoints(); }
public void ReturnsTrueWhenTheLastRunTimeIsNull() { var monitorJob = new MonitorJob { LastTimeThisJobRan = null, }; var result = _monitorJobActions.ThisJobShouldRunNow(monitorJob); Assert.That(result, Is.True); }
public void CallsEmailActionsWhenThereIsAFileInTheFolderOlderThanTheThreshold() { var monitorJobToUse = new MonitorJob { MontiredJobType = MontiredJobType.StaleFileMonitor, Path = @"..\monitorey.Integration.Tests\TestBadFileFolder", Threshold = 10, ThresholdType = ThresholdType.Seconds, FileExtensionToWatch = "*" }; _staleFileMonitorer.Process(monitorJobToUse); var message = "There is a file 'badFile.txt' of type '*' older than the threshold '10' Seconds in the directory '..\\monitorey.Integration.Tests\\TestBadFileFolder'"; _emailActions.AssertWasCalled(x => x.SendAlert(message)); }
public void CallsEmailActionsWhenThereIsAFileInTheFolderSmallerThanTheThresholdSize() { var monitorJobToUse = new MonitorJob { MontiredJobType = MontiredJobType.FileSmallerThanThreshold, Path = @"..\monitorey.Integration.Tests\TestBadFileFolder", MinFileSizeInBytes = 999, FileExtensionToWatch = "*" }; _fileSmallerThanThresholdMonitorer.Process(monitorJobToUse); var message = "There is a file 'badFile.txt' of type '*' smaller than the min filesize'999' in the directory '..\\monitorey.Integration.Tests\\TestBadFileFolder'"; _emailActions.AssertWasCalled(x => x.SendAlert(message)); }
private async Task ScheduleJobAsync(MonitorJob 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); }
public void NextTimeThisJobShouldRunReturnsTheExpectedDate() { var lastTimeRan = new DateTime(2013, 4, 24, 14, 00, 00); var monitorJob = new MonitorJob { LastTimeThisJobRan = lastTimeRan, Threshold = 5, ThresholdType = ThresholdType.Minutes, }; var result = _monitorJobActions.NextTimeThisJobShouldRun(monitorJob); Assert.That(result, Is.EqualTo(lastTimeRan.AddMinutes(5))); }
public async Task UpdateMonitorJobAsync(MonitorJob deleteJob = null) { if (deleteJob != null) { var delete = await scheduler.GetJobDetail(new JobKey(SchedulerTag + deleteJob.Id, SchedulerGroup)); if (delete != null) { await DeleteJobAsync(delete); } } List <MonitorJob> list = await _storage.GetMonitorJobs(); 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 { MonitorJob monitorJob = job.JobDataMap.Get("job") as MonitorJob; // 判断是否有修改,如果修改后,重置Job if (System.Text.Json.JsonSerializer.Serialize(k, _jsonSetting) != System.Text.Json.JsonSerializer.Serialize(monitorJob, _jsonSetting)) { await DeleteJobAsync(job); await ScheduleJobAsync(k); } } } } }
public void StaleDirectoryMonitorerCallsEmailActionsWhenThereIsAFileOlderThanTheThreshold() { var monitorJobToUse = new MonitorJob { MontiredJobType = MontiredJobType.StaleDirectory, Path = @"..\monitorey.Integration.Tests\TestStaleDirectory", Threshold = -15,//NOTE: Negative on purpose so other tests that touch the directory don't make this one fail ThresholdType = ThresholdType.Minutes, }; _staleDirectoryMonitorer.Process(monitorJobToUse); _emailActions.AssertWasCalled(x => x.SendAlert("No new files have shown up in the directory '..\\monitorey.Integration.Tests\\TestStaleDirectory' in the last '-15' 'Minutes'")); }
public void ReturnsFalseWhenItIsNotTimeToRun() { _timeActions.Stub(x => x.Now()).Return(new DateTime(2013, 4, 24)); var monitorJob = new MonitorJob { LastTimeThisJobRan = new DateTime(2013, 4, 24), Threshold = 5, ThresholdType = ThresholdType.Seconds, }; var result = _monitorJobActions.ThisJobShouldRunNow(monitorJob); Assert.That(result, Is.False); }
public void DoesNotCallEmailActionsWhenThereAreNoFilesInTheFolder() { _emailActions.Stub(x => x.SendAlert("This String Is Not Important ==> see ignore arguments...")).IgnoreArguments().Throw(new Exception()); Directory.CreateDirectory("..\\monitorey.Integration.Tests\\DirectoryThatWillBeGoneInASecond"); var monitorJobToUse = new MonitorJob { MontiredJobType = MontiredJobType.StaleFileMonitor, Path = @"..\monitorey.Integration.Tests\DirectoryThatWillBeGoneInASecond", Threshold = 10, ThresholdType = ThresholdType.Seconds, FileExtensionToWatch = "*" }; _staleFileMonitorer.Process(monitorJobToUse); var messageThatWeShouldNeverGet = "There is a file 'badFile.txt' of type '*' older than the threshold '10' Seconds in the directory '..\\monitorey.Integration.Tests\\DirectoryThatWillBeGoneInASecond'"; _emailActions.AssertWasNotCalled(x => x.SendAlert(messageThatWeShouldNeverGet)); Directory.Delete("..\\monitory.Integration.Tests\\DirectoryThatWillBeGoneInASecond"); }
private async Task AlarmAsync(IList <AlarmOption> alarmOption, MonitorJob job) { foreach (var item in alarmOption) { if (item != null) { item.Emails = job.Emails?.Split(',').AsEnumerable(); item.Phones = job.Mobiles?.Split(',').AsEnumerable(); item.WebHook = job.WebHook; await _alarmService.AlarmAsync(item); } } }
public MonitorJobSet GetAllCurrentMonitorJobsForThisServer(string machineName) { Log.DebugFormat("Entering GetAllCurrentMonitorJobsForThisServer for machineName '{0}'", machineName); var monitorJobSet = new MonitorJobSet(); var individualJobs = new List <MonitorJob>(); using (var connection = new SqlConnection(_applicationSettings.ConnectionString)) { connection.Open(); using (var command = new SqlCommand("usp_FolderMonitor_GetJobsByServerName", connection) { CommandType = CommandType.StoredProcedure }) { try { command.Parameters.Add("@MachineName", SqlDbType.VarChar).Value = machineName; using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { var monitorJob = new MonitorJob { ID = (int)reader["id"], MontiredJobType = (MontiredJobType)reader.GetInt32(Convert.ToInt32("MonitorJobType")), Path = reader["PathToMonitor"].ToString(), Threshold = reader["Threshold"] != null ? (int)reader["Threshold"] : 0, ThresholdType = reader["ThresholdType"] != null ? (ThresholdType)((int)reader["ThresholdType"]) : ThresholdType.Minutes, FileExtensionToWatch = reader["FileExtensionToWatch"] == null ? "*" : reader["FileExtensionToWatch"].ToString(), MinFileSizeInBytes = reader["MinFileSizeInBytes"] != null ? (int)reader["MinFileSizeInBytes"] : int.MinValue, }; individualJobs.Add(monitorJob); } } } catch (Exception ex) { Log.ErrorFormat("GetQueueItemsToProcess threw the exception '{0}' for machineName: '{1}'", ex, machineName); throw; } } } monitorJobSet.MonitorJobs = individualJobs; return(monitorJobSet); }
public async Task <bool> UpdateMonitorJob(MonitorJob job) { string sql = $@"Update {TablePrefix}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)); }
public bool ThisJobShouldRunNow(MonitorJob monitorJob) { if (!monitorJob.LastTimeThisJobRan.HasValue) { Log.DebugFormat("ThisJobShouldRunNow is returning 'true' for monitorJobId '{0}'", monitorJob.ID); return(true); } if (NextTimeThisJobShouldRun(monitorJob) < _timeActions.Now()) { Log.DebugFormat("ThisJobShouldRunNow is returning 'true' for monitorJobId '{0}'", monitorJob.ID); return(true); } Log.DebugFormat("ThisJobShouldRunNow is returning 'false' for monitorJobId '{0}'", monitorJob.ID); return(false); }
public async Task <bool> AddMonitorJob(MonitorJob job) { job.Id = MD5_16(Guid.NewGuid().ToString()); string sql = $@"Insert Into {TablePrefix}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)); }
public async Task <string> AddOrUpdateMonitorJob(MonitorJob job) { bool result = false; if (job.Id.IsEmpty()) { result = await _storage.AddMonitorJob(job); } else { result = await _storage.UpdateMonitorJob(job); } await _scheduleService.UpdateMonitorJobAsync(); return(Json(true, result)); }
public async Task Execute(IJobExecutionContext context) { _storage = _storage ?? ServiceContainer.provider.GetService(typeof(IHttpReportsStorage)) as IHttpReportsStorage; _alarmService = _alarmService ?? ServiceContainer.provider.GetService(typeof(IAlarmService)) as IAlarmService; _logger = _logger ?? ServiceContainer.provider.GetService(typeof(ILogger <MonitorBackendJob>)) as ILogger <MonitorBackendJob>; _lang = _lang ?? (ServiceContainer.provider.GetService(typeof(ILocalizeService)) as ILocalizeService).Current; MonitorJob job = context.JobDetail.JobDataMap.Get("job") as MonitorJob; MonitorJobPayload payload = JsonConvert.DeserializeObject <MonitorJobPayload>(job.Payload); var response = GetCheckResponse(new List <Func <MonitorJob, MonitorJobPayload, Task <AlarmOption> > > { ResponseTimeTask, ResponseErrorTask, RequestCountTask }, job, payload); await AlarmAsync(response.Select(x => x.Result).ToList(), job); }
public void Process(MonitorJob monitorJob) { Log.DebugFormat("Stepped into process for monitorjob '{0}'", monitorJob.ID); if (!Directory.Exists(monitorJob.Path)) { Log.ErrorFormat("We could not find the directory '{0}'", monitorJob.Path); throw new DirectoryNotFoundException(monitorJob.Path); } var lastWriteTime = Directory.GetLastWriteTime(monitorJob.Path); if (lastWriteTime < _timeActions.Now().Subtract(GetTimeSpanToUse(monitorJob.Threshold, monitorJob.ThresholdType))) { var emailMessage = string.Format("No new files have shown up in the directory '{0}' in the last '{1}' '{2}'", monitorJob.Path, monitorJob.Threshold, monitorJob.ThresholdType); Log.InfoFormat(emailMessage); _emailActions.SendAlert(emailMessage); } }
public async Task UpdateMonitorJobAsync() { List <MonitorJob> list = await _storage.GetMonitorJobs(); 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 { MonitorJob monitorJob = job.JobDataMap.Get("job") as MonitorJob; // 判断是否有修改,如果修改后,重置Job if (JsonConvert.SerializeObject(k) != JsonConvert.SerializeObject(monitorJob)) { await DeleteJobAsync(job); await ScheduleJobAsync(k); } } } } }
public void Process(MonitorJob monitorJob) { Log.DebugFormat("Stepped into process for monitorjob '{0}'", monitorJob.ID); if (!Directory.Exists(monitorJob.Path)) { Log.ErrorFormat("We could not find the BadFile directory '{0}'", monitorJob.Path); throw new DirectoryNotFoundException(monitorJob.Path); } string[] filesInBadFileDirectory = Directory.GetFiles(monitorJob.Path); if (filesInBadFileDirectory.Length > 0) { var message = string.Format("There are 'Bad Files' in the directory {0}", monitorJob.Path); Log.InfoFormat(message); _emailActions.SendAlert(message); return; } Log.DebugFormat("BadFilesFolderMonitorer did not find any files to gripe about in '{0}'", monitorJob.Path); }
public IMonitorer GetMonitorer(MonitorJob monitorJob) { if (monitorJob.MontiredJobType == MontiredJobType.BadFileDirectory) { return(new BadFilesFolderMonitorer(_emailActions)); } if (monitorJob.MontiredJobType == MontiredJobType.FileSmallerThanThreshold) { return(new FileSmallerThanThresholdMonitorer(_emailActions)); } if (monitorJob.MontiredJobType == MontiredJobType.StaleDirectory) { return(new StaleDirectoryMonitorer(_emailActions, _timeActions)); } if (monitorJob.MontiredJobType == MontiredJobType.StaleFileMonitor) { return(new StaleFileMonitorer(_emailActions, _timeActions)); } throw new UnknownMonitorJobTypeException(); }
public async Task <string> EditMonitor(MonitorJobRequest request) { string vaild = _monitorService.VaildMonitorJob(request); if (!vaild.IsEmpty()) { return(Json(new HttpResultEntity(-1, vaild, null))); } MonitorJob model = _monitorService.GetMonitorJob(request); if (request.Id.IsEmpty() || request.Id == "0") { await _storage.AddMonitorJob(model); } else { await _storage.UpdateMonitorJob(model); } await _scheduleService.UpdateMonitorJobAsync(); return(Json(new HttpResultEntity(1, "ok", null))); }