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

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer; import org.apache.commons.io.FileSystemUtils; public class OSUtils { /**
* 功能:可用磁盘
* */
public static int disk() {
try {
long total = FileSystemUtils.freeSpaceKb("/home");
double disk = (double) total / 1024 / 1024;
return (int) disk;
} catch (IOException e) {
e.printStackTrace();
}
return 0;
} /**
* 功能:获取Linux系统cpu使用率
* */
public static int cpuUsage() {
try {
Map<?, ?> map1 = OSUtils.cpuinfo();
Thread.sleep(5 * 1000);
Map<?, ?> map2 = OSUtils.cpuinfo(); long user1 = Long.parseLong(map1.get("user").toString());
long nice1 = Long.parseLong(map1.get("nice").toString());
long system1 = Long.parseLong(map1.get("system").toString());
long idle1 = Long.parseLong(map1.get("idle").toString()); long user2 = Long.parseLong(map2.get("user").toString());
long nice2 = Long.parseLong(map2.get("nice").toString());
long system2 = Long.parseLong(map2.get("system").toString());
long idle2 = Long.parseLong(map2.get("idle").toString()); long total1 = user1 + system1 + nice1;
long total2 = user2 + system2 + nice2;
float total = total2 - total1; long totalIdle1 = user1 + nice1 + system1 + idle1;
long totalIdle2 = user2 + nice2 + system2 + idle2;
float totalidle = totalIdle2 - totalIdle1; float cpusage = (total / totalidle) * 100;
return (int) cpusage;
} catch (InterruptedException e) {
e.printStackTrace();
}
return 0;
} /**
* 功能:CPU使用信息
* */
public static Map<?, ?> cpuinfo() {
InputStreamReader inputs = null;
BufferedReader buffer = null;
Map<String, Object> map = new HashMap<String, Object>();
try {
inputs = new InputStreamReader(new FileInputStream("/proc/stat"));
buffer = new BufferedReader(inputs);
String line = "";
while (true) {
line = buffer.readLine();
if (line == null) {
break;
}
if (line.startsWith("cpu")) {
StringTokenizer tokenizer = new StringTokenizer(line);
List<String> temp = new ArrayList<String>();
while (tokenizer.hasMoreElements()) {
String value = tokenizer.nextToken();
temp.add(value);
}
map.put("user", temp.get(1));
map.put("nice", temp.get(2));
map.put("system", temp.get(3));
map.put("idle", temp.get(4));
map.put("iowait", temp.get(5));
map.put("irq", temp.get(6));
map.put("softirq", temp.get(7));
map.put("stealstolen", temp.get(8));
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
buffer.close();
inputs.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return map;
} /**
* 功能:内存使用率
* */
public static int memoryUsage() {
Map<String, Object> map = new HashMap<String, Object>();
InputStreamReader inputs = null;
BufferedReader buffer = null;
try {
inputs = new InputStreamReader(new FileInputStream("/proc/meminfo"));
buffer = new BufferedReader(inputs);
String line = "";
while (true) {
line = buffer.readLine();
if (line == null)
break;
int beginIndex = 0;
int endIndex = line.indexOf(":");
if (endIndex != -1) {
String key = line.substring(beginIndex, endIndex);
beginIndex = endIndex + 1;
endIndex = line.length();
String memory = line.substring(beginIndex, endIndex);
String value = memory.replace("kB", "").trim();
map.put(key, value);
}
} long memTotal = Long.parseLong(map.get("MemTotal").toString());
long memFree = Long.parseLong(map.get("MemFree").toString());
long memused = memTotal - memFree;
long buffers = Long.parseLong(map.get("Buffers").toString());
long cached = Long.parseLong(map.get("Cached").toString()); double usage = (double) (memused - buffers - cached) / memTotal * 100;
return (int) usage;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
buffer.close();
inputs.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return 0;
}
}

最新文章

  1. jsp登入oracle
  2. c# ContinueWith 用法
  3. Angular动态编译Html
  4. CentOS搭建NodeJS环境
  5. Sql Server xml 类型字段的增删改查
  6. codeforces 733D
  7. 两系统用asp.net forms 身份验证方式实现跨域登录信息共享
  8. PHP实现微信公众账号开发
  9. iOS Auto Layout
  10. so baby come on~~
  11. sql join 用法
  12. node配置运行环境变量;
  13. Redhat Linux 自动修改密码
  14. Visual paradigm软件介绍
  15. Struts2 之 Action 类访问 WEB 资源
  16. windows下使用 Secure Shell Client工具操作linux常用命令
  17. vue 中的通过搜索框进行数据过滤的过程
  18. [Swift]LeetCode86. 分隔链表 | Partition List
  19. linux与C内存管理机制
  20. Graph Database &amp; 图形数据库

热门文章

  1. EXCEL Skills Commonly Used
  2. Python3简明教程(六)—— 数据结构
  3. unix网络编程-配置unp.h头文件
  4. 解决margin塌陷问题
  5. 小程序08 小程序访问服务器API
  6. 总结Java开发者经常会犯的前十种错误
  7. DB2表空间
  8. scp免密码拷贝和ssh免密码登录
  9. perl学习之:package and module
  10. 条款4:确定对象被使用前已被初始化(Make sure that objects are initialized before they&#39;re used)