怎么用java代码读取linux主机的磁盘使用信息,同时截取出文件系统和已使用情况 放在map中可以得到keyvalu

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了怎么用java代码读取linux主机的磁盘使用信息,同时截取出文件系统和已使用情况 放在map中可以得到keyvalu相关的知识,希望对你有一定的参考价值。

文件系统 容量 已用 可用 已用% 挂载点
/dev/sda8 55G 4.6G 47G 9% /
udev 367M 4.0K 367M 1% /dev
tmpfs 150M 848K 149M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 375M 624K 374M 1% /run/shm
none 100M 40K 100M 1% /run/user

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());

参考技术A 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());

参考技术B 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());

本回答被提问者采纳

Linux c 语言怎么方便的读取 cpu,磁盘信息

我附上我的代码给你参考。

CPU占用 需要查看/proc/stat 的信息

磁盘需要 使用statfs这个函数来确认文件所包含的信息。

我附上我的代码给你参考。

我的代码支持CPU使用率(占用率),内存占用率,及磁盘占用率。

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>

#include <sys/vfs.h>
#include <error.h>
#define Gsize (1024.00*1024.00*1024.00)
#define Msize (1024.00*1024.00)

#ifndef EXT2_SUPER_MAGIC
#define EXT2_SUPER_MAGIC 0xef53
#endif

double time_so_far();
float get_cpu_rate();
float get_memory_rate();
float get_disk_rate();

int main(int argc,char *argv[])

    get_cpu_rate();
    get_memory_rate();
    get_disk_rate();
    return 0;

double time_so_far()
    struct timeval tp;
    if(gettimeofday(&tp,(struct timezone *)NULL) == -1)
        perror("gettimeofday");
    return ((double)(tp.tv_sec))+(((double)tp.tv_usec)*0.000001);

float get_cpu_rate()
    FILE *f1;
    double ti,tf;
    char c[10],d[10];
    int t,i1,i2,i3,i4,i5,i6;
    
    ti=time_so_far();
    f1=fopen("/proc/stat","r");
    fscanf(f1,"%s\\t%d\\t%d\\t%d\\n",c,&i1,&i2,&i3);
    fclose(f1);
    printf("%s\\t%d\\t%d\\t%d\\n",c,i1,i2,i3);
    usleep(1000000);

    tf=time_so_far();
    f1=fopen("/proc/stat","r");
    fscanf(f1,"%s\\t%d\\t%d\\t%d\\n",c,&i4,&i5,&i6);
    fclose(f1);
     printf("%s\\t%d\\t%d\\t%d\\n",c,i4,i5,i6);
    t=(i4+i5+i6)-(i1+i2+i3);
    printf("%d\\n",t);
    printf("cpu usage: %.2f%%\\n",( t/((tf-ti)*100) )*100 );


float get_memory_rate()
    FILE *f1;
    int itemp1,itemp2;
    char c[10],d[10];

    f1=fopen("/proc/meminfo","r");
    fscanf(f1,"%s\\t%d\\t%s",c,&itemp1,d);
    printf("memory total is %d Kb\\n",itemp1);
    printf("memory total is %.2f Mb\\n",itemp1/1024.0);
    fscanf(f1,"%s\\t%d\\t%s",c,&itemp2,d);
    printf("memory free is %d Kb\\n",itemp2);
    printf("memory free is %.2f Mb\\n",itemp2/1024.0);
    fclose(f1);
    printf("men usage : %.2f%%\\n",((itemp1-itemp2)*100.0)/itemp1);



float get_disk_rate()
    struct statfs *fs;
    long long blocks,bfree;
    if(statfs("/",fs) != 0)
        
            perror("stafts");
            printf("exit\\n");
            exit(1);
        
    blocks=fs->f_blocks;
    bfree=fs->f_bfree;
    //if(fs.f_type == EXT2_SUPER_MAGIC)
    //
        printf("Disk size of / is %.2f G\\n",blocks*fs->f_bsize/Gsize);
        printf("Free Disk size of / is %.2f G\\n",bfree*fs->f_bsize/Gsize);
        printf("Disk usage of / is %.2f%% \\n",bfree*100.0/blocks);
    //

参考技术A 都在/proc/ 下面 cpu信息在/proc/cpuinfo 启动时间在/proc/uptime 单位是s /proc/stat 里面有cpu执行的时间,用户态,系统态,空闲都有本回答被提问者采纳

以上是关于怎么用java代码读取linux主机的磁盘使用信息,同时截取出文件系统和已使用情况 放在map中可以得到keyvalu的主要内容,如果未能解决你的问题,请参考以下文章

Linux系统下怎么查看应用CPU、内存、负载?

Linux下怎么读取多个进程的信息

Linux 无损扩容磁盘

给linux虚拟机硬盘扩容(LVM分区)

Linux常用命令大全

利用java得到硬盘信息