它是干什么的:
用来从许多平台收集系统和处理信息
有哪些平台:
Linux, Windows, Solaris, AIX, HP-UX, FreeBSD and Mac OSX
需要哪些依赖:
windows:需要sigar-amd64-winnt.dll或sigar-x86-winnt.dll,直接把这俩放到jdk的bin目录下就行
linux:需要libsigar-amd64-linux.so或libsigar-x86-linux.so,这俩也放到jdk的bin下就行
一些问题:
- 官网貌似有时候访问不了,不知道为什么,所以我在这里通过网盘分享一下需要到官网下载的依赖,永久有效地址:sigar依赖 提取码:kthy
上边说的依赖在哪里呢:
- 再pom文件中导入sigar 的jar包时需要注意,原来导入的jar是这样的:
<dependency>
<groupId>org.hyperic</groupId>
<artifactId>sigar</artifactId>
<version>1.6.4</version>
</dependency>
但是呢这个是不能用的:
所以需要换一个单词:
<dependency>
<groupId>org.fusesource</groupId>
<artifactId>sigar</artifactId>
<version>1.6.4</version>
</dependency>
这样就可以了,功能一样
- 要是你报错信息为:
DEBUG Sigar - no sigar-amd64-winnt.dll in java.library.path org.hyperic.sigar.SigarException: no sigar-amd64-winnt.dll in java.library.path
那么你少配置那俩依赖了
主要代码
复制粘贴就用
/**
* 内存信息
*
* @param:
* @author:Shuoshi.Yan
* @date: 2020/11/11 11:47
*/
private static HashMap memory() throws SigarException {
HashMap<String, String> memoryHashMap = new HashMap<String, String>();
Sigar sigar = new Sigar();
Mem mem = sigar.getMem();
Swap swap = sigar.getSwap();
// 内存总量
memoryHashMap.put("内存总量", mem.getTotal() / (1024L * 1024) + "M av");
memoryHashMap.put("当前内存使用量", mem.getUsed() / (1024L * 1024) + "M used");
memoryHashMap.put("当前内存剩余量", mem.getFree() / (1024L * 1024) + "M free");
memoryHashMap.put("交换区总量", swap.getTotal() / (1024L * 1024) + "M av");
memoryHashMap.put("当前交换区使用量", swap.getUsed() / (1024L * 1024) + "M used");
memoryHashMap.put("当前交换区剩余量", swap.getFree() / (1024L * 1024) + "M free");
// System.out.println("memory:" + memoryHashMap);
return memoryHashMap;
}
/**
* cpu信息
*
* @param:
* @author:Shuoshi.Yan
* @date: 2020/11/11 11:47
*/
private static List<HashMap> cpu() throws SigarException {
List<HashMap> cpuListMap = new ArrayList<HashMap>();
Sigar sigar = new Sigar();
CpuPerc cpuList[] = null;
cpuList = sigar.getCpuPercList();
for (int i = 0; i < cpuList.length; i++) {
cpuListMap.add(printCpuPerc(cpuList[i]));
}
// System.out.println("cpu:" + cpuListMap.get(0));
return cpuListMap;
}
private static HashMap printCpuPerc(CpuPerc cpu) {
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("CPU用户使用率", CpuPerc.format(cpu.getUser()));// 用户使用率
hashMap.put("CPU系统使用率", CpuPerc.format(cpu.getSys()));// 系统使用率
hashMap.put("CPU当前等待率", CpuPerc.format(cpu.getWait()));// 当前等待率
hashMap.put("CPU当前错误率", CpuPerc.format(cpu.getNice()));//
hashMap.put("CPU当前空闲率", CpuPerc.format(cpu.getIdle()));// 当前空闲率
hashMap.put("CPU总的使用率", CpuPerc.format(cpu.getCombined()));// 总的使用率
return hashMap;
}
/**
* 操作系统信息
*
* @param:
* @author:Shuoshi.Yan
* @date: 2020/11/11 11:48
*/
private static HashMap<String, String> os() {
HashMap<String, String> hashMap = new HashMap<String, String>();
OperatingSystem OS = OperatingSystem.getInstance();
hashMap.put("操作系统", OS.getArch());
hashMap.put("操作系统-CpuEndian()", OS.getCpuEndian());//
hashMap.put("操作系统-DataModel()", OS.getDataModel());//
// 系统描述
hashMap.put("操作系统的描述", OS.getDescription());
// 操作系统类型
// hashMap.put("OS.getName()" , OS.getName());
// hashMap.put("OS.getPatchLevel()" , OS.getPatchLevel());//
// 操作系统的卖主
hashMap.put("操作系统的卖主", OS.getVendor());
// 卖主名称
hashMap.put("操作系统的卖主名", OS.getVendorCodeName());
// 操作系统名称
hashMap.put("操作系统名称", OS.getVendorName());
// 操作系统卖主类型
hashMap.put("操作系统卖主类型", OS.getVendorVersion());
// 操作系统的版本号
hashMap.put("操作系统的版本号", OS.getVersion());
// System.out.println("os:" + hashMap);
return hashMap;
}
/**
* 用户信息
*
* @param:
* @author:Shuoshi.Yan
* @date: 2020/11/11 11:48
*/
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++) {
// log.info("当前系统进程表中的用户名" + String.valueOf(i));
Who _who = who[i];
// log.info("用户控制台: " + _who.getDevice());
// log.info("用户host: " + _who.getHost());
// // log.info("getTime(): " + _who.getTime());
// // 当前系统进程表中的用户名
// log.info("当前系统进程表中的用户名: " + _who.getUser());
}
}
}
/**
* 文件系统信息
*
* @param:
* @author:Shuoshi.Yan
* @date: 2020/11/11 11:48
*/
private static List<HashMap> file() throws Exception {
List<HashMap> dirListMap = new ArrayList<HashMap>();
Sigar sigar = new Sigar();
FileSystem fslist[] = sigar.getFileSystemList();
for (int i = 0; i < fslist.length; i++) {
HashMap<String, String> hashMap = new HashMap<String, String>();
FileSystem fs = fslist[i];
hashMap.put("盘符名称", fs.getDevName());
hashMap.put("盘符路径", fs.getDirName());
// hashMap.put("盘符标志", String.valueOf(fs.getFlags()));//
hashMap.put("盘符类型", fs.getSysTypeName());
//hashMap.put("盘符文件系统类型", String.valueOf(fs.getType()));
hashMap.put("分区的盘符名称", String.valueOf(i));
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 : 本地硬盘
hashMap.put("盘符名称", fs.getDevName());
hashMap.put("盘符路径", fs.getDirName());
// hashMap.put("盘符标志", String.valueOf(fs.getFlags()));//
//hashMap.put("盘符类型", fs.getSysTypeName());
// 文件系统总大小
hashMap.put(fs.getDevName() + "总大小", usage.getTotal() + "KB");
// 文件系统剩余大小
hashMap.put(fs.getDevName() + "剩余大小", usage.getFree() + "KB");
// 文件系统可用大小
hashMap.put(fs.getDevName() + "可用大小", usage.getAvail() + "KB");
// 文件系统已经使用量
hashMap.put(fs.getDevName() + "已经使用量", usage.getUsed() + "KB");
double usePercent = usage.getUsePercent() * 100D;
// 文件系统资源的利用率
hashMap.put(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;
}
dirListMap.add(hashMap);
}
// System.out.println("file:" + dirListMap.get(0));
return dirListMap;
}
/**
* 网络信息
*
* @param:
* @author:Shuoshi.Yan
* @date: 2020/11/11 11:48
*/
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);
// log.info("网络设备名: " + name);// 网络设备名
// log.info("IP地址: " + ifconfig.getAddress());// IP地址
// log.info("子网掩码: " + ifconfig.getNetmask());// 子网掩码
if ((ifconfig.getFlags() & 1L) <= 0L) {
// log.info("!IFF_UP...skipping getNetInterfaceStat");
continue;
}
}
}
/**
* 以太网信息
*
* @param:
* @author:Shuoshi.Yan
* @date: 2020/11/11 11:49
*/
private static List<HashMap> ethernet() throws SigarException {
List<HashMap> netListMap = new ArrayList<HashMap>();
Sigar sigar = new Sigar();
String[] ifaces = sigar.getNetInterfaceList();
for (int i = 0; i < ifaces.length; i++) {
HashMap<String, String> hashMap = new HashMap<String, String>();
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;
}
hashMap.put(cfg.getName() + "IP地址", cfg.getAddress());// IP地址
hashMap.put(cfg.getName() + "网关广播地址", cfg.getBroadcast());// 网关广播地址
hashMap.put(cfg.getName() + "网卡MAC地址", cfg.getHwaddr());// 网卡MAC地址
hashMap.put(cfg.getName() + "子网掩码", cfg.getNetmask());// 子网掩码
hashMap.put(cfg.getName() + "网卡描述信息", cfg.getDescription());// 网卡描述信息
hashMap.put(cfg.getName() + "网卡类型", cfg.getType());//
netListMap.add(hashMap);
}
// System.out.println("ethernet:" + netListMap);
return netListMap;
}
/**
* System信息,从jvm获取
*
* @param:
* @author:Shuoshi.Yan
* @date: 2020/11/11 11:47
*/
private static HashMap<String, String> property() throws UnknownHostException {
HashMap<String, String> hashMap = new HashMap<String, String>();
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");// 获取计算机域名
hashMap.put("用户名", userName);
hashMap.put("计算机名", computerName);
hashMap.put("计算机域名", userDomain);
hashMap.put("本地ip地址", ip);
hashMap.put("本地主机名", addr.getHostName());
hashMap.put("JVM可以使用的总内存", String.valueOf(r.totalMemory()));
hashMap.put("JVM可以使用的剩余内存", String.valueOf(r.freeMemory()));
hashMap.put("JVM可以使用的处理器个数", String.valueOf(r.availableProcessors()));
hashMap.put("Java的运行环境版本", props.getProperty("java.version"));
hashMap.put("Java的运行环境供应商", props.getProperty("java.vendor"));
hashMap.put("Java供应商的URL", props.getProperty("java.vendor.url"));
hashMap.put("Java的安装路径", props.getProperty("java.home"));
hashMap.put("Java的虚拟机规范版本", props.getProperty("java.vm.specification.version"));
hashMap.put("Java的虚拟机规范供应商", props.getProperty("java.vm.specification.vendor"));
hashMap.put("Java的虚拟机规范名称", props.getProperty("java.vm.specification.name"));
hashMap.put("Java的虚拟机实现版本", props.getProperty("java.vm.version"));
hashMap.put("Java的虚拟机实现供应商", props.getProperty("java.vm.vendor"));
hashMap.put("Java的虚拟机实现名称", props.getProperty("java.vm.name"));
hashMap.put("Java运行时环境规范版本", props.getProperty("java.specification.version"));
hashMap.put("Java运行时环境规范供应商", props.getProperty("java.specification.vender"));
hashMap.put("Java运行时环境规范名称", props.getProperty("java.specification.name"));
hashMap.put("Java的类格式版本号", props.getProperty("java.class.version"));
hashMap.put("Java的类路径", props.getProperty("java.class.path"));
hashMap.put("加载库时搜索的路径列表", props.getProperty("java.library.path"));
hashMap.put("默认的临时文件路径", props.getProperty("java.io.tmpdir"));
hashMap.put("一个或多个扩展目录的路径", props.getProperty("java.ext.dirs"));
hashMap.put("操作系统的名称", props.getProperty("os.name"));
hashMap.put("操作系统的构架", props.getProperty("os.arch"));
hashMap.put("操作系统的版本", props.getProperty("os.version"));
hashMap.put("文件分隔符", props.getProperty("file.separator"));
hashMap.put("路径分隔符", props.getProperty("path.separator"));
hashMap.put("行分隔符", props.getProperty("line.separator"));
hashMap.put("用户的账户名称", props.getProperty("user.name"));
hashMap.put("用户的主目录", props.getProperty("user.home"));
hashMap.put("用户的当前工作目录", props.getProperty("user.dir"));
// System.out.println("property:" + hashMap);
return hashMap;
}
public static void main(String[] args) {
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();
}
}
推荐
再推荐一个同样能实现此功能的技术:oshi:
oshi
这个地址是直接跳到使用到的那个类,拿过来直接用就行,不需要自己找是哪个类了。