2017-04-20 72 views
-1

我想獲得CPU內存與Java的許多服務器可用空間 任何想法。CPU內存和遠程服務器的可用空間java

+0

到目前爲止您嘗試過了什麼? – dunni

+0

你有什麼試過,你有什麼麻煩?我建議你使用一個工具,它已經爲你做了這個,你可以使用谷歌研究。 –

+0

我試過Process c = Runtime.getRuntime()。exec(commande);但我有我的本地機器的系統inf我至少有其他服務器的RAM cpu – oulaya

回答

0

我只是一名培訓生,但我的解決方案是編寫一個服務器套接字並在服務器上運行它。 通過客戶端套接字打開連接並詢問信息。

比我會simpel返回使用MB或簡單地將其轉換爲%值。

無論如何,我寧願使用現有的和經過測試的解決方案。

編輯:

這裏一個教程就如何獲得免費的,Max和使用RAM。 http://crunchify.com/java-runtime-get-free-used-and-total-memory-in-java/

EDIT2: 這裏我愛迪爾的代碼:

服務器部分:

public static void main(String[] args) { 
    try { 
     ServerSocket sSocket = new ServerSocket(6969); 
     Socket socket = sSocket.accept(); 
     DataInputStream dis = new DataInputStream(socket.getInputStream()); 
     DataOutputStream dout = new DataOutputStream(socket.getOutputStream()); 


     String str1 = ""; 

     //Add Varaibles you want to return 
     double ram = 15.0; 

     while (!str1.equals("stop")) { 
      str1=dis.readUTF(); 

      //Return Value you need by param 
      if (str1.equals("getRam")) { 
       //Add Your calculation here 
       dout.writeUTF(String.valueOf(ram)); 
       dout.flush(); 
      } 
      else if(str1.equals("stop")){ 
       dout.writeUTF("stop"); 
       dout.flush(); 
      } 
      else { 
       dout.writeUTF("Unknown Command"); 
       dout.flush(); 
      } 


     } 
     sSocket.close(); 
     socket.close(); 
     dis.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 


    } 
} 

客戶端部分:

public class Main { 


    public static void main(String[] args) { 
     try { 
      String serverIp = "localhost"; 
      Socket socket = new Socket(serverIp, 6969); 
      DataInputStream dIn = new DataInputStream(socket.getInputStream()); 
      DataOutputStream dOut= new DataOutputStream(socket.getOutputStream()); 
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 

      String str = ""; 
      String str2 = ""; 


       dOut.writeUTF("getRam"); 

       //for manual param transmitting 
       //str = br.readLine(); 
       //dOut.writeUTF(str); 
       dOut.flush(); 
       str2=dIn.readUTF(); 
       System.out.println("Server response:" + str2); 
       br.readLine(); 



     } catch (UnknownHostException e) { 
      e.printStackTrace(); 
      System.out.println(e); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      System.out.println(e); 

     } 

    } 

} 

我希望幫助你。 無論如何,我不認爲你可以在沒有連接或連接到服務器的情況下獲取數據。

+0

謝謝,但問題是我想要一個簡單的代碼本地,因爲我將使用junit後做我的測試 – oulaya

+0

但是什麼是你的服務器連接? – Ice

1

,這具體取決於目標平臺上,有很多種方法:

  • 讓每個系統上運行的Java客戶端將收集要求此信息,然後有一個收集服務器,並顯示所有這些信息。這是大多數平臺獨立的解決方案,但要求您編寫客戶端和服務器,並在每個系統上安裝客戶端。
  • 如果在平臺上支持SSH,則可以使用Java程序登錄到每個系統並使用命令行實用程序來查詢資源。但是,這需要您爲每個系統提供證書或可用的SSH密鑰。
  • 在目標平臺上使用NET-SNMP(如果支持並啓用資源收集)。然後Java程序可以查詢SNMP代理來收集資源使用情況。您需要一個庫來與SNMP代理交談。您可以爲此目的使用SNMP4J或AdventNet。
+0

請你能給我一個例子 – oulaya

相關問題