From 303fb650cb0b19afabc3ef8a97549abba025bc11 Mon Sep 17 00:00:00 2001 From: Wido den Hollander Date: Tue, 5 Feb 2013 13:46:05 +0100 Subject: [PATCH] agent: Use FileUtil.readFileAsString for reading RX and TX bytes Since we already have this un utils it's better to use it here then do the whole File/FileInputStream magic. Keeps the code simpler and more readable. --- .../resource/LibvirtComputingResource.java | 45 +++++-------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java index 88c3aff6fab..f320a66b487 100755 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java @@ -211,6 +211,7 @@ import com.cloud.storage.template.TemplateInfo; import com.cloud.storage.template.TemplateLocation; import com.cloud.utils.NumbersUtil; import com.cloud.utils.Pair; +import com.cloud.utils.FileUtil; import com.cloud.utils.PropertiesUtil; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.NetUtils; @@ -4594,44 +4595,20 @@ ServerResource { private Pair getNicStats(String nicName) { double rx = 0.0; - File rxFile = new File("/sys/class/net/" + nicName + "/statistics/rx_bytes"); - try { - FileInputStream rxStream = new FileInputStream(rxFile); - StringBuffer rxContent = new StringBuffer(""); - byte[] rxBuffer = new byte[1024]; - int rxLength; - - while ((rxLength = rxStream.read(rxBuffer)) != -1) { - rxContent.append(new String(rxBuffer)); - } - rx = Double.parseDouble(rxContent.toString()); - } catch (final FileNotFoundException e) { - throw new CloudRuntimeException("Cannot find the file: " - + rxFile.getAbsolutePath(), e); - } catch (final IOException e) { - throw new CloudRuntimeException("IOException in reading " - + rxFile.getAbsolutePath(), e); + 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); double tx = 0.0; - File txFile = new File("/sys/class/net/" + nicName + "/statistics/tx_bytes"); - try { - FileInputStream txStream = new FileInputStream(txFile); - StringBuffer txContent = new StringBuffer(""); - byte[] txBuffer = new byte[1024]; - int txLength; - - while((txLength = txStream.read(txBuffer)) != -1) { - txContent.append(new String(txBuffer)); - } - tx = Double.parseDouble(txContent.toString()); - } catch (final FileNotFoundException e) { - throw new CloudRuntimeException("Cannot find the file: " - + txFile.getAbsolutePath(), e); - } catch (final IOException e) { - throw new CloudRuntimeException("IOException in reading " - + txFile.getAbsolutePath(), e); + 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); } + tx = Double.parseDouble(txContent); return new Pair(rx, tx); }