当前位置:   article > 正文

java 获取服务器CPU、内存、硬盘使用量_java怎么获取服务器物理内存

java怎么获取服务器物理内存

java 获取服务器CPU、内存、硬盘使用量


  1. /**
  2. * <获取系统信息类>
  3. * <获取系统信息,如CPU、内存、硬盘使用情况>
  4. * @author wenkaixuan
  5. * @version [版本号, 2012-5-9]
  6. * @see [相关类/方法]
  7. * @since [产品/模块版本]
  8. */
  9. public class GetSystemInfo
  10. {
  11. private static final int CPUTIME = 500;
  12. private static final int PERCENT = 100;
  13. private static final int FAULTLENGTH = 10;
  14. //获取内存使用率
  15. public static String getMemery()
  16. {
  17. OperatingSystemMXBean osmxb = (OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean();
  18. // 总的物理内存+虚拟内存
  19. long totalvirtualMemory = osmxb.getTotalSwapSpaceSize();
  20. // 剩余的物理内存
  21. long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
  22. Double compare = (Double)(1 - freePhysicalMemorySize * 1.0 / totalvirtualMemory) * 100;
  23. String str = "内存已使用:" + compare.intValue() + "%";
  24. return str;
  25. }
  26. //获取文件系统使用率
  27. public static List<String> getDisk()
  28. {
  29. // 操作系统
  30. List<String> list = new ArrayList<String>();
  31. for (char c = 'A'; c <= 'Z'; c++)
  32. {
  33. String dirName = c + ":/";
  34. File win = new File(dirName);
  35. if (win.exists())
  36. {
  37. long total = (long)win.getTotalSpace();
  38. long free = (long)win.getFreeSpace();
  39. Double compare = (Double)(1 - free * 1.0 / total) * 100;
  40. String str = c + ":盘 已使用 " + compare.intValue() + "%";
  41. list.add(str);
  42. }
  43. }
  44. return list;
  45. }
  46. //获得cpu使用率
  47. public static String getCpuRatioForWindows()
  48. {
  49. try
  50. {
  51. String procCmd =
  52. System.getenv("windir")
  53. + "\\system32\\wbem\\wmic.exe process get Caption,CommandLine,KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";
  54. // 取进程信息
  55. long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));
  56. Thread.sleep(CPUTIME);
  57. long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));
  58. if (c0 != null && c1 != null)
  59. {
  60. long idletime = c1[0] - c0[0];
  61. long busytime = c1[1] - c0[1];
  62. return "CPU使用率:" + Double.valueOf(PERCENT * (busytime) * 1.0 / (busytime + idletime)).intValue() + "%";
  63. }
  64. else
  65. {
  66. return "CPU使用率:" + 0 + "%";
  67. }
  68. }
  69. catch (Exception ex)
  70. {
  71. ex.printStackTrace();
  72. return "CPU使用率:" + 0 + "%";
  73. }
  74. }
  75. //读取cpu相关信息
  76. private static long[] readCpu(final Process proc)
  77. {
  78. long[] retn = new long[2];
  79. try
  80. {
  81. proc.getOutputStream().close();
  82. InputStreamReader ir = new InputStreamReader(proc.getInputStream());
  83. LineNumberReader input = new LineNumberReader(ir);
  84. String line = input.readLine();
  85. if (line == null || line.length() < FAULTLENGTH)
  86. {
  87. return null;
  88. }
  89. int capidx = line.indexOf("Caption");
  90. int cmdidx = line.indexOf("CommandLine");
  91. int rocidx = line.indexOf("ReadOperationCount");
  92. int umtidx = line.indexOf("UserModeTime");
  93. int kmtidx = line.indexOf("KernelModeTime");
  94. int wocidx = line.indexOf("WriteOperationCount");
  95. long idletime = 0;
  96. long kneltime = 0;
  97. long usertime = 0;
  98. while ((line = input.readLine()) != null)
  99. {
  100. if (line.length() < wocidx)
  101. {
  102. continue;
  103. }
  104. // 字段出现顺序:Caption,CommandLine,KernelModeTime,ReadOperationCount,
  105. // ThreadCount,UserModeTime,WriteOperation
  106. String caption = substring(line, capidx, cmdidx - 1).trim();
  107. String cmd = substring(line, cmdidx, kmtidx - 1).trim();
  108. if (cmd.indexOf("wmic.exe") >= 0)
  109. {
  110. continue;
  111. }
  112. String s1 = substring(line, kmtidx, rocidx - 1).trim();
  113. String s2 = substring(line, umtidx, wocidx - 1).trim();
  114. if (caption.equals("System Idle Process") || caption.equals("System"))
  115. {
  116. if (s1.length() > 0)
  117. idletime += Long.valueOf(s1).longValue();
  118. if (s2.length() > 0)
  119. idletime += Long.valueOf(s2).longValue();
  120. continue;
  121. }
  122. if (s1.length() > 0)
  123. kneltime += Long.valueOf(s1).longValue();
  124. if (s2.length() > 0)
  125. usertime += Long.valueOf(s2).longValue();
  126. }
  127. retn[0] = idletime;
  128. retn[1] = kneltime + usertime;
  129. return retn;
  130. }
  131. catch (Exception ex)
  132. {
  133. ex.printStackTrace();
  134. }
  135. finally
  136. {
  137. try
  138. {
  139. proc.getInputStream().close();
  140. }
  141. catch (Exception e)
  142. {
  143. e.printStackTrace();
  144. }
  145. }
  146. return null;
  147. }
  148. /**
  149. * 由于String.subString对汉字处理存在问题(把一个汉字视为一个字节),因此在 包含汉字的字符串时存在隐患,现调整如下:
  150. * @param src 要截取的字符串
  151. * @param start_idx 开始坐标(包括该坐标)
  152. * @param end_idx 截止坐标(包括该坐标)
  153. * @return
  154. */
  155. private static String substring(String src, int start_idx, int end_idx)
  156. {
  157. byte[] b = src.getBytes();
  158. String tgt = "";
  159. for (int i = start_idx; i <= end_idx; i++)
  160. {
  161. tgt += (char)b[i];
  162. }
  163. return tgt;
  164. }
转自:http://lengchaotian.iteye.com/blog/1588961

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/58861
推荐阅读
相关标签
  

闽ICP备14008679号