|
211 | 211 | import com.cloud.storage.template.TemplateLocation; |
212 | 212 | import com.cloud.utils.NumbersUtil; |
213 | 213 | import com.cloud.utils.Pair; |
| 214 | +import com.cloud.utils.FileUtil; |
214 | 215 | import com.cloud.utils.PropertiesUtil; |
215 | 216 | import com.cloud.utils.exception.CloudRuntimeException; |
216 | 217 | import com.cloud.utils.net.NetUtils; |
@@ -4594,44 +4595,20 @@ private boolean isSnapshotSupported() { |
4594 | 4595 |
|
4595 | 4596 | private Pair<Double, Double> getNicStats(String nicName) { |
4596 | 4597 | 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); |
4614 | 4602 | } |
| 4603 | + rx = Double.parseDouble(rxContent); |
4615 | 4604 |
|
4616 | 4605 | 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); |
4634 | 4610 | } |
| 4611 | + tx = Double.parseDouble(txContent); |
4635 | 4612 |
|
4636 | 4613 | return new Pair<Double, Double>(rx, tx); |
4637 | 4614 | } |
|
0 commit comments