Add info server
command
`info` coming soon™️
This commit is contained in:
parent
414a990b74
commit
77614ad7ce
4 changed files with 143 additions and 2 deletions
|
@ -13,6 +13,7 @@ import java.io.BufferedReader;
|
||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
|
@ -21,7 +22,7 @@ import com.google.gson.JsonParser;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
private static JsonObject getConfig (File file) throws Exception {
|
private static JsonObject getConfig (File file) throws IOException {
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
// Read the default config
|
// Read the default config
|
||||||
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("default_config.json");
|
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("default_config.json");
|
||||||
|
|
|
@ -0,0 +1,90 @@
|
||||||
|
package land.chipmunk.chipmunkbot.commands;
|
||||||
|
|
||||||
|
import land.chipmunk.chipmunkbot.util.CPUInfo;
|
||||||
|
import land.chipmunk.chipmunkbot.command.*;
|
||||||
|
import static land.chipmunk.chipmunkbot.plugins.CommandManager.literal;
|
||||||
|
import static land.chipmunk.chipmunkbot.plugins.CommandManager.argument;
|
||||||
|
import static com.mojang.brigadier.arguments.StringArgumentType.greedyString;
|
||||||
|
import static com.mojang.brigadier.arguments.StringArgumentType.getString;
|
||||||
|
import com.mojang.brigadier.CommandDispatcher;
|
||||||
|
import com.mojang.brigadier.tree.CommandNode;
|
||||||
|
import com.mojang.brigadier.context.CommandContext;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.text.format.NamedTextColor;
|
||||||
|
import net.kyori.adventure.text.event.HoverEvent;
|
||||||
|
import net.kyori.adventure.text.JoinConfiguration;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.lang.management.*;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class InfoCommand extends Command {
|
||||||
|
public InfoCommand () {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.node(
|
||||||
|
literal("info")
|
||||||
|
.then(
|
||||||
|
literal("server")
|
||||||
|
.executes(this::sendServerInfo)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int sendServerInfo (CommandContext<CommandSource> context) {
|
||||||
|
final CommandSource source = context.getSource();
|
||||||
|
|
||||||
|
final OperatingSystemMXBean os = ManagementFactory.getOperatingSystemMXBean();
|
||||||
|
final RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
|
||||||
|
final MemoryMXBean memory = ManagementFactory.getMemoryMXBean();
|
||||||
|
|
||||||
|
// ? Should I send a single message that uses newlines instead?
|
||||||
|
source.sendOutput(formatEntry(Component.translatable("Working directory"), Component.text(System.getProperty("user.dir"))), false);
|
||||||
|
try {
|
||||||
|
final InetAddress localhost = InetAddress.getLocalHost();
|
||||||
|
source.sendOutput(formatEntry(Component.translatable("Hostname"), Component.text(localhost.getHostName())), false);
|
||||||
|
source.sendOutput(formatEntry(Component.translatable("IP address"), Component.text(localhost.getHostAddress())), false);
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
}
|
||||||
|
|
||||||
|
source.sendOutput(formatEntry(Component.translatable("OS name"), Component.text(os.getName())), false);
|
||||||
|
source.sendOutput(formatEntry(Component.translatable("OS version"), Component.text(os.getVersion())), false);
|
||||||
|
source.sendOutput(formatEntry(Component.translatable("OS architecture"), Component.text(os.getArch())), false);
|
||||||
|
source.sendOutput(formatEntry(Component.translatable("CPU cores"), Component.text(os.getAvailableProcessors())), false);
|
||||||
|
source.sendOutput(formatEntry(Component.translatable("CPU load"), Component.text(os.getSystemLoadAverage())), false);
|
||||||
|
|
||||||
|
try {
|
||||||
|
final String cpuModel = CPUInfo.readCpuInfo().get("model name");
|
||||||
|
source.sendOutput(formatEntry(Component.translatable("CPU Model"), Component.text(cpuModel)), false);
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
}
|
||||||
|
|
||||||
|
source.sendOutput(formatEntry(Component.translatable("Java VM"), Component.text(runtime.getVmName())), false);
|
||||||
|
source.sendOutput(formatEntry(Component.translatable("Java version"), Component.text(runtime.getVmVersion())), false);
|
||||||
|
|
||||||
|
source.sendOutput(formatEntry(Component.translatable("Heap memory usage"), formatMemoryUsage(memory.getHeapMemoryUsage())), false);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Component formatEntry (Component key, Component value) {
|
||||||
|
return Component.translatable(
|
||||||
|
"%s: %s",
|
||||||
|
key.color(NamedTextColor.GREEN),
|
||||||
|
value.color(NamedTextColor.DARK_GREEN)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Component formatMemoryUsage (long used, long max) {
|
||||||
|
return Component.translatable(
|
||||||
|
"%sMB/%sMB",
|
||||||
|
Component.text(used / 1024l / 1024l),
|
||||||
|
Component.text(max / 1024l / 1024l)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Component formatMemoryUsage (MemoryUsage usage) {
|
||||||
|
return formatMemoryUsage(usage.getUsed(), usage.getMax());
|
||||||
|
}
|
||||||
|
}
|
|
@ -23,7 +23,7 @@ import lombok.Setter;
|
||||||
public class CommandManager {
|
public class CommandManager {
|
||||||
private ChipmunkBot client;
|
private ChipmunkBot client;
|
||||||
@Getter @Setter private CommandDispatcher<CommandSource> dispatcher = new CommandDispatcher<>();
|
@Getter @Setter private CommandDispatcher<CommandSource> dispatcher = new CommandDispatcher<>();
|
||||||
private final Command[] commands = {new TestCommand(), new HelpCommand(), new RunCommand(), new EchoCommand()};
|
private final Command[] commands = {new TestCommand(), new HelpCommand(), new RunCommand(), new EchoCommand(), new InfoCommand()};
|
||||||
|
|
||||||
static {
|
static {
|
||||||
// ? Is messing with static properties a good idea?
|
// ? Is messing with static properties a good idea?
|
||||||
|
|
50
src/main/java/land/chipmunk/chipmunkbot/util/CPUInfo.java
Normal file
50
src/main/java/land/chipmunk/chipmunkbot/util/CPUInfo.java
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
package land.chipmunk.chipmunkbot.util;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
public class CPUInfo {
|
||||||
|
public static Map<String, String> readCpuInfo () throws IOException {
|
||||||
|
return readCpuInfo(new File("/proc/cpuinfo"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, String> readCpuInfo (File file) throws IOException {
|
||||||
|
final InputStream opt = new FileInputStream(file);
|
||||||
|
final Reader reader = new BufferedReader(new InputStreamReader(opt));
|
||||||
|
|
||||||
|
return readCpuInfo(reader);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, String> readCpuInfo (Reader reader) throws IOException {
|
||||||
|
final Map<String, String> map = new HashMap<>();
|
||||||
|
|
||||||
|
do {
|
||||||
|
final String key = ((char) reader.read()) + readUntil(reader, ':').trim();
|
||||||
|
final String value = readUntil(reader, '\n').trim();
|
||||||
|
|
||||||
|
if (!key.isEmpty()) map.put(key, value);
|
||||||
|
} while (reader.ready());
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String readUntil (Reader reader, char character) throws IOException {
|
||||||
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
int readInt = reader.read();
|
||||||
|
if (readInt == -1 || (char) readInt == character) break;
|
||||||
|
stringBuilder.append((char) readInt);
|
||||||
|
}
|
||||||
|
|
||||||
|
return stringBuilder.toString();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue