Metrics.java 5.2 KB

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