public async Task AddMonitorPulse(PushHealthCheckModel pushHealthCheckModel) { await _monitorCounterRepository.AddAsync(new Entities.MonitorCounter { Id = DataUtil.GenerateUniqueId(), BeatDate = pushHealthCheckModel.BeatDate, ServiceId = pushHealthCheckModel.ServiceId, ServiceName = pushHealthCheckModel.ServiceName, HttpCounter = new Entities.HttpCounter { AvgDuration = Math.Round(pushHealthCheckModel.HttpHealthCheck.AvgDuration, 0, MidpointRounding.AwayFromZero), FailedRequests = pushHealthCheckModel.HttpHealthCheck.FailedRequests, MeansureDate = pushHealthCheckModel.BeatDate, SuccessRequests = pushHealthCheckModel.HttpHealthCheck.SuccessRequests, TotalRequestsPerDay = pushHealthCheckModel.HttpHealthCheck.TotalRequestsPerDay, ServiceId = pushHealthCheckModel.ServiceId }, HardwareCounter = new Entities.HardwareCounter { CpuUsage = Math.Round(pushHealthCheckModel.HardwareInfoHealthCheck.CpuUsage, 0, MidpointRounding.AwayFromZero), MemoryUsed = pushHealthCheckModel.HardwareInfoHealthCheck.MemoryUsed, MemoryUsedInMb = (int)Math.Round((double)(pushHealthCheckModel.HardwareInfoHealthCheck.MemoryUsed / 1024), 0, MidpointRounding.AwayFromZero), IsCpuBottleneck = pushHealthCheckModel.HardwareInfoHealthCheck.IsCpuBottleneck, IsMemoryThreshold = pushHealthCheckModel.HardwareInfoHealthCheck.IsMemoryThreshold, MeansureDate = pushHealthCheckModel.BeatDate, ServiceId = pushHealthCheckModel.ServiceId } }); }
public void PushHealthCheck(PushHealthCheckModel pushHealthCheckModel) { pushHealthCheckModel.ServiceId = serviceId; pushHealthCheckModel.ServiceName = _serviceOptions.CurrentValue.Name; var monitorTask = _monitorProvider.AddMonitorPulse(pushHealthCheckModel); monitorTask.Wait(); }
public void PushHealthCheck(PushHealthCheckModel pushHealthCheckModel) { if (_monitorOptions.CurrentValue.NotifyOptions.Enable) { try { pushHealthCheckModel.ServiceId = serviceId; var task = _httpClient.PostAsJsonAsync( (!string.IsNullOrEmpty(_monitorOptions.CurrentValue.NotifyOptions.HealthcheckEndpoint) ? _monitorOptions.CurrentValue.NotifyOptions.HealthcheckEndpoint : (_serviceOptions.CurrentValue.ServiceManagementEndpoint + "/api/monitors")), pushHealthCheckModel); var httpResponseMessage = task.Result; httpResponseMessage.EnsureSuccessStatusCode(); } catch (Exception ex) { throw new Exception("There are some erros when trying to notify monitor behavior in Service Management, please check stack trace.", ex); } } }
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)); } }
public async Task <IActionResult> AddHeartBeat([FromBody] PushHealthCheckModel pushHealthCheckModel) { await _monitorProvider.AddMonitorPulse(pushHealthCheckModel); return(Ok()); }