Server Monitor

Discussion in 'Spigot Plugin Help' started by UtilsByteMedia, Jan 8, 2020.

  1. Hi I am trying to code a server monitor for my server, I'm not sure how to get the TPS, Uptime, Max memory, Free Memory left, Any help please!!

    My code so far
    Code (Java):
    public class MonitorReport extends BukkitRunnable {

        public void start() {
            this.runTaskTimer( Monitor.getInstance(), 100L, 100L); // Timer just to test it in chat,
        }

        public void run() {
            Bukkit.broadcast("", "servermonitor.monitor");
            Bukkit.broadcast("§e§l[!] §f§lServer §e§lMonitor Report §7(" + Bukkit.getOnlinePlayers().size() + " / " + Bukkit.getMaxPlayers() + "§7)", "servermonitor.monitor");
            Bukkit.broadcast(" §7» §fUptine§7: " /** UpTime in here.*/ ", "servermonitor.monitor");
            Bukkit.broadcast("
    §7» §fTPS§7: " /** TPS in here.*/ ", ""servermonitor.monitor");
            Bukkit.broadcast("
    §7» §fMaximum Memory§7: " /** Max Memory in here.*/ " MB.", "servermonitor.monitor");
            Bukkit.broadcast("
    §7» §fFree Memory§7: " /** Free Memory left in here.*/ " MB.", "servermonitor.monitor");
            Bukkit.broadcast("
    ", "servermonitor.monitor");
        }
    }
     
  2. TPS: Use a Task that lasts one tick and divide by the actual time System#currentTimeMillis() that passed
    Uptime: Not exact, but store the time that the server was started in onLoad(); maybe there's a better method...
    Maximum Memory, Free memory: Use this at own Risk https://stackoverflow.com/a/12807848/12190301
     
  3. How do I do that? Please help.
     
  4. How do you do what exactly? There are 3 things I suggested :p
     
  5. It doesn't help it shows the same numbers.
     
  6. Tried them all they all show the sames numbers, when ever the server is laging or not.

    Code (Java):
        long maxMemory = Runtime.getRuntime().maxMemory();

        long definitelyFreeMemory = Runtime.getRuntime().freeMemory();


        public void start() {
            this.runTaskTimer( Monitor.getInstance(), 100L, 100L);
        }

        public void run() {
            Bukkit.broadcast("", "");
            Bukkit.broadcast("§e§l[!] §f§lServer §e§lMonitor Report §7(" + Bukkit.getOnlinePlayers().size() + " / " + Bukkit.getMaxPlayers() + "§7)", "core.admin.servermonitorreport");
            Bukkit.broadcast(" §7» §fUptine§7: ", "core.admin.servermonitorreport");
            Bukkit.broadcast(" §7» §fTPS§7: ", "core.admin.servermonitorreport");
            Bukkit.broadcast(" §7» §fMaximum Memory§7: " + maxMemory +" MB.", "core.admin.servermonitorreport");
            Bukkit.broadcast(" §7» §fFree Memory§7: " + definitelyFreeMemory + " MB.", "core.admin.servermonitorreport");
            Bukkit.broadcast("", "core.admin.servermonitorreport");
        }
    }
     
     
  7. Ah. You need to put the maxMemory and freeMemory that inside the run() method
     
  8. So it's like this?

    Code (Java):
        public void run() {
            long maxMemory = Runtime.getRuntime().maxMemory();

            long definitelyFreeMemory = Runtime.getRuntime().freeMemory();
            Bukkit.broadcast("", "");
            Bukkit.broadcast("§e§l[!] §f§lServer §e§lMonitor Report §7(" + Bukkit.getOnlinePlayers().size() + " / " + Bukkit.getMaxPlayers() + "§7)", "core.admin.servermonitorreport");
            Bukkit.broadcast(" §7» §fUptine§7: ", "core.admin.servermonitorreport");
            Bukkit.broadcast(" §7» §fTPS§7: ", "core.admin.servermonitorreport");
            Bukkit.broadcast(" §7» §fMaximum Memory§7: " + maxMemory +" MB.", "core.admin.servermonitorreport");
            Bukkit.broadcast(" §7» §fFree Memory§7: " + allocatedMemory + " MB.", "core.admin.servermonitorreport");
            Bukkit.broadcast("", "core.admin.servermonitorreport");
     
  9. Yes.
     
  10. Ok, That work's now, How do I get the TPS of the server?
     
  11. And Uptime of the server.
     
  12. TPS:
    Code (Java):
    long lastTickTime = System.currentTimeMillis();
                     
    Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () -> {
        long newTickTime = System.currentTimeMillis();
        long difference = (newTickTime - lastTickTime) / 1000;
        double tps = 1 / (double) difference;
        lastTickTime = newTickTime;
    }, 1, 0);
    Uptime:

    Code (Java):
    long startTime = 0;

    @Override
    public void onLoad() {
        this.startTime = System.currentTimeMillis();
    }
    ...
    }
    and while run subtract with System.currentTimeInMilis()

    DONT just blindly copy this. I'm writing this while at the same time stirring my tomato sauce so try to understand my logic behind this and then see if, and if so where I did not quite get things right and improve.
     
    • Funny Funny x 1
  13. Where do I put this??
     
  14. And the TPS how do I do that cause I get error It only comes up with 157851474399, and not 20.00 or 19.66, How do I fix and where do I put the Bukkit.getSchedluer.
     
  15. Uptime only comes up with 0 and nothing else.
     
  16. This:
    Code (Java):
    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> {
        long newTickTime = System.currentTimeMillis();
        long difference = newTickTime - lastTickTime;
        lastTps = (double) 1000 / (double) difference;
        lastTickTime = newTickTime;
    }, 0, 1);
    in your onEnable() method,

    and this:
    Code (Text):
    private double lastTps;
    public double getTPS() {
        return lastTPS;
    }
    in your main class for tps (no tomato sauce, so I can test)
     
  17. This
    Code (Java):
    public long startTime = 0;
    private double getUpTimeSeconds() {
        return (System.currentTimeMillis() - startTime) / 1000D;
    }
    in your main class

    and this

    Code (Java):
    startTime = System.currentTimeMillis();
    in your onLoad() method
     
  18. Code (Java):
    public class Monitor  extends JavaPlugin {
        private static Monitor instance;
        private static Plugin plugin;
        public long startTime = 0;
        private double getUpTimeSeconds() {
            return (System.currentTimeMillis() - startTime) / 1000D;
        }

        public static Monitor getInstance(){
            return Monitor.instance;
        }

        public void onEnable(){
            Bukkit.getConsoleSender().sendMessage("CortexMonitor has been ENABLED");
            Monitor.instance = this;
            new MonitorReport().start();
            startTime = System.currentTimeMillis();
        }

        public void onDisable(){

        }
    }
    Like this?? I'm not sure.
     
  19. You need to override onLoad as well. Simply add


    Code (Java):
    @Override
    public void onLoad() {

    }
    to your class and insert the code I provided that should go into onLoad()
     
  20. On load do you mean onEnable??