qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請?jiān)L問 http://qaseven.github.io/

          Java如何獲取系統(tǒng)cpu、內(nèi)存、硬盤信息

           1 概述
            前段時(shí)間摸索在Java中怎么獲取系統(tǒng)信息包括cpu、內(nèi)存、硬盤信息等,剛開始使用Java自帶的包進(jìn)行獲取,但這樣獲取的內(nèi)存信息不夠準(zhǔn)確并且容易出現(xiàn)找不到相應(yīng)包等錯(cuò)誤,所以后面使用sigar插件進(jìn)行獲取。下面列舉出了這兩種方式獲取系統(tǒng)信息的方式及代碼。
            2 使用Java自帶包獲取系統(tǒng)信息
            2.1 使用Java自帶包獲取系統(tǒng)信息代碼如下:
            2.1.1 Bytes.java
          public class Bytes {
          public static String substring(String src, int start_idx, int end_idx){
          byte[] b = src.getBytes();
          String tgt = "";
          for(int i=start_idx; i<=end_idx; i++){
          tgt +=(char)b[i];
          }
          return tgt;
          }
          }
            2.1.2 IMonitorService.java
            public interface IMonitorService {
            public MonitorInfoBean getMonitorInfoBean() throws Exception;
            }
            2.1.3 MonitorInfoBean.java
          public class MonitorInfoBean {
          private long totalMemory;
          private long freeMemory;
          private long maxMemory;
          private String osName;
          private long totalMemorySize;
          private long freePhysicalMemorySize;
          private long usedMemory;
          private int totalThread;
          private double cpuRatio;
          public long getFreeMemory() {
          return freeMemory;
          }
          public void setFreeMemory(long freeMemory) {
          this.freeMemory = freeMemory;
          }
          public long getFreePhysicalMemorySize() {
          return freePhysicalMemorySize;
          }
          public void setFreePhysicalMemorySize(long freePhysicalMemorySize) {
          this.freePhysicalMemorySize = freePhysicalMemorySize;
          }
          public long getMaxMemory() {
          return maxMemory;
          }
          public void setMaxMemory(long maxMemory) {
          this.maxMemory = maxMemory;
          }
          public String getOsName() {
          return osName;
          }
          public void setOsName(String osName) {
          this.osName = osName;
          }
          public long getTotalMemory() {
          return totalMemory;
          }
          public void setTotalMemory(long totalMemory) {
          this.totalMemory = totalMemory;
          }
          public long getTotalMemorySize() {
          return totalMemorySize;
          }
          public void setTotalMemorySize(long totalMemorySize) {
          this.totalMemorySize = totalMemorySize;
          }
          public int getTotalThread() {
          return totalThread;
          }
          public void setTotalThread(int totalThread) {
          this.totalThread = totalThread;
          }
          public long getUsedMemory() {
          return usedMemory;
          }
          public void setUsedMemory(long usedMemory) {
          this.usedMemory = usedMemory;
          }
          public double getCpuRatio() {
          return cpuRatio;
          }
          public void setCpuRatio(double cpuRatio) {
          this.cpuRatio = cpuRatio;
          }
          }
           2.1.4
          import java.io.InputStreamReader;
          import java.io.LineNumberReader;
          //import sun.management.ManagementFactory;
          //import com.sun.management.OperatingSystemMXBean;
          import java.io.*;
          import java.lang.management.ManagementFactory;
          import java.util.StringTokenizer;
          public class MonitorServiceImpl implements IMonitorService {
          private static final int CPUTIME = 30;
          private static final int PERCENT = 100;
          private static final int FAULTLENGTH = 10;
          private static final File versionFile = new File("/proc/version");
          private static String linuxVersion = null;
          public MonitorInfoBean getMonitorInfoBean() throws Exception {
          int kb = 1024;
          long totalMemory = Runtime.getRuntime().totalMemory() / kb;
          long freeMemory = Runtime.getRuntime().freeMemory() / kb;
          long maxMemory = Runtime.getRuntime().maxMemory() / kb;
          // OperatingSystemMXBean osmxb = (OperatingSystemMXBean)
          // ManagementFactory
          // .getOperatingSystemMXBean();
          // String osName = System.getProperty("os.name");
          // long totalMemorySize = osmxb.getTotalPhysicalMemorySize() / kb;
          // long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize() / kb;
          // long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb
          // .getFreePhysicalMemorySize()) / kb;
          ThreadGroup parentThread;
          for (parentThread = Thread.currentThread().getThreadGroup(); parentThread.getParent() != null; parentThread = parentThread.getParent());
          int totalThread = parentThread.activeCount();
          double cpuRatio = 0;
          // if (osName.toLowerCase().startsWith("windows")) {
          // cpuRatio = this.getCpuRatioForWindows();
          // } else {
          // cpuRatio = this.getCpuRateForLinux();
          // }
          MonitorInfoBean infoBean = new MonitorInfoBean();
          infoBean.setFreeMemory(freeMemory);
          // infoBean.setFreePhysicalMemorySize(freePhysicalMemorySize);
          infoBean.setMaxMemory(maxMemory);
          // infoBean.setOsName(osName);
          infoBean.setTotalMemory(totalMemory);
          // infoBean.setTotalMemorySize(totalMemorySize);
          infoBean.setTotalThread(totalThread);
          // infoBean.setUsedMemory(usedMemory);
          infoBean.setCpuRatio(cpuRatio);
          return infoBean;
          }
          private static double getCpuRateForLinux() {
          InputStream is = null;
          InputStreamReader isr = null;
          BufferedReader brStat = null;
          StringTokenizer tokenStat = null;
          try {
          System.out.println("Get usage rate of CUP , linux version: " + linuxVersion);
          Process process = Runtime.getRuntime().exec("top -b -n 1");
          is = process.getInputStream();
          isr = new InputStreamReader(is);
          brStat = new BufferedReader(isr);
          if (linuxVersion.equals("2.4")) {
          brStat.readLine();
          brStat.readLine();
          brStat.readLine();
          brStat.readLine();
          tokenStat = new StringTokenizer(brStat.readLine());
          tokenStat.nextToken();
          tokenStat.nextToken();
          String user = tokenStat.nextToken();
          tokenStat.nextToken();
          String system = tokenStat.nextToken();
          tokenStat.nextToken();
          String nice = tokenStat.nextToken();
          System.out.println(user + " , " + system + " , " + nice);
          user = user.substring(0, user.indexOf("%"));
          system = system.substring(0, system.indexOf("%"));
          nice = nice.substring(0, nice.indexOf("%"));
          float userUsage = new Float(user).floatValue();
          float systemUsage = new Float(system).floatValue();
          float niceUsage = new Float(nice).floatValue();
          return (userUsage + systemUsage + niceUsage) / 100;
          } else {
          brStat.readLine();
          brStat.readLine();
          tokenStat = new StringTokenizer(brStat.readLine());
          tokenStat.nextToken();
          tokenStat.nextToken();
          tokenStat.nextToken();
          tokenStat.nextToken();
          tokenStat.nextToken();
          tokenStat.nextToken();
          tokenStat.nextToken();
          String cpuUsage = tokenStat.nextToken();
          System.out.println("CPU idle : " + cpuUsage);
          Float usage = new Float(cpuUsage.substring(0, cpuUsage.indexOf("%")));
          return (1 - usage.floatValue() / 100);
          }
          } catch (IOException ioe) {
          System.out.println(ioe.getMessage());
          freeResource(is, isr, brStat);
          return 1;
          } finally {
          freeResource(is, isr, brStat);
          }
          }
          private static void freeResource(InputStream is, InputStreamReader isr, BufferedReader br) {
          try {
          if (is != null)
          is.close();
          if (isr != null)
          isr.close();
          if (br != null)
          br.close();
          } catch (IOException ioe) {
          System.out.println(ioe.getMessage());
          }
          }
          private double getCpuRatioForWindows() {
          try {
          String procCmd = System.getenv("windir")
          + "\\system32\\wbem\\wmic.exe process get Caption,CommandLine,"
          + "KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount";
          long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));
          Thread.sleep(CPUTIME);
          long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));
          if (c0 != null && c1 != null) {
          long idletime = c1[0] - c0[0];
          long busytime = c1[1] - c0[1];
          return Double.valueOf(
          PERCENT * (busytime) / (busytime + idletime)).doubleValue();
          } else {
          return 0.0;
          }
          } catch (Exception ex) {
          ex.printStackTrace();
          return 0.0;
          }
          }
          private long[] readCpu(final Process proc) {
          long[] retn = new long[2];
          try {
          proc.getOutputStream().close();
          InputStreamReader ir = new InputStreamReader(proc.getInputStream());
          LineNumberReader input = new LineNumberReader(ir);
          String line = input.readLine();
          if (line == null || line.length() < FAULTLENGTH) {
          return null;
          }
          int capidx = line.indexOf("Caption");
          int cmdidx = line.indexOf("CommandLine");
          int rocidx = line.indexOf("ReadOperationCount");
          int umtidx = line.indexOf("UserModeTime");
          int kmtidx = line.indexOf("KernelModeTime");
          int wocidx = line.indexOf("WriteOperationCount");
          long idletime = 0;
          long kneltime = 0;
          long usertime = 0;
          while ((line = input.readLine()) != null) {
          if (line.length() < wocidx) {
          continue;
          }
          String caption = Bytes.substring(line, capidx, cmdidx - 1) .trim();
          String cmd = Bytes.substring(line, cmdidx, kmtidx - 1).trim();
          if (cmd.indexOf("wmic.exe") >= 0) {
          continue;
          }
          // log.info("line="+line);
          if (caption.equals("System Idle Process") || caption.equals("System")) {
          idletime += Long.valueOf(
          Bytes.substring(line, kmtidx, rocidx - 1).trim()).longValue();
          idletime += Long.valueOf(
          Bytes.substring(line, umtidx, wocidx - 1).trim()).longValue();
          continue;
          }
          kneltime += Long.valueOf(
          Bytes.substring(line, kmtidx, rocidx - 1).trim()).longValue();
          usertime += Long.valueOf(
          Bytes.substring(line, umtidx, wocidx - 1).trim()).longValue();
          }
          retn[0] = idletime;
          retn[1] = kneltime + usertime;
          return retn;
          } catch (Exception ex) {
          ex.printStackTrace();
          } finally {
          try {
          proc.getInputStream().close();
          } catch (Exception e) {
          e.printStackTrace();
          }
          }
          return null;
          }
          public static void main(String[] args) throws Exception {
          IMonitorService service = new MonitorServiceImpl();
          MonitorInfoBean monitorInfo = service.getMonitorInfoBean();
          System.out.println("cpu percent: " + monitorInfo.getCpuRatio());
          System.out.println("can use memory: " + monitorInfo.getTotalMemory());
          System.out.println("ideal memory: " + monitorInfo.getFreeMemory());
          System.out.println("largest memory: " + monitorInfo.getMaxMemory());
          System.out.println("all memory: " + monitorInfo.getTotalMemorySize() + "kb");
          System.out.println("ideal memory: " + monitorInfo.getFreeMemory() + "kb");
          System.out.println("used memory: " + monitorInfo.getUsedMemory() + "kb");
          System.out.println("thread num: " + monitorInfo.getTotalThread() + "kb");
          }
          }
            2.2 執(zhí)行結(jié)果如下圖所示:

           3 使用sigar獲取系統(tǒng)信息
            3.1 下載安裝sigar-1.6.4.zip
            使用java自帶的包獲取系統(tǒng)數(shù)據(jù),容易找不到包,尤其是內(nèi)存信息不夠準(zhǔn)確,所以選擇使用sigar獲取系統(tǒng)信息。
            解壓壓縮包,將lib下sigar.jar導(dǎo)入eclipse的CLASSPATH中,再將sigar-x86-winnt.dll存入Java的bin目錄即可。
            3.2 代碼實(shí)現(xiàn)如下:
          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("----------------------------------");
          // 內(nèi)存信息
          memory();
          System.out.println("----------------------------------");
          // 操作系統(tǒng)信息
          os();
          System.out.println("----------------------------------");
          // 用戶信息
          who();
          System.out.println("----------------------------------");
          // 文件系統(tǒng)信息
          file();
          System.out.println("----------------------------------");
          // 網(wǎng)絡(luò)信息
          net();
          System.out.println("----------------------------------");
          // 以太網(wǎng)信息
          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");// 獲取計(jì)算機(jī)名
          String userDomain = map.get("USERDOMAIN");// 獲取計(jì)算機(jī)域名
          System.out.println("用戶名:    " + userName);
          System.out.println("計(jì)算機(jī)名:    " + computerName);
          System.out.println("計(jì)算機(jī)域名:    " + userDomain);
          System.out.println("本地ip地址:    " + ip);
          System.out.println("本地主機(jī)名:    " + addr.getHostName());
          System.out.println("JVM可以使用的總內(nèi)存:    " + r.totalMemory());
          System.out.println("JVM可以使用的剩余內(nèi)存:    " + r.freeMemory());
          System.out.println("JVM可以使用的處理器個(gè)數(shù):    " + r.availableProcessors());
          System.out.println("Java的運(yùn)行環(huán)境版本:    " + props.getProperty("java.version"));
          System.out.println("Java的運(yùn)行環(huán)境供應(yīng)商:    " + props.getProperty("java.vendor"));
          System.out.println("Java供應(yīng)商的URL:    " + props.getProperty("java.vendor.url"));
          System.out.println("Java的安裝路徑:    " + props.getProperty("java.home"));
          System.out.println("Java的虛擬機(jī)規(guī)范版本:    " + props.getProperty("java.vm.specification.version"));
          System.out.println("Java的虛擬機(jī)規(guī)范供應(yīng)商:    " + props.getProperty("java.vm.specification.vendor"));
          System.out.println("Java的虛擬機(jī)規(guī)范名稱:    " + props.getProperty("java.vm.specification.name"));
          System.out.println("Java的虛擬機(jī)實(shí)現(xiàn)版本:    " + props.getProperty("java.vm.version"));
          System.out.println("Java的虛擬機(jī)實(shí)現(xiàn)供應(yīng)商:    " + props.getProperty("java.vm.vendor"));
          System.out.println("Java的虛擬機(jī)實(shí)現(xiàn)名稱:    " + props.getProperty("java.vm.name"));
          System.out.println("Java運(yùn)行時(shí)環(huán)境規(guī)范版本:    " + props.getProperty("java.specification.version"));
          System.out.println("Java運(yùn)行時(shí)環(huán)境規(guī)范供應(yīng)商:    " + props.getProperty("java.specification.vender"));
          System.out.println("Java運(yùn)行時(shí)環(huán)境規(guī)范名稱:    " + props.getProperty("java.specification.name"));
          System.out.println("Java的類格式版本號(hào):    " + props.getProperty("java.class.version"));
          System.out.println("Java的類路徑:    " + props.getProperty("java.class.path"));
          System.out.println("加載庫時(shí)搜索的路徑列表:    " + props.getProperty("java.library.path"));
          System.out.println("默認(rèn)的臨時(shí)文件路徑:    " + props.getProperty("java.io.tmpdir"));
          System.out.println("一個(gè)或多個(gè)擴(kuò)展目錄的路徑:    " + props.getProperty("java.ext.dirs"));
          System.out.println("操作系統(tǒng)的名稱:    " + props.getProperty("os.name"));
          System.out.println("操作系統(tǒng)的構(gòu)架:    " + props.getProperty("os.arch"));
          System.out.println("操作系統(tǒng)的版本:    " + 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("用戶的當(dāng)前工作目錄:    " + props.getProperty("user.dir"));
          }
          private static void memory() throws SigarException {
          Sigar sigar = new Sigar();
          Mem mem = sigar.getMem();
          // 內(nèi)存總量
          System.out.println("內(nèi)存總量:    " + mem.getTotal() / 1024L + "K av");
          // 當(dāng)前內(nèi)存使用量
          System.out.println("當(dāng)前內(nèi)存使用量:    " + mem.getUsed() / 1024L + "K used");
          // 當(dāng)前內(nèi)存剩余量
          System.out.println("當(dāng)前內(nèi)存剩余量:    " + mem.getFree() / 1024L + "K free");
          Swap swap = sigar.getSwap();
          // 交換區(qū)總量
          System.out.println("交換區(qū)總量:    " + swap.getTotal() / 1024L + "K av");
          // 當(dāng)前交換區(qū)使用量
          System.out.println("當(dāng)前交換區(qū)使用量:    " + swap.getUsed() / 1024L + "K used");
          // 當(dāng)前交換區(qū)剩余量
          System.out.println("當(dāng)前交換區(qū)剩余量:    " + 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生產(chǎn)商:    " + info.getVendor());// 獲得CPU的賣主,如:Intel
          System.out.println("CPU類別:    " + info.getModel());// 獲得CPU的類別,如:Celeron
          System.out.println("CPU緩存數(shù)量:    " + info.getCacheSize());// 緩沖存儲(chǔ)器數(shù)量
          printCpuPerc(cpuList[i]);
          }
          }
          private static void printCpuPerc(CpuPerc cpu) {
          System.out.println("CPU用戶使用率:    " + CpuPerc.format(cpu.getUser()));// 用戶使用率
          System.out.println("CPU系統(tǒng)使用率:    " + CpuPerc.format(cpu.getSys()));// 系統(tǒng)使用率
          System.out.println("CPU當(dāng)前等待率:    " + CpuPerc.format(cpu.getWait()));// 當(dāng)前等待率
          System.out.println("CPU當(dāng)前錯(cuò)誤率:    " + CpuPerc.format(cpu.getNice()));//
          System.out.println("CPU當(dāng)前空閑率:    " + CpuPerc.format(cpu.getIdle()));// 當(dāng)前空閑率
          System.out.println("CPU總的使用率:    " + CpuPerc.format(cpu.getCombined()));// 總的使用率
          }
          private static void os() {
          OperatingSystem OS = OperatingSystem.getInstance();
          // 操作系統(tǒng)內(nèi)核類型如: 386、486、586等x86
          System.out.println("操作系統(tǒng):    " + OS.getArch());
          System.out.println("操作系統(tǒng)CpuEndian():    " + OS.getCpuEndian());//
          System.out.println("操作系統(tǒng)DataModel():    " + OS.getDataModel());//
          // 系統(tǒng)描述
          System.out.println("操作系統(tǒng)的描述:    " + OS.getDescription());
          // 操作系統(tǒng)類型
          // System.out.println("OS.getName():    " + OS.getName());
          // System.out.println("OS.getPatchLevel():    " + OS.getPatchLevel());//
          // 操作系統(tǒng)的賣主
          System.out.println("操作系統(tǒng)的賣主:    " + OS.getVendor());
          // 賣主名稱
          System.out.println("操作系統(tǒng)的賣主名:    " + OS.getVendorCodeName());
          // 操作系統(tǒng)名稱
          System.out.println("操作系統(tǒng)名稱:    " + OS.getVendorName());
          // 操作系統(tǒng)賣主類型
          System.out.println("操作系統(tǒng)賣主類型:    " + OS.getVendorVersion());
          // 操作系統(tǒng)的版本號(hào)
          System.out.println("操作系統(tǒng)的版本號(hào):    " + 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("當(dāng)前系統(tǒng)進(jìn)程表中的用戶名" + String.valueOf(i));
          Who _who = who[i];
          System.out.println("用戶控制臺(tái):    " + _who.getDevice());
          System.out.println("用戶host:    " + _who.getHost());
          // System.out.println("getTime():    " + _who.getTime());
          // 當(dāng)前系統(tǒng)進(jìn)程表中的用戶名
          System.out.println("當(dāng)前系統(tǒng)進(jìn)程表中的用戶名:    " + _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("分區(qū)的盤符名稱" + i);
          FileSystem fs = fslist[i];
          // 分區(qū)的盤符名稱
          System.out.println("盤符名稱:    " + fs.getDevName());
          // 分區(qū)的盤符名稱
          System.out.println("盤符路徑:    " + fs.getDirName());
          System.out.println("盤符標(biāo)志:    " + fs.getFlags());//
          // 文件系統(tǒng)類型,比如 FAT32、NTFS
          System.out.println("盤符類型:    " + fs.getSysTypeName());
          // 文件系統(tǒng)類型名,比如本地硬盤、光驅(qū)、網(wǎng)絡(luò)文件系統(tǒng)等
          System.out.println("盤符類型名:    " + fs.getTypeName());
          // 文件系統(tǒng)類型
          System.out.println("盤符文件系統(tǒng)類型:    " + 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 : 本地硬盤
          // 文件系統(tǒng)總大小
          System.out.println(fs.getDevName() + "總大小:    " + usage.getTotal() + "KB");
          // 文件系統(tǒng)剩余大小
          System.out.println(fs.getDevName() + "剩余大小:    " + usage.getFree() + "KB");
          // 文件系統(tǒng)可用大小
          System.out.println(fs.getDevName() + "可用大小:    " + usage.getAvail() + "KB");
          // 文件系統(tǒng)已經(jīng)使用量
          System.out.println(fs.getDevName() + "已經(jīng)使用量:    " + usage.getUsed() + "KB");
          double usePercent = usage.getUsePercent() * 100D;
          // 文件系統(tǒng)資源的利用率
          System.out.println(fs.getDevName() + "資源的利用率:    " + usePercent + "%");
          break;
          case 3:// TYPE_NETWORK :網(wǎng)絡(luò)
          break;
          case 4:// TYPE_RAM_DISK :閃存
          break;
          case 5:// TYPE_CDROM :光驅(qū)
          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("網(wǎng)絡(luò)設(shè)備名:    " + name);// 網(wǎng)絡(luò)設(shè)備名
          System.out.println("IP地址:    " + ifconfig.getAddress());// IP地址
          System.out.println("子網(wǎng)掩碼:    " + ifconfig.getNetmask());// 子網(wǎng)掩碼
          if ((ifconfig.getFlags() & 1L) <= 0L) {
          System.out.println("!IFF_UP...skipping getNetInterfaceStat");
          continue;
          }
          NetInterfaceStat ifstat = sigar.getNetInterfaceStat(name);
          System.out.println(name + "接收的總包裹數(shù):" + ifstat.getRxPackets());// 接收的總包裹數(shù)
          System.out.println(name + "發(fā)送的總包裹數(shù):" + ifstat.getTxPackets());// 發(fā)送的總包裹數(shù)
          System.out.println(name + "接收到的總字節(jié)數(shù):" + ifstat.getRxBytes());// 接收到的總字節(jié)數(shù)
          System.out.println(name + "發(fā)送的總字節(jié)數(shù):" + ifstat.getTxBytes());// 發(fā)送的總字節(jié)數(shù)
          System.out.println(name + "接收到的錯(cuò)誤包數(shù):" + ifstat.getRxErrors());// 接收到的錯(cuò)誤包數(shù)
          System.out.println(name + "發(fā)送數(shù)據(jù)包時(shí)的錯(cuò)誤數(shù):" + ifstat.getTxErrors());// 發(fā)送數(shù)據(jù)包時(shí)的錯(cuò)誤數(shù)
          System.out.println(name + "接收時(shí)丟棄的包數(shù):" + ifstat.getRxDropped());// 接收時(shí)丟棄的包數(shù)
          System.out.println(name + "發(fā)送時(shí)丟棄的包數(shù):" + ifstat.getTxDropped());// 發(fā)送時(shí)丟棄的包數(shù)
          }
          }
          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() + "網(wǎng)關(guān)廣播地址:" + cfg.getBroadcast());// 網(wǎng)關(guān)廣播地址
          System.out.println(cfg.getName() + "網(wǎng)卡MAC地址:" + cfg.getHwaddr());// 網(wǎng)卡MAC地址
          System.out.println(cfg.getName() + "子網(wǎng)掩碼:" + cfg.getNetmask());// 子網(wǎng)掩碼
          System.out.println(cfg.getName() + "網(wǎng)卡描述信息:" + cfg.getDescription());// 網(wǎng)卡描述信息
          System.out.println(cfg.getName() + "網(wǎng)卡類型" + cfg.getType());//
          }
          }
          }

          posted on 2014-11-18 09:49 順其自然EVO 閱讀(341) 評(píng)論(0)  編輯  收藏 所屬分類: 測試學(xué)習(xí)專欄

          <2014年11月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 崇信县| 阿坝县| 舒城县| 五华县| 西华县| 连城县| 电白县| 东海县| 汕尾市| 临西县| 鹰潭市| 平果县| 华阴市| 梅河口市| 兖州市| 澄迈县| 伊川县| 佛冈县| 武宣县| 沾化县| 郯城县| 青阳县| 新昌县| 开原市| 凌海市| 贡觉县| 高州市| 调兵山市| 手机| 菏泽市| 蓝田县| 徐汇区| 曲靖市| 平江县| 志丹县| 大埔区| 海宁市| 灵璧县| 泸溪县| 景洪市| 榆林市|