java sigar 系统监控

        <dependency>
            <groupId>org</groupId>
            <artifactId>sigar</artifactId>
            <version>1.6.4</version>
        </dependency>
package org.runtime;

import org.hyperic.sigar.*;

import java.util.Date;

import static org.rx.core.Contract.toJsonString;

public final class SystemRuntime {
    public static String formatSize(long size) {
        return Sigar.formatSize(size);
    }

    public static SystemStatusProperty getProperty() throws Exception {
        Sigar sigar = new Sigar();
        SystemStatusProperty status = new SystemStatusProperty();
        status.setDate(new Date());
        CpuInfo cpuInfo = sigar.getCpuInfoList()[0];
        status.setCpuInfo(String.format("%s %s @ %s Threads", cpuInfo.getVendor(), cpuInfo.getModel(), cpuInfo.getTotalCores()));
        status.setCpuCombined(sigar.getCpuPerc().getCombined());
        status.setCpuCombinedPercent(CpuPerc.format(status.getCpuCombined()));
        Mem mem = sigar.getMem();
        status.setMemoryTotal(mem.getTotal());
        status.setMemoryUsed(mem.getActualUsed());
        org.hyperic.sigar.Sigar.formatSize()
//        org.hyperic.sigar.CpuPerc.format()
        long used = 0, total = 0, read = 0, write = 0;
        for (FileSystem fileSystem : sigar.getFileSystemList()) {
            switch (fileSystem.getType()) {
                case FileSystem.TYPE_LOCAL_DISK:
                case FileSystem.TYPE_RAM_DISK:
                    FileSystemUsage usage = sigar.getFileSystemUsage(fileSystem.getDirName());
                    used += usage.getUsed();
                    total += usage.getTotal();
                    read += usage.getDiskReads();
                    write += usage.getDiskWrites();
                    break;
            }
        }
        status.setDiskUsed(used);
        status.setDiskTotal(total);
        status.setDiskReadBytes(read);
        status.setDiskWriteBytes(write);

        NetInterfaceStat netStatLast = find(sigar);
        if (netStatLast != null) {
            Thread.sleep(1000);
            NetInterfaceStat netStatCurrent = find(new Sigar());
            if (netStatCurrent != null) {
                status.setNetInBytes(netStatCurrent.getRxBytes() - netStatLast.getRxBytes());
                status.setNetOutBytes(netStatCurrent.getTxBytes() - netStatLast.getTxBytes());
            }
        }

        sigar.close();
        System.out.println(toJsonString(status));
        return status;
    }

    private static NetInterfaceStat find(Sigar sigar) throws SigarException {
        NetInterfaceStat netStat = null;
        for (String netName : sigar.getNetInterfaceList()) {
            NetInterfaceConfig netConfig = sigar.getNetInterfaceConfig(netName);
            if ((netConfig.getFlags() & 1L) <= 0L) {
//                System.out.println("!IFF_UP...skipping getNetInterfaceStat");
                continue;
            }
            if (NetFlags.LOOPBACK_ADDRESS.equals(netConfig.getAddress())
                    || (netConfig.getFlags() & NetFlags.IFF_LOOPBACK) != 0
                    || NetFlags.NULL_HWADDR.equals(netConfig.getHwaddr())) {
                continue;
            }
//            if (NetFlags.ANY_ADDR.equals(netConfig.getAddress())) {
//                continue;
//            }
//            System.out.println("netConfig:" + JSON.toJSONString(netConfig));
            netStat = sigar.getNetInterfaceStat(netName);
            break;
//            System.out.println("netStat:" + JSON.toJSONString(netStat));
//            System.out.println(Sigar.formatSize(netStat.getRxBytes()));
//            System.out.println();
        }
        return netStat;
    }
}
package org.runtime;

import lombok.Data;

import java.io.Serializable;
import java.util.Date;

@Data
public class SystemStatusProperty implements Serializable {
    private Date date;
    private String cpuInfo;
    private double cpuCombined;
    private String cpuCombinedPercent;
    private long memoryUsed;
    private long memoryTotal;
    private long diskUsed;
    private long diskTotal;
    private long diskReadBytes;
    private long diskWriteBytes;
    private long netInBytes;
    private long netOutBytes;
}

 

上一篇:sigar 1.6.4源码包, 编译


下一篇:Eclipse的详细安装步骤