1 下载安装sigar-1.6.4.zip

    使用java自带的包获取系统数据,容易找不到包,尤其是内存信息不够准确,所以选择使用sigar获取系统信息。  

    下载地址:http://sourceforge.net/projects/sigar/files/latest/download?source=files

    解压压缩包,将lib下sigar.jar导入eclipse的CLASSPATH中,再将sigar-x86-winnt.dll存入Java的bin目录即可。

2 如果maven项目可以用坐标或者下载下来手动安装

<!-- https://mvnrepository.com/artifact/org.hyperic/sigar -->
<dependency>
    <groupId>org.hyperic</groupId>
    <artifactId>sigar</artifactId>
    <version>1.6.4</version>
</dependency>
安装命令:mvn install:install-file -Dfile=jar包的位置 -DgroupId=上面的groupId -DartifactId=上面的artifactId -Dversion=上面的version -Dpackaging=jar

 便捷安装命令:自己改下jar包的位置:mvn install:install-file -Dfile=C:\Users\Administrator\Desktop\服务器信息\hyperic-sigar-1.6.4\sigar-bin\lib\sigar.jar -DgroupId=org.hyperic -DartifactId=sigar -Dversion=1.6.4 -Dpackaging=jar

3 出现no sigar-x86-winnt.dll in java.library.path 异常

 解压压缩包,将lib下sigar.jar导入eclipse的CLASSPATH中,再将sigar-x86-winnt.dll存入Java的bin目录即可。

4 附上工具类

package com.boc.core.util;

import org.hyperic.sigar.*;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.Properties;

/**
 * @author: 
 * @createTime: 2021/10/14 13:58
 * @company: 
 * @description: pc相关工具类
 */
public class SystemUtil {


    public static Map<String, String> getPCDetail() {
        Map<String, String> map = System.getenv();
        String userName = map.get("USERNAME");// 获取用户名
        String computerName = map.get("COMPUTERNAME");// 获取计算机名
        String userDomain = map.get("USERDOMAIN");// 获取计算机域名
        System.out.println(userName);
        System.out.println(computerName);
        System.out.println(userDomain);
        return map;
    }

    private static void property() throws UnknownHostException {
        Runtime r = Runtime.getRuntime();
        Properties props = System.getProperties();
        InetAddress addr;
        addr = InetAddress.getLocalHost();
        String ip = addr.getHostAddress();
        Map<String, String> map = System.getenv();
        String userName = map.get("USERNAME");// 获取用户名
        String computerName = map.get("COMPUTERNAME");// 获取计算机名
        String userDomain = map.get("USERDOMAIN");// 获取计算机域名
        System.out.println("用户名:    " + userName);
        System.out.println("计算机名:    " + computerName);
        System.out.println("计算机域名:    " + userDomain);
        System.out.println("本地ip地址:    " + ip);
        System.out.println("本地主机名:    " + addr.getHostName());
        System.out.println("JVM可以使用的总内存:    " + r.totalMemory());
        System.out.println("JVM可以使用的剩余内存:    " + r.freeMemory());
        System.out.println("JVM可以使用的处理器个数:    " + r.availableProcessors());
        System.out.println("Java的运行环境版本:    " + props.getProperty("java.version"));
        System.out.println("Java的运行环境供应商:    " + props.getProperty("java.vendor"));
        System.out.println("Java供应商的URL:    " + props.getProperty("java.vendor.url"));
        System.out.println("Java的安装路径:    " + props.getProperty("java.home"));
        System.out.println("Java的虚拟机规范版本:    " + props.getProperty("java.vm.specification.version"));
        System.out.println("Java的虚拟机规范供应商:    " + props.getProperty("java.vm.specification.vendor"));
        System.out.println("Java的虚拟机规范名称:    " + props.getProperty("java.vm.specification.name"));
        System.out.println("Java的虚拟机实现版本:    " + props.getProperty("java.vm.version"));
        System.out.println("Java的虚拟机实现供应商:    " + props.getProperty("java.vm.vendor"));
        System.out.println("Java的虚拟机实现名称:    " + props.getProperty("java.vm.name"));
        System.out.println("Java运行时环境规范版本:    " + props.getProperty("java.specification.version"));
        System.out.println("Java运行时环境规范供应商:    " + props.getProperty("java.specification.vender"));
        System.out.println("Java运行时环境规范名称:    " + props.getProperty("java.specification.name"));
        System.out.println("Java的类格式版本号:    " + props.getProperty("java.class.version"));
        System.out.println("Java的类路径:    " + props.getProperty("java.class.path"));
        System.out.println("加载库时搜索的路径列表:    " + props.getProperty("java.library.path"));
        System.out.println("默认的临时文件路径:    " + props.getProperty("java.io.tmpdir"));
        System.out.println("一个或多个扩展目录的路径:    " + props.getProperty("java.ext.dirs"));
        System.out.println("操作系统的名称:    " + props.getProperty("os.name"));
        System.out.println("操作系统的构架:    " + props.getProperty("os.arch"));
        System.out.println("操作系统的版本:    " + props.getProperty("os.version"));
        System.out.println("文件分隔符:    " + props.getProperty("file.separator"));
        System.out.println("路径分隔符:    " + props.getProperty("path.separator"));
        System.out.println("行分隔符:    " + props.getProperty("line.separator"));
        System.out.println("用户的账户名称:    " + props.getProperty("user.name"));
        System.out.println("用户的主目录:    " + props.getProperty("user.home"));
        System.out.println("用户的当前工作目录:    " + props.getProperty("user.dir"));
    }

