Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        public void Initialize(dynamic jsonConfig)
        {
            //email alerts
            {
                //add or update email alerts
                foreach (dynamic jsonEmailAlert in jsonConfig.emailAlerts)
                {
                    string name;

                    if (jsonEmailAlert.name is null)
                    {
                        name = "default";
                    }
                    else
                    {
                        name = jsonEmailAlert.name.Value;
                    }

                    if (_emailAlerts.TryGetValue(name, out EmailAlert existingEmailAlert))
                    {
                        //update
                        existingEmailAlert.Reload(jsonEmailAlert);
                    }
                    else
                    {
                        //add
                        EmailAlert emailAlert = new EmailAlert(this, jsonEmailAlert);

                        _emailAlerts.TryAdd(emailAlert.Name, emailAlert);
                    }
                }

                //remove email alerts that dont exists in config
                foreach (KeyValuePair <string, EmailAlert> emailAlert in _emailAlerts)
                {
                    bool emailAlertExists = false;

                    foreach (dynamic jsonEmailAlert in jsonConfig.emailAlerts)
                    {
                        string name;

                        if (jsonEmailAlert.name is null)
                        {
                            name = "default";
                        }
                        else
                        {
                            name = jsonEmailAlert.name.Value;
                        }

                        if (name == emailAlert.Key)
                        {
                            emailAlertExists = true;
                            break;
                        }
                    }

                    if (!emailAlertExists)
                    {
                        if (_emailAlerts.TryRemove(emailAlert.Key, out EmailAlert removedEmailAlert))
                        {
                            removedEmailAlert.Dispose();
                        }
                    }
                }
            }

            //web hooks
            {
                //add or update email alerts
                foreach (dynamic jsonWebHook in jsonConfig.webHooks)
                {
                    string name;

                    if (jsonWebHook.name is null)
                    {
                        name = "default";
                    }
                    else
                    {
                        name = jsonWebHook.name.Value;
                    }

                    if (_webHooks.TryGetValue(name, out WebHook existingWebHook))
                    {
                        //update
                        existingWebHook.Reload(jsonWebHook);
                    }
                    else
                    {
                        //add
                        WebHook webHook = new WebHook(this, jsonWebHook);

                        _webHooks.TryAdd(webHook.Name, webHook);
                    }
                }

                //remove email alerts that dont exists in config
                foreach (KeyValuePair <string, WebHook> webHook in _webHooks)
                {
                    bool webHookExists = false;

                    foreach (dynamic jsonWebHook in jsonConfig.webHooks)
                    {
                        string name;

                        if (jsonWebHook.name is null)
                        {
                            name = "default";
                        }
                        else
                        {
                            name = jsonWebHook.name.Value;
                        }

                        if (name == webHook.Key)
                        {
                            webHookExists = true;
                            break;
                        }
                    }

                    if (!webHookExists)
                    {
                        if (_webHooks.TryRemove(webHook.Key, out WebHook removedWebHook))
                        {
                            removedWebHook.Dispose();
                        }
                    }
                }
            }

            //health checks
            {
                //add or update health checks
                foreach (dynamic jsonHealthCheck in jsonConfig.healthChecks)
                {
                    string name;

                    if (jsonHealthCheck.name is null)
                    {
                        name = "default";
                    }
                    else
                    {
                        name = jsonHealthCheck.name.Value;
                    }

                    if (_healthChecks.TryGetValue(name, out HealthCheck existingHealthCheck))
                    {
                        //update
                        existingHealthCheck.Reload(jsonHealthCheck);
                    }
                    else
                    {
                        //add
                        HealthCheck healthCheck = new HealthCheck(this, jsonHealthCheck);

                        _healthChecks.TryAdd(healthCheck.Name, healthCheck);
                    }
                }

                //remove health checks that dont exists in config
                foreach (KeyValuePair <string, HealthCheck> healthCheck in _healthChecks)
                {
                    bool healthCheckExists = false;

                    foreach (dynamic jsonHealthCheck in jsonConfig.healthChecks)
                    {
                        string name;

                        if (jsonHealthCheck.name is null)
                        {
                            name = "default";
                        }
                        else
                        {
                            name = jsonHealthCheck.name.Value;
                        }

                        if (name == healthCheck.Key)
                        {
                            healthCheckExists = true;
                            break;
                        }
                    }

                    if (!healthCheckExists)
                    {
                        if (_healthChecks.TryRemove(healthCheck.Key, out HealthCheck removedHealthCheck))
                        {
                            //remove health monitors using this health check
                            foreach (KeyValuePair <IPAddress, AddressMonitoring> monitoring in _addressMonitoring)
                            {
                                monitoring.Value.RemoveHealthMonitor(healthCheck.Key);
                            }

                            foreach (KeyValuePair <string, DomainMonitoring> monitoring in _domainMonitoringA)
                            {
                                monitoring.Value.RemoveHealthMonitor(healthCheck.Key);
                            }

                            foreach (KeyValuePair <string, DomainMonitoring> monitoring in _domainMonitoringAAAA)
                            {
                                monitoring.Value.RemoveHealthMonitor(healthCheck.Key);
                            }

                            removedHealthCheck.Dispose();
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        public void Initialize(dynamic jsonConfig)
        {
            //email alerts
            {
                //add or update email alerts
                foreach (dynamic jsonEmailAlert in jsonConfig.emailAlerts)
                {
                    string name;

                    if (jsonEmailAlert.name is null)
                    {
                        name = "default";
                    }
                    else
                    {
                        name = jsonEmailAlert.name.Value;
                    }

                    if (_emailAlerts.TryGetValue(name, out EmailAlert existingEmailAlert))
                    {
                        //update
                        existingEmailAlert.Reload(jsonEmailAlert);
                    }
                    else
                    {
                        //add
                        EmailAlert emailAlert = new EmailAlert(this, jsonEmailAlert);

                        _emailAlerts.TryAdd(emailAlert.Name, emailAlert);
                    }
                }

                //remove email alerts that dont exists in config
                foreach (KeyValuePair <string, EmailAlert> emailAlert in _emailAlerts)
                {
                    bool emailAlertExists = false;

                    foreach (dynamic jsonEmailAlert in jsonConfig.emailAlerts)
                    {
                        string name;

                        if (jsonEmailAlert.name is null)
                        {
                            name = "default";
                        }
                        else
                        {
                            name = jsonEmailAlert.name.Value;
                        }

                        if (name == emailAlert.Key)
                        {
                            emailAlertExists = true;
                            break;
                        }
                    }

                    if (!emailAlertExists)
                    {
                        if (_emailAlerts.TryRemove(emailAlert.Key, out EmailAlert removedEmailAlert))
                        {
                            removedEmailAlert.Dispose();
                        }
                    }
                }
            }

            //web hooks
            {
                //add or update email alerts
                foreach (dynamic jsonWebHook in jsonConfig.webHooks)
                {
                    string name;

                    if (jsonWebHook.name is null)
                    {
                        name = "default";
                    }
                    else
                    {
                        name = jsonWebHook.name.Value;
                    }

                    if (_webHooks.TryGetValue(name, out WebHook existingWebHook))
                    {
                        //update
                        existingWebHook.Reload(jsonWebHook);
                    }
                    else
                    {
                        //add
                        WebHook webHook = new WebHook(this, jsonWebHook);

                        _webHooks.TryAdd(webHook.Name, webHook);
                    }
                }

                //remove email alerts that dont exists in config
                foreach (KeyValuePair <string, WebHook> webHook in _webHooks)
                {
                    bool webHookExists = false;

                    foreach (dynamic jsonWebHook in jsonConfig.webHooks)
                    {
                        string name;

                        if (jsonWebHook.name is null)
                        {
                            name = "default";
                        }
                        else
                        {
                            name = jsonWebHook.name.Value;
                        }

                        if (name == webHook.Key)
                        {
                            webHookExists = true;
                            break;
                        }
                    }

                    if (!webHookExists)
                    {
                        if (_webHooks.TryRemove(webHook.Key, out WebHook removedWebHook))
                        {
                            removedWebHook.Dispose();
                        }
                    }
                }
            }

            //health checks
            {
                //add or update health checks
                foreach (dynamic jsonHealthCheck in jsonConfig.healthChecks)
                {
                    string name;

                    if (jsonHealthCheck.name is null)
                    {
                        name = "default";
                    }
                    else
                    {
                        name = jsonHealthCheck.name.Value;
                    }

                    if (_healthChecks.TryGetValue(name, out HealthCheck existingHealthCheck))
                    {
                        //update
                        existingHealthCheck.Reload(jsonHealthCheck);
                    }
                    else
                    {
                        //add
                        HealthCheck healthCheck = new HealthCheck(this, jsonHealthCheck);

                        _healthChecks.TryAdd(healthCheck.Name, healthCheck);
                    }
                }

                //remove health checks that dont exists in config
                foreach (KeyValuePair <string, HealthCheck> healthCheck in _healthChecks)
                {
                    bool healthCheckExists = false;

                    foreach (dynamic jsonHealthCheck in jsonConfig.healthChecks)
                    {
                        string name;

                        if (jsonHealthCheck.name is null)
                        {
                            name = "default";
                        }
                        else
                        {
                            name = jsonHealthCheck.name.Value;
                        }

                        if (name == healthCheck.Key)
                        {
                            healthCheckExists = true;
                            break;
                        }
                    }

                    if (!healthCheckExists)
                    {
                        if (_healthChecks.TryRemove(healthCheck.Key, out HealthCheck removedHealthCheck))
                        {
                            //remove health monitors using this health check
                            RemoveHealthMonitor(healthCheck.Key);

                            removedHealthCheck.Dispose();
                        }
                    }
                }
            }

            //under maintenance networks
            _underMaintenance.Clear();

            if (jsonConfig.underMaintenance is not null)
            {
                foreach (dynamic jsonNetwork in jsonConfig.underMaintenance)
                {
                    string network = jsonNetwork.network.Value;
                    bool   enable  = jsonNetwork.enable.Value;

                    _underMaintenance.TryAdd(NetworkAddress.Parse(network), enable);
                }
            }
        }