Skip to content

Commit 743ebe7

Browse files
kvm: get vm disk stats for ceph disks (#7045)
1 parent 55d2d26 commit 743ebe7

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4016,15 +4016,10 @@ public List<VmDiskStatsEntry> getVmDiskStat(final Connect conn, final String vmN
40164016
break;
40174017
}
40184018
final DomainBlockStats blockStats = dm.blockStats(disk.getDiskLabel());
4019-
final String path = disk.getDiskPath(); // for example, path = /mnt/pool_uuid/disk_path/
4020-
String diskPath = null;
4021-
if (path != null) {
4022-
final String[] token = path.split("/");
4023-
if (token.length > 3) {
4024-
diskPath = token[3];
4025-
final VmDiskStatsEntry stat = new VmDiskStatsEntry(vmName, diskPath, blockStats.wr_req, blockStats.rd_req, blockStats.wr_bytes, blockStats.rd_bytes);
4026-
stats.add(stat);
4027-
}
4019+
String diskPath = getDiskPathFromDiskDef(disk);
4020+
if (diskPath != null) {
4021+
final VmDiskStatsEntry stat = new VmDiskStatsEntry(vmName, diskPath, blockStats.wr_req, blockStats.rd_req, blockStats.wr_bytes, blockStats.rd_bytes);
4022+
stats.add(stat);
40284023
}
40294024
}
40304025

@@ -4036,6 +4031,23 @@ public List<VmDiskStatsEntry> getVmDiskStat(final Connect conn, final String vmN
40364031
}
40374032
}
40384033

4034+
protected String getDiskPathFromDiskDef(DiskDef disk) {
4035+
final String path = disk.getDiskPath();
4036+
if (path != null) {
4037+
final String[] token = path.split("/");
4038+
if (DiskProtocol.RBD.equals(disk.getDiskProtocol())) {
4039+
// for example, path = <RBD pool>/<disk path>
4040+
if (token.length > 1) {
4041+
return token[1];
4042+
}
4043+
} else if (token.length > 3) {
4044+
// for example, path = /mnt/pool_uuid/disk_path/
4045+
return token[3];
4046+
}
4047+
}
4048+
return null;
4049+
}
4050+
40394051
private class VmStats {
40404052
long _usedTime;
40414053
long _tx;

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtVMDef.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,10 @@ public void setDiskPath(String volPath) {
990990
_sourcePath = volPath;
991991
}
992992

993+
public DiskProtocol getDiskProtocol() {
994+
return _diskProtocol;
995+
}
996+
993997
public DiskBus getBusType() {
994998
return _bus;
995999
}

plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6053,6 +6053,39 @@ public void testGetHaproxyStatsMethod() throws Exception {
60536053
verify(scriptMock).add(String.valueOf(port));
60546054
}
60556055

6056+
@Test
6057+
public void testGetDiskPathFromDiskDefForRBD() {
6058+
DiskDef diskDef = new DiskDef();
6059+
diskDef.defNetworkBasedDisk("cloudstack/diskpath", "1.1.1.1", 3300, "username", "uuid", 0,
6060+
DiskDef.DiskBus.VIRTIO, DiskDef.DiskProtocol.RBD, DiskDef.DiskFmtType.RAW);
6061+
String diskPath = libvirtComputingResourceSpy.getDiskPathFromDiskDef(diskDef);
6062+
Assert.assertEquals("diskpath", diskPath);
6063+
}
6064+
6065+
@Test
6066+
public void testGetDiskPathFromDiskDefForNFS() {
6067+
DiskDef diskDef = new DiskDef();
6068+
diskDef.defFileBasedDisk("/mnt/pool/filepath", 0, DiskDef.DiskBus.VIRTIO, DiskDef.DiskFmtType.QCOW2);
6069+
String diskPath = libvirtComputingResourceSpy.getDiskPathFromDiskDef(diskDef);
6070+
Assert.assertEquals("filepath", diskPath);
6071+
}
6072+
6073+
@Test
6074+
public void testGetDiskPathFromDiskDefForNFSWithNullPath() {
6075+
DiskDef diskDef = new DiskDef();
6076+
diskDef.defFileBasedDisk(null, 0, DiskDef.DiskBus.VIRTIO, DiskDef.DiskFmtType.QCOW2);
6077+
String diskPath = libvirtComputingResourceSpy.getDiskPathFromDiskDef(diskDef);
6078+
Assert.assertNull(diskPath);
6079+
}
6080+
6081+
@Test
6082+
public void testGetDiskPathFromDiskDefForNFSWithUnsupportedPath() {
6083+
DiskDef diskDef = new DiskDef();
6084+
diskDef.defFileBasedDisk("/mnt/unsupported-path", 0, DiskDef.DiskBus.VIRTIO, DiskDef.DiskFmtType.QCOW2);
6085+
String diskPath = libvirtComputingResourceSpy.getDiskPathFromDiskDef(diskDef);
6086+
Assert.assertNull(diskPath);
6087+
}
6088+
60566089
@Test
60576090
@PrepareForTest(value = {LibvirtComputingResource.class})
60586091
public void testNetworkUsageMethod1() throws Exception {

0 commit comments

Comments
 (0)