????3 ???sigar????????
????3.1 ??????sigar-1.6.4.zip
???????java???????????????????????????????????????????????????????????sigar??????????
????????????????lib??sigar.jar????eclipse??CLASSPATH?У????sigar-x86-winnt.dll????Java??bin?????ɡ?
????3.2 ??????????£?
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???????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();
}
}
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());//
}
}
}