java获取cpu使用率/内存使用率/硬盘的使用率
import java.io.File;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.List;
import com.sun.management.OperatingSystemMXBean;
/**
* 获取windows系统信息(CPU,内存,文件系统)
*
* @author libing
*
*/
public class WindowsInfoUtil {
private static final int CPUTIME = 500;
private static final int PERCENT = 100;
private static final int FAULTLENGTH = 10;
public static void main(String[] args) {
System.out.println(getCpuRatioForWindows());
System.out.println(getMemery());
System.out.println(getDisk());
}
// 获取内存使用率
public static String getMemery() {
OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory
.getOperatingSystemMXBean();
// 总的物理内存+虚拟内存
long totalvirtualMemory = osmxb.getTotalSwapSpaceSize();
// 剩余的物理内存
long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
Double compare = (Double) (1 - freePhysicalMemorySize * 1.0
/ totalvirtualMemory) * 100;
String str = "内存已使用:" + compare.intValue() + "%";
return str;
}
// 获取文件系统使用率
public static List<String> getDisk() {
// 操作系统
List<String> list = new ArrayList<String>();
for (char c = 'A'; c <= 'Z'; c++) {
String dirName = c + ":/";
File win = new File(dirName);
if (win.exists()) {
long total = (long) win.getTotalSpace();
long free = (long) win.getFreeSpace();
Double compare = (Double) (1 - free * 1.0 / total) * 100;
String str = c + ":盘 已使用 " + compare.intValue() + "%";
list.add(str);
}
}
return list;
}
// 获得cpu使用率
public static String getCpuRatioForWindows() {
try {
String procCmd = System.getenv("windir")
+ "//system32//wbem//wmic.exe process get Caption,CommandLine,KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";
// 取进程信息
long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));
Thread.sleep(CPUTIME);
long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));
if (c0 != null && c1 != null) {
long idletime = c1[0] - c0[0];
long busytime = c1[1] - c0[1];
return "CPU使用率:"
+ Double.valueOf(
PERCENT * (busytime) * 1.0
/ (busytime + idletime)).intValue()
+ "%";
} else {
return "CPU使用率:" + 0 + "%";
}
} catch (Exception ex) {
ex.printStackTrace();
return "CPU使用率:" + 0 + "%";
}
}
// 读取cpu相关信息
private static long[] readCpu(final Process proc) {
long[] retn = new long[2];
try {
proc.getOutputStream().close();
InputStreamReader ir = new InputStreamReader(proc.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line = input.readLine();
if (line == null || line.length() < FAULTLENGTH) {
return null;
}
int capidx = line.indexOf("Caption");
int cmdidx = line.indexOf("CommandLine");
int rocidx = line.indexOf("ReadOperationCount");
int umtidx = line.indexOf("UserModeTime");
int kmtidx = line.indexOf("KernelModeTime");
int wocidx = line.indexOf("WriteOperationCount");
long idletime = 0;
long kneltime = 0;
long usertime = 0;
while ((line = input.readLine()) != null) {
if (line.length() < wocidx) {
continue;
}
// 字段出现顺序:Caption,CommandLine,KernelModeTime,ReadOperationCount,
// ThreadCount,UserModeTime,WriteOperation
String caption = substring(line, capidx, cmdidx - 1).trim();
String cmd = substring(line, cmdidx, kmtidx - 1).trim();
if (cmd.indexOf("wmic.exe") >= 0) {
continue;
}
String s1 = substring(line, kmtidx, rocidx - 1).trim();
String s2 = substring(line, umtidx, wocidx - 1).trim();
if (caption.equals("System Idle Process")
|| caption.equals("System")) {
if (s1.length() > 0)
idletime += Long.valueOf(s1).longValue();
if (s2.length() > 0)
idletime += Long.valueOf(s2).longValue();
continue;
}
if (s1.length() > 0)
kneltime += Long.valueOf(s1).longValue();
if (s2.length() > 0)
usertime += Long.valueOf(s2).longValue();
}
retn[0] = idletime;
retn[1] = kneltime + usertime;
return retn;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
proc.getInputStream().close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
/**
* 由于String.subString对汉字处理存在问题(把一个汉字视为一个字节),因此在 包含汉字的字符串时存在隐患,现调整如下:
*
* @param src
* 要截取的字符串
* @param start_idx
* 开始坐标(包括该坐标)
* @param end_idx
* 截止坐标(包括该坐标)
* @return
*/
private static String substring(String src, int start_idx, int end_idx) {
byte[] b = src.getBytes();
String tgt = "";
for (int i = start_idx; i <= end_idx; i++) {
tgt += (char) b[i];
}
return tgt;
}
}
java获取cpu使用率/内存使用率/硬盘的使用率相关推荐
- linux php cpu,获取Linux服务器性能CPU、内存、硬盘等使用率 PHP
数据库配置文件: conn.php define("MONITORED_IP", "172.16.0.191"); //被监控的服务器IP地址 也就是本机地 ...
- java 收集系统资源_方法:Linux 下用JAVA获取CPU、内存、磁盘的系统资源信息
CPU使用率: InputStream is = null; InputStreamReader isr = null; BufferedReader brStat = null; StringTok ...
- 戴尔服务器r740硬盘指示灯,戴尔R740服务器获取cpu、内存、硬盘参数信息。
戴尔R740服务器获取cpu.内存.硬盘参数信息.使用redfish协议,只使用了system的一个总URL即可获取所有参数. import requests import json requests ...
- r740服务器增加内存,戴尔R740服务器获取cpu、内存、硬盘参数信息。
import requests import json requests.packages.urllib3.disable_warnings() ##使用一个system总的URL分别获取到cpu.内 ...
- php 获取服务器进程数,PHP 获取linux服务器性能CPU、内存、硬盘、进程等使用率...
数据库配置文件: conn.php define("MONITORED_IP", "172.16.0.191"); //被监控的服务器IP地址 也就是本机地址 ...
- php获取linux服务器CPU、内存、硬盘使用率的实现代码
define("MONITORED_IP", "172.16.0.191"); //被监控的服务器IP地址 也就是本机地址 define("DB_SE ...
- Linux小知识---利用Snmp远程获取CPU,内存和磁盘使用率
知识点回顾 <网络协议学习-SNMP> 前面写了一篇Snmp协议的知识点文章,今天要利用这些知识点,再加一些shell编程知识点,实现一个利用shell脚本,远程获取某个Agent的CPU ...
- c#获取系统信息:CPU、内存、硬盘、用户、网络
全栈工程师开发手册 (作者:栾鹏) c#教程全解 c#获取cpu.内存.硬盘.用户.系统等的信息. 另外还包括:系统路径.window路径.cpu的id号.设备硬件卷号.本机MAC地址.邻节点MAC地 ...
- Java如何获取系统cpu、内存、硬盘信息
1 概述 前段时间摸索在Java中怎么获取系统信息包括cpu.内存.硬盘信息等,刚开始使用Java自带的包进行获取,但这样获取的内存信息不够准确并且容易出现找不到相应包等错误,所以后面使用sigar插 ...
- Windows获取CPU、内存和磁盘使用率脚本
获取CPU使用率脚本(vbs),另存为cpu.vbs: On Error Resume Next Set objProc = GetObject("winmgmts:\\.\root\cim ...
最新文章
- 如何通过抓包实战来学习Web协议?
- 绿色经营:从优秀到卓越最显性准则
- NDK开发环境安装,CDT安装,Cygwin安装
- after markup mount - how is converted source code got executed
- numpy 中np.max--求序列的最大值和np.maximum--X和Y逐位进行比较,选择最大值
- CentOs6.x yum源停止维护,安装yum源
- 陷阱:在 WebApp 中谨防 Singleton 错误
- 【android studio】解决android studio drawable新建项目时只有一个drawable目录的问题
- vue 地图使用navigator_初识ABP vNext(6):vue+ABP实现国际化
- UVa815 - Flooded!
- 【同行说技术】Python开发、调试、爬虫类工具大全
- 「山东城商行联盟数据库准实时数据采集系统」入选2021中国大数据应用样板案例
- C/C++学习笔记(2020.11---2021.5)
- python标准库calendar判断年份是闰年和平年
- 小红书SEO之关键词排名优化详解【从入门到精通】
- 基于Verilog HDL与虚拟实验平台的【计算机组成】与CPU实验第三章:三态门和多路器
- 扑克牌游戏——老牛拉破车
- useEffect副作用的使用
- 第十九节 串口通讯与终端设备
- 高校邦java_高校邦Java核心开发技术【实境编程】答案
热门文章
- y空间兑换代码_动态空间面板模型教程(一文读懂动态面板空间spregdpd操作应用)...
- 阿里会成为下一个谷歌?谁才是Google真正的挑战者
- 身份证号码的合法性校验
- FLIR E95 在8层楼看马路上行驶的CAR的热成像形态?
- 被众人拾柴的微信电商 何时能火焰高?
- z山丹培黎学校计算机教材,在山丹培黎学校的往事回忆
- unity塔防游戏怪物转向_萌宠打怪物红包福利版-萌宠打怪物红包版(可提现)下载v1.0...
- UC研发团队——遇见你,不离不弃(10月18日更新版)
- UC研发团队2013春季热招中!(3月5日更新版)
- Office 2007打开文档提示安装Web Developer和MUI解决