3

I am trying to develop an app that will monitor a memory usage of a process located on a server from my PC. I have created an app that will monitor memory usage of a process located on the same PC that the app is running on but cannot figure out how to run the app from my PC and monitor another PC's/Server's process.

Any help would be great of pointing me to the right path.

UPDATE

Here is what I have so far:

    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;
    }
Nick Manojlovic
  • 1,171
  • 10
  • 30
  • 49
  • Possible duplicate of http://stackoverflow.com/questions/865412/check-if-a-process-is-running-on-a-remote-system-using-c-sharp Check the link, it might help! – Munim Jun 28 '12 at 16:37

1 Answers1

5

You can monitor performance counters, including memory utilization, on a remote machine.

You use the PerformanceCounter class, specifically the constructor that takes a remote machine name.

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

The following article walks you through how use remote performance counters, step by step:

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

To understand how to use memory (and CPU) performance counters for a specific process see

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

rene
  • 41,474
  • 78
  • 114
  • 152
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • Hey thanks for your quick comment. The part that I am confused is how do you pass username or password to connect to the server. I am able to ping the server but then when I try to get PerformanceCounter it fils and gives me an error of "Unable to access computer..." – Nick Manojlovic Jun 28 '12 at 18:54
  • See: http://superuser.com/questions/196515/what-permissions-are-necessary-to-view-remote-performance-counter-data – Eric J. Jun 28 '12 at 23:30