kvm: get vm disk stats for ceph disks (#7045)

This commit is contained in:
Wei Zhou 2023-01-16 14:19:14 +01:00 committed by GitHub
parent 55d2d26449
commit 743ebe7278
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 9 deletions

View File

@ -4016,17 +4016,12 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
break;
}
final DomainBlockStats blockStats = dm.blockStats(disk.getDiskLabel());
final String path = disk.getDiskPath(); // for example, path = /mnt/pool_uuid/disk_path/
String diskPath = null;
if (path != null) {
final String[] token = path.split("/");
if (token.length > 3) {
diskPath = token[3];
String diskPath = getDiskPathFromDiskDef(disk);
if (diskPath != null) {
final VmDiskStatsEntry stat = new VmDiskStatsEntry(vmName, diskPath, blockStats.wr_req, blockStats.rd_req, blockStats.wr_bytes, blockStats.rd_bytes);
stats.add(stat);
}
}
}
return stats;
} finally {
@ -4036,6 +4031,23 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv
}
}
protected String getDiskPathFromDiskDef(DiskDef disk) {
final String path = disk.getDiskPath();
if (path != null) {
final String[] token = path.split("/");
if (DiskProtocol.RBD.equals(disk.getDiskProtocol())) {
// for example, path = <RBD pool>/<disk path>
if (token.length > 1) {
return token[1];
}
} else if (token.length > 3) {
// for example, path = /mnt/pool_uuid/disk_path/
return token[3];
}
}
return null;
}
private class VmStats {
long _usedTime;
long _tx;

View File

@ -990,6 +990,10 @@ public class LibvirtVMDef {
_sourcePath = volPath;
}
public DiskProtocol getDiskProtocol() {
return _diskProtocol;
}
public DiskBus getBusType() {
return _bus;
}

View File

@ -6053,6 +6053,39 @@ public class LibvirtComputingResourceTest {
verify(scriptMock).add(String.valueOf(port));
}
@Test
public void testGetDiskPathFromDiskDefForRBD() {
DiskDef diskDef = new DiskDef();
diskDef.defNetworkBasedDisk("cloudstack/diskpath", "1.1.1.1", 3300, "username", "uuid", 0,
DiskDef.DiskBus.VIRTIO, DiskDef.DiskProtocol.RBD, DiskDef.DiskFmtType.RAW);
String diskPath = libvirtComputingResourceSpy.getDiskPathFromDiskDef(diskDef);
Assert.assertEquals("diskpath", diskPath);
}
@Test
public void testGetDiskPathFromDiskDefForNFS() {
DiskDef diskDef = new DiskDef();
diskDef.defFileBasedDisk("/mnt/pool/filepath", 0, DiskDef.DiskBus.VIRTIO, DiskDef.DiskFmtType.QCOW2);
String diskPath = libvirtComputingResourceSpy.getDiskPathFromDiskDef(diskDef);
Assert.assertEquals("filepath", diskPath);
}
@Test
public void testGetDiskPathFromDiskDefForNFSWithNullPath() {
DiskDef diskDef = new DiskDef();
diskDef.defFileBasedDisk(null, 0, DiskDef.DiskBus.VIRTIO, DiskDef.DiskFmtType.QCOW2);
String diskPath = libvirtComputingResourceSpy.getDiskPathFromDiskDef(diskDef);
Assert.assertNull(diskPath);
}
@Test
public void testGetDiskPathFromDiskDefForNFSWithUnsupportedPath() {
DiskDef diskDef = new DiskDef();
diskDef.defFileBasedDisk("/mnt/unsupported-path", 0, DiskDef.DiskBus.VIRTIO, DiskDef.DiskFmtType.QCOW2);
String diskPath = libvirtComputingResourceSpy.getDiskPathFromDiskDef(diskDef);
Assert.assertNull(diskPath);
}
@Test
@PrepareForTest(value = {LibvirtComputingResource.class})
public void testNetworkUsageMethod1() throws Exception {