java获取本地信息大全

1.下载sigar.jar

sigar官方主页


sigar-1.6.4.zip


2.按照主页上的说明解压包后将相应的文件copy到java路径。比如windows32位操作系统需要将lib中sigar-x86-winnt.dll文件拷贝到java SDK目录的bin内

参考官方主页上的配置项。


File Language Description Required
sigar.jar Java Java API Yes (for Java only)
log4j.jar Java Java logging API No
libsigar-x86-linux.so C Linux AMD/Intel 32-bit *
libsigar-amd64-linux.so C Linux AMD/Intel 64-bit *
libsigar-ppc-linux.so C Linux PowerPC 32-bit *
libsigar-ppc64-linux.so C Linux PowerPC 64-bit *
libsigar-ia64-linux.so C Linux Itanium 64-bit *
libsigar-s390x-linux.so C Linux zSeries 64-bit *
sigar-x86-winnt.dll C Windows AMD/Intel 32-bit *
sigar-amd64-winnt.dll C Windows AMD/Intel 64-bit *
libsigar-ppc-aix-5.so C AIX PowerPC 32-bit *
libsigar-ppc64-aix-5.so C AIX PowerPC 64-bit *
libsigar-pa-hpux-11.sl C HP-UX PA-RISC 32-bit *
libsigar-ia64-hpux-11.sl C HP-UX Itanium 64-bt *
libsigar-sparc-solaris.so C Solaris Sparc 32-bit *
libsigar-sparc64-solaris.so C Solaris Sparc 64-bit *
libsigar-x86-solaris.so C Solaris AMD/Intel 32-bit *
libsigar-amd64-solaris.so C Solaris AMD/Intel 64-bit *
libsigar-universal-macosx.dylib C Mac OS X PowerPC/Intel 32-bit *
libsigar-universal64-macosx.dylib C Mac OS X PowerPC/Intel 64-bit *
libsigar-x86-freebsd-5.so C FreeBSD 5.x AMD/Intel 32-bit *
libsigar-x86-freebsd-6.so C FreeBSD 6.x AMD/Intel 64-bit *
libsigar-amd64-freebsd-6.so C FreeBSD 6.x AMD/Intel 64-bit *



3.

