Updated Branches:
  refs/heads/master c9e764818 -> 4709756bd

agent: Use FileInputStream for reading files instead of cat

Cat'ing a file and parsing that output is not reliable and this can be done
by using Java's native methods for that.


Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/4709756b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/4709756b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/4709756b

Branch: refs/heads/master
Commit: 4709756bd597c56d47e8ca86ea3216a7e960b2cd
Parents: c9e7648
Author: Wido den Hollander <[email protected]>
Authored: Fri Feb 1 15:01:52 2013 +0100
Committer: Wido den Hollander <[email protected]>
Committed: Sat Feb 2 18:08:24 2013 +0100

----------------------------------------------------------------------
 .../kvm/resource/LibvirtComputingResource.java     |   44 +++++++++++---
 1 files changed, 34 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/4709756b/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 b36b4c2..d3a4a22 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
@@ -4592,19 +4592,43 @@ ServerResource {
 
     private Pair<Double, Double> getNicStats(String nicName) {
         double rx = 0.0;
-        OutputInterpreter.OneLineParser rxParser = new 
OutputInterpreter.OneLineParser();
-        String result = executeBashScript("cat /sys/class/net/" + nicName
-                + "/statistics/rx_bytes", rxParser);
-        if (result == null && rxParser.getLine() != null) {
-            rx = Double.parseDouble(rxParser.getLine());
+        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);
         }
 
         double tx = 0.0;
-        OutputInterpreter.OneLineParser txParser = new 
OutputInterpreter.OneLineParser();
-        result = executeBashScript("cat /sys/class/net/" + nicName
-                + "/statistics/tx_bytes", txParser);
-        if (result == null && txParser.getLine() != null) {
-            tx = Double.parseDouble(txParser.getLine());
+        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);
         }
 
         return new Pair<Double, Double>(rx, tx);

Reply via email to