public HealthMonitor(IDnsServer dnsServer, IPAddress address, HealthCheck healthCheck, Uri healthCheckUrl) { _dnsServer = dnsServer; _address = address; _healthCheck = healthCheck; _healthCheckTimer = new Timer(async delegate(object state) { try { if (_healthCheck is null) { _lastHealthCheckResponse = null; } else { HealthCheckResponse healthCheckResponse = await _healthCheck.IsHealthyAsync(_address, healthCheckUrl); bool statusChanged = false; bool maintenance = false; if (_lastHealthCheckResponse is null) { switch (healthCheckResponse.Status) { case HealthStatus.Failed: statusChanged = true; break; case HealthStatus.Maintenance: statusChanged = true; maintenance = true; break; } } else { if (_lastHealthCheckResponse.Status != healthCheckResponse.Status) { statusChanged = true; if ((_lastHealthCheckResponse.Status == HealthStatus.Maintenance) || (healthCheckResponse.Status == HealthStatus.Maintenance)) { maintenance = true; } } } if (statusChanged) { switch (healthCheckResponse.Status) { case HealthStatus.Failed: _dnsServer.WriteLog("ALERT! Address [" + _address.ToString() + "] status is FAILED based on '" + _healthCheck.Name + "' health check. The failure reason is: " + healthCheckResponse.FailureReason); break; default: _dnsServer.WriteLog("ALERT! Address [" + _address.ToString() + "] status is " + healthCheckResponse.Status.ToString().ToUpper() + " based on '" + _healthCheck.Name + "' health check."); break; } if (healthCheckResponse.Exception is not null) { _dnsServer.WriteLog(healthCheckResponse.Exception); } if (!maintenance) { //avoid sending email alerts when switching from or to maintenance EmailAlert emailAlert = _healthCheck.EmailAlert; if (emailAlert is not null) { _ = emailAlert.SendAlertAsync(_address, _healthCheck.Name, healthCheckResponse); } } WebHook webHook = _healthCheck.WebHook; if (webHook is not null) { _ = webHook.CallAsync(_address, _healthCheck.Name, healthCheckResponse); } } _lastHealthCheckResponse = healthCheckResponse; } } catch (Exception ex) { _dnsServer.WriteLog(ex); if (_lastHealthCheckResponse is null) { EmailAlert emailAlert = _healthCheck.EmailAlert; if (emailAlert is not null) { _ = emailAlert.SendAlertAsync(_address, _healthCheck.Name, ex); } WebHook webHook = _healthCheck.WebHook; if (webHook is not null) { _ = webHook.CallAsync(_address, _healthCheck.Name, ex); } _lastHealthCheckResponse = new HealthCheckResponse(HealthStatus.Failed, ex.ToString(), ex); } else { _lastHealthCheckResponse = null; } } finally { if (!_disposed && (_healthCheck is not null)) { _healthCheckTimer.Change(_healthCheck.Interval, Timeout.Infinite); } } }, null, Timeout.Infinite, Timeout.Infinite); _healthCheckTimer.Change(HEALTH_CHECK_TIMER_INITIAL_INTERVAL, Timeout.Infinite); }
public HealthMonitor(IDnsServer dnsServer, IPAddress address, HealthCheck healthCheck) { _dnsServer = dnsServer; _address = address; _healthCheck = healthCheck; _healthCheckTimer = new Timer(async delegate(object state) { try { if (_healthCheck is null) { _healthCheckStatus = null; } else { HealthCheckStatus healthCheckStatus = await _healthCheck.IsHealthyAsync(_address); bool sendAlert = false; if (_healthCheckStatus is null) { if (!healthCheckStatus.IsHealthy) { sendAlert = true; } } else { if (_healthCheckStatus.IsHealthy != healthCheckStatus.IsHealthy) { sendAlert = true; } else if (_healthCheckStatus.FailureReason != healthCheckStatus.FailureReason) { sendAlert = true; } } if (sendAlert) { EmailAlert emailAlert = _healthCheck.EmailAlert; if (emailAlert is not null) { _ = emailAlert.SendAlertAsync(_address, _healthCheck.Name, healthCheckStatus); } WebHook webHook = _healthCheck.WebHook; if (webHook is not null) { _ = webHook.CallAsync(_address, _healthCheck.Name, healthCheckStatus); } } _healthCheckStatus = healthCheckStatus; } } catch (Exception ex) { _dnsServer.WriteLog(ex); if (_healthCheckStatus is null) { EmailAlert emailAlert = _healthCheck.EmailAlert; if (emailAlert is not null) { _ = emailAlert.SendAlertAsync(_address, _healthCheck.Name, ex); } WebHook webHook = _healthCheck.WebHook; if (webHook is not null) { _ = webHook.CallAsync(_address, _healthCheck.Name, ex); } _healthCheckStatus = new HealthCheckStatus(false, ex.ToString()); } else { _healthCheckStatus = null; } } finally { if (!_disposed && (_healthCheck is not null)) { _healthCheckTimer.Change(_healthCheck.Interval, Timeout.Infinite); } } }, null, Timeout.Infinite, Timeout.Infinite); _healthCheckTimer.Change(0, Timeout.Infinite); }