Skip to content

Commit 68197d9

Browse files
committed
使用oshi代替sigar获取系统属性。
1 parent cd41e98 commit 68197d9

10 files changed

Lines changed: 83 additions & 151 deletions

File tree

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@
154154
<artifactId>spring-boot-starter-mail</artifactId>
155155
</dependency>
156156

157-
<!-- sigar获取系统信息 -->
157+
<!-- oshi获取系统信息工具 -->
158158
<dependency>
159-
<groupId>org.fusesource</groupId>
160-
<artifactId>sigar</artifactId>
161-
<version>1.6.4</version>
159+
<groupId>com.github.oshi</groupId>
160+
<artifactId>oshi-core</artifactId>
161+
<version>3.9.1</version>
162162
</dependency>
163163

164164
<!-- kafka开发 -->

src/main/java/com/xwintop/xJavaFxTool/controller/debugTools/SocketToolController.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.xwintop.xJavaFxTool.services.debugTools.socketTool.SocketToolService;
44
import com.xwintop.xJavaFxTool.utils.JavaFxViewUtil;
5-
import com.xwintop.xJavaFxTool.utils.SigarUtil;
65
import com.xwintop.xJavaFxTool.view.debugTools.SocketToolView;
76
import javafx.collections.FXCollections;
87
import javafx.collections.ObservableList;
@@ -15,6 +14,10 @@
1514
import lombok.Getter;
1615
import lombok.Setter;
1716
import lombok.extern.slf4j.Slf4j;
17+
import org.apache.commons.lang3.ArrayUtils;
18+
import oshi.SystemInfo;
19+
import oshi.hardware.HardwareAbstractionLayer;
20+
import oshi.hardware.NetworkIF;
1821

1922
import java.net.URL;
2023
import java.util.Map;
@@ -46,15 +49,21 @@ public void initialize(URL location, ResourceBundle resources) {
4649
}
4750

