/// <summary> /// /// </summary> /// <param name="responseMetric"></param> /// <param name="serverMetricType"></param> /// <returns></returns> private static ServerMetric GetServerMetricFromResponse(string responseMetric, string serverMetricType) { dynamic data = JObject.Parse(responseMetric); ServerMetric serverMetric = new ServerMetric(); serverMetric.Start = data.metrics.start; serverMetric.End = data.metrics.end; serverMetric.Step = data.metrics.step; serverMetric.Type = serverMetricType; serverMetric.TimeSeries = new ServerMetricTimeSeries(); switch (serverMetricType) { case ServerMetricType.CPU: { // process the cpu-values serverMetric.TimeSeries.CpuValues = new List <ServerMetricValue>(); foreach (var cpuValue in data.metrics.time_series.cpu.values) { long timestamp = Convert.ToInt64(cpuValue[0]); double value = Math.Round(Convert.ToDouble(Convert.ToString(cpuValue[1]).Replace('.', ',')), 5); serverMetric.TimeSeries.CpuValues.Add(GetServerMetricValue(timestamp, value)); } } break; case ServerMetricType.DISK: { // process the disk-values serverMetric.TimeSeries.DiskValues = GetDiskMetricFromResponseData(data, serverMetric); } break; case ServerMetricType.NETWORK: { // process the network-values serverMetric.TimeSeries.NetworkValues = GetNetworkMetricFromResponseData(data, serverMetric); } break; default: { throw new InvalidArgumentException(string.Format("unknown value for argument '{0}' => '{1}'. only use the lkcode.hetznercloudapi.Api.ServerMetricType values", "ServerMetricType", serverMetricType)); } } return(serverMetric); }
/// <summary> /// /// </summary> /// <param name="data"></param> /// <param name="serverMetric"></param> /// <returns></returns> private static List <ServerMetricDiskValues> GetDiskMetricFromResponseData(dynamic data, ServerMetric serverMetric) { List <ServerMetricDiskValues> diskValues = new List <ServerMetricDiskValues>(); bool hasNext = true; int pos = 0; while (hasNext) { ServerMetricDiskValues diskMetricValues = new ServerMetricDiskValues(); string diskIopsReadKey = string.Format("disk.{0}.iops.read", pos); string diskIopsWriteKey = string.Format("disk.{0}.iops.write", pos); string diskBandwidthReadKey = string.Format("disk.{0}.bandwidth.read", pos); string diskBandwidthWriteKey = string.Format("disk.{0}.bandwidth.write", pos); var diskIopsReadValues = data.metrics.time_series[diskIopsReadKey]; var diskIopsWriteValues = data.metrics.time_series[diskIopsWriteKey]; var diskBandwidthReadValues = data.metrics.time_series[diskBandwidthReadKey]; var diskBandwidthWriteValues = data.metrics.time_series[diskBandwidthWriteKey]; if (diskIopsReadValues == null && diskIopsWriteValues == null && diskBandwidthReadValues == null && diskBandwidthWriteValues == null) { hasNext = false; continue; } else { if (diskIopsReadValues != null) { diskMetricValues.IOPSRead = new List <ServerMetricValue>(); foreach (var diskValue in diskIopsReadValues.values) { long timestamp = Convert.ToInt64(diskValue[0]); double value = GetConvertedServerMetricValue(diskValue[1]); diskMetricValues.IOPSRead.Add(GetServerMetricValue(timestamp, value)); } } if (diskIopsWriteValues != null) { diskMetricValues.IOPSWrite = new List <ServerMetricValue>(); foreach (var diskValue in diskIopsWriteValues.values) { long timestamp = Convert.ToInt64(diskValue[0]); double value = GetConvertedServerMetricValue(diskValue[1]); diskMetricValues.IOPSWrite.Add(GetServerMetricValue(timestamp, value)); } } if (diskBandwidthReadValues != null) { diskMetricValues.BandwithRead = new List <ServerMetricValue>(); foreach (var diskValue in diskBandwidthReadValues.values) { long timestamp = Convert.ToInt64(diskValue[0]); double value = GetConvertedServerMetricValue(diskValue[1]); diskMetricValues.BandwithRead.Add(GetServerMetricValue(timestamp, value)); } } if (diskBandwidthWriteValues != null) { diskMetricValues.BandwithWrite = new List <ServerMetricValue>(); foreach (var diskValue in diskBandwidthWriteValues.values) { long timestamp = Convert.ToInt64(diskValue[0]); double value = GetConvertedServerMetricValue(diskValue[1]); diskMetricValues.BandwithWrite.Add(GetServerMetricValue(timestamp, value)); } } } diskValues.Add(diskMetricValues); pos++; } return(diskValues); }
/// <summary> /// /// </summary> /// <param name="data"></param> /// <param name="serverMetric"></param> /// <returns></returns> private static List <ServerMetricNetworkValues> GetNetworkMetricFromResponseData(dynamic data, ServerMetric serverMetric) { List <ServerMetricNetworkValues> networkValues = new List <ServerMetricNetworkValues>(); bool hasNext = true; int pos = 0; while (hasNext) { ServerMetricNetworkValues networkMetricValues = new ServerMetricNetworkValues(); string networkPpsInKey = string.Format("network.{0}.pps.in", pos); string networkPpsOutKey = string.Format("network.{0}.pps.out", pos); string networkBandwidthInKey = string.Format("network.{0}.bandwidth.in", pos); string networkBandwidthOutKey = string.Format("network.{0}.bandwidth.out", pos); var networkPpsInValues = data.metrics.time_series[networkPpsInKey]; var networkPpsOutValues = data.metrics.time_series[networkPpsOutKey]; var networkBandwidthInValues = data.metrics.time_series[networkBandwidthInKey]; var networkBandwidthOutValues = data.metrics.time_series[networkBandwidthOutKey]; if (networkPpsInValues == null && networkPpsOutValues == null && networkBandwidthInValues == null && networkBandwidthOutValues == null) { hasNext = false; continue; } else { if (networkPpsInValues != null) { networkMetricValues.PPSIn = new List <ServerMetricValue>(); foreach (var diskValue in networkPpsInValues.values) { long timestamp = Convert.ToInt64(diskValue[0]); double value = GetConvertedServerMetricValue(diskValue[1]); networkMetricValues.PPSIn.Add(GetServerMetricValue(timestamp, value)); } } if (networkPpsOutValues != null) { networkMetricValues.PPSOut = new List <ServerMetricValue>(); foreach (var diskValue in networkPpsOutValues.values) { long timestamp = Convert.ToInt64(diskValue[0]); double value = GetConvertedServerMetricValue(diskValue[1]); networkMetricValues.PPSOut.Add(GetServerMetricValue(timestamp, value)); } } if (networkBandwidthInValues != null) { networkMetricValues.BandwithIn = new List <ServerMetricValue>(); foreach (var diskValue in networkBandwidthInValues.values) { long timestamp = Convert.ToInt64(diskValue[0]); double value = GetConvertedServerMetricValue(diskValue[1]); networkMetricValues.BandwithIn.Add(GetServerMetricValue(timestamp, value)); } } if (networkBandwidthOutValues != null) { networkMetricValues.BandwithOut = new List <ServerMetricValue>(); foreach (var diskValue in networkBandwidthOutValues.values) { long timestamp = Convert.ToInt64(diskValue[0]); double value = GetConvertedServerMetricValue(diskValue[1]); networkMetricValues.BandwithOut.Add(GetServerMetricValue(timestamp, value)); } } } networkValues.Add(networkMetricValues); pos++; } return(networkValues); }
/// <summary> /// Metrics are available for the last 30 days only. /// If you do not provide the step argument we will automatically adjust it so that a maximum of 100 samples are returned. /// We limit the number of samples returned to a maximum of 500 and will adjust the step parameter accordingly. /// </summary> /// <param name="type">Type of metrics to get (cpu, disk, network)</param> /// <param name="start">Start of period to get Metrics for (in ISO-8601 format).</param> /// <param name="end">End of period to get Metrics for (in ISO-8601 format).</param> /// <param name="step">Resolution of results in seconds</param> /// <returns></returns> public async Task <ServerMetric> GetMetrics(string type, string start, string end, int step) { Dictionary <string, string> arguments = new Dictionary <string, string>(); if (!string.IsNullOrEmpty(type.Trim()) && !string.IsNullOrWhiteSpace(type.Trim())) { arguments.Add("type", type); } else { throw new InvalidArgumentException("missing required argument 'type'"); } if (!string.IsNullOrEmpty(start.Trim()) && !string.IsNullOrWhiteSpace(start.Trim())) { arguments.Add("start", start); } else { throw new InvalidArgumentException("missing required argument 'start'"); } if (!string.IsNullOrEmpty(end.Trim()) && !string.IsNullOrWhiteSpace(end.Trim())) { arguments.Add("end", end); } else { throw new InvalidArgumentException("missing required argument 'end'"); } if (step > 0) { arguments.Add("step", step.ToString()); } string url = string.Format("/servers/{0}/metrics", this.Id); int i = 0; foreach (KeyValuePair <string, string> argument in arguments) { if (i == 0) { url += "?"; } else { url += "&"; } url += string.Format("{0}={1}", argument.Key, argument.Value); i++; } string responseContent = await ApiCore.SendRequest(url); ServerMetric serverMetric = GetServerMetricFromResponse(responseContent, type); return(serverMetric); }