Skip to content

Commit

Permalink
Added CPU & RAM information to /co status
Browse files Browse the repository at this point in the history
  • Loading branch information
Intelli committed Jul 10, 2024
1 parent e093970 commit dbd8723
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lang/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ COMMAND_THROTTLED: "Please wait a moment and try again."
CONSUMER_ERROR: "Consumer queue processing already {paused|resumed}."
CONSUMER_TOGGLED: "Consumer queue processing has been {paused|resumed}."
CONTAINER_HEADER: "Container Transactions"
CPU_CORES: "CPU cores."
DATABASE_BUSY: "Database busy. Please try again later."
DATABASE_INDEX_ERROR: "Unable to validate database indexes."
DATABASE_LOCKED_1: "Database locked. Waiting up to 15 seconds..."
Expand All @@ -23,7 +24,7 @@ DATABASE_LOCKED_3: "To disable database locking, set \"database-lock: false\"."
DATABASE_LOCKED_4: "Disabling database locking can result in data corruption."
DATABASE_UNREACHABLE: "Database is unreachable. Discarding data and shutting down."
DEVELOPMENT_BRANCH: "Development branch detected, skipping patch scripts."
DIRT_BLOCK: "Placed a dirt block under you."
DIRT_BLOCK: "Placed a temporary safety block under you."
DISABLE_SUCCESS: "Success! Disabled {0}"
ENABLE_FAILED: "{0} was unable to start."
ENABLE_SUCCESS: "{0} has been successfully enabled!"
Expand Down Expand Up @@ -159,6 +160,7 @@ PURGE_REPAIRING: "Attempting to repair. This may take some time..."
PURGE_ROWS: "{0} {row|rows} of data deleted."
PURGE_STARTED: "Data purge started on \"{0}\"."
PURGE_SUCCESS: "Data purge successful."
RAM_STATS: "{0}GB / {1}GB RAM"
RELOAD_STARTED: "Reloading configuration - please wait."
RELOAD_SUCCESS: "Configuration successfully reloaded."
ROLLBACK_ABORTED: "Rollback or restore aborted."
Expand All @@ -180,6 +182,7 @@ STATUS_CONSUMER: "Consumer: {0} {item|items} in queue."
STATUS_DATABASE: "Database: Using {0}."
STATUS_INTEGRATION: "{0}: Integration {enabled|disabled}."
STATUS_LICENSE: "License: {0}"
STATUS_SYSTEM: "System: {0}"
STATUS_VERSION: "Version: {0}"
TELEPORTED: "Teleported to {0}."
TELEPORTED_SAFETY: "Teleported you to safety."
Expand Down
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
<exclude>org.intellij:*</exclude>
<exclude>org.jetbrains:*</exclude>
<exclude>org.slf4j:*</exclude>
<exclude>org.apache.logging.log4j:*</exclude>
<exclude>net.java.dev.jna:*</exclude>
</excludes>
</artifactSet>
<relocations>
Expand Down Expand Up @@ -152,5 +154,15 @@
<artifactId>HikariCP</artifactId>
<version>5.0.1</version>
</dependency>
<dependency>
<groupId>org.jhardware</groupId>
<artifactId>jHardware</artifactId>
<version>0.8.6</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.23.1</version>
</dependency>
</dependencies>
</project>
37 changes: 36 additions & 1 deletion src/main/java/net/coreprotect/command/StatusCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public void run() {
}

/*
CoreProtect show RAM usage
Items processed (since server start)
Items processed (last 60 minutes)
*/
Expand Down Expand Up @@ -102,6 +101,42 @@ else if (instance.getServer().getPluginManager().getPlugin("WorldEdit") != null)
e.printStackTrace();
}

try {
String cpuInfo = "";
if (ConfigHandler.processorInfo != null) {
String modelName = ConfigHandler.processorInfo.getModelName();
if (modelName.contains(" CPU")) {
String[] split = ConfigHandler.processorInfo.getModelName().split(" CPU")[0].split(" ");
modelName = split[split.length - 1];
}
else if (modelName.contains(" Processor")) {
String[] split = ConfigHandler.processorInfo.getModelName().split(" Processor")[0].split(" ");
modelName = split[split.length - 1];
}

String cpuSpeed = ConfigHandler.processorInfo.getMhz();
cpuSpeed = String.format("%.2f", Double.valueOf(cpuSpeed) / 1000.0);
cpuInfo = modelName + " " + Runtime.getRuntime().availableProcessors() + " x " + cpuSpeed + "GHz.";
}
else {
cpuInfo = "x" + Runtime.getRuntime().availableProcessors() + " " + Phrase.build(Phrase.CPU_CORES);
}

int mb = 1024 * 1024;
Runtime runtime = Runtime.getRuntime();
String usedRAM = String.format("%.2f", Double.valueOf((runtime.totalMemory() - runtime.freeMemory()) / mb) / 1000.0);
String totalRAM = String.format("%.2f", Double.valueOf(runtime.maxMemory() / mb) / 1000.0);
String systemInformation = Phrase.build(Phrase.RAM_STATS, usedRAM, totalRAM);
if (cpuInfo.length() > 0) {
systemInformation = cpuInfo + " (" + systemInformation + ")";
}

Chat.sendMessage(player, Color.DARK_AQUA + Phrase.build(Phrase.STATUS_SYSTEM, Color.WHITE, systemInformation));
}
catch (Exception e) {
e.printStackTrace();
}

