Metrics.java 5.0 KB

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