Javafx实现远程系统监控
实现功能:
1、监控远程机器(用到ganymed-ssh2-build210.jar包)。
2、实时显示cpu使用情况(包括系统进程使用cpu、用户进程使用cpu以及空闲进程使用cpu)。
3、实时系统内存使用情况(包括系统已使用内存以及空闲内存)。
效果图:
接口设计: 1、UpdateListener接口,用于实时更新数据
public interface UpdateListener { public void updateCPU(String user, String sys, String idle); public void updateMemory(String used, String free); }
参考实现: 定义FX图表变量: LineChart<Number, Number> cpuLineChart; LineChart<Number, Number> memLineChart; 定义监控数据存储变量: ObservableList<LineChart.Series<Number, Number>> cpuLineChartSeries = FXCollections .observableArrayList(); ObservableList<LineChart.Series<Number, Number>> memLineChartSeries = FXCollections .observableArrayList(); 核心代码:
@Override public void updateCPU(String user, String sys, String idle) { cpuLineChartSeries.get(0).getData() .add(new LineChart.Data<Number, Number>(cpuIndex, Float .parseFloat(user))); cpuLineChartSeries.get(1).getData() .add(new LineChart.Data<Number, Number>(cpuIndex, Float .parseFloat(sys))); cpuLineChartSeries.get(2).getData() .add(new LineChart.Data<Number, Number>(cpuIndex, Float .parseFloat(idle))); NumberAxis xAxis = (NumberAxis) cpuLineChart.getXAxis(); if (cpuIndex < xAxis.getUpperBound()) { cpuIndex += 1; } else { xAxis.setLowerBound(xAxis.getLowerBound() + 1); xAxis.setUpperBound(xAxis.getUpperBound() + 1); cpuLineChartSeries.get(0).getData().remove(0); cpuLineChartSeries.get(1).getData().remove(0); cpuLineChartSeries.get(2).getData().remove(0); } } @Override public void updateMemory(String used, String free) { memLineChartSeries.get(0).getData() .add(new LineChart.Data<Number, Number>(memIndex, Float .parseFloat(used) / 1024.0)); memLineChartSeries.get(1).getData() .add(new LineChart.Data<Number, Number>(memIndex, Float .parseFloat(free) / 1024.0)); NumberAxis xAxis = (NumberAxis) memLineChart.getXAxis(); if (memIndex < xAxis.getUpperBound()) { memIndex += 1; } else { xAxis.setLowerBound(xAxis.getLowerBound() + 1); xAxis.setUpperBound(xAxis.getUpperBound() + 1); memLineChartSeries.get(0).getData().remove(0); memLineChartSeries.get(1).getData().remove(0); } }
2、数据解析接口
public interface DataParser { public String[] getCPUData(String line); public String[] getMemoryData(String line); public boolean isCPULine(String line); public boolean isMemoryLine(String line); }
Linux下数据解析参考实现:
public String[] getCPUData(String line) { String cpuStr = line.substring(line.indexOf("Cpu(s): ") + 8); int indexOfUser = cpuStr.indexOf("%us,"); String user = cpuStr.substring(0, indexOfUser).trim(); int indexOfSys = cpuStr.indexOf("%sy,"); String sys = cpuStr.substring(indexOfUser + 5, indexOfSys).trim(); int indexOfIdle = cpuStr.indexOf("%id,"); int indexOfNi = cpuStr.indexOf("%ni,"); String idle = cpuStr.substring(indexOfNi + 5, indexOfIdle).trim(); return new String[] { user, sys, idle }; } public String[] getMemoryData(String line) { String memStr = line.substring(line.indexOf("total, ") + 7); int indexOfUsed = memStr.indexOf("k used,"); String used = memStr.substring(0, indexOfUsed).trim(); double usedN = Double.parseDouble(used); used = "" + (usedN / 1024.0); int indexOfFree = memStr.indexOf("k free,"); String free = "0.5"; if (indexOfFree >= 0) { free = memStr.substring(indexOfUsed + 7, indexOfFree).trim(); double freeN = Double.parseDouble(free); free = "" + (freeN / 1024.0); } return new String[] { used, free }; }
源码地址: