<!-- https://mvnrepository.com/artifact/org.hyperic/sigar -->
<dependency>
<groupId>org.hyperic</groupId>
<artifactId>sigar</artifactId>
<version>1.6.4</version>
</dependency>
copy from : https://my.oschina.net/mkh/blog/312911
import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Map; import java.util.Properties; 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.Swap; import org.hyperic.sigar.Who; public class RuntimeTest { public static void main(String[] args) { try { // System information, obtained from jvm property(); System.out.println("----------------------------------"); // cpu information cpu(); System.out.println("----------------------------------"); // memory information memory(); System.out.println("----------------------------------"); // OS info you(); System.out.println("----------------------------------"); // User Info who(); System.out.println("----------------------------------"); // file system information file(); System.out.println("----------------------------------"); // Internet Information net(); System.out.println("----------------------------------"); // ethernet info ethernet(); System.out.println("----------------------------------"); } catch (Exception e1) { e1.printStackTrace(); } } 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");// Get the username String computerName = map.get("COMPUTERNAME");// Get the computer name String userDomain = map.get("USERDOMAIN");// Get the computer domain name System.out.println("Username: " + userName); System.out.println("Computer name: " + computerName); System.out.println("Computer domain name: " + userDomain); System.out.println("local ip address: " + ip); System.out.println("Local hostname: " + addr.getHostName()); System.out.println("Total memory available to JVM: " + r.totalMemory()); System.out.println("Remaining memory available to JVM: " + r.freeMemory()); System.out.println("Number of processors available to JVM: " + r.availableProcessors()); System.out.println("Java runtime environment version: " + props.getProperty("java.version")); System.out.println("Java runtime environment vendor: " + props.getProperty("java.vendor")); System.out.println("Java vendor URL: " + props.getProperty("java.vendor.url")); System.out.println("Java installation path: " + props.getProperty("java.home")); System.out.println("Java's virtual machine specification version: " + props.getProperty("java.vm.specification.version")); System.out.println("Java virtual machine specification vendor: " + props.getProperty("java.vm.specification.vendor")); System.out.println("Java's virtual machine specification name: " + props.getProperty("java.vm.specification.name")); System.out.println("Java's virtual machine implementation version: " + props.getProperty("java.vm.version")); System.out.println("Java virtual machine implementation vendor: " + props.getProperty("java.vm.vendor")); System.out.println("Java's virtual machine implementation name: " + props.getProperty("java.vm.name")); System.out.println("Java runtime environment specification version: " + props.getProperty("java.specification.version")); System.out.println("Java runtime environment specification vendor: " + props.getProperty("java.specification.vender")); System.out.println("Java runtime environment specification name: " + props.getProperty("java.specification.name")); System.out.println("Java class format version number: " + props.getProperty("java.class.version")); System.out.println("Java's classpath: " + props.getProperty("java.class.path")); System.out.println("List of paths searched when loading libraries: " + props.getProperty("java.library.path")); System.out.println("Default temporary file path: " + props.getProperty("java.io.tmpdir")); System.out.println("Path to one or more extension directories: " + props.getProperty("java.ext.dirs")); System.out.println("The name of the operating system: " + props.getProperty("os.name")); System.out.println("The architecture of the operating system: " + props.getProperty("os.arch")); System.out.println("OS version: " + props.getProperty("os.version")); System.out.println("File separator: " + props.getProperty("file.separator")); System.out.println("Path separator: " + props.getProperty("path.separator")); System.out.println("Line separator: " + props.getProperty("line.separator")); System.out.println("User's account name: " + props.getProperty("user.name")); System.out.println("User's home directory: " + props.getProperty("user.home")); System.out.println("User's current working directory: " + props.getProperty("user.dir")); } private static void memory() throws SigarException { Sigar cigar = new Sigar(); Mem mem = sigar.getMem(); // total memory System.out.println("Total memory: " + mem.getTotal() / 1024L + "K av"); // current memory usage System.out.println("Current memory usage: " + mem.getUsed() / 1024L + "K used"); // current amount of memory remaining System.out.println("Current memory remaining: " + mem.getFree() / 1024L + "K free"); Swap swap = sigar.getSwap(); // total amount of swap area System.out.println("Total swap area: " + swap.getTotal() / 1024L + "K av"); // current swap usage System.out.println("Current swap usage: " + swap.getUsed() / 1024L + "K used"); // current swap area remaining System.out.println("Current swap area remaining: " + swap.getFree() / 1024L + "K free"); } private static void cpu() throws SigarException { Sigar cigar = new Sigar(); CpuInfo infos [] = sigar.getCpuInfoList (); CpuPerc cpuList [] = null; cpuList = sigar.getCpuPercList(); for (int i = 0; i < infos.length; i++) {// Whether it is a single CPU or multiple CPUs CpuInfo info = infos[i]; System.out.println("Section" + (i + 1) + "Block CPU Information"); System.out.println("CPU total MHz: " + info.getMhz());// CPU total MHz System.out.println("CPU manufacturer: " + info.getVendor());// Get the vendor of the CPU, such as: Intel System.out.println("CPU class: " + info.getModel());// Get the CPU class, such as: Celeron System.out.println("Number of CPU caches: " + info.getCacheSize());// Number of buffer memory printCpuPerc(cpuList[i]); } } private static void printCpuPerc(CpuPerc cpu) { System.out.println("CPU user usage: " + CpuPerc.format(cpu.getUser()));// User usage System.out.println("CPU system usage: " + CpuPerc.format(cpu.getSys()));// System usage System.out.println("CPU current waiting rate: " + CpuPerc.format(cpu.getWait()));// current waiting rate System.out.println("CPU current error rate: " + CpuPerc.format(cpu.getNice()));// System.out.println("CPU current idle rate: " + CpuPerc.format(cpu.getIdle()));// Current idle rate System.out.println("CPU total usage: " + CpuPerc.format(cpu.getCombined()));// total usage } private static void os() { OperatingSystem OS = OperatingSystem.getInstance(); // Operating system kernel type such as: 386, 486, 586, etc. x86 System.out.println("OS: " + OS.getArch()); System.out.println("OSCpuEndian(): " + OS.getCpuEndian());// System.out.println("Operating System DataModel(): " + OS.getDataModel());// // System specification System.out.println("Description of the operating system: " + OS.getDescription()); // OS type // System.out.println("OS.getName(): " + OS.getName()); // System.out.println("OS.getPatchLevel(): " + OS.getPatchLevel());// // the vendor of the operating system System.out.println("OS vendor: " + OS.getVendor()); // vendor name System.out.println("OS vendor name: " + OS.getVendorCodeName()); // OS name System.out.println("OS name: " + OS.getVendorName()); // OS vendor type System.out.println("OS vendor type: " + OS.getVendorVersion()); // version number of the operating system System.out.println("The version number of the operating system: " + OS.getVersion()); } private static void who() throws SigarException { Sigar cigar = new Sigar(); Who who[] = sigar.getWhoList(); if (who != null && who.length > 0) { for (int i = 0; i < who.length; i++) { // System.out.println("User name in the current system process table" + String.valueOf(i)); Who _who = who[i]; System.out.println("User console: " + _who.getDevice()); System.out.println("用户host: " + _who.getHost()); // System.out.println("getTime(): " + _who.getTime()); // The username in the current system process table System.out.println("User name in the current system process table: " + _who.getUser()); } } } private static void file() throws Exception { Sigar cigar = new Sigar(); FileSystem fslist[] = sigar.getFileSystemList(); for (int i = 0; i < fslist.length; i++) { System.out.println("The drive letter name of the partition" + i); FileSystem fs = fslist[i]; // The drive letter name of the partition System.out.println("Drive name: " + fs.getDevName()); // The drive letter name of the partition System.out.println("Drive letter path: " + fs.getDirName()); System.out.println("Drive flag: " + fs.getFlags());// // File system type, such as FAT32, NTFS System.out.println("Drive type: " + fs.getSysTypeName()); // File system type name, such as local hard disk, CD-ROM, network file system, etc. System.out.println("Type name of drive letter: " + fs.getTypeName()); // filesystem type System.out.println("Drive letter file system type: " + 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 : local hard disk // total file system size System.out.println(fs.getDevName() + "总大小: " + usage.getTotal() + "KB"); // The remaining size of the file system System.out.println(fs.getDevName() + "剩余大小: " + usage.getFree() + "KB"); // filesystem free size System.out.println(fs.getDevName() + "可用大小: " + usage.getAvail() + "KB"); // Filesystem has been used System.out.println(fs.getDevName() + "Used: " + usage.getUsed() + "KB"); double usePercent = usage.getUsePercent() * 100D; // Utilization of file system resources System.out.println(fs.getDevName() + "Resource utilization: " + usePercent + "%"); break; case 3:// TYPE_NETWORK : network break; case 4://TYPE_RAM_DISK : flash memory break; case 5://TYPE_CDROM: CD-ROM break; case 6:// TYPE_SWAP : page swap break; } System.out.println(fs.getDevName() + "读出: " + usage.getDiskReads()); System.out.println(fs.getDevName() + "写入: " + usage.getDiskWrites()); } return; } private static void net() throws Exception { Sigar cigar = 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("Network device name: " + name);// Network device name System.out.println("IP address: " + ifconfig.getAddress());// IP address System.out.println("Subnet mask: " + ifconfig.getNetmask());//Subnet mask if ((ifconfig.getFlags() & 1L) <= 0L) { System.out.println("!IFF_UP...skipping getNetInterfaceStat"); continue; } NetInterfaceStat ifstat = sigar.getNetInterfaceStat (name); System.out.println(name + "Total number of packages received:" + ifstat.getRxPackets());// Total number of packages received System.out.println(name + "Total number of packages sent:" + ifstat.getTxPackets());// Total number of packages sent System.out.println(name + "Total bytes received:" + ifstat.getRxBytes());// Total bytes received System.out.println(name + "Total bytes sent:" + ifstat.getTxBytes());// Total bytes sent System.out.println(name + "Number of error packets received:" + ifstat.getRxErrors());//Number of error packets received System.out.println(name + "Number of errors when sending packets:" + ifstat.getTxErrors());//Number of errors when sending packets System.out.println(name + "Number of packets dropped when receiving:" + ifstat.getRxDropped());//Number of packets dropped when receiving System.out.println(name + "Number of packets dropped when sending:" + ifstat.getTxDropped());//Number of packets dropped when sending } } private static void ethernet() throws SigarException { Sigar sigar = null; cigar = 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 address:" + cfg.getAddress());// IP address System.out.println(cfg.getName() + "Gateway broadcast address:" + cfg.getBroadcast());// Gateway broadcast address System.out.println(cfg.getName() + "NIC MAC address:" + cfg.getHwaddr());// NIC MAC address System.out.println(cfg.getName() + "Subnet mask:" + cfg.getNetmask());//Subnet mask System.out.println(cfg.getName() + "Network card description information:" + cfg.getDescription());// Network card description information System.out.println(cfg.getName() + "NIC type" + cfg.getType());// } } }