简体   繁体   English

IIS进程监视器以​​列出带有PHP(php-cgi.exe)进程的IIS w3wp.exe进程

[英]IIS process monitor to list IIS w3wp.exe processes with their PHP (php-cgi.exe) processes

I'm trying to write small IIS process monitor (CPU and memory usage) which should be as light for the system as possible. 我正在尝试编写小的IIS进程监视器(CPU和内存使用情况),它应该对系统尽可能轻。 All I need is IIS App Pool Name, current Memory Usage and current CPU Usage. 我需要的只是IIS应用程序池名称,当前内存使用率和当前CPU使用率。

I've managed to do it without bigger problems using these two commands: 使用以下两个命令,我已经设法做到了没有更大的问题:

using (var srvman = new ServerManager())
{
    workerProcesses = srvman.WorkerProcesses;
}

and

processes = Process.GetProcesses().Where(processItem => processItem.ProcessName.Contains("w3wp") || processItem.ProcessName.Contains("php-cgi"))

The first one returns running app pools with process ID's and second one is returning process information from system (so here is where I'm getting CPU and Memory usage). 第一个返回具有进程ID的正在运行的应用程序池,第二个从系统返回进程信息(所以这是获取CPU和内存使用情况的位置)。 Joining this two informations gives me almost what I want. 结合这两个信息,几乎可以得到我想要的一切。 The one exception is PHP apps which are running on IIS. 一个例外是在IIS上运行的PHP应用程序。

For PHP apps, php-cgi.exe processes are being spawned which I need to correlate to the IIS App Pool worker process. 对于PHP应用程序,正在生成php-cgi.exe进程,我需要将其与IIS App Pool工作进程相关联。 Do you have any idea how to connect php-cgi.exe processes to their w3wp.exe parents? 您是否知道如何将php-cgi.exe进程连接到其w3wp.exe父级?

Here is an example of the output I would like to produce: 这是我想产生的输出示例:

1x w3wp.exe for site.com is using 15MB memory
4x php-cgi.exe is using 4x 15MB = 60MB memory

I'm planning to sum this information (which in this scenario would be 75 MB of memory usage for site.com). 我打算总结这些信息(在这种情况下,site.com的内存使用量为75 MB)。

You can get the parent process PID using a bit of P/Invoke as described by Simon Mourier . 您可以使用一点P / Invoke来获取父进程PID ,如Simon Mourier所述 Doing so is a low cost operation and will tie the list of all php-cgi process back to their creators. 这样做是一种低成本的操作,并将所有php-cgi进程的列表与其创建者联系在一起。

using (var srvman = new ServerManager())
{
    var procs = from worker in srvman.WorkerProcesses
                let workerProcess = Process.GetProcessById(worker.ProcessId)
                join cgi in Process.GetProcessesByName("php-cgi")
                    on workerProcess.Id equals ParentProcessUtilities.GetParentProcess(cgi.Handle).Id
                    into childProcesses
                select new
                {
                    Worker = worker,
                    WorkerProcess = workerProcess,
                    Children = childProcesses,
                    TotalMemoryUse = workerProcess.PrivateMemorySize64
                        + childProcesses.Sum(p => p.PrivateMemorySize64)
                };

    foreach (var proc in procs)
    {
        Console.WriteLine("Worker {0}:{1} using {2} total bytes", proc.Worker.AppPoolName,
            proc.Worker.ProcessId, proc.TotalMemoryUse);

        foreach (var child in proc.Children)
        {
            Console.WriteLine("\tphp-cgi process {0} using {1} bytes", child.Id, child.PrivateMemorySize64);
        }
    }
}

Output 产量

C:\drop> phpProcessTest.exe
Worker DefaultAppPool:4396 using 61530112 total bytes
    php-cgi process 3540 using 7024640 bytes
    php-cgi process 3144 using 6389760 bytes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM