private static Long format(long value) { return new Long(value / 1024); } public void memInfo() throws SigarException { clearInfo(); println("============Display information about free and used memory================"); try { sigarInit(false); Mem mem = this.sigar.getMem(); Swap swap = this.sigar.getSwap();
Object[] header = new Object[] { "total", "used", "free" }; printf("s s s", header); Object[] memRow = new Object[] { format(mem.getTotal()), format(mem.getUsed()), format(mem.getFree()) }; printf("Mem: ldK ldK ldK", memRow); Object[] memPercent = new Object[3]; memPercent[0] = "100%"; double d = mem.getUsedPercent(); d=((int)(d*100))/100; // 取小数点后两位的简便方法 memPercent[1] = d + "%"; d = mem.getFreePercent(); d=((int)(d*100))/100; memPercent[2] = d + "%"; // 取小数点后两位的简便方法 printf("MemPer: ls ls ls", memPercent); //e.g. linux Object[] actualRow = new Object[] { format(mem.getActualUsed()), format(mem.getActualFree()) }; if ((mem.getUsed() != mem.getActualUsed()) || (mem.getFree() != mem.getActualFree())) { printf("-/+ buffers/cache: " + "ldK dK", actualRow); }
Object[] swapRow = new Object[] { format(swap.getTotal()), format(swap.getUsed()), format(swap.getFree()) }; printf("Swap: ldK ldK ldK", swapRow); printf("RAM: ldM", new Object[] { mem.getRam()});
used = usage.getTotal() - usage.getFree(); avail = usage.getAvail(); total = usage.getTotal();
pct = (long)(usage.getUsePercent() * 100); } catch (SigarException e) { //e.g. on win32 D:\ fails with "Device not ready" //if there is no cd in the drive. used = avail = total = pct = 0; }
Object[] items = new Object[] { fs.getDevName(), Sigar.formatSize(total* 1024), Sigar.formatSize(used* 1024), Sigar.formatSize(avail* 1024), usePct, fs.getDirName(), fs.getSysTypeName() + "/" + fs.getTypeName() }; printf(" ls ls ls ls ls ls ls",items); }
public void duInfo(String root) throws SigarException { clearInfo(); println("============Display usage for a directory recursively===============");
String rootDir = root; try { sigarInit(false); printf(" ls ls ",new Object[]{"size","directory"}); List<String> rootList = new ArrayList<String>(); rootList.add(rootDir); treePrint(rootList);
} catch (RuntimeException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { shutdown(); } } public void treePrint(List<String> treeList) throws SigarException{ for(String temp:treeList){ File dirFile = new File(temp); if ( dirFile.isDirectory()){ DirUsage du = this.sigar.getDirUsage(temp); Object[] items = new Object[] { Sigar.formatSize(du.getDiskUsage()), temp }; printf(" ls ls ",items); String[] files = dirFile.list(); List<String> tList = new ArrayList(); for (int i = 0 ; i < files.length ; i++){ String dirFull = temp + "\\" + files[i]; if( new File(dirFull).isDirectory()){ tList.add(dirFull); } } treePrint(tList); } }
}
public static void main(String[] args) throws SigarException { SystemInfo one = new SystemInfo(); one.cpuInfo(); System.out.println(one.getInfo()); one.memInfo(); System.out.println(one.getInfo());
============Current system Cpu information================ Vendor.........Intel Model..........Mobile Intel(R) Pentium(R) 4 - M CPU 2.00GHz Mhz............1195 Total CPUs.....1 Physical CPUs..1 Cores per CPU..1
CPU 0......... User Time.....5.9% Sys Time......8.1% Idle Time.....85.8% Wait Time.....0.0% Nice Time.....0.0% Combined......14.1% Irq Time......0.0%
============Display information about free and used memory================ total used free Mem: 1015276K 877456K 137820K MemPer: 100% 86.0% 13.0% Swap: 2447596K 997476K 1450120K RAM: 992M
============Report filesystem disk space usage=============== Filesystem Size Used Avail Use% Mounted on Type C:\ 20G 13G 6.5G 67% C:\ FAT32/local D:\ 88G 72G 15G 83% D:\ FAT32/local E:\ 0 0 0 - E:\ cdrom/cdrom F:\ 0 0 0 - F:\ cdrom/cdrom