最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Java代碼獲取服務(wù)器性能信息及局域網(wǎng)內(nèi)主機(jī)名

 更新時(shí)間:2015年11月17日 15:39:10   作者:snoopy7713  
這篇文章主要介紹了使用Java代碼獲取服務(wù)器性能信息及局域網(wǎng)內(nèi)主機(jī)名的方法,方便對(duì)服務(wù)器的遠(yuǎn)程管理和團(tuán)隊(duì)協(xié)作時(shí)用到,而且文中的方法無需調(diào)用jni,需要的朋友可以參考下

最近做個(gè)項(xiàng)目,就是要取得cpu占有率等等的系統(tǒng)信息,一開始以為要用動(dòng)態(tài)鏈接庫(kù)了,但后來發(fā)現(xiàn)可以像下面這樣做,不去調(diào)用jni,這樣省去了很多看新技術(shù)的時(shí)間o(∩_∩)o...
在Java中,可以獲得總的物理內(nèi)存、剩余的物理內(nèi)存、已使用的物理內(nèi)存等信息,下面例子可以取得這些信息,并且獲得在Windows下的內(nèi)存使用率。
     首先編寫一個(gè)MonitorInfoBean類,用來裝載監(jiān)控的一些信息,包括物理內(nèi)存、剩余的物理內(nèi)存、已使用的物理內(nèi)存、內(nèi)存使用率等字段,該類的代碼如下:

package com.amgkaka.performance; 
 
/** */ /** 
 * 監(jiān)視信息的JavaBean類. 
 * @author amg 
 * @version 1.0  
 * Creation date: 2008-4-25 - 上午10:37:00 
 */  
public  class MonitorInfoBean { 
  /** */ /** 可使用內(nèi)存. */  
  private  long totalMemory; 
   
  /** */ /** 剩余內(nèi)存. */  
  private  long freeMemory; 
   
  /** */ /** 最大可使用內(nèi)存. */  
  private  long maxMemory; 
   
  /** */ /** 操作系統(tǒng). */  
  private String osName; 
   
  /** */ /** 總的物理內(nèi)存. */  
  private  long totalMemorySize; 
   
  /** */ /** 剩余的物理內(nèi)存. */  
  private  long freePhysicalMemorySize; 
   
  /** */ /** 已使用的物理內(nèi)存. */  
  private  long usedMemory; 
   
  /** */ /** 線程總數(shù). */  
  private  int totalThread; 
   
  /** */ /** cpu使用率. */  
  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; 
  } 
} 

 
接著編寫一個(gè)獲得當(dāng)前的監(jiān)控信息的接口,該類的代碼如下所示:

package com.amgkaka.performance; 
 
/** */ /** 
 * 獲取系統(tǒng)信息的業(yè)務(wù)邏輯類接口. 
 * @author amg * @version 1.0  
 * Creation date: 2008-3-11 - 上午10:06:06 
 */  
public  interface IMonitorService { 
  /** */ /** 
   * 獲得當(dāng)前的監(jiān)控對(duì)象. 
   * @return 返回構(gòu)造好的監(jiān)控對(duì)象 
   * @throws Exception 
   * @author amgkaka 
   * Creation date: 2008-4-25 - 上午10:45:08 
   */  
  public MonitorInfoBean getMonitorInfoBean() throws Exception; 
 
} 

  該類的實(shí)現(xiàn)類MonitorServiceImpl如下所示:

package com.amgkaka.performance; 
 
import java.io.InputStreamReader; 
import java.io.LineNumberReader; 
 
import sun.management.ManagementFactory; 
 
import com.sun.management.OperatingSystemMXBean; 
 
/** */ /** 
 * 獲取系統(tǒng)信息的業(yè)務(wù)邏輯實(shí)現(xiàn)類. 
 * @author amg * @version 1.0 Creation date: 2008-3-11 - 上午10:06:06 
 */  
public  class MonitorServiceImpl implements IMonitorService { 
  //可以設(shè)置長(zhǎng)些,防止讀到運(yùn)行此次系統(tǒng)檢查時(shí)的cpu占用率,就不準(zhǔn)了  
  private  static  final  int CPUTIME = 5000 ; 
 
  private  static  final  int PERCENT = 100 ; 
 
  private  static  final  int FAULTLENGTH = 10 ; 
 
