public async Task RecordAsync(NcMonitor monitor, MonitorResult result) { var record = monitorManager.LastOpenedMonitorRecord(monitor.Id); if (result.IsSuccess) { if ((record != null && record.EndDate == null) || monitor.StatusEnum() != MonitorStatus.OK) {//ukonceni zaznamu if (record != null) { await monitorManager.UpdateEndDateAsync(record, DateTime.Now); } await notificator.AddNotifyUpAsync(monitor);//poslani notifikace o tom ze web bezi, staci poslat pouze jendou. await monitorManager.SetStatusAndResetLastTestCycleIntervalAsync(monitor, MonitorStatus.OK); } } else { //nastala chyba if (record == null || record.EndDate != null || monitor.StatusEnum() != MonitorStatus.Error) { //neexistuje zadny zaznam nebo jsou vsechny uzavreny await monitorManager.SetStatusAndResetLastTestCycleIntervalAsync(monitor, MonitorStatus.Error); await AddNewRecordAsync(monitor, result); } //posilani notifikaci se provadi stale dokud je monitor v chybovem stavu await notificator.AddNotifyDownAsync(monitor); } }
private async Task AddNewRecordAsync(NcMonitor monitor, MonitorResult result) { await monitorManager.AddNewRecordAsync(new NcMonitorRecord { StartDate = DateTime.Now, Monitor = monitor, Note = GetLogMessage(result), }); }
private MonitorResult CheckStatusCode(string url, string statusCodeNum, int timeout) { try { if (int.TryParse(statusCodeNum, out int statusInt)) { HttpStatusCode statusCode = GetStatusCode(url, timeout); if ((int)statusCode == statusInt) { return(MonitorResult.Success); } else { return(MonitorResult.Failed($"Url '{url}' response with status code '{(int)statusCode} - {statusCode}', but required '{statusCodeNum}'.")); } } return(MonitorResult.Failed($"Status code '{statusCodeNum}' is not valid.")); } catch (Exception ex) { if (ex is WebException) { switch ((ex as WebException).Status) { case WebExceptionStatus.ConnectFailure: return(MonitorResult.Failed("Connection failed.", ex)); case WebExceptionStatus.NameResolutionFailure: return(MonitorResult.Failed("Name reosulution failed.", ex)); case WebExceptionStatus.Timeout: return(MonitorResult.Failed("Timeout expiration.", ex)); case WebExceptionStatus.ProtocolError: if (ex.Message == "The remote server returned an error: (401) unauthorized.") { return(MonitorResult.Failed("Anauthorized content (401).", ex)); } return(MonitorResult.Failed("Protocol error.", ex)); default: return(MonitorResult.Failed("General WebException error.", ex)); } } return(MonitorResult.Failed(ex)); } }
private MonitorResult CheckMonitor(NcMonitor monitor) { if (monitor.Enabled) { int timeout = (int)monitor.Timeout.TotalMilliseconds; switch (monitor.VerificationEnum()) { case MonitorVerification.Keyword: return(FindKeywordOnWebsite(monitor.Url, monitor.VerificationValue, timeout)); case MonitorVerification.StatusCode: return(CheckStatusCode(monitor.Url, monitor.VerificationValue, timeout)); } return(MonitorResult.Failed($"{nameof(monitor.VerificationType)} '{monitor.VerificationTypeId} - {monitor.VerificationType}' not found.")); } return(MonitorResult.Success); }
private MonitorResult FindKeywordOnWebsite(string url, string keyword, int timeout) { try { if (string.IsNullOrEmpty(keyword)) { throw new MonitoringException($"{nameof(keyword)} cant be empty."); } if (DownloadContent(url, timeout).Contains(keyword)) { return(MonitorResult.Success); } return(MonitorResult.Failed($"Keyword '{keyword}' not found on url '{url}'.")); } catch (Exception ex) { return(MonitorResult.Failed($"Error on checking keyword '{keyword}' on url '{url}'.", ex)); } }
private string GetLogMessage(MonitorResult result) { if (result.Exception != null) {//nastala neocekavana chyba logger.LogError(result.Exception, result.Message); } if (string.IsNullOrWhiteSpace(result.Message)) { if (result.Exception != null) { return("Unexpected error of exception " + result.Exception.GetType()); } else { return("General error"); } } else { return(result.Message); } }