StatusHandler.java 8.2 KB

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