Metrics.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. if (data.osName.equals("Linux")) {
  82. try {
  83. data.linuxDistro = getDistro();
  84. } catch(SecurityException exception) {
  85. // cath this exception
  86. data.linuxDistro = "unknown";
  87. }
  88. }
  89. return gson.toJson(data);
  90. }
  91. private int sendData(String dataJson) throws Exception {
  92. java.net.URL obj = new java.net.URL(URL);
  93. HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
  94. con.setRequestMethod("POST");
  95. con.setRequestProperty("User-Agent", "Java/Bukkit");
  96. con.setRequestProperty("Metrics-Version", this.VERSION);
  97. con.setDoOutput(true);
  98. DataOutputStream wr = new DataOutputStream(con.getOutputStream());
  99. wr.writeBytes(dataJson);
  100. wr.flush();
  101. wr.close();
  102. return Integer.parseInt(con.getHeaderField("interval-millis"));
  103. }
  104. private String getVersion() {
  105. String packageName = pl.getServer().getClass().getPackage().getName();
  106. return packageName.substring(packageName.lastIndexOf('.') + 1);
  107. }
  108. // method source: http://www.jcgonzalez.com/linux-get-distro-from-java-examples
  109. private String getDistro() {
  110. //lists all the files ending with -release in the etc folder
  111. File dir = new File("/etc/");
  112. File fileList[] = new File[0];
  113. if (dir.exists()) {
  114. fileList = dir.listFiles(new FilenameFilter() {
  115. public boolean accept(File dir, String filename) {
  116. return filename.endsWith("-release");
  117. }
  118. });
  119. }
  120. //looks for the version file (not all linux distros)
  121. File fileVersion = new File("/proc/version");
  122. if (fileVersion.exists() && fileList != null) {
  123. fileList = Arrays.copyOf(fileList, fileList.length + 1);
  124. fileList[fileList.length - 1] = fileVersion;
  125. }
  126. if(fileList != null) {
  127. //prints first version-related file
  128. for (File f : fileList) {
  129. try {
  130. BufferedReader br = new BufferedReader(new FileReader(f));
  131. String strLine = null;
  132. while ((strLine = br.readLine()) != null) {
  133. return strLine;
  134. }
  135. br.close();
  136. } catch (Exception ignored) {
  137. }
  138. }
  139. }
  140. return "unknown";
  141. }
  142. }
  143. class Data {
  144. HashMap<String, String> plugs = new HashMap<String, String>();
  145. int onlinePlayers;
  146. String pluginVersion;
  147. public List<String> pluginAuthors;
  148. String serverVersion;
  149. int coreCnt;
  150. String javaRuntime;
  151. boolean onlineMode;
  152. String osName;
  153. String osArch;
  154. String osVersion;
  155. String linuxDistro;
  156. }