使用sigar监控系统和程序状态---简单的程序状态获取

配置参看上一篇文章

import org.hyperic.sigar.*;


public class SystemInfoDetector {
    public SystemInfo detect(){
        Sigar sigar = new Sigar();
        long pid = sigar.getPid();
        SystemInfo systemInfo = new SystemInfo();
        try {
            ProcMem name1 = sigar.getProcMem(pid);
            //app使用内存
            systemInfo.setAppMemUse(name1.getRss());
            //app使用cpu
            systemInfo.setAppCpuUse(sigar.getProcCpu(pid).getPercent());
            //系统内存
            systemInfo.setSystemTotalMem(sigar.getMem().getTotal());
            systemInfo.setSystemMemUse(sigar.getMem().getUsed());
            //cpu
            CpuPerc[] cpuList = sigar.getCpuPercList();
            double cpuUsed = 0;
            for(CpuPerc cpuPerc:cpuList){
                cpuUsed+=cpuPerc.getCombined();
            }
            cpuUsed = cpuUsed/cpuList.length;
            systemInfo.setCpuUse(cpuUsed);
            //硬盘
            String osName = System.getProperties().getProperty("os.name").toLowerCase();
            if(osName.indexOf("window")>=0){
                //windows系统,判断盘符大小
                String cwd = sigar.getProcExe(pid).getCwd();
                char fsName = cwd.charAt(0);
                FileSystemUsage usage = sigar.getFileSystemUsage(fsName+":\\");
                systemInfo.setDiskSize(usage.getTotal());
                systemInfo.setDiskUsedSize(usage.getUsed());
            }else if(osName.indexOf("linux")>=0){
                //linux系统,判断整个大小
                FileSystemUsage usage = sigar.getFileSystemUsage("/");
                systemInfo.setDiskSize(usage.getTotal());
                systemInfo.setDiskUsedSize(usage.getAvail());
            }
        } catch (SigarException e) {
            e.printStackTrace();
        }
        return systemInfo;
    }

    public static void main(String[] args){
        SystemInfoDetector systemInfoDetector = new SystemInfoDetector();
        SystemInfo systemInfo = systemInfoDetector.detect();
        System.out.println(systemInfo);
    }
}
public class SystemInfo {
    private long systemTotalMem;//系统总内存
    private long systemMemUse;//系统内存使用
    private double cpuUse;//当前cpu占用
    private long appMemUse;//本程序内存使用
    private double appCpuUse;//本程序CPU占用
    private long diskSize;//硬盘大小
    private long diskUsedSize;//硬盘已使用

    public long getSystemTotalMem() {
        return systemTotalMem;
    }

    public void setSystemTotalMem(long systemTotalMem) {
        this.systemTotalMem = systemTotalMem;
    }

    public long getSystemMemUse() {
        return systemMemUse;
    }

    public void setSystemMemUse(long systemMemUse) {
        this.systemMemUse = systemMemUse;
    }

    public double getCpuUse() {
        return cpuUse;
    }

    public void setCpuUse(double cpuUse) {
        this.cpuUse = cpuUse;
    }

    public long getAppMemUse() {
        return appMemUse;
    }

    public void setAppMemUse(long appMemUse) {
        this.appMemUse = appMemUse;
    }

    public double getAppCpuUse() {
        return appCpuUse;
    }

    public void setAppCpuUse(double appCpuUse) {
        this.appCpuUse = appCpuUse;
    }

    public long getDiskSize() {
        return diskSize;
    }

    public void setDiskSize(long diskSize) {
        this.diskSize = diskSize;
    }

    public long getDiskUsedSize() {
        return diskUsedSize;
    }

    public void setDiskUsedSize(long diskUsedSize) {
        this.diskUsedSize = diskUsedSize;
    }

    @Override
    public String toString() {
        int m = 1024*1024;
        return "SystemInfo{" +
                "systemTotalMem=" + (long)(systemTotalMem/m) +"M"+
                ", systemMemUse=" + (long)(systemMemUse/m) +"M"+
                ", cpuUse=" + cpuUse +
                ", appMemUse=" + (long)(appMemUse/m) +"M"+
                ", appCpuUse=" + appCpuUse +
                ", diskSize=" + (int)(diskSize/m)+"G" +
                ", diskUsedSize=" + (int)(diskUsedSize/m) +"G" +
                '}';
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值