Metrics.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package de.Linus122.TimeIsMoney;
  2. import com.google.gson.Gson;
  3. import org.bukkit.plugin.Plugin;
  4. import javax.net.ssl.HttpsURLConnection;
  5. import java.io.BufferedReader;
  6. import java.io.DataOutputStream;
  7. import java.io.File;
  8. import java.io.FileReader;
  9. import java.io.FilenameFilter;
  10. import java.io.IOException;
  11. import java.nio.file.Files;
  12. import java.util.Arrays;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. /**
  16. * SpaceIOMetrics main class by
  17. *
  18. * @author Linus122
  19. * @version 0.03
  20. * @since 1.9.6.1
  21. */
  22. public class Metrics {
  23. private Plugin pl;
  24. private final Gson gson = new Gson();
  25. private String URL = "https://spaceio.de/update/%s";
  26. private final String VERSION = "0.03";
  27. private int REFRESH_INTERVAL = 600000;
  28. public Metrics(Plugin pl) {
  29. this.pl = pl;
  30. // check if Metrics are disabled (checks if file "disablemetrics" is added to the plugins's folder
  31. try {
  32. Files.list(pl.getDataFolder().getParentFile().toPath()).filter(Files::isRegularFile).forEach(v -> {
  33. if (v.getFileName().toString().equalsIgnoreCase("disablemetrics")) {
  34. return;
  35. }
  36. });
  37. } catch (IOException e1) {
  38. e1.printStackTrace();
  39. }
  40. URL = String.format(URL, pl.getName());
  41. // fetching refresh interval first
  42. pl.getServer().getScheduler().runTaskLaterAsynchronously(pl, () -> {
  43. String dataJson = collectData();
  44. try {
  45. REFRESH_INTERVAL = sendData(dataJson);
  46. } catch (Exception ignored) {
  47. }
  48. }, 20L * 5);
  49. // executing repeating task, our main metrics updater
  50. pl.getServer().getScheduler().runTaskTimerAsynchronously(pl, () -> {
  51. String dataJson = collectData();
  52. try {
  53. sendData(dataJson);
  54. } catch (Exception ignored) {
  55. }
  56. }, 20L * (REFRESH_INTERVAL / 1000), 20L * (REFRESH_INTERVAL / 1000));
  57. }
  58. private String collectData() {
  59. Data data = new Data();
  60. // collect plugin list
  61. for (Plugin plug : pl.getServer().getPluginManager().getPlugins())
  62. data.plugs.put(plug.getName(), plug.getDescription().getVersion());
  63. // fetch online players
  64. data.onlinePlayers = pl.getServer().getOnlinePlayers().size();
  65. // server version
  66. data.serverVersion = getVersion();
  67. // plugin version
  68. data.pluginVersion = pl.getDescription().getVersion();
  69. // plugin author
  70. data.pluginAuthors = pl.getDescription().getAuthors();
  71. // core count
  72. data.coreCnt = Runtime.getRuntime().availableProcessors();
  73. // java version
  74. data.javaRuntime = System.getProperty("java.runtime.version");
  75. // online mode
  76. data.onlineMode = pl.getServer().getOnlineMode();
  77. // software information
  78. data.osName = System.getProperty("os.name");
  79. data.osArch = System.getProperty("os.arch");
  80. data.osVersion = System.getProperty("os.version");
  81. data.diskSize = new File("/").getTotalSpace();
  82. if (data.osName.equals("Linux")) {
  83. data.linuxDistro = getDistro();
  84. }
  85. return gson.toJson(data);
  86. }
  87. private int sendData(String dataJson) throws Exception {
  88. java.net.URL obj = new java.net.URL(URL);
  89. HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
  90. con.setRequestMethod("POST");
  91. con.setRequestProperty("User-Agent", "Java/Bukkit");
  92. con.setRequestProperty("Metrics-Version", this.VERSION);
  93. con.setDoOutput(true);
  94. DataOutputStream wr = new DataOutputStream(con.getOutputStream());
  95. wr.writeBytes(dataJson);
  96. wr.flush();
  97. wr.close();
  98. return Integer.parseInt(con.getHeaderField("interval-millis"));
  99. }
  100. private String getVersion() {
  101. String packageName = pl.getServer().getClass().getPackage().getName();
  102. return packageName.substring(packageName.lastIndexOf('.') + 1);
  103. }
  104. // method source: http://www.jcgonzalez.com/linux-get-distro-from-java-examples
  105. private String getDistro() {
  106. //lists all the files ending with -release in the etc folder
  107. File dir = new File("/etc/");
  108. File fileList[] = new File[0];
  109. if (dir.exists()) {
  110. fileList = dir.listFiles(new FilenameFilter() {
  111. public boolean accept(File dir, String filename) {
  112. return filename.endsWith("-release");
  113. }
  114. });
  115. }
  116. //looks for the version file (not all linux distros)
  117. File fileVersion = new File("/proc/version");
  118. if (fileVersion.exists()) {
  119. fileList = Arrays.copyOf(fileList, fileList.length + 1);
  120. fileList[fileList.length - 1] = fileVersion;
  121. }
  122. //prints first version-related file
  123. for (File f : fileList) {
  124. try {
  125. BufferedReader br = new BufferedReader(new FileReader(f));
  126. String strLine = null;
  127. while ((strLine = br.readLine()) != null) {
  128. return strLine;
  129. }
  130. br.close();
  131. } catch (Exception ignored) {
  132. }
  133. }
  134. return "unknown";
  135. }
  136. }
  137. class Data {
  138. HashMap<String, String> plugs = new HashMap<String, String>();
  139. int onlinePlayers;
  140. String pluginVersion;
  141. public List<String> pluginAuthors;
  142. String serverVersion;
  143. long diskSize;
  144. int coreCnt;
  145. String javaRuntime;
  146. boolean onlineMode;
  147. String osName;
  148. String osArch;
  149. String osVersion;
  150. String linuxDistro;
  151. }