• 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;
    }
  • 相关阅读:
    我的Win32开发抉择,Delphi老将复出
    【月入41万】Mono For Android中使用百度地图SDK
    “开源”是什么,能吃吗?
    (转)oracle中用户删除不了,ORA-01940提示 “无法删除当前已连接用户”
    Oracle 去除两边空格
    (转)C#中键值对类型Hashtable与Dictionary比较和相关用法
    (转)对Oracle导出文件错误和DMP文件结构的分析,EXP-00008: 遇到 ORACLE 错误 904 ORA-00904: "MAXSIZE": invalid identifier
    (转)ajax.dll,ajaxpro.dll的区别和用法
    npoi实现 从固定的行读取数据作为表头并返回datable
    字符串格式化测试
  • 原文地址:https://www.cnblogs.com/Googler/p/12006653.html
Copyright © 2020-2023  润新知