package com.hncy.zfy.pojo; import java.sql.Timestamp; public class SystemInfor { //服务器信息展示 //当前系统用户名 private String systemName; //服务器名 private String serverName; //服务器IP private String serverIP; //服务器端口 private String serverPort; //服务器总内存 private Object totalMemory; //服务器可用内存 private Object freeMemory; //操作系统 private String osName; //系统所在文件夹 private String osDir; //服务器当前时间 private Timestamp nowTime; //CUP总数 private Object cpuTotal; //cpu类型 private Object cpuType; public String getSystemName() { return systemName; } public void setSystemName(String systemName) { this.systemName = systemName; } public String getServerName() { return serverName; } public void setServerName(String serverName) { this.serverName = serverName; } public String getServerIP() { return serverIP; } public void setServerIP(String serverIP) { this.serverIP = serverIP; } public String getServerPort() { return serverPort; } public void setServerPort(String serverPort) { this.serverPort = serverPort; } public Object getTotalMemory() { return totalMemory; } public void setTotalMemory(Object totalMemory) { this.totalMemory = totalMemory; } public Object getFreeMemory() { return freeMemory; } public void setFreeMemory(Object freeMemory) { this.freeMemory = freeMemory; } public Timestamp getNowTime() { return nowTime; } public void setNowTime(Timestamp nowTime) { this.nowTime = nowTime; } public Object getCpuTotal() { return cpuTotal; } public void setCpuTotal(Object cpuTotal) { this.cpuTotal = cpuTotal; } public Object getCpuType() { return cpuType; } public void setCpuType(Object cpuType) { this.cpuType = cpuType; } public String getOsName() { return osName; } public void setOsName(String osName) { this.osName = osName; } public String getOsDir() { return osDir; } public void setOsDir(String osDir) { this.osDir = osDir; } }
实体类
package com.hncy.common; import java.net.InetAddress; import java.sql.Timestamp; import java.util.HashMap; import java.util.Map; import java.util.Properties; import com.hncy.zfy.pojo.SystemInfor; import org.hyperic.sigar.CpuInfo; import org.hyperic.sigar.Mem; import org.hyperic.sigar.Sigar; public class OutSystemInfo { public static SystemInfor getSysTemInfo() throws Exception{ Map<Object,Object> map = new HashMap<Object,Object>(); SystemInfor sys = new SystemInfor(); map = property(); sys.setSystemName((String)map.get("userName")); sys.setServerName((String)map.get("serverName")); sys.setCpuTotal(map.get("cpuTotal")); sys.setServerIP((String)map.get("serverIP")); sys.setOsName((String)map.get("osName")); sys.setOsDir((String)map.get("osDir")); sys.setTotalMemory( map.get("totalMemory")); sys.setFreeMemory( map.get("freeMemory")); sys.setCpuType((String) map.get("cupType")); sys.setNowTime(new Timestamp(System.currentTimeMillis()) ); return sys; } private static Map<Object,Object> property() throws Exception { Runtime r = Runtime.getRuntime(); Properties props = System.getProperties(); Sigar sigar = SigarUtils.sigar; 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");// 获取计算机名 Map<Object,Object> systemMap = new HashMap<Object,Object>(); systemMap.put("userName", userName); systemMap.put("serverName", computerName); systemMap.put("serverIP", ip); systemMap.put("cpuTotal", r.availableProcessors()); systemMap.put("osName", props.getProperty("os.name")); systemMap.put("osDir", props.getProperty("user.dir")); /***************************************************/ Mem mem = sigar.getMem(); systemMap.put("totalMemory", mem.getTotal() / 1024); systemMap.put("freeMemory", mem.getFree() / 1024L); /*******************************/ CpuInfo infos[] = sigar.getCpuInfoList(); CpuInfo info = infos[0]; systemMap.put("cupType", info.getModel()); return systemMap; } }
获取当系统信息,在期间我们需要sigar包,可以把其中的依赖文件加入到jdk的bin中,如果在项目中buildPath会报错,但是sigar.jar是需要的