How to determine which script is being executed in PHP-FPM process
Asked Answered
B

5

32

I am running nginx + php-fpm. Is there any way how can I know what is each of the PHP processes doing? Something like extended mod_status in apache, where I can see that apache process with PID x is processing URL y. I'm not sure if the PHP process knows the URL, but getting the script path and name will be sufficient.

Bless answered 22/2, 2013 at 11:48 Comment(0)
B
36

After some googling hours and browsing PHP.net bug tracking system I have found the solution. It is available since PHP 5.3.8 or 5.3.9, but doesn't seem to be documented. Based on feature request #54577, the status page supports option full, which will display status of each worker separately. So for example the URL will be http://server.com/php-status?full and sample output looks like:

pid:                  22816
state:                Idle
start time:           22/Feb/2013:15:03:42 +0100
start since:          10933
requests:             28352
request duration:     1392
request method:       GET
request URI:          /ad.php?zID=597
content length:       0
user:                 -
script:               /home/web/server.com/ad/ad.php
last request cpu:     718.39
last request memory:  1310720
Bless answered 22/2, 2013 at 17:9 Comment(5)
Another very useful option is ?html, e.g.: server.com/php-status?full&html (it will format the output as html table, which makes it a lot easier to see all running scripts at once)Sabah
how can i access it shoud i add any paramter to nginx config ?Jeaz
@babakfaghihian yes, you need to pass that URL (/php-status for example) to php-fpm.Bless
Unfortunately if your PHP app uses URL rewriting (aka 'friendly URLs'), which most MVC frameworks do, then request URI will always display as /index.php. The actual URL is passed in the REQUEST_URI env var which doesn't appear in the status output.Runty
@Bless : Dude, you just saved my life ! I had been chasing some scripts that were eating up memory on my php-fpm container and had no luck with all the methods i had found...until this http://server.com/php-status?full That "?full" parameter just brought the light and showed me the faulty URIs/scripts i was looking after. Thank you very much !Gramps
S
16

PHP-FPM has a built in status monitor, though it's not as details as mod_status. From the php-fpm config file /etc/php-fpm.d/www.conf (on CentOS 6)

; The URI to view the FPM status page. If this value is not set, no URI will be
; recognized as a status page. By default, the status page shows the following
; information:
;   accepted conn    - the number of request accepted by the pool;
;   pool             - the name of the pool;
;   process manager  - static or dynamic;
;   idle processes   - the number of idle processes;
;   active processes - the number of active processes;
;   total processes  - the number of idle + active processes.
; The values of 'idle processes', 'active processes' and 'total processes' are
; updated each second. The value of 'accepted conn' is updated in real time.
; Example output:
;   accepted conn:   12073
;   pool:             www
;   process manager:  static
;   idle processes:   35
;   active processes: 65
;   total processes:  100
; By default the status page output is formatted as text/plain. Passing either
; 'html' or 'json' as a query string will return the corresponding output
; syntax. Example:
;   http://www.foo.bar/status
;   http://www.foo.bar/status?json
;   http://www.foo.bar/status?html
; Note: The value must start with a leading slash (/). The value can be
;       anything, but it may not be a good idea to use the .php extension or it
;       may conflict with a real PHP file.
; Default Value: not set
;pm.status_path = /status

If you enable this, you can then pass the path from nginx to your socket/port for PHP-FPM and you can view the status page.

nginx.conf:

location /status {

    include fastcgi_params;
    fastcgi_pass unix:/var/lib/php/php-fpm.sock;

}
Slaton answered 22/2, 2013 at 12:16 Comment(4)
yes I know about this status from php-fpm, I am already using it in munin and zabbix monitoring. However it provides only aggregate information and not per-process info.Bless
Don't think there is much more you can do apart from creating a custom logging solution or setting up access log processing, but that wouldn't give you the detailed information you need such as processing time. I'm sure as PHP-FPM matures they will extend the status reporting.Slaton
Finally I have found that the status page supports per-process info (see my answer).Bless
what a horrible idea for the developers to make the debugging info available on a path like this. if you are doing this, make sure the url is restrictedMarkley
D
7