  /** */ /** 
   * 獲得當(dāng)前的監(jiān)控對(duì)象. 
   * @return 返回構(gòu)造好的監(jiān)控對(duì)象 
   * @throws Exception 
   * @author amg   * Creation date: 2008-4-25 - 上午10:45:08 
   */  
  public MonitorInfoBean getMonitorInfoBean() throws Exception { 
    int kb = 1024 ; 
     
    // 可使用內(nèi)存  
    long totalMemory = Runtime.getRuntime().totalMemory() / kb; 
    // 剩余內(nèi)存  
    long freeMemory = Runtime.getRuntime().freeMemory() / kb; 
    // 最大可使用內(nèi)存  
    long maxMemory = Runtime.getRuntime().maxMemory() / kb; 
 
    OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory 
        .getOperatingSystemMXBean(); 
 
    // 操作系統(tǒng)  
    String osName = System.getProperty("os.name" ); 
    // 總的物理內(nèi)存  
    long totalMemorySize = osmxb.getTotalPhysicalMemorySize() / kb; 
    // 剩余的物理內(nèi)存  
    long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize() / kb; 
    // 已使用的物理內(nèi)存  
    long usedMemory = (osmxb.getTotalPhysicalMemorySize() - osmxb 
        .getFreePhysicalMemorySize()) 
        / kb; 
 
    // 獲得線程總數(shù)  
    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(); 
    } 
     
    // 構(gòu)造返回對(duì)象  
    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; 
  } 
 
  /** */ /** 
   * 獲得CPU使用率. 
   * @return 返回cpu使用率 
   * @author amg   * Creation date: 2008-4-25 - 下午06:05:11 
   */  
  private  double getCpuRatioForWindows() { 
    try { 
      String procCmd = System.getenv("windir" ) 
          + "\\system32\\wbem\\wmic.exe process get Caption,CommandLine,"  
          + "KernelModeTime,ReadOperationCount,ThreadCount,UserModeTime,WriteOperationCount" ; 
      // 取進(jìn)程信息  
      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 ; 
    } 
  } 
 
  /** */ /** 
   * 讀取CPU信息. 
   * @param proc 
   * @return 
   * @author amg   * Creation date: 2008-4-25 - 下午06:10:14 
   */  
  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 ; 
        } 
        // 字段出現(xiàn)順序:Caption,CommandLine,KernelModeTime,ReadOperationCount,  
        // ThreadCount,UserModeTime,WriteOperation  
        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 ; 
  } 
   
  /** */ /** 
   * 測(cè)試方法. 
   * @param args 
   * @throws Exception 
   * @author amg   * Creation date: 2008-4-30 - 下午04:47:29 
   */  
  public  static  void main(String[] args) throws Exception { 
    IMonitorService service = new MonitorServiceImpl(); 
    MonitorInfoBean monitorInfo = service.getMonitorInfoBean(); 
    System.out.println("cpu占有率=" + monitorInfo.getCpuRatio()); 
     
    System.out.println("可使用內(nèi)存=" + monitorInfo.getTotalMemory()); 
    System.out.println("剩余內(nèi)存=" + monitorInfo.getFreeMemory()); 
    System.out.println("最大可使用內(nèi)存=" + monitorInfo.getMaxMemory()); 
     
    System.out.println("操作系統(tǒng)=" + monitorInfo.getOsName()); 
    System.out.println("總的物理內(nèi)存=" + monitorInfo.getTotalMemorySize() + "kb" ); 
    System.out.println("剩余的物理內(nèi)存=" + monitorInfo.getFreeMemory() + "kb" ); 
    System.out.println("已使用的物理內(nèi)存=" + monitorInfo.getUsedMemory() + "kb" ); 
    System.out.println("線程總數(shù)=" + monitorInfo.getTotalThread() + "kb" ); 
  } 
} 

 
該實(shí)現(xiàn)類中需要用到一個(gè)自己編寫byte的工具類,該類的代碼如下所示:

package com.amgkaka.performance; 
 
/** */ /** 
 * byte操作類. 
 * @author amg * @version 1.0  
 * Creation date: 2008-4-30 - 下午04:57:23 
 */  
public  class Bytes { 
  /** */ /** 
   * 由于String.subString對(duì)漢字處理存在問題(把一個(gè)漢字視為一個(gè)字節(jié)),因此在 
   * 包含漢字的字符串時(shí)存在隱患,現(xiàn)調(diào)整如下: 
   * @param src 要截取的字符串 
   * @param start_idx 開始坐標(biāo)(包括該坐標(biāo)) 
   * @param end_idx  截止坐標(biāo)(包括該坐標(biāo)) 
   * @return 
   */  
  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; 
  } 
} 

 
運(yùn)行下MonitorBeanImpl類,讀者將會(huì)看到當(dāng)前的內(nèi)存、cpu利用率等信息。

PS:得到局域網(wǎng)內(nèi)所有主機(jī)名的方法

import java.net.InetAddress;
import java.net.UnknownHostException;
public class A { 
 
