java获得linux环境的硬盘、内存、cpu使用率
来源:互联网 发布:b2轰炸机知乎 编辑:程序博客网 时间:2024/06/10 16:54
思路:java代码读取linux主机的磁盘使用信息,同时截取出文件系统和已使用情况 放在map中可以得到keyvalue
package com.cmmb.util;import java.io.*;/** * linux 下cpu 内存 磁盘 jvm的使用监控 * @author avery_leo * */ public class DiskSpace { /** * 获取cpu使用情况 * @return * @throws Exception */ public double getcpuUsage() throws Exception { double cpuUsed = 0; Runtime rt = Runtime.getRuntime(); Process p = rt.exec("top -b -n 1");// 调用系统的“top"命令 BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader(p.getInputStream())); String str = null; String[] strArray = null; while ((str = in.readLine()) != null) { int m = 0; if (str.indexOf(" R ") != -1) {// 只分析正在运行的进程,top进程本身除外 && strArray = str.split(" "); for (String tmp : strArray) { if (tmp.trim().length() == 0) continue; if (++m == 9) {// 第9列为cpu的使用百分比(RedHat cpuUsed += Double.parseDouble(tmp); } } } } } catch (Exception e) { e.printStackTrace(); } finally { in.close(); } return cpuUsed; } /** * 内存监控 * @return * @throws Exception */ public double getMemUsage() throws Exception { double menUsed = 0; Runtime rt = Runtime.getRuntime(); Process p = rt.exec("top -b -n 1");// 调用系统的“top"命令 BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader(p.getInputStream())); String str = null; String[] strArray = null; while ((str = in.readLine()) != null) { int m = 0; if (str.indexOf(" R ") != -1) {// 只分析正在运行的进程,top进程本身除外 && // // System.out.println("------------------3-----------------"); strArray = str.split(" "); for (String tmp : strArray) { if (tmp.trim().length() == 0) continue; if (++m == 10) { // 9)--第10列为mem的使用百分比(RedHat 9) menUsed += Double.parseDouble(tmp); } } } } } catch (Exception e) { e.printStackTrace(); } finally { in.close(); } return menUsed; } /** * 获取磁盘空间大小 * * @return * @throws Exception */ public double getDeskUsage() throws Exception { double totalhd = 0; double usedhd = 0; Runtime rt = Runtime.getRuntime(); Process p = rt.exec("df -hl /home");//df -hl 查看硬盘空间 BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader(p.getInputStream())); String str = null; String[] strArray = null; while ((str = in.readLine()) != null) { int m = 0; strArray = str.split(" "); for (String tmp : strArray) { if (tmp.trim().length() == 0) continue; ++m; System.out.println("----tmp----" + tmp); if (tmp.indexOf("G") != -1) { if (m == 2) { System.out.println("---G----" + tmp); if (!tmp.equals("") && !tmp.equals("0")) totalhd += Double.parseDouble(tmp .substring(0, tmp.length() - 1)) * 1024; } if (m == 3) { System.out.println("---G----" + tmp); if (!tmp.equals("none") && !tmp.equals("0")) usedhd += Double.parseDouble(tmp.substring( 0, tmp.length() - 1)) * 1024; } } if (tmp.indexOf("M") != -1) { if (m == 2) { System.out.println("---M---" + tmp); if (!tmp.equals("") && !tmp.equals("0")) totalhd += Double.parseDouble(tmp .substring(0, tmp.length() - 1)); } if (m == 3) { System.out.println("---M---" + tmp); if (!tmp.equals("none") && !tmp.equals("0")) usedhd += Double.parseDouble(tmp.substring( 0, tmp.length() - 1)); System.out.println("----3----" + usedhd); } } } } } catch (Exception e) { e.printStackTrace(); } finally { in.close(); } //上面写在userd和total写反了,懒得改了,就反着用了 System.out.println("----totalhd----" + usedhd); System.out.println("----usedhd----" + totalhd); return (totalhd / usedhd) * 100; } public static void main(String[] args) throws Exception { DiskSpace cpu = new DiskSpace(); System.out.println("---------------cpu used:" + cpu.getcpuUsage() + "%"); System.out.println("---------------mem used:" + cpu.getMemUsage() + "%"); System.out.println("---------------HD used:" + cpu.getDeskUsage() + "%"); System.out.println("------------jvm监控----------------------"); Runtime lRuntime = Runtime.getRuntime(); System.out.println("--------------Free Momery:" + lRuntime.freeMemory()+"K"); System.out.println("--------------Max Momery:" + lRuntime.maxMemory()+"K"); System.out.println("--------------Total Momery:" + lRuntime.totalMemory()+"K"); System.out.println("---------------Available Processors :" + lRuntime.availableProcessors()); } }
0 0
- java获得linux环境的硬盘、内存、cpu使用率
- java获取cpu使用率/内存使用率/硬盘的使用率
- java获取cpu使用率/内存使用率/硬盘的使用率
- java获取cpu使用率/内存使用率/硬盘的使用率
- 用java获得cpu,内存使用率
- Linux 环境下监控进程内存和CPU的使用率
- Python在Linux下获取CPU温度、使用率、内存使用率、硬盘使用率
- java获得CPU使用率,系统内存,虚拟机内存等情况
- java获得CPU使用率,系统内存,虚拟机内存等情况
- java获得CPU使用率,系统内存,虚拟机内存等情况
- java获得CPU使用率,系统内存,虚拟机内存等情况
- java获得CPU使用率,系统内存,虚拟机内存等情况
- java获得CPU使用率,系统内存,虚拟机内存等情况
- java获得CPU使用率,系统内存,虚拟机内存等情况
- java获得CPU使用率,系统内存,虚拟机内存等情况
- Linux环境下用Java获取CPU的使用率
- Linux下怎样查看机器配置,及cpu/内存/硬盘使用率
- Linux下怎样查看机器配置,及cpu/内存/硬盘使用率
- 有返回值的多线程的实现:FutureTask+Callable
- [原]有关Caffe训练好的模型在Python接口下使用分类不准确的问题解决
- 给source insight添加.cc的C++文件后缀识别
- CC2541 晶振
- android动态加载apk
- java获得linux环境的硬盘、内存、cpu使用率
- iOS每日一记之——————炒鸡好用的27个第三方库!!!
- 经典算法题每日演练——第二十一题 十字链表
- Protocol Buffers(protobuf)在Java开发中使用测试
- 【MongoDB for Java】Java操作MongoDB
- 经典算法题每日演练——第二十二题 奇偶排序
- Eclipse调整字体
- SSH整合抛异常:action未发现
- 当阳光洒在肩上