Skip to content

Commit 303fb65

Browse files
committed
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.
1 parent cb6b96c commit 303fb65

1 file changed

Lines changed: 11 additions & 34 deletions

File tree

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

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@
211211
import com.cloud.storage.template.TemplateLocation;
212212
import com.cloud.utils.NumbersUtil;
213213
import com.cloud.utils.Pair;
214+
import com.cloud.utils.FileUtil;
214215
import com.cloud.utils.PropertiesUtil;
215216
import com.cloud.utils.exception.CloudRuntimeException;
216217
import com.cloud.utils.net.NetUtils;
@@ -4594,44 +4595,20 @@ private boolean isSnapshotSupported() {
45944595

45954596
private Pair<Double, Double> getNicStats(String nicName) {
45964597
double rx = 0.0;
4597-
File rxFile = new File("/sys/class/net/" + nicName + "/statistics/rx_bytes");
4598-
try {
4599-
FileInputStream rxStream = new FileInputStream(rxFile);
4600-
StringBuffer rxContent = new StringBuffer("");
4601-
byte[] rxBuffer = new byte[1024];
4602-
int rxLength;
4603-
4604-
while ((rxLength = rxStream.read(rxBuffer)) != -1) {
4605-
rxContent.append(new String(rxBuffer));
4606-
}
4607-
rx = Double.parseDouble(rxContent.toString());
4608-
} catch (final FileNotFoundException e) {
4609-
throw new CloudRuntimeException("Cannot find the file: "
4610-
+ rxFile.getAbsolutePath(), e);
4611-
} catch (final IOException e) {
4612-
throw new CloudRuntimeException("IOException in reading "
4613-
+ rxFile.getAbsolutePath(), e);
4598+
String rxFile = "/sys/class/net/" + nicName + "/statistics/rx_bytes";
4599+
String rxContent = FileUtil.readFileAsString(rxFile);
4600+
if (rxContent == null) {
4601+
s_logger.warn("Failed to read the rx_bytes for " + nicName + " from " + rxFile);
46144602
}
4603+
rx = Double.parseDouble(rxContent);
46154604

46164605
double tx = 0.0;
4617-
File txFile = new File("/sys/class/net/" + nicName + "/statistics/tx_bytes");
4618-
try {
4619-
FileInputStream txStream = new FileInputStream(txFile);
4620-
StringBuffer txContent = new StringBuffer("");
4621-
byte[] txBuffer = new byte[1024];
4622-
int txLength;
4623-
4624-
while((txLength = txStream.read(txBuffer)) != -1) {
4625-
txContent.append(new String(txBuffer));
4626-
}
4627-
tx = Double.parseDouble(txContent.toString());
4628-
} catch (final FileNotFoundException e) {
4629-
throw new CloudRuntimeException("Cannot find the file: "
4630-
+ txFile.getAbsolutePath(), e);
4631-
} catch (final IOException e) {
4632-
throw new CloudRuntimeException("IOException in reading "
4633-
+ txFile.getAbsolutePath(), e);
4606+
String txFile = "/sys/class/net/" + nicName + "/statistics/tx_bytes";
4607+
String txContent = FileUtil.readFileAsString(txFile);
4608+
if (txContent == null) {
4609+
s_logger.warn("Failed to read the tx_bytes for " + nicName + " from " + txFile);
46344610
}
4611+
tx = Double.parseDouble(txContent);
46354612

46364613
return new Pair<Double, Double>(rx, tx);
46374614
}

0 commit comments

Comments
 (0)