private void SaveStatDataToDb(string containerId) { string response = _agent.GetResponse(UriResourceName.GetContainerStatsResourceName(containerId)); DockerStatDataModel model = JsonConvert.DeserializeObject <DockerStatDataModel>(response); Task.Run(() => _dbConnector.AddDataPoint(model)).Wait(); }
// This method was taken from here - // https://github.com/moby/moby/blob/eb131c5383db8cac633919f82abad86c99bffbe5/cli/command/container/stats_helpers.go#L175 // Useful Link - https://github.com/moby/moby/issues/29306 public static double CalculateCPUPercentWindows(DockerStatDataModel model) { long possIntervals = (model.Read - model.Preread).Milliseconds * 1000000; possIntervals /= 100; possIntervals *= model.NumProcs; long intervalsUsed = model.CpuStats.CpuUsage.TotalUsage - model.PrecpuStats.CpuUsage.TotalUsage; if (possIntervals > 0) { return((float)(intervalsUsed) / (float)(possIntervals)); } return(0.00); }
public async Task AddDataPoint(DockerStatDataModel model) { var valMixed = new InfluxDatapoint <InfluxValueField>(); valMixed.UtcTimestamp = model.Read.UtcDateTime; valMixed.Tags.Add("ContainerId", model.Id); valMixed.Tags.Add("ContainerName", model.Name); valMixed.Fields.Add("CpuPercent", new InfluxValueField(CpuCycleConverter.CalculateCPUPercentWindows(model))); valMixed.Fields.Add("MemoryUsage", new InfluxValueField(BinaryConverter.ConvertBytesToMegabytes(model.MemoryStats.Privateworkingset))); valMixed.Fields.Add("DiskInput", new InfluxValueField(BinaryConverter.ConvertBytesToMegabytes(model.StorageStats.WriteSizeBytes))); valMixed.Fields.Add("DiskOutput", new InfluxValueField(BinaryConverter.ConvertBytesToMegabytes(model.StorageStats.ReadSizeBytes))); valMixed.MeasurementName = _settings.MeasurementName; valMixed.Precision = TimePrecision.Seconds; var r = await _dbClient.PostPointAsync(_settings.DbName, valMixed); }