cgi command line is more convinient:

SCRIPT_NAME=/status \
SCRIPT_FILENAME=/status \
REQUEST_METHOD=GET \
cgi-fcgi -bind -connect 127.0.0.1:9000
Diestock answered 10/11, 2014 at 3:50 Comment(2)
Also note that you must add QUERY_STRING=full to return what OP is looking for. Changing SCRIPT_FILENAME to /status?full doesn't work.Imputable
You will still need to enable the status page by uncommenting pm.status_path; the advantage to this method is that it doesn't need to be exposed by the web server. SCRIPT_NAME and SCRIPT_FILENAME should be the same as pm.status_path from the php-fpm.conf file. That last parameter (127.0.0.1:9000) is the connection to the FCGI server, not the web server--it should be whatever the listen parameter is in that same INI file. If it's a socket, you may need to use sudo to connect, in which case also use -E to tell sudo to pass through the environment variables to cgi-fcgi.Tatianna
G
0

You can use strace to show the scripts being run - and many other things - in real time. It's pretty verbose, but it can give you a good overall picture of what's going on:

# switch php-fpm7.0 for process you're using
sudo strace -f $(pidof php-fpm7.0 | sed 's/\([0-9]*\)/\-p \1/g')

The above will attach to the forked processes of php fpm. Use -p to attach to a particular pid.

The above would get the scrip path. To get the urls, you would look at your nginx / apache access logs.

As a side note, to see the syscalls and which ones are taking longest:

sudo strace -c -f $(pidof php-fpm7.0 | sed 's/\([0-9]*\)/\-p \1/g')

Wait a while, then hit Ctr-C

Glaab answered 11/4, 2018 at 4:24 Comment(1)
This works strace -f $(pidof php-fpm | sed 's/\([0-9]*\)/\-p \1/g')Farmland
V
0

There is also a PHP function called fpm_get_status()

