咸鱼

咸鱼是以盐腌渍后,晒干的鱼

0%

Java获取Linux系统资源信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Test {

public static void main(String[] args) throws IOException {

//cat /proc/meminfo

String commands="cat /proc/meminfo";

//String message= runCommands(commands);
//System.out.println(message);

getDiskInfo(1);
}

public static String runCommands(String commands) throws IOException{

Process process = Runtime.getRuntime().exec(commands);
InputStreamReader ir = new InputStreamReader(process.getInputStream(), "UTF-8");
BufferedReader input = new BufferedReader(ir);
String line;
String message="";
while ((line = input.readLine()) != null) {
//System.out.println("+++"+line);
message = message + line + "\n" ;
}
return message;
}

public static List<List> getDiskInfo(int sda_number) throws IOException{
String commands = "df -h |grep /dev/sda"+sda_number;
// String commands = "df -h";

Process process = Runtime.getRuntime().exec(commands);
InputStreamReader ir = new InputStreamReader(process.getInputStream(), "UTF-8");
BufferedReader input = new BufferedReader(ir);
String line;

List<List> result = new ArrayList();
/**
* [文件系统, 容量, 已用, 可用, 已用%, 挂载点]
* [/dev/sda1, 8.3G, 6.4G, 1.5G, 82%, /]
*/
while ((line = input.readLine()) != null) {
//System.out.println("----------"+line);
String[] temp = line.split(" ");
List<String> list_dev_sda = new ArrayList();
for (String string : temp) {
string = string.replaceAll(" ", "");
if(!string.equals("")){
//System.out.println(string);
list_dev_sda.add(string);
}
}
System.out.println(list_dev_sda.toString());
result.add(list_dev_sda);
}
return result;
}

public static HashMap<String, Float> getMemInfo() throws IOException{

String commands = "cat /proc/meminfo";
Process process = Runtime.getRuntime().exec(commands);
InputStreamReader ir = new InputStreamReader(process.getInputStream(), "UTF-8");
BufferedReader input = new BufferedReader(ir);
//读第一行和第二行
String[] memTotalArray = input.readLine().replace(" ", "").split(":");
String[] memFreeArray = input.readLine().replace(" ", "").split(":");

float memTotal = Float.valueOf(memTotalArray[1].replace("kB", ""));
float memFree = Float.valueOf(memFreeArray[1].replace("kB", ""));

System.out.println("memTotal = "+memTotal/(1024*1024) + "GB");
System.out.println("memFree = "+memFree/1024 + "MB");
HashMap<String, Float> mem = new HashMap<>();
mem.put("memTotal", memTotal);
mem.put("memFree", memFree);
return mem;
}
}