Updated Branches: refs/heads/master cb6b96cda -> 303fb650c
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. Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/303fb650 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/303fb650 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/303fb650 Branch: refs/heads/master Commit: 303fb650cb0b19afabc3ef8a97549abba025bc11 Parents: cb6b96c Author: Wido den Hollander <[email protected]> Authored: Tue Feb 5 13:46:05 2013 +0100 Committer: Wido den Hollander <[email protected]> Committed: Tue Feb 5 22:17:40 2013 +0100 ---------------------------------------------------------------------- .../kvm/resource/LibvirtComputingResource.java | 45 ++++----------- 1 files changed, 11 insertions(+), 34 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/303fb650/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java ---------------------------------------------------------------------- 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 88c3aff..f320a66 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<Double, Double> 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<Double, Double>(rx, tx); }
