【问题标题】:Monitor memory usage of a process on another server监控另一台服务器上进程的内存使用情况
【发布时间】:2012-06-28 16:29:02
【问题描述】:

我正在尝试开发一个应用程序,该应用程序将通过我的 PC 监控位于服务器上的进程的内存使用情况。我创建了一个应用程序,该应用程序将监视位于运行该应用程序的同一台 PC 上的进程的内存使用情况,但无法弄清楚如何从我的 PC 运行该应用程序并监视另一台 PC/服务器的进程。

任何帮助都可以为我指明正确的道路。

更新

这是我目前所拥有的:

    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;
    }

【问题讨论】:

标签: c# networking network-monitoring


【解决方案1】:

您可以在远程机器上监控performance counters,包括内存利用率。

您使用PerformanceCounter 类,特别是采用远程机器名称的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?

【讨论】:

  • 您好,感谢您的快速评论。我感到困惑的部分是你如何传递用户名或密码来连接到服务器。我可以 ping 服务器,但是当我尝试获取 PerformanceCounter 时,它会报错并给我一个错误“无法访问计算机...”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-02
  • 1970-01-01
  • 1970-01-01
  • 2011-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多