Repository navigation
Expand file tree
/
Copy pathHardwareMonitor.java
More file actions
132 lines (97 loc) · 3.38 KB
/
Copy pathHardwareMonitor.java
File metadata and controls
132 lines (97 loc) · 3.38 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import java.io.*;
import java.net.*;
import java.nio.file.*;
import java.time.*;
import java.util.*;
public class HardwareMonitor {
private static final Path LOG =
Paths.get("hardware-monitor.log");
private static final Map<String, String> baseline =
new HashMap<>();
public static void main(String[] args) throws Exception {
System.out.println("Hardware monitor started.");
takeSnapshot();
while (true) {
Thread.sleep(10_000);
Map<String, String> current = snapshot();
for (Map.Entry<String, String> entry : current.entrySet()) {
String oldValue = baseline.get(entry.getKey());
if (oldValue != null && !oldValue.equals(entry.getValue())) {
alert(entry.getKey(), oldValue, entry.getValue());
}
}
}
}
private static void takeSnapshot() throws Exception {
baseline.clear();
baseline.putAll(snapshot());
log("Baseline created.");
}
private static Map<String, String> snapshot() throws Exception {
Map<String, String> result = new HashMap<>();
Runtime runtime = Runtime.getRuntime();
result.put("availableProcessors",
String.valueOf(runtime.availableProcessors()));
result.put("maxMemory",
String.valueOf(runtime.maxMemory()));
result.put("os",
System.getProperty("os.name"));
result.put("osVersion",
System.getProperty("os.version"));
result.put("architecture",
System.getProperty("os.arch"));
Enumeration<NetworkInterface> interfaces =
NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface ni = interfaces.nextElement();
if (ni.isUp()) {
String key = "network:" + ni.getName();
String mac = "unknown";
byte[] hardware = ni.getHardwareAddress();
if (hardware != null) {
mac = formatMac(hardware);
}
result.put(key, mac);
}
}
return result;
}
private static String formatMac(byte[] mac) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
if (i > 0) {
sb.append(":");
}
sb.append(String.format("%02X", mac[i]));
}
return sb.toString();
}
private static void alert(
String component,
String oldValue,
String newValue) {
String message =
"CHANGE DETECTED: " +
component +
" | old=" + oldValue +
" | new=" + newValue;
System.err.println(message);
log(message);
}
private static void log(String message) {
String line =
"[" + Instant.now() + "] " +
message + System.lineSeparator();
try {
Files.writeString(
LOG,
line,
StandardOpenOption.CREATE,
StandardOpenOption.APPEND
);
} catch (IOException e) {
System.err.println(
"Unable to write log: " + e.getMessage());
}
}
}