代码实现
package com.xingcloud.test;
002
003 import java.net.InetAddress;
004 import java.net.UnknownHostException;
005 import java.util.Map;
006 import java.util.Properties;
007 import org.hyperic.sigar.CpuInfo;
008 import org.hyperic.sigar.CpuPerc;
009 import org.hyperic.sigar.FileSystem;
010 import org.hyperic.sigar.FileSystemUsage;
011 import org.hyperic.sigar.Mem;
012 import org.hyperic.sigar.NetFlags;
013 import org.hyperic.sigar.NetInterfaceConfig;
014 import org.hyperic.sigar.NetInterfaceStat;
015 import org.hyperic.sigar.OperatingSystem;
016 import org.hyperic.sigar.Sigar;
017 import org.hyperic.sigar.SigarException;
018 import org.hyperic.sigar.Swap;
019 import org.hyperic.sigar.Who;
020
021 public class RuntimeTest {
022     public static void main(String[] args) {
023         try {
024             // System信息,从jvm获取
025             property();
026             // cpu信息
027             cpu();
028             // 内存信息
029             memory();
030             // 操作系统信息
031             os();
032             // 用户信息
033             who();
034             // 文件系统信息
035             file();
036             // 网络信息
037             net();
038             // 以太网信息
039             ethernet();
040         } catch (Exception e1) {
041             e1.printStackTrace();
042         }
043     }
044
045     private static void property() throws UnknownHostException {
046         Runtime r = Runtime.getRuntime();
047         Properties props = System.getProperties();
048         InetAddress addr;
049         addr = InetAddress.getLocalHost();
050         String ip = addr.getHostAddress();
051         Map<String, String> map = System.getenv();
052         String userName = map.get("USERNAME");// 获取用户名
053         String computerName = map.get("COMPUTERNAME");// 获取计算机名
054         String userDomain = map.get("USERDOMAIN");// 获取计算机域名
055         System.out.println("用户名:    " + userName);
056         System.out.println("计算机名:   " + computerName);
057         System.out.println("计算机域名:  " + userDomain);
058         System.out.println("本地ip地址: " + ip);
059         System.out.println("本地主机名:  " + addr.getHostName());
060         System.out.println("JVM可以使用的总内存:    " + r.totalMemory());
061         System.out.println("JVM可以使用的剩余内存:   " + r.freeMemory());
062         System.out.println("JVM可以使用的处理器个数:  " + r.availableProcessors());
063         System.out.println("Java的运行环境版本:    " + props.getProperty("java.version"));
064         System.out.println("Java的运行环境供应商:   " + props.getProperty("java.vendor"));
065         System.out.println("Java供应商的URL:    " + props.getProperty("java.vendor.url"));
066         System.out.println("Java的安装路径:  " + props.getProperty("java.home"));
067         System.out.println("Java的虚拟机规范版本:   " + props.getProperty("java.vm.specification.version"));
068         System.out.println("Java的虚拟机规范供应商:  " + props.getProperty("java.vm.specification.vendor"));
069         System.out.println("Java的虚拟机规范名称:   " + props.getProperty("java.vm.specification.name"));
070         System.out.println("Java的虚拟机实现版本:   " + props.getProperty("java.vm.version"));
071         System.out.println("Java的虚拟机实现供应商:  " + props.getProperty("java.vm.vendor"));
072         System.out.println("Java的虚拟机实现名称:   " + props.getProperty("java.vm.name"));
073         System.out.println("Java运行时环境规范版本:  " + props.getProperty("java.specification.version"));
074         System.out.println("Java运行时环境规范供应商: " + props.getProperty("java.specification.vender"));
075         System.out.println("Java运行时环境规范名称:  " + props.getProperty("java.specification.name"));
076         System.out.println("Java的类格式版本号:    " + props.getProperty("java.class.version"));
077         System.out.println("Java的类路径:   " + props.getProperty("java.class.path"));
078         System.out.println("加载库时搜索的路径列表:    " + props.getProperty("java.library.path"));
079         System.out.println("默认的临时文件路径:  " + props.getProperty("java.io.tmpdir"));
080         System.out.println("一个或多个扩展目录的路径:   " + props.getProperty("java.ext.dirs"));
081         System.out.println("操作系统的名称:    " + props.getProperty("os.name"));
082         System.out.println("操作系统的构架:    " + props.getProperty("os.arch"));
083         System.out.println("操作系统的版本:    " + props.getProperty("os.version"));
084         System.out.println("文件分隔符:  " + props.getProperty("file.separator"));
085         System.out.println("路径分隔符:  " + props.getProperty("path.separator"));
086         System.out.println("行分隔符:   " + props.getProperty("line.separator"));
087         System.out.println("用户的账户名称:    " + props.getProperty("user.name"));
088         System.out.println("用户的主目录: " + props.getProperty("user.home"));
089         System.out.println("用户的当前工作目录:  " + props.getProperty("user.dir"));
090     }
091
092     private static void memory() throws SigarException {
093         Sigar sigar = new Sigar();
094         Mem mem = sigar.getMem();
095         // 内存总量
096         System.out.println("内存总量:   " + mem.getTotal() / 1024L + "K av");
097         // 当前内存使用量
098         System.out.println("当前内存使用量:    " + mem.getUsed() / 1024L + "K used");
099         // 当前内存剩余量
100         System.out.println("当前内存剩余量:    " + mem.getFree() / 1024L + "K free");
101         Swap swap = sigar.getSwap();
102         // 交换区总量
103         System.out.println("交换区总量:  " + swap.getTotal() / 1024L + "K av");
104         // 当前交换区使用量
105         System.out.println("当前交换区使用量:   " + swap.getUsed() / 1024L + "K used");
106         // 当前交换区剩余量
107         System.out.println("当前交换区剩余量:   " + swap.getFree() / 1024L + "K free");
108     }
109
110     private static void cpu() throws SigarException {
111         Sigar sigar = new Sigar();
112         CpuInfo infos[] = sigar.getCpuInfoList();
113         CpuPerc cpuList[] = null;
114         cpuList = sigar.getCpuPercList();
115         for (int i = 0; i < infos.length; i++) {// 不管是单块CPU还是多CPU都适用
116             CpuInfo info = infos[i];
117             System.out.println("第" + (i + 1) + "块CPU信息");
118             System.out.println("CPU的总量MHz:  " + info.getMhz());// CPU的总量MHz
119             System.out.println("CPU生产商: " + info.getVendor());// 获得CPU的卖主,如:Intel
120             System.out.println("CPU类别:  " + info.getModel());// 获得CPU的类别,如:Celeron
121             System.out.println("CPU缓存数量:    " + info.getCacheSize());// 缓冲存储器数量
122             printCpuPerc(cpuList[i]);
123         }
124     }
125
126     private static void printCpuPerc(CpuPerc cpu) {
127         System.out.println("CPU用户使用率:   " + CpuPerc.format(cpu.getUser()));// 用户使用率
128         System.out.println("CPU系统使用率:   " + CpuPerc.format(cpu.getSys()));// 系统使用率
129         System.out.println("CPU当前等待率:   " + CpuPerc.format(cpu.getWait()));// 当前等待率
130         System.out.println("CPU当前错误率:   " + CpuPerc.format(cpu.getNice()));//
131         System.out.println("CPU当前空闲率:   " + CpuPerc.format(cpu.getIdle()));// 当前空闲率
132         System.out.println("CPU总的使用率:   " + CpuPerc.format(cpu.getCombined()));// 总的使用率
133     }
134
135     private static void os() {
136         OperatingSystem OS = OperatingSystem.getInstance();
137         // 操作系统内核类型如: 386、486、586等x86
138         System.out.println("操作系统:   " + OS.getArch());
139         System.out.println("操作系统CpuEndian():    " + OS.getCpuEndian());//
140         System.out.println("操作系统DataModel():    " + OS.getDataModel());//
141         // 系统描述
142         System.out.println("操作系统的描述:    " + OS.getDescription());
143         // 操作系统类型
144         // System.out.println("OS.getName():    " + OS.getName());
145         // System.out.println("OS.getPatchLevel():  " + OS.getPatchLevel());//
146         // 操作系统的卖主
147         System.out.println("操作系统的卖主:    " + OS.getVendor());
148         // 卖主名称
149         System.out.println("操作系统的卖主名:   " + OS.getVendorCodeName());
150         // 操作系统名称
151         System.out.println("操作系统名称: " + OS.getVendorName());
152         // 操作系统卖主类型
153         System.out.println("操作系统卖主类型:   " + OS.getVendorVersion());
154         // 操作系统的版本号
155         System.out.println("操作系统的版本号:   " + OS.getVersion());
156     }
157
158     private static void who() throws SigarException {
159         Sigar sigar = new Sigar();
160         Who who[] = sigar.getWhoList();
161         if (who != null && who.length > 0) {
162             for (int i = 0; i < who.length; i++) {
163                 // System.out.println("当前系统进程表中的用户名" + String.valueOf(i));
164                 Who _who = who[i];
165                 System.out.println("用户控制台:  " + _who.getDevice());
166                 System.out.println("用户host: " + _who.getHost());
167                 // System.out.println("getTime():   " + _who.getTime());
168                 // 当前系统进程表中的用户名
169                 System.out.println("当前系统进程表中的用户名:   " + _who.getUser());
170             }
171         }
172     }
173
174     private static void file() throws Exception {
175         Sigar sigar = new Sigar();
176         FileSystem fslist[] = sigar.getFileSystemList();
177         for (int i = 0; i < fslist.length; i++) {
178             System.out.println("分区的盘符名称" + i);
179             FileSystem fs = fslist[i];
180             // 分区的盘符名称
181             System.out.println("盘符名称:   " + fs.getDevName());
182             // 分区的盘符名称
183             System.out.println("盘符路径:   " + fs.getDirName());
184             System.out.println("盘符标志:   " + fs.getFlags());//
185             // 文件系统类型,比如 FAT32、NTFS
186             System.out.println("盘符类型:   " + fs.getSysTypeName());
187             // 文件系统类型名,比如本地硬盘、光驱、网络文件系统等
188             System.out.println("盘符类型名:  " + fs.getTypeName());
189             // 文件系统类型
190             System.out.println("盘符文件系统类型:   " + fs.getType());
191             FileSystemUsage usage = null;
192             usage = sigar.getFileSystemUsage(fs.getDirName());
193             switch (fs.getType()) {
194             case 0: // TYPE_UNKNOWN :未知
195                 break;
196             case 1: // TYPE_NONE
197                 break;
198             case 2: // TYPE_LOCAL_DISK : 本地硬盘
199                 // 文件系统总大小
200                 System.out.println(fs.getDevName() + "总大小:  " + usage.getTotal() + "KB");
201                 // 文件系统剩余大小
202                 System.out.println(fs.getDevName() + "剩余大小: " + usage.getFree() + "KB");
203                 // 文件系统可用大小
204                 System.out.println(fs.getDevName() + "可用大小: " + usage.getAvail() + "KB");
205                 // 文件系统已经使用量
206                 System.out.println(fs.getDevName() + "已经使用量:    " + usage.getUsed() + "KB");
207                 double usePercent = usage.getUsePercent() * 100D;
208                 // 文件系统资源的利用率
209                 System.out.println(fs.getDevName() + "资源的利用率:   " + usePercent + "%");
210                 break;
211             case 3:// TYPE_NETWORK :网络
212                 break;
213             case 4:// TYPE_RAM_DISK :闪存
214                 break;
215             case 5:// TYPE_CDROM :光驱
216                 break;
217             case 6:// TYPE_SWAP :页面交换
218                 break;
219             }
220             System.out.println(fs.getDevName() + "读出:   " + usage.getDiskReads());
221             System.out.println(fs.getDevName() + "写入:   " + usage.getDiskWrites());
222         }
223         return;
224     }
225
226     private static void net() throws Exception {
227         Sigar sigar = new Sigar();
228         String ifNames[] = sigar.getNetInterfaceList();
229         for (int i = 0; i < ifNames.length; i++) {
230             String name = ifNames[i];
231             NetInterfaceConfig ifconfig = sigar.getNetInterfaceConfig(name);
232             System.out.println("网络设备名:  " + name);// 网络设备名
233             System.out.println("IP地址:   " + ifconfig.getAddress());// IP地址
234             System.out.println("子网掩码:   " + ifconfig.getNetmask());// 子网掩码
235             if ((ifconfig.getFlags() & 1L) <= 0L) {
236                 System.out.println("!IFF_UP...skipping getNetInterfaceStat");
237                 continue;
238             }
239             NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);
240             System.out.println(name + "接收的总包裹数:" + ifstat.getRxPackets());// 接收的总包裹数
241             System.out.println(name + "发送的总包裹数:" + ifstat.getTxPackets());// 发送的总包裹数
242             System.out.println(name + "接收到的总字节数:" + ifstat.getRxBytes());// 接收到的总字节数
243             System.out.println(name + "发送的总字节数:" + ifstat.getTxBytes());// 发送的总字节数
244             System.out.println(name + "接收到的错误包数:" + ifstat.getRxErrors());// 接收到的错误包数
245             System.out.println(name + "发送数据包时的错误数:" + ifstat.getTxErrors());// 发送数据包时的错误数
246             System.out.println(name + "接收时丢弃的包数:" + ifstat.getRxDropped());// 接收时丢弃的包数
247             System.out.println(name + "发送时丢弃的包数:" + ifstat.getTxDropped());// 发送时丢弃的包数
248         }
249     }
250
251     private static void ethernet() throws SigarException {
252         Sigar sigar = null;
253         sigar = new Sigar();
254         String[] ifaces = sigar.getNetInterfaceList();
255         for (int i = 0; i < ifaces.length; i++) {
256             NetInterfaceConfig cfg = sigar.getNetInterfaceConfig(ifaces[i]);
257             if (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress()) || (cfg.getFlags() & NetFlags.IFF_LOOPBACK) != 0
258                     || NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())) {
259                 continue;
260             }
261             System.out.println(cfg.getName() + "IP地址:" + cfg.getAddress());// IP地址
262             System.out.println(cfg.getName() + "网关广播地址:" + cfg.getBroadcast());// 网关广播地址
263             System.out.println(cfg.getName() + "网卡MAC地址:" + cfg.getHwaddr());// 网卡MAC地址
264             System.out.println(cfg.getName() + "子网掩码:" + cfg.getNetmask());// 子网掩码
265             System.out.println(cfg.getName() + "网卡描述信息:" + cfg.getDescription());// 网卡描述信息
266             System.out.println(cfg.getName() + "网卡类型" + cfg.getType());//
267         }
268     }
269 }

你可能感兴趣的:(java)