java提取Linux 系统资源

    技术2022-05-13  2

    1. import java.io.*; 2. 3. /** 4. * linux 下cpu 内存 磁盘 jvm的使用监控 5. * @author avery_leo 6. * 7. */ 8. public class TT { 9. /** 10. * 获取cpu使用情况 11. * @return 12. * @throws Exception 13. */ 14. public double getCpuUsage() throws Exception { 15. double cpuUsed = 0; 16. 17. Runtime rt = Runtime.getRuntime(); 18. Process p = rt.exec("top -b -n 1");// 调用系统的“top"命令 19. 20. 21. BufferedReader in = null; 22. try { 23. in = new BufferedReader(new InputStreamReader(p.getInputStream())); 24. String str = null; 25. String[] strArray = null; 26. 27. while ((str = in.readLine()) != null) { 28. int m = 0; 29. 30. if (str.indexOf(" R ") != -1) {// 只分析正在运行的进程,top进程本身除外 && 31. 32. strArray = str.split(" "); 33. for (String tmp : strArray) { 34. if (tmp.trim().length() == 0) 35. continue; 36. if (++m == 9) {// 第9列为CPU的使用百分比(RedHat 37. 38. cpuUsed += Double.parseDouble(tmp); 39. 40. } 41. 42. } 43. 44. } 45. } 46. } catch (Exception e) { 47. e.printStackTrace(); 48. } finally { 49. in.close(); 50. } 51. return cpuUsed; 52. } 53. /** 54. * 内存监控 55. * @return 56. * @throws Exception 57. */ 58. public double getMemUsage() throws Exception { 59. 60. double menUsed = 0; 61. Runtime rt = Runtime.getRuntime(); 62. Process p = rt.exec("top -b -n 1");// 调用系统的“top"命令 63. 64. 65. BufferedReader in = null; 66. try { 67. in = new BufferedReader(new InputStreamReader(p.getInputStream())); 68. String str = null; 69. String[] strArray = null; 70. 71. while ((str = in.readLine()) != null) { 72. int m = 0; 73. 74. if (str.indexOf(" R ") != -1) {// 只分析正在运行的进程,top进程本身除外 && 75. // 76. // System.out.println("------------------3-----------------"); 77. strArray = str.split(" "); 78. for (String tmp : strArray) { 79. if (tmp.trim().length() == 0) 80. continue; 81. 82. if (++m == 10) { 83. // 9)--第10列为mem的使用百分比(RedHat 9) 84. 85. menUsed += Double.parseDouble(tmp); 86. 87. } 88. } 89. 90. } 91. } 92. } catch (Exception e) { 93. e.printStackTrace(); 94. } finally { 95. in.close(); 96. } 97. return menUsed; 98. } 99. 100. /** 101. * 获取磁盘空间大小 102. * 103. * @return 104. * @throws Exception 105. */ 106. public double getDeskUsage() throws Exception { 107. double totalHD = 0; 108. double usedHD = 0; 109. Runtime rt = Runtime.getRuntime(); 110. Process p = rt.exec("df -hl");//df -hl 查看硬盘空间 111. 112. 113. BufferedReader in = null; 114. try { 115. in = new BufferedReader(new InputStreamReader(p.getInputStream())); 116. String str = null; 117. String[] strArray = null; 118. int flag = 0; 119. while ((str = in.readLine()) != null) { 120. int m = 0; 121. // if (flag > 0) { 122. // flag++; 123. strArray = str.split(" "); 124. for (String tmp : strArray) { 125. if (tmp.trim().length() == 0) 126. continue; 127. ++m; 128. // System.out.println("----tmp----" + tmp); 129. if (tmp.indexOf("G") != -1) { 130. if (m == 2) { 131. // System.out.println("---G----" + tmp); 132. if (!tmp.equals("") && !tmp.equals("0")) 133. totalHD += Double.parseDouble(tmp 134. .substring(0, tmp.length() - 1)) * 1024; 135. 136. } 137. if (m == 3) { 138. // System.out.println("---G----" + tmp); 139. if (!tmp.equals("none") && !tmp.equals("0")) 140. usedHD += Double.parseDouble(tmp.substring( 141. 0, tmp.length() - 1)) * 1024; 142. 143. } 144. } 145. if (tmp.indexOf("M") != -1) { 146. if (m == 2) { 147. // System.out.println("---M---" + tmp); 148. if (!tmp.equals("") && !tmp.equals("0")) 149. totalHD += Double.parseDouble(tmp 150. .substring(0, tmp.length() - 1)); 151. 152. } 153. if (m == 3) { 154. // System.out.println("---M---" + tmp); 155. if (!tmp.equals("none") && !tmp.equals("0")) 156. usedHD += Double.parseDouble(tmp.substring( 157. 0, tmp.length() - 1)); 158. // System.out.println("----3----" + usedHD); 159. } 160. } 161. 162. } 163. 164. // } 165. } 166. } catch (Exception e) { 167. e.printStackTrace(); 168. } finally { 169. in.close(); 170. } 171. return (usedHD / totalHD) * 100; 172. } 173. 174. public static void main(String[] args) throws Exception { 175. TT cpu = new TT(); 176. System.out.println("---------------cpu used:" + cpu.getCpuUsage() + "%"); 177. System.out.println("---------------mem used:" + cpu.getMemUsage() + "%"); 178. System.out.println("---------------HD used:" + cpu.getDeskUsage() + "%"); 179. System.out.println("------------jvm监控----------------------"); 180. Runtime lRuntime = Runtime.getRuntime(); 181. System.out.println("--------------Free Momery:" + lRuntime.freeMemory()+"K"); 182. System.out.println("--------------Max Momery:" + lRuntime.maxMemory()+"K"); 183. System.out.println("--------------Total Momery:" + lRuntime.totalMemory()+"K"); 184. System.out.println("---------------Available Processors :" 185. + lRuntime.availableProcessors()); 186. } 187. } _


    最新回复(0)