java系统监控CPU 磁盘
来源:互联网 发布:2017网络购物数据分析 编辑:程序博客网 时间:2024/06/10 06:37
- import java.io.*;
- /**
- * linux 下cpu 内存 磁盘 jvm的使用监控
- * @author avery_leo
- *
- */
- public class TT {
- /**
- * 获取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");//df -hl 查看硬盘空间
- BufferedReader in = null;
- try {
- in = new BufferedReader(new InputStreamReader(p.getInputStream()));
- String str = null;
- String[] strArray = null;
- int flag = 0;
- while ((str = in.readLine()) != null) {
- int m = 0;
- // if (flag > 0) {
- // flag++;
- 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();
- }
- return (usedHD / totalHD) * 100;
- }
- public static void main(String[] args) throws Exception {
- TT cpu = new TT();
- 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());
- }
- }
- java系统监控CPU 磁盘
- 【SHELL】监控系统信息CPU,磁盘,内存
- CPU 内存 磁盘监控
- psutil监控CPU内存磁盘并告警
- python监控windows的cpu,内存,磁盘
- Linux 内存 CPU 磁盘 网络流量的监控
- top监控CPU、内存、磁盘脚本
- python监控脚本,监控CPU,磁盘报警时发送邮件
- 系统服务监控--CPU利用率
- python监控linux系统信息(流量/内存/cpu/负载/磁盘/运转时间) Centos安装X Window和桌面环境
- java Telnet 监控磁盘信息
- Erlang监测系统CPU、内存、磁盘
- Erlang监测系统CPU、内存、磁盘
- 获取系统磁盘、CPU、内存信息
- linux 下cpu 内存 磁盘 jvm的使用监控
- linux 下cpu 内存 磁盘 jvm的使用监控
- linux 下cpu 内存 磁盘 jvm的使用监控
- linux性能监控(cpu 内存 磁盘IO 网络)
- 从西天取经事件看官场潜规则
- configure: error: Can not link to python
- hdu1201 算日子
- Subversion和TortoiseSVN安装使用手记
- Hdu1130:二叉搜索树的数目
- java系统监控CPU 磁盘
- 福布斯:值得关注的中国互联网年轻创业家
- Response.Write 输出 alert CSS走样
- DELPHI 中如何提升进程令牌
- SQL嵌套子查询
- 厘米是英制长度与米制长度换算的基本关系
- 在没有X-Window下工作
- 转载:MFC中的文件读写技术
- 态度