Skip to content

Commit 4709756

Browse files
committed
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.
1 parent c9e7648 commit 4709756

1 file changed

Lines changed: 34 additions & 10 deletions

File tree

plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4592,19 +4592,43 @@ private boolean isSnapshotSupported() {
45924592

45934593
private Pair<Double, Double> getNicStats(String nicName) {
45944594
double rx = 0.0;
4595-
OutputInterpreter.OneLineParser rxParser = new OutputInterpreter.OneLineParser();
4596-
String result = executeBashScript("cat /sys/class/net/" + nicName
4597-
+ "/statistics/rx_bytes", rxParser);
4598-
if (result == null && rxParser.getLine() != null) {
4599-
rx = Double.parseDouble(rxParser.getLine());
4595+
File rxFile = new File("/sys/class/net/" + nicName + "/statistics/rx_bytes");
4596+
try {
4597+
FileInputStream rxStream = new FileInputStream(rxFile);
4598+
StringBuffer rxContent = new StringBuffer("");
4599+
byte[] rxBuffer = new byte[1024];
4600+
int rxLength;
4601+
4602+
while ((rxLength = rxStream.read(rxBuffer)) != -1) {
4603+
rxContent.append(new String(rxBuffer));
4604+
}
4605+
rx = Double.parseDouble(rxContent.toString());
4606+
} catch (final FileNotFoundException e) {
4607+
throw new CloudRuntimeException("Cannot find the file: "
4608+
+ rxFile.getAbsolutePath(), e);
4609+
} catch (final IOException e) {
4610+
throw new CloudRuntimeException("IOException in reading "
4611+
+ rxFile.getAbsolutePath(), e);
46004612
}
46014613

46024614
double tx = 0.0;
4603-
OutputInterpreter.OneLineParser txParser = new OutputInterpreter.OneLineParser();
4604-
result = executeBashScript("cat /sys/class/net/" + nicName
4605-
+ "/statistics/tx_bytes", txParser);
4606-
if (result == null && txParser.getLine() != null) {
4607-
tx = Double.parseDouble(txParser.getLine());
4615+
File txFile = new File("/sys/class/net/" + nicName + "/statistics/tx_bytes");
4616+
try {
4617+
FileInputStream txStream = new FileInputStream(txFile);
4618+
StringBuffer txContent = new StringBuffer("");
4619+
byte[] txBuffer = new byte[1024];
4620+
int txLength;
4621+
4622+
while((txLength = txStream.read(txBuffer)) != -1) {
4623+
txContent.append(new String(txBuffer));
4624+
}
4625+
tx = Double.parseDouble(txContent.toString());
4626+
} catch (final FileNotFoundException e) {
4627+
throw new CloudRuntimeException("Cannot find the file: "
4628+
+ txFile.getAbsolutePath(), e);
4629+
} catch (final IOException e) {
4630+
throw new CloudRuntimeException("IOException in reading "
4631+
+ txFile.getAbsolutePath(), e);
46084632
}
46094633

46104634
return new Pair<Double, Double>(rx, tx);

0 commit comments

Comments
 (0)