From 6446797fdc546cb05a1f207503e682e577483b7a Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Thu, 19 Aug 2021 10:00:01 +0530 Subject: [PATCH 1/2] metrics: fix hostsmetricsresponse for zero cpu, locale (#5329) * server: Fixed hosts not displaying with incompatible locale (#4900) Fixes: #4733 * added unit test Signed-off-by: Abhishek Kumar * eof newline Signed-off-by: Abhishek Kumar Co-authored-by: Spaceman1984 <49917670+Spaceman1984@users.noreply.github.com> --- .../response/HostMetricsResponse.java | 29 ++++++++++++--- .../response/HostMetricsResponseTest.java | 37 +++++++++++++++++++ 2 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 plugins/metrics/src/test/java/org/apache/cloudstack/response/HostMetricsResponseTest.java diff --git a/plugins/metrics/src/main/java/org/apache/cloudstack/response/HostMetricsResponse.java b/plugins/metrics/src/main/java/org/apache/cloudstack/response/HostMetricsResponse.java index 05e2f5f57f6..30f7e06ce58 100644 --- a/plugins/metrics/src/main/java/org/apache/cloudstack/response/HostMetricsResponse.java +++ b/plugins/metrics/src/main/java/org/apache/cloudstack/response/HostMetricsResponse.java @@ -17,10 +17,14 @@ package org.apache.cloudstack.response; +import java.text.DecimalFormat; +import java.text.ParseException; + import org.apache.cloudstack.api.response.HostResponse; import org.apache.cloudstack.outofbandmanagement.OutOfBandManagement; import com.cloud.serializer.Param; +import com.cloud.utils.exception.CloudRuntimeException; import com.google.gson.annotations.SerializedName; public class HostMetricsResponse extends HostResponse { @@ -118,7 +122,7 @@ public class HostMetricsResponse extends HostResponse { public void setCpuUsed(final String cpuUsed, final Integer cpuNumber, final Long cpuSpeed) { if (cpuUsed != null && cpuNumber != null && cpuSpeed != null) { - this.cpuUsed = String.format("%.2f Ghz", Double.valueOf(cpuUsed.replace("%", "")) * cpuNumber * cpuSpeed / (100.0 * 1000.0)); + this.cpuUsed = String.format("%.2f Ghz", parseCPU(cpuUsed) * cpuNumber * cpuSpeed / (100.0 * 1000.0)); } } @@ -130,10 +134,14 @@ public class HostMetricsResponse extends HostResponse { public void setCpuAllocated(final String cpuAllocated, final Integer cpuNumber, final Long cpuSpeed) { if (cpuAllocated != null && cpuNumber != null && cpuSpeed != null) { - this.cpuAllocated = String.format("%.2f Ghz", Double.valueOf(cpuAllocated.replace("%", "")) * cpuNumber * cpuSpeed / (100.0 * 1000.0)); + this.cpuAllocated = String.format("%.2f Ghz", parseCPU(cpuAllocated) * cpuNumber * cpuSpeed / (100.0 * 1000.0)); } } + public String getCpuAllocatedGhz() { + return cpuAllocated; + } + public void setMemTotal(final Long memTotal) { if (memTotal != null) { this.memTotal = String.format("%.2f GB", memTotal / (1024.0 * 1024.0 * 1024.0)); @@ -166,25 +174,25 @@ public class HostMetricsResponse extends HostResponse { public void setCpuUsageThreshold(final String cpuUsed, final Double threshold) { if (cpuUsed != null && threshold != null) { - this.cpuThresholdExceeded = Double.valueOf(cpuUsed.replace("%", "")) > (100.0 * threshold); + this.cpuThresholdExceeded = parseCPU(cpuUsed) > (100.0 * threshold); } } public void setCpuUsageDisableThreshold(final String cpuUsed, final Float threshold) { if (cpuUsed != null && threshold != null) { - this.cpuDisableThresholdExceeded = Double.valueOf(cpuUsed.replace("%", "")) > (100.0 * threshold); + this.cpuDisableThresholdExceeded = parseCPU(cpuUsed) > (100.0 * threshold); } } public void setCpuAllocatedThreshold(final String cpuAllocated, final Double threshold) { if (cpuAllocated != null && threshold != null) { - this.cpuAllocatedThresholdExceeded = Double.valueOf(cpuAllocated.replace("%", "")) > (100.0 * threshold ); + this.cpuAllocatedThresholdExceeded = parseCPU(cpuAllocated) > (100.0 * threshold ); } } public void setCpuAllocatedDisableThreshold(final String cpuAllocated, final Float threshold) { if (cpuAllocated != null && threshold != null) { - this.cpuAllocatedDisableThresholdExceeded = Double.valueOf(cpuAllocated.replace("%", "")) > (100.0 * threshold); + this.cpuAllocatedDisableThresholdExceeded = parseCPU(cpuAllocated) > (100.0 * threshold); } } @@ -212,4 +220,13 @@ public class HostMetricsResponse extends HostResponse { } } + private Double parseCPU(String cpu) { + DecimalFormat decimalFormat = new DecimalFormat("#.##"); + try { + return decimalFormat.parse(cpu).doubleValue(); + } catch (ParseException e) { + throw new CloudRuntimeException(e); + } + } + } diff --git a/plugins/metrics/src/test/java/org/apache/cloudstack/response/HostMetricsResponseTest.java b/plugins/metrics/src/test/java/org/apache/cloudstack/response/HostMetricsResponseTest.java new file mode 100644 index 00000000000..4e6f35ab6e6 --- /dev/null +++ b/plugins/metrics/src/test/java/org/apache/cloudstack/response/HostMetricsResponseTest.java @@ -0,0 +1,37 @@ +package org.apache.cloudstack.response; + +import org.junit.Assert; +import org.junit.Test; + +import com.cloud.utils.exception.CloudRuntimeException; + +public class HostMetricsResponseTest { + + @Test + public void testSetCpuAllocatedWithZeroCpu() { + final HostMetricsResponse hostResponse = new HostMetricsResponse(); + hostResponse.setCpuAllocated("50.25%", 0, 1000L); + Assert.assertEquals("0.00 Ghz", hostResponse.getCpuAllocatedGhz()); + } + + @Test + public void testSetCpuAllocatedWithInfiniteCpuAllocated() { + final HostMetricsResponse hostResponse = new HostMetricsResponse(); + hostResponse.setCpuAllocated("∞%", 10, 1000L); + Assert.assertEquals("Infinity Ghz", hostResponse.getCpuAllocatedGhz()); + } + + @Test(expected = CloudRuntimeException.class) + public void testSetCpuAllocatedWithInvalidCpu() { + final HostMetricsResponse hostResponse = new HostMetricsResponse(); + hostResponse.setCpuAllocated("abc", 10, 1000L); + } + + @Test + public void testSetCpuAllocatedWithValidCpu() { + final HostMetricsResponse hostResponse = new HostMetricsResponse(); + hostResponse.setCpuAllocated("50.25%", 10, 1000L); + Assert.assertEquals("5.03 Ghz", hostResponse.getCpuAllocatedGhz()); + } + +} From 5ed3246e4163f3f4552d7622abe899a4f14c70b7 Mon Sep 17 00:00:00 2001 From: Rakesh Date: Thu, 19 Aug 2021 09:45:26 +0200 Subject: [PATCH 2/2] Fix iptable rules in ubuntu 20 for bridge name (#5318) In ubuntu20 the interface name contains @ synbol and because of that even the iptable rules for brdige name contains this symbol which causes ping issues. Remove the @ symbol from iptable rule to fix the issue Co-authored-by: Rakesh Venkatesh --- scripts/vm/network/security_group.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/vm/network/security_group.py b/scripts/vm/network/security_group.py index 6732f642c05..404edd4e94d 100755 --- a/scripts/vm/network/security_group.py +++ b/scripts/vm/network/security_group.py @@ -185,7 +185,7 @@ def destroy_network_rules_for_nic(vm_name, vm_ip, vm_mac, vif, sec_ips): logging.debug("Ignoring failure to delete ebtable rules for vm: " + vm_name) def get_bridge_physdev(brname): - physdev = execute("bridge -o link show | awk '/master %s / && !/^[0-9]+: vnet/ {print $2}' | head -1 | cut -d ':' -f1" % brname) + physdev = execute("bridge -o link show | awk '/master %s / && !/^[0-9]+: vnet/ {print $2}' | head -1 | cut -d ':' -f1 | cut -d '@' -f1" % brname) return physdev.strip()