StatusHandler.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package handlers.telnethandlers;
  16. import java.io.PrintWriter;
  17. import java.net.Socket;
  18. import java.text.DecimalFormat;
  19. import java.text.SimpleDateFormat;
  20. import java.util.Calendar;
  21. import com.l2jserver.gameserver.GameTimeController;
  22. import com.l2jserver.gameserver.GmListTable;
  23. import com.l2jserver.gameserver.LoginServerThread;
  24. import com.l2jserver.gameserver.handler.ITelnetHandler;
  25. import com.l2jserver.gameserver.model.L2Object;
  26. import com.l2jserver.gameserver.model.L2World;
  27. import com.l2jserver.gameserver.model.actor.L2Character;
  28. import com.l2jserver.gameserver.model.actor.L2Npc;
  29. import com.l2jserver.gameserver.model.actor.L2Summon;
  30. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  31. import com.l2jserver.gameserver.model.actor.instance.L2MonsterInstance;
  32. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  33. import com.l2jserver.gameserver.model.item.instance.L2ItemInstance;
  34. /**
  35. * @author UnAfraid
  36. */
  37. public class StatusHandler implements ITelnetHandler
  38. {
  39. private final String[] _commands =
  40. {
  41. "status",
  42. "forcegc",
  43. "memusage",
  44. "gmlist"
  45. };
  46. private int uptime;
  47. @Override
  48. public boolean useCommand(String command, PrintWriter _print, Socket _cSocket, int _uptime)
  49. {
  50. if (command.equals("status"))
  51. {
  52. uptime = _uptime;
  53. _print.print(getServerStatus());
  54. _print.flush();
  55. }
  56. else if (command.equals("forcegc"))
  57. {
  58. System.gc();
  59. StringBuilder sb = new StringBuilder();
  60. sb.append("RAM Used: " + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1048576)); // 1024 * 1024 = 1048576
  61. _print.println(sb.toString());
  62. }
  63. else if (command.startsWith("memusage"))
  64. {
  65. double max = Runtime.getRuntime().maxMemory() / 1024; // maxMemory is the upper
  66. // limit the jvm can use
  67. double allocated = Runtime.getRuntime().totalMemory() / 1024; // totalMemory the
  68. // size of the
  69. // current
  70. // allocation pool
  71. double nonAllocated = max - allocated; // non allocated memory till jvm limit
  72. double cached = Runtime.getRuntime().freeMemory() / 1024; // freeMemory the
  73. // unused memory in
  74. // the allocation pool
  75. double used = allocated - cached; // really used memory
  76. double useable = max - used; // allocated, but non-used and non-allocated memory
  77. DecimalFormat df = new DecimalFormat(" (0.0000'%')");
  78. DecimalFormat df2 = new DecimalFormat(" # 'KB'");
  79. _print.println("+----");// ...
  80. _print.println("| Allowed Memory:" + df2.format(max));
  81. _print.println("| |= Allocated Memory:" + df2.format(allocated) + df.format(allocated / max * 100));
  82. _print.println("| |= Non-Allocated Memory:" + df2.format(nonAllocated) + df.format(nonAllocated / max * 100));
  83. _print.println("| Allocated Memory:" + df2.format(allocated));
  84. _print.println("| |= Used Memory:" + df2.format(used) + df.format(used / max * 100));
  85. _print.println("| |= Unused (cached) Memory:" + df2.format(cached) + df.format(cached / max * 100));
  86. _print.println("| Useable Memory:" + df2.format(useable) + df.format(useable / max * 100)); // ...
  87. _print.println("+----");
  88. }
  89. else if (command.equals("gmlist"))
  90. {
  91. int igm = 0;
  92. String gmList = "";
  93. for (String player : GmListTable.getInstance().getAllGmNames(true))
  94. {
  95. gmList = gmList + ", " + player;
  96. igm++;
  97. }
  98. _print.println("There are currently " + igm + " GM(s) online...");
  99. if (!gmList.isEmpty())
  100. _print.println(gmList);
  101. }
  102. return false;
  103. }
  104. public String getServerStatus()
  105. {
  106. int playerCount = 0, objectCount = 0;
  107. int max = LoginServerThread.getInstance().getMaxPlayer();
  108. playerCount = L2World.getInstance().getAllPlayersCount();
  109. objectCount = L2World.getInstance().getAllVisibleObjectsCount();
  110. int itemCount = 0;
  111. int itemVoidCount = 0;
  112. int monsterCount = 0;
  113. int minionCount = 0;
  114. int minionsGroupCount = 0;
  115. int npcCount = 0;
  116. int charCount = 0;
  117. int pcCount = 0;
  118. int detachedCount = 0;
  119. int doorCount = 0;
  120. int summonCount = 0;
  121. int AICount = 0;
  122. L2Object[] objs = L2World.getInstance().getAllVisibleObjectsArray();
  123. for (L2Object obj : objs)
  124. {
  125. if (obj == null)
  126. continue;
  127. if (obj instanceof L2Character)
  128. if (((L2Character) obj).hasAI())
  129. AICount++;
  130. if (obj instanceof L2ItemInstance)
  131. if (((L2ItemInstance) obj).getLocation() == L2ItemInstance.ItemLocation.VOID)
  132. itemVoidCount++;
  133. else
  134. itemCount++;
  135. else if (obj instanceof L2MonsterInstance)
  136. {
  137. monsterCount++;
  138. if (((L2MonsterInstance) obj).hasMinions())
  139. {
  140. minionCount += ((L2MonsterInstance) obj).getMinionList().countSpawnedMinions();
  141. minionsGroupCount += ((L2MonsterInstance) obj).getMinionList().lazyCountSpawnedMinionsGroups();
  142. }
  143. }
  144. else if (obj instanceof L2Npc)
  145. npcCount++;
  146. else if (obj instanceof L2PcInstance)
  147. {
  148. pcCount++;
  149. if (((L2PcInstance) obj).getClient() != null && ((L2PcInstance) obj).getClient().isDetached())
  150. detachedCount++;
  151. }
  152. else if (obj instanceof L2Summon)
  153. summonCount++;
  154. else if (obj instanceof L2DoorInstance)
  155. doorCount++;
  156. else if (obj instanceof L2Character)
  157. charCount++;
  158. }
  159. StringBuilder sb = new StringBuilder();
  160. sb.append("Server Status: ");
  161. sb.append("\r\n ---> Player Count: " + playerCount + "/" + max);
  162. sb.append("\r\n ---> Offline Count: " + detachedCount + "/" + playerCount);
  163. sb.append("\r\n +--> Object Count: " + objectCount);
  164. sb.append("\r\n +--> AI Count: " + AICount);
  165. sb.append("\r\n +.... L2Item(Void): " + itemVoidCount);
  166. sb.append("\r\n +.......... L2Item: " + itemCount);
  167. sb.append("\r\n +....... L2Monster: " + monsterCount);
  168. sb.append("\r\n +......... Minions: " + minionCount);
  169. sb.append("\r\n +.. Minions Groups: " + minionsGroupCount);
  170. sb.append("\r\n +........... L2Npc: " + npcCount);
  171. sb.append("\r\n +............ L2Pc: " + pcCount);
  172. sb.append("\r\n +........ L2Summon: " + summonCount);
  173. sb.append("\r\n +.......... L2Door: " + doorCount);
  174. sb.append("\r\n +.......... L2Char: " + charCount);
  175. sb.append("\r\n ---> Ingame Time: " + gameTime());
  176. sb.append("\r\n ---> Server Uptime: " + getUptime(uptime));
  177. sb.append("\r\n ---> GM Count: " + getOnlineGMS());
  178. sb.append("\r\n ---> Threads: " + Thread.activeCount());
  179. sb.append("\r\n RAM Used: " + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1048576)); // 1024 * 1024 = 1048576
  180. sb.append("\r\n");
  181. return sb.toString();
  182. }
  183. private int getOnlineGMS()
  184. {
  185. return GmListTable.getInstance().getAllGms(true).size();
  186. }
  187. private String getUptime(int time)
  188. {
  189. int uptime = (int) System.currentTimeMillis() - time;
  190. uptime = uptime / 1000;
  191. int h = uptime / 3600;
  192. int m = (uptime - (h * 3600)) / 60;
  193. int s = ((uptime - (h * 3600)) - (m * 60));
  194. return h + "hrs " + m + "mins " + s + "secs";
  195. }
  196. private String gameTime()
  197. {
  198. int t = GameTimeController.getInstance().getGameTime();
  199. int h = t / 60;
  200. int m = t % 60;
  201. SimpleDateFormat format = new SimpleDateFormat("H:mm");
  202. Calendar cal = Calendar.getInstance();
  203. cal.set(Calendar.HOUR_OF_DAY, h);
  204. cal.set(Calendar.MINUTE, m);
  205. return format.format(cal.getTime());
  206. }
  207. @Override
  208. public String[] getCommandList()
  209. {
  210. return _commands;
  211. }
  212. }