mirror of https://github.com/apache/cloudstack.git
FileUtil simplified
- writeToFile removed since no references to it - readFileAsString replaced with FileUtils.readFileToString - minor code duplication removed in dependent method getNicStats - unit test added Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>
This commit is contained in:
parent
6ea38bff16
commit
882e5fa4e0
|
|
@ -64,6 +64,7 @@ import org.apache.cloudstack.utils.qemu.QemuImg.PhysicalDiskFormat;
|
|||
import org.apache.cloudstack.utils.qemu.QemuImgException;
|
||||
import org.apache.cloudstack.utils.qemu.QemuImgFile;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.libvirt.Connect;
|
||||
import org.libvirt.Domain;
|
||||
import org.libvirt.DomainBlockStats;
|
||||
|
|
@ -229,7 +230,6 @@ import com.cloud.storage.template.Processor.FormatInfo;
|
|||
import com.cloud.storage.template.QCOW2Processor;
|
||||
import com.cloud.storage.template.TemplateLocation;
|
||||
import com.cloud.storage.template.TemplateProp;
|
||||
import com.cloud.utils.FileUtil;
|
||||
import com.cloud.utils.NumbersUtil;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.PropertiesUtil;
|
||||
|
|
@ -4847,24 +4847,18 @@ ServerResource {
|
|||
}
|
||||
}
|
||||
|
||||
private Pair<Double, Double> getNicStats(String nicName) {
|
||||
double rx = 0.0;
|
||||
String rxFile = "/sys/class/net/" + nicName + "/statistics/rx_bytes";
|
||||
String rxContent = FileUtil.readFileAsString(rxFile);
|
||||
if (rxContent == null) {
|
||||
s_logger.warn("Failed to read the rx_bytes for " + nicName + " from " + rxFile);
|
||||
}
|
||||
rx = Double.parseDouble(rxContent);
|
||||
static Pair<Double, Double> getNicStats(String nicName) {
|
||||
return new Pair<Double, Double>(readDouble(nicName, "rx_bytes"), readDouble(nicName, "tx_bytes"));
|
||||
}
|
||||
|
||||
double tx = 0.0;
|
||||
String txFile = "/sys/class/net/" + nicName + "/statistics/tx_bytes";
|
||||
String txContent = FileUtil.readFileAsString(txFile);
|
||||
if (txContent == null) {
|
||||
s_logger.warn("Failed to read the tx_bytes for " + nicName + " from " + txFile);
|
||||
static double readDouble(String nicName, String fileName) {
|
||||
final String path = "/sys/class/net/" + nicName + "/statistics/" + fileName;
|
||||
try {
|
||||
return Double.parseDouble(FileUtils.readFileToString(new File(path)));
|
||||
} catch (IOException ioe) {
|
||||
s_logger.warn("Failed to read the " + fileName + " for " + nicName + " from " + path, ioe);
|
||||
return 0.0;
|
||||
}
|
||||
tx = Double.parseDouble(txContent);
|
||||
|
||||
return new Pair<Double, Double>(rx, tx);
|
||||
}
|
||||
|
||||
private Answer execute(NetworkRulesSystemVmCommand cmd) {
|
||||
|
|
|
|||
|
|
@ -21,12 +21,17 @@ package com.cloud.hypervisor.kvm.resource;
|
|||
|
||||
import com.cloud.agent.api.to.VirtualMachineTO;
|
||||
import com.cloud.template.VirtualMachineTemplate.BootloaderType;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.vm.VirtualMachine;
|
||||
|
||||
import org.apache.commons.lang.SystemUtils;
|
||||
import org.junit.Assume;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class LibvirtComputingResourceTest {
|
||||
|
||||
|
|
@ -183,4 +188,13 @@ public class LibvirtComputingResourceTest {
|
|||
|
||||
assertEquals(vmStr, vm.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNicStats() {
|
||||
//this test is only working on linux because of the loopback interface name
|
||||
//also the tested code seems to work only on linux
|
||||
Assume.assumeTrue(SystemUtils.IS_OS_LINUX);
|
||||
Pair<Double, Double> stats = LibvirtComputingResource.getNicStats("lo");
|
||||
assertNotNull(stats);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,54 +16,15 @@
|
|||
// under the License.
|
||||
package com.cloud.utils;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
public class FileUtil {
|
||||
|
||||
public static String readFileAsString(String filePath) {
|
||||
File file = new File(filePath);
|
||||
if(!file.exists())
|
||||
return null;
|
||||
|
||||
try {
|
||||
byte[] buffer = new byte[(int)file.length()];
|
||||
BufferedInputStream f = null;
|
||||
try {
|
||||
f = new BufferedInputStream(new FileInputStream(filePath));
|
||||
f.read(buffer);
|
||||
} finally {
|
||||
if (f != null) {
|
||||
try {
|
||||
f.close();
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return new String(buffer);
|
||||
} catch(IOException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void writeToFile(String content, String filePath) throws IOException {
|
||||
BufferedWriter out = null;
|
||||
try {
|
||||
out = new BufferedWriter(new FileWriter(filePath));
|
||||
out.write(content);
|
||||
} finally {
|
||||
if(out != null)
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void copyfile(File f1, File f2) throws IOException {
|
||||
InputStream in = new FileInputStream(f1);
|
||||
OutputStream out = new FileOutputStream(f2);
|
||||
|
|
|
|||
Loading…
Reference in New Issue