2012-06-28 56 views
2

我正在嘗試開發一款應用程序,該應用程序將監視位於我的PC上的服務器上的進程的內存使用情況。我已經創建了一個應用程序,該應用程序可以監視與應用程序運行在同一臺PC上的進程的內存使用情況,但無法弄清楚如何從我的PC運行應用程序並監視另一臺PC /服務器的進程。監視另一臺服務器上的進程的內存使用情況

任何幫助都會指引我走向正確的道路。

UPDATE

這是我到目前爲止有:

private static string sProcName = "PCMain"; 
    private static string machine = "serverName"; 
    private static int sProcMemoryInKB = 10000; 
    static void Main(string[] args) { 
     VerifyRemoteMachineStatus(machine); 
     GetMachineName(); 
     Process ReqProcess; 
     GC.GetTotalMemory(true); 
     ReqProcess = CurrentlyRunning(sProcName); 
     do { 
      if (ReqProcess != null) { 
       System.Threading.Thread.Sleep(1000); 
       // calculate the CPU load 
       System.TimeSpan CPULoad = (DateTime.Now - ReqProcess.StartTime); 
       Console.WriteLine("CPU Load: " + (ReqProcess.TotalProcessorTime.TotalMilliseconds/CPULoad.TotalMilliseconds) * 100); 

       PerformanceCounter WorkingSetMemoryCounter = new PerformanceCounter("Process", "Working Set", sProcName, machine); 
       //System.Threading.Thread.Sleep(1000); 
       Console.WriteLine("Momory (Working Set) " + (WorkingSetMemoryCounter.NextValue()/1024) + "K"); 

       PerformanceCounter WorkingSetPrivateMemoryCounter = new PerformanceCounter("Process", "Working Set - Private", ReqProcess.ProcessName, machine); 
       //System.Threading.Thread.Sleep(1000); 
       Console.WriteLine("Momory (Private Working Set) " + (WorkingSetPrivateMemoryCounter.NextValue()/1024) + "K" + Environment.NewLine + Environment.NewLine); 

       //if ((WorkingSetMemoryCounter.NextValue()/1024) >= sProcMemoryInKB) { 
       // SendMail(); 
       //} 
      } 
     } while (true); 
    } 

    private static Process CurrentlyRunning(string sProcessName) { 
     //get a list of all running processes on current system 
     Process[] Processes = Process.GetProcesses(); 

     //Iterate to every process to check if it is out required process 
     foreach (Process SingleProcess in Processes) { 

      if (SingleProcess.ProcessName.Contains(sProcessName)) { 
       //process found     
       return SingleProcess; 
      } 
     } 

     //Process not found 
     return null; 
    } 

    private static void GetMachineName() { 
     string[] cmdArgs = System.Environment.GetCommandLineArgs(); 
     if ((cmdArgs != null) && (cmdArgs.Length > 1)) { machine = cmdArgs[1]; } 
    } 

    // ping the remote computer 
    private static bool VerifyRemoteMachineStatus(string machineName) { 
     try { 
      using (Ping ping = new Ping()) { 
       PingReply reply = ping.Send(machineName); 
       if (reply.Status == IPStatus.Success) { return true; } 
      } 
     } catch (Exception ex) { 
      // return false for any exception encountered 
      // we'll probably want to just shut down anyway 
     } 
     return false; 
    } 
+0

可能的重複 http://stackoverflow.com/questions/865412/check-if-a-process-is-running-on-a-remote-system-using-c-sharp 檢查鏈接,它可能會有所幫助! –

回答

4

您可以監視performance counters,包括內存利用率,在遠程機器上。

您使用PerformaceCounter類,特別是採用遠程計算機名稱的constructor

public PerformanceCounter(
    string categoryName, 
    string counterName, 
    string instanceName, 
    string machineName // Remote machine name goes here 
) 

下面的文章,詳細介紹瞭如何一步使用遠程性能計數器,步:

http://www.codeproject.com/Articles/29986/A-Simple-Performance-Counter-Application

要了解如何使用內存(與CPU)性能計數器特定的進程

What is the correct Performance Counter to get CPU and Memory Usage of a Process?

+0

嗨,感謝您的快速評論。我感到困惑的部分是你如何傳遞用戶名或密碼連接到服務器。我能ping通服務器,但後來當我嘗試獲得PerformanceCounter它fils並給了我一個錯誤「無法訪問計算機...」 –

+0

請參閱:http://superuser.com/questions/196515/what-permissions - 需要查看的遠程性能計數器數據 –

+0

再次感謝。現在我得到一個錯誤「訪問被拒絕」我是否需要從我自己的PC訪問PerformanceCounter的權限? –

相關問題