StatusHandler.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (C) 2004-2013 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.handler.ITelnetHandler;
  29. import com.l2jserver.gameserver.model.L2Object;
  30. import com.l2jserver.gameserver.model.L2World;
  31. import com.l2jserver.gameserver.model.actor.L2Character;
  32. import com.l2jserver.gameserver.model.actor.L2Npc;
  33. import com.l2jserver.gameserver.model.actor.L2Summon;
  34. import com.l2jserver.gameserver.model.actor.instance.L2DoorInstance;
  35. import com.l2jserver.gameserver.model.actor.instance.L2MonsterInstance;
  36. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  37. import com.l2jserver.gameserver.model.items.instance.L2ItemInstance;
  38. /**
  39. * @author UnAfraid
  40. */
  41. public class StatusHandler implements ITelnetHandler
  42. {
  43. private final String[] _commands =
  44. {
  45. "status",
  46. "forcegc",
  47. "memusage",
  48. "gmlist"
  49. };
  50. private int uptime;
  51. @Override
  52. public boolean useCommand(String command, PrintWriter _print, Socket _cSocket, int _uptime)
  53. {
  54. if (command.equals("status"))
  55. {
  56. uptime = _uptime;
  57. _print.print(getServerStatus());
  58. _print.flush();
  59. }
  60. else if (command.equals("forcegc"))
  61. {
  62. System.gc();
  63. StringBuilder sb = new StringBuilder();
  64. sb.append("RAM Used: " + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1048576)); // 1024 * 1024 = 1048576
  65. _print.println(sb.toString());
  66. }
  67. else if (command.startsWith("memusage"))
  68. {
  69. double max = Runtime.getRuntime().maxMemory() / 1024; // maxMemory is the upper
  70. // limit the jvm can use
  71. double allocated = Runtime.getRuntime().totalMemory() / 1024; // totalMemory the
  72. // size of the
  73. // current
  74. // allocation pool
  75. double nonAllocated = max - allocated; // non allocated memory till jvm limit
  76. double cached = Runtime.getRuntime().freeMemory() / 1024; // freeMemory the
  77. // unused memory in
  78. // the allocation pool
  79. double used = allocated - cached; // really used memory
  80. double useable = max - used; // allocated, but non-used and non-allocated memory
  81. DecimalFormat df = new DecimalFormat(" (0.0000'%')");
  82. DecimalFormat df2 = new DecimalFormat(" # 'KB'");
  83. _print.println("+----");// ...
  84. _print.println("| Allowed Memory:" + df2.format(max));
  85. _print.println("| |= Allocated Memory:" + df2.format(allocated) + df.format((allocated / max) * 100));
  86. _print.println("| |= Non-Allocated Memory:" + df2.format(nonAllocated) + df.format((nonAllocated / max) * 100));
  87. _print.println("| Allocated Memory:" + df2.format(allocated));
  88. _print.println("| |= Used Memory:" + df2.format(used) + df.format((used / max) * 100));
  89. _print.println("| |= Unused (cached) Memory:" + df2.format(cached) + df.format((cached / max) * 100));
  90. _print.println("| Useable Memory:" + df2.format(useable) + df.format((useable / max) * 100)); // ...
  91. _print.println("+----");
  92. }
  93. else if (command.equals("gmlist"))
  94. {
  95. int igm = 0;
  96. String gmList = "";
  97. for (String player : AdminTable.getInstance().getAllGmNames(true))
  98. {
  99. gmList = gmList + ", " + player;
  100. igm++;
  101. }
  102. _print.println("There are currently " + igm + " GM(s) online...");
  103. if (!gmList.isEmpty())
  104. {
  105. _print.println(gmList);
  106. }
  107. }
  108. return false;
  109. }
  110. public String getServerStatus()
  111. {
  112. int playerCount = 0, objectCount = 0;
  113. int max = LoginServerThread.getInstance().getMaxPlayer();
  114. playerCount = L2World.getInstance().getAllPlayersCount();
  115. objectCount = L2World.getInstance().getAllVisibleObjectsCount();
  116. int itemCount = 0;
  117. int itemVoidCount = 0;
  118. int monsterCount = 0;
  119. int minionCount = 0;
  120. int minionsGroupCount = 0;
  121. int npcCount = 0;
  122. int charCount = 0;
  123. int pcCount = 0;
  124. int detachedCount = 0;
  125. int doorCount = 0;
  126. int summonCount = 0;
  127. int AICount = 0;
  128. L2Object[] objs = L2World.getInstance().getAllVisibleObjectsArray();
  129. for (L2Object obj : objs)
  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).getLocation() == L2ItemInstance.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. }