    private static void memory() throws SigarException {
        Sigar sigar = new Sigar();
        Mem mem = sigar.getMem();
        // 内存总量
        System.out.println("内存总量:    " + mem.getTotal() / 1024L + "K av");
        // 当前内存使用量
        System.out.println("当前内存使用量:    " + mem.getUsed() / 1024L + "K used");
        // 当前内存剩余量
        System.out.println("当前内存剩余量:    " + mem.getFree() / 1024L + "K free");
        Swap swap = sigar.getSwap();
        // 交换区总量
        System.out.println("交换区总量:    " + swap.getTotal() / 1024L + "K av");
        // 当前交换区使用量
        System.out.println("当前交换区使用量:    " + swap.getUsed() / 1024L + "K used");
        // 当前交换区剩余量
        System.out.println("当前交换区剩余量:    " + swap.getFree() / 1024L + "K free");
    }

    private static void cpu() throws SigarException {
        Sigar sigar = new Sigar();
        CpuInfo infos[] = sigar.getCpuInfoList();
        CpuPerc cpuList[] = null;
        cpuList = sigar.getCpuPercList();
        for (int i = 0; i < infos.length; i++) {// 不管是单块CPU还是多CPU都适用
            CpuInfo info = infos[i];
            System.out.println("第" + (i + 1) + "块CPU信息");
            System.out.println("CPU的总量MHz:    " + info.getMhz());// CPU的总量MHz
            System.out.println("CPU生产商:    " + info.getVendor());// 获得CPU的卖主,如:Intel
            System.out.println("CPU类别:    " + info.getModel());// 获得CPU的类别,如:Celeron
            System.out.println("CPU缓存数量:    " + info.getCacheSize());// 缓冲存储器数量
            printCpuPerc(cpuList[i]);
        }
    }

    private static void printCpuPerc(CpuPerc cpu) {
        System.out.println("CPU用户使用率:    " + CpuPerc.format(cpu.getUser()));// 用户使用率
        System.out.println("CPU系统使用率:    " + CpuPerc.format(cpu.getSys()));// 系统使用率
        System.out.println("CPU当前等待率:    " + CpuPerc.format(cpu.getWait()));// 当前等待率
        System.out.println("CPU当前错误率:    " + CpuPerc.format(cpu.getNice()));//
        System.out.println("CPU当前空闲率:    " + CpuPerc.format(cpu.getIdle()));// 当前空闲率
        System.out.println("CPU总的使用率:    " + CpuPerc.format(cpu.getCombined()));// 总的使用率
    }