<?php print_r(fpm_get_status()); ?>
Array
(
    [pool] => example.com
    [process-manager] => ondemand
    [start-time] => 1722156649
    [start-since] => 42082
    [accepted-conn] => 208729
    [listen-queue] => 0
    [max-listen-queue] => 0
    [listen-queue-len] => 0
    [idle-processes] => 7
    [active-processes] => 12
    [total-processes] => 19
    [max-active-processes] => 18
    [max-children-reached] => 0
    [slow-requests] => 0
    [procs] => Array
        (
            [0] => Array
                (
                    [pid] => 2576722
                    [state] => Running
                    [start-time] => 1722160069
                    [start-since] => 38662
                    [requests] => 289
                    [request-duration] => 38367984198
                    [request-method] => GET
                    [request-uri] => /index.php
                    [query-string] => 
                    [request-length] => 0
                    [user] => -
                    [script] => /var/www/vhosts/example.com/httpdocs/index.php
                    [last-request-cpu] => 0
                    [last-request-memory] => 0
                )

            [1] => Array
                (
                    [pid] => 2803975
                    [state] => Idle
                    [start-time] => 1722198476
                    [start-since] => 255
                    [requests] => 146
                    [request-duration] => 70370
                    [request-method] => GET
                    [request-uri] => /keyword_compact_list_style.php
                    [query-string] => keyword_url_string=recreation
                    [request-length] => 0
                    [user] => -
                    [script] => /var/www/vhosts/example.com/httpdocs/keyword_compact_list_style.php
                    [last-request-cpu] => 0
                    [last-request-memory] => 4194304
                )

            [2] => Array
                (
                    [pid] => 2266367
                    [state] => Running
                    [start-time] => 1722157343
                    [start-since] => 41388
                    [requests] => 465
                    [request-duration] => 40694347343
                    [request-method] => GET
                    [request-uri] => /index.php
                    [query-string] => 
                    [request-length] => 0
                    [user] => -
                    [script] => /var/www/vhosts/example.com/httpdocs/index.php
                    [last-request-cpu] => 0
                    [last-request-memory] => 0
                )

            [3] => Array
                (
                    [pid] => 2927979
                    [state] => Running
                    [start-time] => 1722163080
                    [start-since] => 35651
                    [requests] => 119
                    [request-duration] => 35579263056
                    [request-method] => GET
                    [request-uri] => /index.php
                    [query-string] => 
                    [request-length] => 0
                    [user] => -
                    [script] => /var/www/vhosts/example.com/httpdocs/index.php
                    [last-request-cpu] => 0
                    [last-request-memory] => 0
                )

            [4] => Array
                (
                    [pid] => 2303575
                    [state] => Running
                    [start-time] => 1722157701
                    [start-since] => 41030
                    [requests] => 370
                    [request-duration] => 40516198463
                    [request-method] => GET
                    [request-uri] => /index.php
                    [query-string] => 
                    [request-length] => 0
                    [user] => -
                    [script] => /var/www/vhosts/example.com/httpdocs/index.php
                    [last-request-cpu] => 0
                    [last-request-memory] => 0
                )

            [5] => Array
                (
                    [pid] => 2581327
                    [state] => Running
                    [start-time] => 1722160109
                    [start-since] => 38622
                    [requests] => 44
                    [request-duration] => 38570980353
                    [request-method] => GET
                    [request-uri] => /index.php
                    [query-string] => 
                    [request-length] => 0
                    [user] => -
                    [script] => /var/www/vhosts/example.com/httpdocs/index.php
                    [last-request-cpu] => 0
                    [last-request-memory] => 0
                )

            [6] => Array
                (
                    [pid] => 3815068
                    [state] => Running
                    [start-time] => 1722170797
                    [start-since] => 27934
                    [requests] => 21
                    [request-duration] => 27911204601
                    [request-method] => GET
                    [request-uri] => /domain.php
                    [query-string] => domain=reecoupons.net
                    [request-length] => 0
                    [user] => -
                    [script] => /var/www/vhosts/example.com/httpdocs/domain.php
                    [last-request-cpu] => 0
                    [last-request-memory] => 0
                )

            [7] => Array
                (
                    [pid] => 2413654
                    [state] => Running
                    [start-time] => 1722158686
                    [start-since] => 40045
                    [requests] => 58
                    [request-duration] => 39977743199
                    [request-method] => GET
                    [request-uri] => /index.php
                    [query-string] => 
                    [request-length] => 0
                    [user] => -
                    [script] => /var/www/vhosts/example.com/httpdocs/index.php
                    [last-request-cpu] => 0
                    [last-request-memory] => 0
                )

            [8] => Array
                (
                    [pid] => 2757470
                    [state] => Idle
                    [start-time] => 1722198054
                    [start-since] => 677
                    [requests] => 402
                    [request-duration] => 53141
                    [request-method] => GET
                    [request-uri] => /domain.php
                    [query-string] => domain=autoskola-pernica.cz
                    [request-length] => 0
                    [user] => -
                    [script] => /var/www/vhosts/example.com/httpdocs/domain.php
                    [last-request-cpu] => 0
                    [last-request-memory] => 2097152
                )

            [9] => Array
                (
                    [pid] => 3615111
                    [state] => Running
                    [start-time] => 1722169049
                    [start-since] => 29682
                    [requests] => 81
                    [request-duration] => 29603208877
                    [request-method] => GET
                    [request-uri] => /index.php
                    [query-string] => 
                    [request-length] => 0
                    [user] => -
                    [script] => /var/www/vhosts/example.com/httpdocs/index.php
                    [last-request-cpu] => 0
                    [last-request-memory] => 0
                )

            [10] => Array
                (
                    [pid] => 3432959
                    [state] => Running
                    [start-time] => 1722167470
                    [start-since] => 31261
                    [requests] => 354
                    [request-duration] => 30822627032
                    [request-method] => GET
                    [request-uri] => /domain.php
                    [query-string] => domain=blow-job-movies.com
                    [request-length] => 0
                    [user] => -
                    [script] => /var/www/vhosts/example.com/httpdocs/domain.php
                    [last-request-cpu] => 0
                    [last-request-memory] => 0
                )

            [11] => Array
                (
                    [pid] => 3228543
                    [state] => Running
                    [start-time] => 1722165712
                    [start-since] => 33019
                    [requests] => 211
                    [request-duration] => 32687519764
                    [request-method] => GET
                    [request-uri] => /keyword_compact_list_style.php
                    [query-string] => keyword_url_string=評判
                    [request-length] => 0
                    [user] => -
                    [script] => /var/www/vhosts/example.com/httpdocs/keyword_compact_list_style.php
                    [last-request-cpu] => 0
                    [last-request-memory] => 0
                )

            [12] => Array
                (
                    [pid] => 41812
                    [state] => Running
                    [start-time] => 1722174441
                    [start-since] => 24290
                    [requests] => 393
                    [request-duration] => 23575447402
                    [request-method] => GET
                    [request-uri] => /index.php
                    [query-string] => 
                    [request-length] => 0
                    [user] => -
                    [script] => /var/www/vhosts/example.com/httpdocs/index.php
                    [last-request-cpu] => 0
                    [last-request-memory] => 0
                )

            [13] => Array
                (
                    [pid] => 2768530
                    [state] => Idle
                    [start-time] => 1722198152
                    [start-since] => 579
                    [requests] => 347
                    [request-duration] => 13988
                    [request-method] => GET
                    [request-uri] => /domain.php
                    [query-string] => domain=kiva-netz.com
                    [request-length] => 0
                    [user] => -
                    [script] => /var/www/vhosts/example.com/httpdocs/domain.php
                    [last-request-cpu] => 0
                    [last-request-memory] => 2097152
                )

            [14] => Array
                (
                    [pid] => 2825399
                    [state] => Idle
                    [start-time] => 1722198668
                    [start-since] => 63
                    [requests] => 38
                    [request-duration] => 34865
                    [request-method] => GET
                    [request-uri] => /domain.php
                    [query-string] => domain=abgqqvip.com
                    [request-length] => 0
                    [user] => -
                    [script] => /var/www/vhosts/example.com/httpdocs/domain.php
                    [last-request-cpu] => 57.364117461538
                    [last-request-memory] => 4194304
                )

            [15] => Array
                (
                    [pid] => 2824160
                    [state] => Idle
                    [start-time] => 1722198654
                    [start-since] => 77
                    [requests] => 45
                    [request-duration] => 30466
                    [request-method] => GET
                    [request-uri] => /domain.php
                    [query-string] => domain=domino78.com
                    [request-length] => 0
                    [user] => -
                    [script] => /var/www/vhosts/example.com/httpdocs/domain.php
                    [last-request-cpu] => 32.823474615908
                    [last-request-memory] => 6291456
                )

            [16] => Array
                (
                    [pid] => 2744299
                    [state] => Idle
                    [start-time] => 1722197943
                    [start-since] => 788
                    [requests] => 465
                    [request-duration] => 6325
                    [request-method] => GET
                    [request-uri] => /domain.php
                    [query-string] => domain=e-manabiya.com
                    [request-length] => 0
                    [user] => -
                    [script] => /var/www/vhosts/example.com/httpdocs/domain.php
                    [last-request-cpu] => 0
                    [last-request-memory] => 2097152
                )

            [17] => Array
                (
                    [pid] => 2744955
                    [state] => Idle
                    [start-time] => 1722197948
                    [start-since] => 783
                    [requests] => 465
                    [request-duration] => 53234
                    [request-method] => GET
                    [request-uri] => /domain.php
                    [query-string] => domain=bis.es
                    [request-length] => 0
                    [user] => -
                    [script] => /var/www/vhosts/example.com/httpdocs/domain.php
                    [last-request-cpu] => 18.784986618482
                    [last-request-memory] => 2097152
                )

            [18] => Array
                (
                    [pid] => 2752270
                    [state] => Running
                    [start-time] => 1722198011
                    [start-since] => 720
                    [requests] => 416
                    [request-duration] => 296
                    [request-method] => GET
                    [request-uri] => /x.php
                    [query-string] => 
                    [request-length] => 0
                    [user] => -
                    [script] => /var/www/vhosts/example.com/httpdocs/x.php
                    [last-request-cpu] => 0
                    [last-request-memory] => 0
                )

        )

)
Vivica answered 28/7 at 20:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.