Esempio n. 1
0
        public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            var pushHealthCheckModel = new PushHealthCheckModel();

            if (_monitorOptions.CurrentValue.CheckDatabaseOption &&
                _databaseOptions.CurrentValue != null)
            {
                switch (_databaseOptions.CurrentValue.ConnectionType)
                {
                case ConnectionType.MongoDB:
                    try
                    {
                        var mongoClient   = new MongoClient(_databaseOptions.CurrentValue.ConnectionString);
                        var mongoDatabase = mongoClient.GetDatabase(_databaseOptions.CurrentValue.Datasource);
                        var ping          = await mongoDatabase.RunCommandAsync <BsonDocument>(new BsonDocument { { "ping", 1 } });

                        if (ping.Contains("ok") &&
                            (ping["ok"].IsDouble && (int)ping["ok"].AsDouble == 1 ||
                             ping["ok"].IsInt32 && ping["ok"].AsInt32 == 1))
                        {
                            pushHealthCheckModel.DatabaseHealthy = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        pushHealthCheckModel.DatabaseHealthy = false;
                        pushHealthCheckModel.ErrorStack      = ex.ToString();
                    }
                    break;

                case ConnectionType.SQLServer:
                    try
                    {
                        using (var sqlDbConnection = new SqlConnection(_databaseOptions.CurrentValue.ConnectionString))
                        {
                            sqlDbConnection.Open();
                            using (var sqlCommand = new SqlCommand("Select 1"))
                            {
                                sqlCommand.ExecuteScalar();
                                pushHealthCheckModel.DatabaseHealthy = true;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        pushHealthCheckModel.DatabaseHealthy = false;
                        pushHealthCheckModel.ErrorStack      = ex.ToString();
                    }
                    break;

                case ConnectionType.MySQL:
                    try
                    {
                        using (var mysqlDbConnection = new MySqlConnection(_databaseOptions.CurrentValue.ConnectionString))
                        {
                            mysqlDbConnection.Open();
                            using (var mysqlCommand = new MySqlCommand("Select 1"))
                            {
                                mysqlCommand.ExecuteScalar();
                                pushHealthCheckModel.DatabaseHealthy = true;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        pushHealthCheckModel.DatabaseHealthy = false;
                        pushHealthCheckModel.ErrorStack      = ex.ToString();
                    }
                    break;

                case ConnectionType.PostgreSQL:
                    try
                    {
                        using (var postgreDbConnection = new NpgsqlConnection(_databaseOptions.CurrentValue.ConnectionString))
                        {
                            postgreDbConnection.Open();
                            using (var postgreCommand = new NpgsqlCommand("Select 1"))
                            {
                                postgreCommand.ExecuteScalar();
                                pushHealthCheckModel.DatabaseHealthy = true;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        pushHealthCheckModel.DatabaseHealthy = false;
                        pushHealthCheckModel.ErrorStack      = ex.ToString();
                    }
                    break;

                default:
                    pushHealthCheckModel.DatabaseHealthy = false;
                    pushHealthCheckModel.ErrorStack      = string.Format("A database type isn't supported in Healthcheck. Databse type: {0}", _databaseOptions.CurrentValue.ConnectionType.ToString());
                    break;
                }
            }
            else
            {
                pushHealthCheckModel.DatabaseHealthy = true;
            }

            _monitorHealthCheck.CalculateAvg();
            var hardwareCheck = _monitorHealthCheck.GetCurrentHardwareInfoHealthCheck();

            pushHealthCheckModel.HardwareInfoHealthCheck.CpuUsage   = hardwareCheck.CpuUsage;
            pushHealthCheckModel.HardwareInfoHealthCheck.MemoryUsed = hardwareCheck.MemoryUsed;

            if (_monitorOptions.CurrentValue.CheckCpuBottleneck)
            {
                pushHealthCheckModel.HardwareInfoHealthCheck.IsCpuBottleneck = pushHealthCheckModel.HardwareInfoHealthCheck.CpuUsage > _monitorOptions.CurrentValue.BottleneckCpu;
            }
            else
            {
                pushHealthCheckModel.HardwareInfoHealthCheck.IsCpuBottleneck = false;
            }

            if (_monitorOptions.CurrentValue.CheckMemoryThreshold)
            {
                pushHealthCheckModel.HardwareInfoHealthCheck.IsMemoryThreshold = pushHealthCheckModel.HardwareInfoHealthCheck.MemoryUsed > _monitorOptions.CurrentValue.ThresholdMemory;
            }
            else
            {
                pushHealthCheckModel.HardwareInfoHealthCheck.IsMemoryThreshold = false;
            }

            // Collect more info from MonitorHealthcheck
            pushHealthCheckModel.HttpHealthCheck = _monitorHealthCheck.GetCurrentHttpHealthCheck();

            pushHealthCheckModel.ServiceName = _serviceOptions.CurrentValue.Name;
            pushHealthCheckModel.Healthy     = !pushHealthCheckModel.HardwareInfoHealthCheck.IsCpuBottleneck &&
                                               !pushHealthCheckModel.HardwareInfoHealthCheck.IsMemoryThreshold &&
                                               pushHealthCheckModel.DatabaseHealthy;

            pushHealthCheckModel.BeatDate = DateTime.UtcNow;

            var data = new Dictionary <string, object>()
            {
                { Constants.LetPortalHealthCheckData, pushHealthCheckModel }
            };

            if (pushHealthCheckModel.Healthy)
            {
                return(new HealthCheckResult(HealthStatus.Healthy,
                                             description:
                                             string.Format("Healthy with checking statuses: CPU - {0}; Memory - {1}; Database - {2}",
                                                           GetHealthyWord(pushHealthCheckModel.HardwareInfoHealthCheck.IsCpuBottleneck),
                                                           GetHealthyWord(pushHealthCheckModel.HardwareInfoHealthCheck.IsMemoryThreshold),
                                                           GetHealthyWord(pushHealthCheckModel.DatabaseHealthy)), data: data));
            }
            else
            {
                return(new HealthCheckResult(HealthStatus.Unhealthy,
                                             description:
                                             string.Format("Unhealthy with checking statuses: CPU - {0}; Memory - {1}; Database - {2}",
                                                           GetHealthyWord(pushHealthCheckModel.HardwareInfoHealthCheck.IsCpuBottleneck),
                                                           GetHealthyWord(pushHealthCheckModel.HardwareInfoHealthCheck.IsMemoryThreshold),
                                                           GetHealthyWord(pushHealthCheckModel.DatabaseHealthy)), data: data));
            }
        }