    private static void os() {
        OperatingSystem OS = OperatingSystem.getInstance();
        // 操作系统内核类型如: 386、486、586等x86
        System.out.println("操作系统:    " + OS.getArch());
        System.out.println("操作系统CpuEndian():    " + OS.getCpuEndian());//
        System.out.println("操作系统DataModel():    " + OS.getDataModel());//
        // 系统描述
        System.out.println("操作系统的描述:    " + OS.getDescription());
        // 操作系统类型
        // System.out.println("OS.getName():    " + OS.getName());
        // System.out.println("OS.getPatchLevel():    " + OS.getPatchLevel());//
        // 操作系统的卖主
        System.out.println("操作系统的卖主:    " + OS.getVendor());
        // 卖主名称
        System.out.println("操作系统的卖主名:    " + OS.getVendorCodeName());
        // 操作系统名称
        System.out.println("操作系统名称:    " + OS.getVendorName());
        // 操作系统卖主类型
        System.out.println("操作系统卖主类型:    " + OS.getVendorVersion());
        // 操作系统的版本号
        System.out.println("操作系统的版本号:    " + OS.getVersion());
    }

    private static void who() throws SigarException {
        Sigar sigar = new Sigar();
        Who who[] = sigar.getWhoList();
        if (who != null && who.length > 0) {
            for (int i = 0; i < who.length; i++) {
                // System.out.println("当前系统进程表中的用户名" + String.valueOf(i));
                Who _who = who[i];
                System.out.println("用户控制台:    " + _who.getDevice());
                System.out.println("用户host:    " + _who.getHost());
                // System.out.println("getTime():    " + _who.getTime());
                // 当前系统进程表中的用户名
                System.out.println("当前系统进程表中的用户名:    " + _who.getUser());
            }
        }
    }

    private static void file() throws Exception {
        Sigar sigar = new Sigar();
        FileSystem fslist[] = sigar.getFileSystemList();
        for (int i = 0; i < fslist.length; i++) {
            System.out.println("分区的盘符名称" + i);
            FileSystem fs = fslist[i];
            // 分区的盘符名称
            System.out.println("盘符名称:    " + fs.getDevName());
            // 分区的盘符名称
            System.out.println("盘符路径:    " + fs.getDirName());
            System.out.println("盘符标志:    " + fs.getFlags());//
            // 文件系统类型,比如 FAT32、NTFS
            System.out.println("盘符类型:    " + fs.getSysTypeName());
            // 文件系统类型名,比如本地硬盘、光驱、网络文件系统等
            System.out.println("盘符类型名:    " + fs.getTypeName());
            // 文件系统类型
            System.out.println("盘符文件系统类型:    " + fs.getType());
            FileSystemUsage usage = null;
            usage = sigar.getFileSystemUsage(fs.getDirName());
            switch (fs.getType()) {
                case 0: // TYPE_UNKNOWN :未知
                    break;
                case 1: // TYPE_NONE
                    break;
                case 2: // TYPE_LOCAL_DISK : 本地硬盘
                    // 文件系统总大小
                    System.out.println(fs.getDevName() + "总大小:    " + usage.getTotal() + "KB");
                    // 文件系统剩余大小
                    System.out.println(fs.getDevName() + "剩余大小:    " + usage.getFree() + "KB");
                    // 文件系统可用大小
                    System.out.println(fs.getDevName() + "可用大小:    " + usage.getAvail() + "KB");
                    // 文件系统已经使用量
                    System.out.println(fs.getDevName() + "已经使用量:    " + usage.getUsed() + "KB");
                    double usePercent = usage.getUsePercent() * 100D;
                    // 文件系统资源的利用率
                    System.out.println(fs.getDevName() + "资源的利用率:    " + usePercent + "%");
                    break;
                case 3:// TYPE_NETWORK :网络
                    break;
                case 4:// TYPE_RAM_DISK :闪存
                    break;
                case 5:// TYPE_CDROM :光驱
                    break;
                case 6:// TYPE_SWAP :页面交换
                    break;
            }
            System.out.println(fs.getDevName() + "读出:    " + usage.getDiskReads());
            System.out.println(fs.getDevName() + "写入:    " + usage.getDiskWrites());
        }
        return;
    }