4851
private void initView() throws Exception {
49-
String ifNames[] = SigarUtil.sigar.getNetInterfaceList();
50-
for (int i = ifNames.length - 1; i >= 0; i--) {
51-
String address = SigarUtil.sigar.getNetInterfaceConfig(ifNames[i]).getAddress();
52-
if (!"0.0.0.0".equals(address)) {
52+
serverTcpUrlComboBox.getItems().add("127.0.0.1");
53+
serverUdpUrlComboBox.getItems().add("127.0.0.1");
54+
clientUrlComboBox.getItems().add("127.0.0.1");
55+
SystemInfo si = new SystemInfo();
56+
HardwareAbstractionLayer hal = si.getHardware();
57+
NetworkIF[] networkIFs = hal.getNetworkIFs();
58+
for (NetworkIF networkIF : networkIFs) {
59+
if (ArrayUtils.getLength(networkIF.getIPv4addr()) > 0) {
60+
String address = networkIF.getIPv4addr()[0];
5361
serverTcpUrlComboBox.getItems().add(address);
5462
serverUdpUrlComboBox.getItems().add(address);
5563
clientUrlComboBox.getItems().add(address);
5664
}
5765
}
66+
5867
serverTcpUrlComboBox.getSelectionModel().select(0);
5968
serverUdpUrlComboBox.getSelectionModel().select(0);
6069
clientUrlComboBox.getSelectionModel().select(0);
@@ -86,7 +95,7 @@ private void initEvent() {
8695
if (event.getButton() == MouseButton.SECONDARY && !serverConnectTableData.isEmpty()) {
8796
MenuItem menu_Remove = new MenuItem("断开选中连接");
8897
menu_Remove.setOnAction(event1 -> {
89-
if(socketToolService.getTcpAcceptor() !=null) {
98+
if (socketToolService.getTcpAcceptor() != null) {
9099
socketToolService.getTcpAcceptor().getManagedSessions().forEach((aLong, ioSession) -> {
91100
serverConnectTableView.getSelectionModel().getSelectedItems().forEach(stringStringMap -> {
92101
if (stringStringMap.get("connect").contains(ioSession.getRemoteAddress().toString())) {
@@ -95,7 +104,7 @@ private void initEvent() {
95104
});
96105
});
97106
}
98-
if (socketToolService.getUdpAcceptor() != null){
107+
if (socketToolService.getUdpAcceptor() != null) {
99108
socketToolService.getUdpAcceptor().getManagedSessions().forEach((aLong, ioSession) -> {
100109
serverConnectTableView.getSelectionModel().getSelectedItems().forEach(stringStringMap -> {
101110
if (stringStringMap.get("connect").contains(ioSession.getRemoteAddress().toString())) {
@@ -108,12 +117,12 @@ private void initEvent() {
108117
});
109118
MenuItem menu_RemoveAll = new MenuItem("断开所有连接");
110119
menu_RemoveAll.setOnAction(event1 -> {
111-
if(socketToolService.getTcpAcceptor() !=null) {
120+
if (socketToolService.getTcpAcceptor() != null) {
112121
socketToolService.getTcpAcceptor().getManagedSessions().forEach((aLong, ioSession) -> {
113122
ioSession.closeNow();
114123
});
115124
}
116-
if(socketToolService.getUdpAcceptor() !=null) {
125+
if (socketToolService.getUdpAcceptor() != null) {
117126
socketToolService.getUdpAcceptor().getManagedSessions().forEach((aLong, ioSession) -> {
118127
ioSession.closeNow();
119128
});

src/main/java/com/xwintop/xJavaFxTool/services/javaFxTools/ShowSystemInfoService.java

Lines changed: 61 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,23 @@
22

33
import com.alibaba.fastjson.JSON;
44
import com.xwintop.xJavaFxTool.controller.javaFxTools.ShowSystemInfoController;
5-
import com.xwintop.xJavaFxTool.utils.SigarUtil;
65
import com.xwintop.xcore.util.FileUtil;
7-
8-
import org.hyperic.sigar.CpuInfo;
9-
import org.hyperic.sigar.CpuPerc;
10-
import org.hyperic.sigar.FileSystem;
11-
import org.hyperic.sigar.FileSystemUsage;
12-
import org.hyperic.sigar.Mem;
13-
import org.hyperic.sigar.Sigar;
14-
15-
import java.net.InetAddress;
16-
import java.util.ArrayList;
17-
import java.util.HashMap;
18-
import java.util.List;
19-
import java.util.Map;
20-
import java.util.Properties;
21-
import java.util.Timer;
22-
import java.util.TimerTask;
23-
246
import javafx.application.Platform;
257
import javafx.scene.chart.LineChart;
268
import javafx.scene.chart.XYChart;
279
import lombok.Getter;
2810
import lombok.Setter;
2911
import lombok.extern.log4j.Log4j;
12+
import oshi.SystemInfo;
13+
import oshi.hardware.CentralProcessor;
14+
import oshi.hardware.GlobalMemory;
15+
import oshi.hardware.HardwareAbstractionLayer;
16+
import oshi.software.os.FileSystem;
17+
import oshi.software.os.OSFileStore;
18+
import oshi.software.os.OperatingSystem;
19+
20+
import java.net.InetAddress;
21+
import java.util.*;
3022

3123
/**
3224
* @ClassName: ShowSystemInfoService
@@ -40,39 +32,41 @@
4032
public class ShowSystemInfoService {
4133

4234
private ShowSystemInfoController showSystemInfoController;
43-
private Sigar sigar = SigarUtil.sigar;
4435
private List<Timer> timerList = new ArrayList<Timer>();//统一管理线程
4536

37+
private SystemInfo si = new SystemInfo();
38+
private HardwareAbstractionLayer hal = si.getHardware();
39+
4640
public void showOverviewCpuLineChart() {
4741
try {
48-
XYChart.Series[] series = new XYChart.Series[sigar.getCpuInfoList().length];
42+
CentralProcessor processor = hal.getProcessor();
43+
XYChart.Series[] series = new XYChart.Series[processor.getLogicalProcessorCount()];
4944
for (int i = 0; i < series.length; i++) {
5045
series[i] = new XYChart.Series();
5146
series[i].setName("第" + (i + 1) + "块CPU信息");
5247
}
5348
showSystemInfoController.getOverviewCpuLineChart().getData().addAll(series);
5449
showSystemInfoController.getOverviewCpuLineChart().getXAxis().setTickLabelsVisible(false);
5550
showSystemInfoController.getOverviewCpuLineChart().getYAxis().setMaxHeight(1);
56-
Timer timer = new Timer();
51+
Timer timer = new Timer();
5752
timerList.add(timer);
5853
timer.schedule(new TimerTask() {
5954
@Override
6055
public void run() {
6156
try {
62-
CpuInfo infos[] = sigar.getCpuInfoList();
63-
CpuPerc cpuList[] = sigar.getCpuPercList();
64-
String xValue = ""+System.currentTimeMillis();
65-
for (int i = 0; i < infos.length; i++) {// 不管是单块CPU还是多CPU都适用
57+
String xValue = "" + System.currentTimeMillis();
58+
double[] load = processor.getProcessorCpuLoadBetweenTicks();
59+
for (int i = 0; i < series.length; i++) {// 不管是单块CPU还是多CPU都适用
6660
final int ii = i;
67-
Platform.runLater(()-> {
61+
Platform.runLater(() -> {
6862
if (series[ii].getData().size() > 60) {
6963
series[ii].getData().remove(0);
7064
}
71-
series[ii].getData().add(new XYChart.Data(xValue, cpuList[ii].getCombined()));
65+
series[ii].getData().add(new XYChart.Data(xValue, load[ii]));
7266
});
7367
}
7468
} catch (Exception e) {
75-
log.error(e.getMessage());
69+
log.error("cpu使用率获取失败:", e);
7670
}
7771
}
7872
}, 0, 1000);
@@ -83,14 +77,15 @@ public void run() {
8377

8478
public void showOverviewMemoryLineChart() {
8579
try {
80+
GlobalMemory memory = hal.getMemory();
8681
LineChart lineChart = showSystemInfoController.getOverviewMemoryLineChart();
8782
// lineChart.setCreateSymbols(false);
8883
XYChart.Series<String, Integer> series = new XYChart.Series<String, Integer>();
8984
lineChart.getData().add(series);
9085
lineChart.getXAxis().setTickLabelsVisible(false);
91-
lineChart.getYAxis().setMaxHeight(sigar.getMem().getTotal() / 1048576L);
86+
lineChart.getYAxis().setMaxHeight(memory.getTotal() / 1048576L);
9287
lineChart.setLegendVisible(false);
93-
Timer timer = new Timer();
88+
Timer timer = new Timer();
9489
timerList.add(timer);
9590
timer.schedule(new TimerTask() {
9691
@Override
@@ -100,8 +95,7 @@ public void run() {
10095
if (series.getData().size() > 60) {
10196
series.getData().remove(0);
10297
}
103-
Mem mem = sigar.getMem();
104-
series.getData().add(new XYChart.Data(""+System.currentTimeMillis(), mem.getUsed() / 1048576L));
98+
series.getData().add(new XYChart.Data("" + System.currentTimeMillis(), (memory.getTotal() - memory.getAvailable()) / 1048576L));
10599
} catch (Exception e) {
106100
e.printStackTrace();
107101
}
@@ -115,7 +109,7 @@ public void run() {
115109

116110
public void showOverviewDiskLineChart() {
117111
try {
118-
Timer timer = new Timer();
112+
Timer timer = new Timer();
119113
timerList.add(timer);
120114
timer.schedule(new TimerTask() {
121115
@Override
@@ -130,7 +124,7 @@ public void run() {
130124

131125
public void showOverviewNetLineChart() {
132126
try {
133-
Timer timer = new Timer();
127+
Timer timer = new Timer();
134128
timerList.add(timer);
135129
timer.schedule(new TimerTask() {
136130
@Override
@@ -144,80 +138,45 @@ public void run() {
144138

145139
public void showDiskInfo() {
146140
try {
147-
// FlowPane flowPane = new FlowPane();
148-
FileSystem fslist[] = sigar.getFileSystemList();
141+
OperatingSystem os = si.getOperatingSystem();
142+
FileSystem fileSystem = os.getFileSystem();
143+
OSFileStore[] fsArray = fileSystem.getFileStores();
149144
List dataList = new ArrayList();
150-
// showSystemInfoController.getDiskTab().setContent(flowPane);
151-
for (int i = 0; i < fslist.length; i++) {
152-
try {
153-
FileSystem fs = fslist[i];
154-
FileSystemUsage usage = sigar.getFileSystemUsage(fs.getDirName());
155-
switch (fs.getType()) {
156-
case 0: // TYPE_UNKNOWN :未知
157-
break;
158-
case 1: // TYPE_NONE
159-
break;
160-
case 2: // TYPE_LOCAL_DISK : 本地硬盘
161-
// ObservableList<PieChart.Data> pieChartData =
162-
// FXCollections.observableArrayList(
163-
// new PieChart.Data("已经使用量", usage.getUsed()),
164-
// new PieChart.Data("剩余大小", usage.getFree())
165-
// );
166-
// final PieChart chart = new PieChart(pieChartData);
167-
// chart.setTitle("盘符名称:" + fs.getDevName());
168-
// chart.setStartAngle(90);
169-
// chart.setPrefWidth(300);
170-
// chart.setPrefHeight(300);
171-
// chart.setLegendVisible(false);
172-
// flowPane.getChildren().add(chart);
173-
174-
Map map = new HashMap();
175-
map.put("id", ""+i);
176-
map.put("name", "磁盘" + fs.getDevName());
177-
map.put("parent", "");
178-
map.put("value", usage.getTotal());
179-
map.put("showValue", FileUtil.FormetFileSize(usage.getTotal()*1024));
180-
181-
Map map1 = new HashMap();
182-
map1.put("id", ""+i + '1');
183-
map1.put("name", "已用");
184-
map1.put("parent", ""+i);
185-
map1.put("value", usage.getUsed());
186-
map1.put("showValue", FileUtil.FormetFileSize(usage.getUsed()*1024));
187-
188-
Map map2 = new HashMap();
189-
map2.put("id", ""+i + '2');
190-
map2.put("name", "剩余");
191-
map2.put("parent", ""+i);
192-
map2.put("value", usage.getFree());
193-
map2.put("showValue", FileUtil.FormetFileSize(usage.getFree()*1024));
194-
dataList.add(map);
195-
dataList.add(map1);
196-
dataList.add(map2);
197-
break;
198-
case 3:// TYPE_NETWORK :网络
199-
break;
200-
case 4:// TYPE_RAM_DISK :闪存
201-
break;
202-
case 5:// TYPE_CDROM :光驱
203-
break;
204-
case 6:// TYPE_SWAP :页面交换
205-
break;
206-
default:
207-
break;
208-
}
209-
} catch (Exception e) {
210-
log.error(e.getMessage());
211-
}
145+
for (int i = 0; i < fsArray.length; i++) {
146+
OSFileStore fs = fsArray[i];
147+
Map map = new HashMap();
148+
map.put("id", "" + i);
149+
map.put("name", fs.getName());
150+
map.put("parent", "");
151+
map.put("value", fs.getTotalSpace());
152+
map.put("showValue", FileUtil.FormetFileSize(fs.getTotalSpace()));
153+
154+
Map map1 = new HashMap();
155+
map1.put("id", "" + i + '1');
156+
map1.put("name", "已用");
157+
map1.put("parent", "" + i);
158+
map1.put("value", fs.getTotalSpace() - fs.getUsableSpace());
159+
map1.put("showValue", FileUtil.FormetFileSize((fs.getTotalSpace() - fs.getUsableSpace())));
160+
161+
Map map2 = new HashMap();
162+
map2.put("id", "" + i + '2');
163+
map2.put("name", "剩余");
164+
map2.put("parent", "" + i);
165+
map2.put("value", fs.getUsableSpace());
166+
map2.put("showValue", FileUtil.FormetFileSize(fs.getUsableSpace()));
167+
dataList.add(map);
168+
dataList.add(map1);
169+
dataList.add(map2);
212170
}
213171
String s = JSON.toJSONString(dataList);
214-
showSystemInfoController.getDiskWebView().getEngine().executeScript("updateData("+s+")");
172+
showSystemInfoController.getDiskWebView().getEngine().executeScript("updateData(" + s + ")");
215173
} catch (Exception e) {
216-
log.error(e.getMessage());
174+
log.error("获取磁盘空间失败", e);
217175
}
218176
}
219177

220-
/**
178+
179+
/**
221180
* 显示vm信息
222181
*/
223182
public void showVmInfo() {//显示Vm信息
@@ -277,7 +236,7 @@ public void showVmInfo() {//显示Vm信息
277236
/**
278237
* 停止所以线程
279238
*/
280-
public void stopTimerList(){
239+
public void stopTimerList() {
281240
for (Timer timer : timerList) {
282241
timer.cancel();
283242
timer = null;

src/main/java/com/xwintop/xJavaFxTool/utils/SigarUtil.java

Lines changed: 0 additions & 36 deletions
This file was deleted.
-241 KB
Binary file not shown.
-483 KB
Binary file not shown.
-228 KB
Binary file not shown.
-393 KB
Binary file not shown.
-260 KB
Binary file not shown.
-97.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)