// Functions.sendMessage(player, Color.DARK_AQUA + "Website: " + Color.WHITE + "www.coreprotect.net/updates/");

// Functions.sendMessage(player, Color.DARK_AQUA + Phrase.build(Phrase.LINK_DISCORD, Color.WHITE + "www.coreprotect.net/discord/").replaceFirst(":", ":" + Color.WHITE));
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/coreprotect/config/ConfigHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.jutils.jhardware.model.ProcessorInfo;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
Expand Down Expand Up @@ -55,6 +56,7 @@ public class ConfigHandler extends Queue {
public static int maximumPoolSize = 10;

public static HikariDataSource hikariDataSource = null;
public static final ProcessorInfo processorInfo = Util.getProcessorInfo();
public static final boolean isSpigot = Util.isSpigot();
public static final boolean isPaper = Util.isPaper();
public static final boolean isFolia = Util.isFolia();
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/coreprotect/language/Language.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static void loadPhrases() {
phrases.put(Phrase.CONSUMER_ERROR, "Consumer queue processing already {paused|resumed}.");
phrases.put(Phrase.CONSUMER_TOGGLED, "Consumer queue processing has been {paused|resumed}.");
phrases.put(Phrase.CONTAINER_HEADER, "Container Transactions");
phrases.put(Phrase.CPU_CORES, "CPU cores.");
phrases.put(Phrase.DATABASE_BUSY, "Database busy. Please try again later.");
phrases.put(Phrase.DATABASE_INDEX_ERROR, "Unable to validate database indexes.");
phrases.put(Phrase.DATABASE_LOCKED_1, "Database locked. Waiting up to 15 seconds...");
Expand Down Expand Up @@ -188,6 +189,7 @@ public static void loadPhrases() {
phrases.put(Phrase.PURGE_ROWS, "{0} {row|rows} of data deleted.");
phrases.put(Phrase.PURGE_STARTED, "Data purge started on \"{0}\".");
phrases.put(Phrase.PURGE_SUCCESS, "Data purge successful.");
phrases.put(Phrase.RAM_STATS, "{0}GB / {1}GB RAM");
phrases.put(Phrase.RELOAD_STARTED, "Reloading configuration - please wait.");
phrases.put(Phrase.RELOAD_SUCCESS, "Configuration successfully reloaded.");
phrases.put(Phrase.ROLLBACK_ABORTED, "Rollback or restore aborted.");
Expand All @@ -209,6 +211,7 @@ public static void loadPhrases() {
phrases.put(Phrase.STATUS_DATABASE, "Database: Using {0}.");
phrases.put(Phrase.STATUS_INTEGRATION, "{0}: Integration {enabled|disabled}.");
phrases.put(Phrase.STATUS_LICENSE, "License: {0}");
phrases.put(Phrase.STATUS_SYSTEM, "System: {0}");
phrases.put(Phrase.STATUS_VERSION, "Version: {0}");
phrases.put(Phrase.TELEPORTED, "Teleported to {0}.");
phrases.put(Phrase.TELEPORTED_SAFETY, "Teleported you to safety.");
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/coreprotect/language/Phrase.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public enum Phrase {
CONSUMER_ERROR,
CONSUMER_TOGGLED,
CONTAINER_HEADER,
CPU_CORES,
DATABASE_BUSY,
DATABASE_INDEX_ERROR,
DATABASE_LOCKED_1,
Expand Down Expand Up @@ -171,6 +172,7 @@ public enum Phrase {
PURGE_ROWS,
PURGE_STARTED,
PURGE_SUCCESS,
RAM_STATS,
RELOAD_STARTED,
RELOAD_SUCCESS,
ROLLBACK_ABORTED,
Expand All @@ -192,6 +194,7 @@ public enum Phrase {
STATUS_DATABASE,
STATUS_INTEGRATION,
STATUS_LICENSE,
STATUS_SYSTEM,
STATUS_VERSION,
TELEPORTED,
TELEPORTED_SAFETY,
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/net/coreprotect/utility/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.SortedSet;
import java.util.TreeSet;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
Expand Down Expand Up @@ -51,6 +53,8 @@
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.Plugin;
import org.bukkit.util.io.BukkitObjectOutputStream;
import org.jutils.jhardware.HardwareInfo;
import org.jutils.jhardware.model.ProcessorInfo;

import net.coreprotect.CoreProtect;
import net.coreprotect.bukkit.BukkitAdapter;
Expand Down Expand Up @@ -112,6 +116,19 @@ public static String getPluginName() {
return name;
}

public static ProcessorInfo getProcessorInfo() {
ProcessorInfo result = null;
try {
Configurator.setLevel("com.profesorfalken.jsensors.manager.unix.UnixSensorsManager", Level.WARN);
result = HardwareInfo.getProcessorInfo();
}
catch (Exception e) {
// unable to read processor information
}

return result;
}

public static int getBlockId(Material material) {
if (material == null) {
material = Material.AIR;
Expand Down

0 comments on commit dbd8723

Please sign in to comment.