    private static void net() throws Exception {
        Sigar sigar = new Sigar();
        String ifNames[] = sigar.getNetInterfaceList();
        for (int i = 0; i < ifNames.length; i++) {
            String name = ifNames[i];
            NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
            System.out.println("网络设备名:    " + name);// 网络设备名
            System.out.println("IP地址:    " + ifconfig.getAddress());// IP地址
            System.out.println("子网掩码:    " + ifconfig.getNetmask());// 子网掩码
            if ((ifconfig.getFlags() & 1L) <= 0L) {
                System.out.println("!IFF_UP...skipping getNetInterfaceStat");
                continue;
            }
            NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);
            System.out.println(name + "接收的总包裹数:" + ifstat.getRxPackets());// 接收的总包裹数
            System.out.println(name + "发送的总包裹数:" + ifstat.getTxPackets());// 发送的总包裹数
            System.out.println(name + "接收到的总字节数:" + ifstat.getRxBytes());// 接收到的总字节数
            System.out.println(name + "发送的总字节数:" + ifstat.getTxBytes());// 发送的总字节数
            System.out.println(name + "接收到的错误包数:" + ifstat.getRxErrors());// 接收到的错误包数
            System.out.println(name + "发送数据包时的错误数:" + ifstat.getTxErrors());// 发送数据包时的错误数
            System.out.println(name + "接收时丢弃的包数:" + ifstat.getRxDropped());// 接收时丢弃的包数
            System.out.println(name + "发送时丢弃的包数:" + ifstat.getTxDropped());// 发送时丢弃的包数
        }
    }

    private static void ethernet() throws SigarException {
        Sigar sigar = null;
        sigar = new Sigar();
        String[] ifaces = sigar.getNetInterfaceList();
        for (int i = 0; i < ifaces.length; i++) {
            NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(ifaces[i]);
            if (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress()) || (cfg.getFlags() & NetFlags.IFF_LOOPBACK) != 0
                    || NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())) {
                continue;
            }
            System.out.println(cfg.getName() + "IP地址:" + cfg.getAddress());// IP地址
            System.out.println(cfg.getName() + "网关广播地址:" + cfg.getBroadcast());// 网关广播地址
            System.out.println(cfg.getName() + "网卡MAC地址:" + cfg.getHwaddr());// 网卡MAC地址
            System.out.println(cfg.getName() + "子网掩码:" + cfg.getNetmask());// 子网掩码
            System.out.println(cfg.getName() + "网卡描述信息:" + cfg.getDescription());// 网卡描述信息
            System.out.println(cfg.getName() + "网卡类型" + cfg.getType());//
        }
    }

    public static void main(String[] args) {
        System.out.println(getPCDetail());
        try {
            // System信息,从jvm获取
            property();
            System.out.println("----------------------------------");
            // cpu信息
            cpu();
            System.out.println("----------------------------------");
            // 内存信息
            memory();
            System.out.println("----------------------------------");
            // 操作系统信息
            os();
            System.out.println("----------------------------------");
            // 用户信息
            who();
            System.out.println("----------------------------------");
            // 文件系统信息
            file();
            System.out.println("----------------------------------");
            // 网络信息
            net();
            System.out.println("----------------------------------");
            // 以太网信息
            ethernet();
            System.out.println("----------------------------------");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
}
package com.boc.core.util;

import java.net.InetAddress;

import java.net.UnknownHostException;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.NetFlags;
import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.NetInterfaceStat;
import org.hyperic.sigar.OperatingSystem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SigarNotImplementedException;
import org.hyperic.sigar.Swap;

/**
 * @author: 
 * @createTime: 2021/10/14 14:11
 * @company: 
 * @description:
 */
public class SystemInfo {
    // 1.CPU资源信息 // a)CPU数量(单位:个)
    public static int getCpuCount() throws SigarException {
        Sigar sigar = new Sigar();
        try {
            return sigar.getCpuInfoList().length;
        } finally {
            sigar.close();
        }
    }
    // b)CPU的总量(单位:HZ)及CPU的相关信息
    public void getCpuTotal() {
        Sigar sigar = new Sigar();
        CpuInfo[] infos;
        try {
            infos = sigar.getCpuInfoList();
            for (int i = 0; i < infos.length; i++) {// 不管是单块CPU还是多CPU都适用
                CpuInfo info = infos[i];
                System.out.println("mhz=" + info.getMhz());// CPU的总量MHz
                System.out.println("vendor=" + info.getVendor());// 获得CPU的卖主,如:Intel
                System.out.println("model=" + info.getModel());// 获得CPU的类别,如:Celeron
                System.out.println("cache size=" + info.getCacheSize());// 缓冲存储器数量
            }
        } catch (SigarException e) {
            e.printStackTrace();
        }
    }
    // c)CPU的用户使用量、系统使用剩余量、总的剩余量、总的使用占用量等(单位:100%)
    public void testCpuPerc() {
        Sigar sigar = new Sigar();
        // 方式一,主要是针对一块CPU的情况
        CpuPerc cpu;
        try {
            cpu = sigar.getCpuPerc();
            printCpuPerc(cpu);
        } catch (SigarException e) {
            e.printStackTrace();
        }
        // 方式二,不管是单块CPU还是多CPU都适用
        CpuPerc cpuList[] = null;
        try {
            cpuList = sigar.getCpuPercList();
        } catch (SigarException e) {
            e.printStackTrace();
            return;
        }
        for (int i = 0; i < cpuList.length; i++) {
            printCpuPerc(cpuList[i]);
        }
    }
    private void printCpuPerc(CpuPerc cpu) {
        System.out.println("User :" + CpuPerc.format(cpu.getUser()));// 用户使用率
        System.out.println("Sys :" + CpuPerc.format(cpu.getSys()));// 系统使用率
        System.out.println("Wait :" + CpuPerc.format(cpu.getWait()));// 当前等待率
        System.out.println("Nice :" + CpuPerc.format(cpu.getNice()));//
        System.out.println("Idle :" + CpuPerc.format(cpu.getIdle()));// 当前空闲率
        System.out.println("Total :" + CpuPerc.format(cpu.getCombined()));// 总的使用率
    }
    // 2.内存资源信息
    public void getPhysicalMemory() {
        // a)物理内存信息
        Sigar sigar = new Sigar();
        Mem mem;
        try {
            mem = sigar.getMem();
            // 内存总量
            System.out.println("Total = " + mem.getTotal() / 1024L + "K av");
            // 当前内存使用量
            System.out.println("Used = " + mem.getUsed() / 1024L + "K used");
            // 当前内存剩余量
            System.out.println("Free = " + mem.getFree() / 1024L + "K free");  // b)系统页面文件交换区信息
            Swap swap = sigar.getSwap();
            // 交换区总量
            System.out.println("Total = " + swap.getTotal() / 1024L + "K av");
            // 当前交换区使用量
            System.out.println("Used = " + swap.getUsed() / 1024L + "K used");
            // 当前交换区剩余量
            System.out.println("Free = " + swap.getFree() / 1024L + "K free");
        } catch (SigarException e) {
            e.printStackTrace();
        }
    } // 3.操作系统信息 // a)取到当前操作系统的名称:
    public String getPlatformName() {
        String hostname = "";
        try {
            hostname = InetAddress.getLocalHost().getHostName();
        } catch (Exception exc) {
            Sigar sigar = new Sigar();
            try {
                hostname = sigar.getNetInfo().getHostName();
            } catch (SigarException e) {
                hostname = "localhost.unknown";
            } finally {
                sigar.close();
            }
        }
        return hostname;
    }
    // b)取当前操作系统的信息
    public void testGetOSInfo() {
        OperatingSystem OS = OperatingSystem.getInstance();
        // 操作系统内核类型如: 386、486、586等x86
        System.out.println("OS.getArch() = " + OS.getArch());
        System.out.println("OS.getCpuEndian() = " + OS.getCpuEndian());//
        System.out.println("OS.getDataModel() = " + OS.getDataModel());//
        // 系统描述
        System.out.println("OS.getDescription() = " + OS.getDescription());
        System.out.println("OS.getMachine() = " + OS.getMachine());//
        // 操作系统类型
        System.out.println("OS.getName() = " + OS.getName());
        System.out.println("OS.getPatchLevel() = " + OS.getPatchLevel());//
        // 操作系统的卖主
        System.out.println("OS.getVendor() = " + OS.getVendor());
        // 卖主名称
        System.out.println("OS.getVendorCodeName() = " + OS.getVendorCodeName());
        // 操作系统名称
        System.out.println("OS.getVendorName() = " + OS.getVendorName());
        // 操作系统卖主类型
        System.out.println("OS.getVendorVersion() = " + OS.getVendorVersion());
        // 操作系统的版本号
        System.out.println("OS.getVersion() = " + OS.getVersion());
    }
    // c)取当前系统进程表中的用户信息
    public void testWho() {
        try {
            Sigar sigar = new Sigar();
            org.hyperic.sigar.Who[] who = sigar.getWhoList();
            if (who != null && who.length > 0) {
                for (int i = 0; i < who.length; i++) {
                    System.out.println("\n~~~~~~~~~" + String.valueOf(i)+ "~~~~~~~~~");
                    org.hyperic.sigar.Who _who = who[i];
                    System.out.println("getDevice() = " + _who.getDevice());
                    System.out.println("getHost() = " + _who.getHost());
                    System.out.println("getTime() = " + _who.getTime());
                    // 当前系统进程表中的用户名
                    System.out.println("getUser() = " + _who.getUser());
                }
            }
        } catch (SigarException e) {
            e.printStackTrace();
        }
    }
    // 4.资源信息(主要是硬盘) // a)取硬盘已有的分区及其详细信息(通过sigar.getFileSystemList()来获得FileSystem列表对象,然后对其进行编历):
    public void testFileSystemInfo() throws Exception {
        Sigar sigar = new Sigar();
        FileSystem fslist[] = sigar.getFileSystemList();
        //String dir = System.getProperty("user.home");// 当前用户文件夹路径
        for (int i = 0; i < fslist.length; i++) {
            System.out.println("\n~~~~~~~~~~" + i + "~~~~~~~~~~");
            FileSystem fs = fslist[i];
            // 分区的盘符名称
            System.out.println("fs.getDevName() = " + fs.getDevName());
            // 分区的盘符名称
            System.out.println("fs.getDirName() = " + fs.getDirName());
            System.out.println("fs.getFlags() = " + fs.getFlags());//
            // 文件系统类型,比如 FAT32、NTFS
            System.out.println("fs.getSysTypeName() = " + fs.getSysTypeName());
            // 文件系统类型名,比如本地硬盘、光驱、网络文件系统等
            System.out.println("fs.getTypeName() = " + fs.getTypeName());
            // 文件系统类型
            System.out.println("fs.getType() = " + fs.getType());
            FileSystemUsage usage = null;
            try {
                usage = sigar.getFileSystemUsage(fs.getDirName());
            } catch (SigarException e) {
                if (fs.getType() == 2)
                    throw e;
                continue;
            }
            switch (fs.getType()) {
                case 0: // TYPE_UNKNOWN :未知
                    break;
                case 1: // TYPE_NONE
                    break;
                case 2: // TYPE_LOCAL_DISK : 本地硬盘
                    // 文件系统总大小
                    System.out.println(" Total = " + usage.getTotal() + "KB");
                    // 文件系统剩余大小
                    System.out.println(" Free = " + usage.getFree() + "KB");
                    // 文件系统可用大小
                    System.out.println(" Avail = " + usage.getAvail() + "KB");
                    // 文件系统已经使用量
                    System.out.println(" Used = " + usage.getUsed() + "KB");
                    double usePercent = usage.getUsePercent() * 100D;
                    // 文件系统资源的利用率
                    System.out.println(" Usage = " + usePercent + "%");
                    break;
                case 3:// TYPE_NETWORK :网络
                    break;
                case 4:// TYPE_RAM_DISK :闪存
                    break;
                case 5:// TYPE_CDROM :光驱
                    break;
                case 6:// TYPE_SWAP :页面交换
                    break;
            }
            System.out.println(" DiskReads = " + usage.getDiskReads());
            System.out.println(" DiskWrites = " + usage.getDiskWrites());
        }
        return;
    }
    // 5.网络信息 // a)当前机器的正式域名
    public String getFQDN() {
        Sigar sigar = null;
        try {
            return InetAddress.getLocalHost().getCanonicalHostName();
        } catch (UnknownHostException e) {
            try {
                sigar = new Sigar();
                return sigar.getFQDN();
            } catch (SigarException ex) {
                return null;
            } finally {
                sigar.close();
            }
        }
    } // b)取到当前机器的IP地址
    public String getDefaultIpAddress() {
        String address = null;
        try {
            address = InetAddress.getLocalHost().getHostAddress();
            // 没有出现异常而正常当取到的IP时,如果取到的不是网卡循回地址时就返回
            // 否则再通过Sigar工具包中的方法来获取
            if (!NetFlags.LOOPBACK_ADDRESS.equals(address)) {
                return address;
            }
        } catch (UnknownHostException e) {
            // hostname not in DNS or /etc/hosts
        }
        Sigar sigar = new Sigar();
        try {
            address = sigar.getNetInterfaceConfig().getAddress();
        } catch (SigarException e) {
            address = NetFlags.LOOPBACK_ADDRESS;
        } finally {
            sigar.close();
        }
        return address;
    } // c)取到当前机器的MAC地址
    public String getMAC() {
        Sigar sigar = null;
        try {
            sigar = new Sigar();
            String[] ifaces = sigar.getNetInterfaceList();
            String hwaddr = null;
            for (int i = 0; i < ifaces.length; i++) {
                NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(ifaces[i]);
                if (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress()) || (cfg.getFlags() & NetFlags.IFF_LOOPBACK) != 0 || NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())) {
                    continue;
                }
                /*
                 * 如果存在多张网卡包括虚拟机的网卡,默认只取第一张网卡的MAC地址,如果要返回所有的网卡(包括物理的和虚拟的)则可以修改方法的返回类型为数组或Collection
                 * ,通过在for循环里取到的多个MAC地址。
                 */
                hwaddr = cfg.getHwaddr();
                break;
            }
            return hwaddr != null?hwaddr : null;
        } catch (Exception e) {
            return null;
        } finally {
            if (sigar != null)
                sigar.close();
        }
    }
    // d)获取网络流量等信息
    public void testNetIfList() throws Exception {
        Sigar sigar = new Sigar();
        String ifNames[] = sigar.getNetInterfaceList();
        for (int i = 0; i < ifNames.length; i++) {
            String name = ifNames[i];
            NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
            print("\nname = " + name);// 网络设备名
            print("Address = " + ifconfig.getAddress());// IP地址
            print("Netmask = " + ifconfig.getNetmask());// 子网掩码
            if ((ifconfig.getFlags() & 1L) <= 0L) {
                print("!IFF_UP...skipping getNetInterfaceStat");
                continue;
            }
            try {
                NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);
                print("RxPackets = " + ifstat.getRxPackets());// 接收的总包裹数
                print("TxPackets = " + ifstat.getTxPackets());// 发送的总包裹数
                print("RxBytes = " + ifstat.getRxBytes());// 接收到的总字节数
                print("TxBytes = " + ifstat.getTxBytes());// 发送的总字节数
                print("RxErrors = " + ifstat.getRxErrors());// 接收到的错误包数
                print("TxErrors = " + ifstat.getTxErrors());// 发送数据包时的错误数
                print("RxDropped = " + ifstat.getRxDropped());// 接收时丢弃的包数
                print("TxDropped = " + ifstat.getTxDropped());// 发送时丢弃的包数
            } catch (SigarNotImplementedException e) {
            } catch (SigarException e) {
                print(e.getMessage());
            }
        }
    } void print(String msg) {
        System.out.println(msg);
    }
    // e)一些其他的信息
    public void getEthernetInfo() {
        Sigar sigar = null;
        try {
            sigar = new Sigar();
            String[] ifaces = sigar.getNetInterfaceList();
            for (int i = 0; i < ifaces.length; i++) {
                NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(ifaces[i]);
                if (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress()) || (cfg.getFlags() & NetFlags.IFF_LOOPBACK) != 0 || NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())) {
                    continue;
                }
                System.out.println("cfg.getAddress() = " + cfg.getAddress());// IP地址
                System.out.println("cfg.getBroadcast() = " + cfg.getBroadcast());// 网关广播地址
                System.out.println("cfg.getHwaddr() = " + cfg.getHwaddr());// 网卡MAC地址
                System.out.println("cfg.getNetmask() = " + cfg.getNetmask());// 子网掩码
                System.out.println("cfg.getDescription() = " + cfg.getDescription());// 网卡描述信息
                System.out.println("cfg.getType() = " + cfg.getType());//
                System.out.println("cfg.getDestination() = " + cfg.getDestination());
                System.out.println("cfg.getFlags() = " + cfg.getFlags());//
                System.out.println("cfg.getMetric() = " + cfg.getMetric());
                System.out.println("cfg.getMtu() = " + cfg.getMtu());
                System.out.println("cfg.getName() = " + cfg.getName());
                System.out.println();
            }
        } catch (Exception e) {
            System.out.println("Error while creating GUID" + e);
        } finally {
            if (sigar != null)
                sigar.close();
        }
    }
    public static void main(String[] args) throws SigarException{
        SystemInfo s = new SystemInfo();
//		s.getCpuTotal();
//		s.getEthernetInfo();
//		s.getDefaultIpAddress();
        s.testGetOSInfo();
//		s.getPhysicalMemory();
        System.out.println(getCpuCount());
    }

}