原文:http://www.open-open.com/code/view/1426152165201

  1. import java.io.BufferedReader;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.StringTokenizer;
  10.  
  11. import org.apache.commons.io.FileSystemUtils;
  12.  
  13. public class OSUtils {
  14.  
  15. /**
  16. * 功能:可用磁盘
  17. * */
  18. public static int disk() {
  19. try {
  20. long total = FileSystemUtils.freeSpaceKb("/home");
  21. double disk = (double) total / 1024 / 1024;
  22. return (int) disk;
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. return 0;
  27. }
  28.  
  29. /**
  30. * 功能:获取Linux系统cpu使用率
  31. * */
  32. public static int cpuUsage() {
  33. try {
  34. Map<?, ?> map1 = OSUtils.cpuinfo();
  35. Thread.sleep(5 * 1000);
  36. Map<?, ?> map2 = OSUtils.cpuinfo();
  37.  
  38. long user1 = Long.parseLong(map1.get("user").toString());
  39. long nice1 = Long.parseLong(map1.get("nice").toString());
  40. long system1 = Long.parseLong(map1.get("system").toString());
  41. long idle1 = Long.parseLong(map1.get("idle").toString());
  42.  
  43. long user2 = Long.parseLong(map2.get("user").toString());
  44. long nice2 = Long.parseLong(map2.get("nice").toString());
  45. long system2 = Long.parseLong(map2.get("system").toString());
  46. long idle2 = Long.parseLong(map2.get("idle").toString());
  47.  
  48. long total1 = user1 + system1 + nice1;
  49. long total2 = user2 + system2 + nice2;
  50. float total = total2 - total1;
  51.  
  52. long totalIdle1 = user1 + nice1 + system1 + idle1;
  53. long totalIdle2 = user2 + nice2 + system2 + idle2;
  54. float totalidle = totalIdle2 - totalIdle1;
  55.  
  56. float cpusage = (total / totalidle) * 100;
  57. return (int) cpusage;
  58. } catch (InterruptedException e) {
  59. e.printStackTrace();
  60. }
  61. return 0;
  62. }
  63.  
  64. /**
  65. * 功能:CPU使用信息
  66. * */
  67. public static Map<?, ?> cpuinfo() {
  68. InputStreamReader inputs = null;
  69. BufferedReader buffer = null;
  70. Map<String, Object> map = new HashMap<String, Object>();
  71. try {
  72. inputs = new InputStreamReader(new FileInputStream("/proc/stat"));
  73. buffer = new BufferedReader(inputs);
  74. String line = "";
  75. while (true) {
  76. line = buffer.readLine();
  77. if (line == null) {
  78. break;
  79. }
  80. if (line.startsWith("cpu")) {
  81. StringTokenizer tokenizer = new StringTokenizer(line);
  82. List<String> temp = new ArrayList<String>();
  83. while (tokenizer.hasMoreElements()) {
  84. String value = tokenizer.nextToken();
  85. temp.add(value);
  86. }
  87. map.put("user", temp.get(1));
  88. map.put("nice", temp.get(2));
  89. map.put("system", temp.get(3));
  90. map.put("idle", temp.get(4));
  91. map.put("iowait", temp.get(5));
  92. map.put("irq", temp.get(6));
  93. map.put("softirq", temp.get(7));
  94. map.put("stealstolen", temp.get(8));
  95. break;
  96. }
  97. }
  98. } catch (Exception e) {
  99. e.printStackTrace();
  100. } finally {
  101. try {
  102. buffer.close();
  103. inputs.close();
  104. } catch (Exception e2) {
  105. e2.printStackTrace();
  106. }
  107. }
  108. return map;
  109. }
  110.  
  111. /**
  112. * 功能:内存使用率
  113. * */
  114. public static int memoryUsage() {
  115. Map<String, Object> map = new HashMap<String, Object>();
  116. InputStreamReader inputs = null;
  117. BufferedReader buffer = null;
  118. try {
  119. inputs = new InputStreamReader(new FileInputStream("/proc/meminfo"));
  120. buffer = new BufferedReader(inputs);
  121. String line = "";
  122. while (true) {
  123. line = buffer.readLine();
  124. if (line == null)
  125. break;
  126. int beginIndex = 0;
  127. int endIndex = line.indexOf(":");
  128. if (endIndex != -1) {
  129. String key = line.substring(beginIndex, endIndex);
  130. beginIndex = endIndex + 1;
  131. endIndex = line.length();
  132. String memory = line.substring(beginIndex, endIndex);
  133. String value = memory.replace("kB", "").trim();
  134. map.put(key, value);
  135. }
  136. }
  137.  
  138. long memTotal = Long.parseLong(map.get("MemTotal").toString());
  139. long memFree = Long.parseLong(map.get("MemFree").toString());
  140. long memused = memTotal - memFree;
  141. long buffers = Long.parseLong(map.get("Buffers").toString());
  142. long cached = Long.parseLong(map.get("Cached").toString());
  143.  
  144. double usage = (double) (memused - buffers - cached) / memTotal * 100;
  145. return (int) usage;
  146. } catch (Exception e) {
  147. e.printStackTrace();
  148. } finally {
  149. try {
  150. buffer.close();
  151. inputs.close();
  152. } catch (Exception e2) {
  153. e2.printStackTrace();
  154. }
  155. }
  156. return 0;
  157. }
  158. }

Java获取Linux系统cpu使用率的更多相关文章

  1. C/C++获取Linux系统CPU和内存及硬盘使用情况

    需求分析: 不使用Top  df  free 等命令,利用C/C++获取Linux系统CPU和内存及硬盘使用情况 实现: //通过获取/proc/stat (CPU)和/proc/meminfo(内存 ...

  2. JAVA如何利用Swiger获取Linux系统电脑配置相关信息

    最近开发java应用程序,涉及到获取Linux服务器相关配置的问题,特地网上搜寻了下,采用Swiger包可以直接获取,再次小结一下,以便于以后能方便使用,也便于其他童鞋们学习. 推荐大家参考链接:ht ...

  3. Java获取Linux和Window系统CPU、内存和磁盘总使用率的情况

    这是一个工具类,获取的内容: CPU使用率:得到的是当前CPU的使用情况,这是算出的是两次500毫秒时间差的CPU使用率 内存使用率:[1 -  剩余的物理内存/(总的物理内存+虚拟内存) ] * 1 ...

  4. java 获取系统信息及CPU的使用率(转)

    java 获取系统信息及CPU的使用率 原文:http://kakaluyi.javaeye.com/blog/211492 最近做个项目,就是要取得cpu占有率等等的系统信息,一开始以为要用动态链接 ...

  5. 关于linux系统CPU篇--->CPU使用率升高

    1.CPU使用率为单位时间内CPU使用情况的统计,以百分比的方式展示. LINUX作为一个多任务操作系统,将每个CPU的时间划分为很短的时间片,再通过调度器轮流分配给各个任务使用,因此造成多任务同时运 ...

  6. linux系统CPU,内存,磁盘,网络流量监控脚本

    前序 1,#cat /proc/stat/ 信息包含了所有CPU活动的信息,该文件中的所有值都是从系统启动开始累积到当前时刻 2,#vmstat –s 或者#vmstat 虚拟内存统计 3, #cat ...

  7. 关于linux系统CPU篇--->上下文切换

    1.什么是CPU上下文切换? linux是一个多任务操作系统,它支持远大于CPU数量的任务同时运行,当然这些任务实际上并不是真的同时在运行,而是因为系统在很短的时间内,将CPU轮流分配给它们,造成多任 ...

  8. Linux系统CPU的性能监控及调优

    前言: 性能优化是一个老生常谈的话题,典型的性能问题如页面响应慢.接口超时,服务器负载高.并发数低,数据库频繁死锁等.尤其是在“糙快猛”的互联网开发模式大行其道的今天,随着系统访问量的日益增加和代码的 ...

  9. centOs 查看系统cpu使用率等--top

    原文:centOs 查看系统cpu使用率等--top 在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应信息分析系统状况的需要.在 CentOS 中,可以通过 top 命令来查看 CP ...

随机推荐

  1. python struct.pack方法报错argument for 's' must be a bytes object 解决

    参考 https://blog.csdn.net/weixin_38383877/article/details/81100192 在python3下使用struct模块代码 fileHead = s ...

  2. 关于apache access log 统计的那些事儿

    统计APACHE ACCESS.LOG IP访问记录 可以根据自己的需要,统计很多,每个IP访问多少个页面等等! cat access.log-20090904 |awk '{print $3}'|s ...

  3. 485. Max Consecutive Ones@python

    Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...

  4. [JOYOI] 1415 西瓜种植

    题目描述 笨笨种了一块西瓜地,但这块西瓜地的种植范围是一条直线的-- 笨笨在一番研究过后,得出了m个结论,这m个结论可以使他收获的西瓜最多. 笨笨的结论是这样的: 从西瓜地B处到E处至少要种植T个西瓜 ...

  5. 我的Python分析成长之路1

    Python是什么?                                                                                           ...

  6. 转载 js弹出框、对话框、提示框、弹窗总结

    转载:https://blog.csdn.net/huileiforever/article/details/9464659 一.JS的三种最常见的对话框   //================== ...

  7. 【06】sass编译工具(弃)

    [06]编译工具(弃) SASS转译工具 除了使用sass命令来转译SASS文件之外,还可以借助第三方工具完成,目前世面上较为流行的转译工具主要有: Compass.app Scout Codekit ...

  8. luogu2485 [SDOI2011]计算器 poj3243 Clever Y BSGS算法

    BSGS 算法,即 Baby Step,Giant Step 算法.拔山盖世算法. 计算 \(a^x \equiv b \pmod p\). \(p\)为质数时 特判掉 \(a,p\) 不互质的情况. ...

  9. luogu3302 [SDOI2013]森林

    前置技能:Count on a tree 然后带上一个启发式合并 #include <algorithm> #include <iostream> #include <c ...

  10. 大数据学习——mapreduce案例join算法

    需求: 用mapreduce实现select order.orderid,order.pdtid,pdts.pdt_name,oder.amount from orderjoin pdtson ord ...