 static public void main(String[] args) { 
 try {
   //通過主機(jī)名稱得到IP地址
  InetAddress address = InetAddress.getByName("192.168.9.148");
  System.out.println("192.168.9.148"+": "+address.getHostAddress());
//  通過IP得到主機(jī)名稱
  String ips="192.168.9.",ip;
  InetAddress addip;
    for(int i=148;i<255;i++){
    ip=ips+i;  
    addip=InetAddress.getByName(ip); 
     System.out.println(ip+": "+addip.getHostName());
   
    }
  
   
   
  }
  catch(UnknownHostException uhe) {
  System.err.println("Unable to find: "+"192.168.9.148");
  }
 } 
 }

相關(guān)文章

  • Hadoop 使用IntelliJ IDEA 進(jìn)行遠(yuǎn)程調(diào)試代碼的配置方法

    Hadoop 使用IntelliJ IDEA 進(jìn)行遠(yuǎn)程調(diào)試代碼的配置方法

    這篇文章主要介紹了Hadoop 使用IntelliJ IDEA 進(jìn)行遠(yuǎn)程調(diào)試代碼的配置方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • java中實(shí)現(xiàn)自定義注解方式

    java中實(shí)現(xiàn)自定義注解方式

    注解是Java中的一種元數(shù)據(jù),可以修飾方法、類、參數(shù)和包等,自定義注解需要public修飾符、@interface關(guān)鍵字,以及注解名稱和類型元素,元注解如@Target、@Retention等用于修飾注解,指定注解的適用范圍和生命周期,自定義注解的使用涉及到通過反射解析注解
    2024-11-11
  • Java設(shè)計(jì)模塊系列之書店管理系統(tǒng)單機(jī)版(二)

    Java設(shè)計(jì)模塊系列之書店管理系統(tǒng)單機(jī)版(二)

    這篇文章主要為大家詳細(xì)介紹了Java單機(jī)版的書店管理系統(tǒng)設(shè)計(jì)模塊和思想第二章,感興趣的小伙伴們可以參考一下
    2016-08-08
  • 最簡(jiǎn)單的在IntelliJ IDEA導(dǎo)入一個(gè)本地項(xiàng)目教程(圖文)

    最簡(jiǎn)單的在IntelliJ IDEA導(dǎo)入一個(gè)本地項(xiàng)目教程(圖文)

    這篇文章主要介紹了最簡(jiǎn)單的在IntelliJ IDEA導(dǎo)入一個(gè)本地項(xiàng)目教程(圖文),文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Nacos注冊(cè)中心的部署與用法示例詳解

    Nacos注冊(cè)中心的部署與用法示例詳解

    注冊(cè)中心是微服務(wù)架構(gòu)中的紐帶,類似于“通訊錄”,它記錄了服務(wù)和服務(wù)地址的映射關(guān)系,本文通過示例代碼給大家介紹Nacos注冊(cè)中心的部署與用法,感興趣的朋友跟隨小編一起看看吧
    2022-02-02
  • Java兩個(gè)集合取差集4種方式舉例

    Java兩個(gè)集合取差集4種方式舉例

    在Java?編程中,經(jīng)常需要對(duì)集合進(jìn)行一些操作,比如取兩個(gè)集合的交集、并集和差集,下面這篇文章主要給大家介紹了關(guān)于Java兩個(gè)集合取差集的4種方式,需要的朋友可以參考下
    2024-08-08
  • springmvc 傳遞和接收數(shù)組參數(shù)的實(shí)例

    springmvc 傳遞和接收數(shù)組參數(shù)的實(shí)例

    下面小編就為大家分享一篇springmvc 傳遞和接收數(shù)組參數(shù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • springboot實(shí)現(xiàn)修改請(qǐng)求狀態(tài)404改為200

    springboot實(shí)現(xiàn)修改請(qǐng)求狀態(tài)404改為200

    這篇文章主要介紹了springboot實(shí)現(xiàn)修改請(qǐng)求狀態(tài)404改為200方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Java讀取txt文件中的數(shù)據(jù)賦給String變量方法

    Java讀取txt文件中的數(shù)據(jù)賦給String變量方法

    今天小編就為大家分享一篇Java讀取txt文件中的數(shù)據(jù)賦給String變量方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • 深入理解JAVA抽象類和接口的比較與異同

    深入理解JAVA抽象類和接口的比較與異同

    這篇文章主要為大家詳細(xì)介紹了JAVA抽象類和接口的比較,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03

最新評(píng)論

杂多县| 义乌市| 马龙县| 娱乐| 当雄县| 仁布县| 贵南县| 芦溪县| 香港 | 开原市| 河源市| 广河县| 通化县| 淮滨县| 广元市| 隆安县| 南岸区| 吴忠市| 淮安市| 全椒县| 阜南县| 宽甸| 赞皇县| 青浦区| 桐城市| 涞水县| 视频| 沙湾县| 合川市| 大宁县| 元阳县| 大邑县| 盘山县| 万年县| 石屏县| 房产| 海宁市| 临泽县| 蒙山县| 隆子县| 铜山县|