summaryrefslogtreecommitdiff
path: root/sapi/fpm/tests/status.inc
blob: 7706942b3d1109b24543fc7e3485f451fcf864ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php

namespace FPM;

class Status
{
    const HTML_TITLE = 'PHP-FPM Status Page';

    /**
     * @var array
     */
    private $contentTypes = [
        'plain'       => 'text/plain',
        'html'        => 'text/html',
        'xml'         => 'text/xml',
        'json'        => 'application/json',
        'openmetrics' => 'application/openmetrics-text; version=1.0.0; charset=utf-8',
    ];

    /**
     * @var array
     */
    private $defaultFields = [
        'pool'                 => '\w+',
        'process manager'      => '(static|dynamic|ondemand)',
        'start time'           => '\d+\/\w{3}\/\d{4}:\d{2}:\d{2}:\d{2}\s[+-]\d{4}',
        'start since'          => '\d+',
        'accepted conn'        => '\d+',
        'listen queue'         => '\d+',
        'max listen queue'     => '\d+',
        'listen queue len'     => '\d+',
        'idle processes'       => '\d+',
        'active processes'     => '\d+',
        'total processes'      => '\d+',
        'max active processes' => '\d+',
        'max children reached' => '\d+',
        'slow requests'        => '\d+',
    ];

    /**
     * Check status page.
     *
     * @param Response $response
     * @param array $fields
     * @param string $type
     * @throws \Exception
     */
    public function checkStatus(Response $response, array $fields, string $type)
    {
        if (!isset($this->contentTypes[$type])) {
            throw new \Exception('Invalid content type ' . $type);
        }

        $body = $response->getBody($this->contentTypes[$type]);
        if ($body === null) {
            return;
        }
        $method = "checkStatus" . ucfirst($type);

        $this->$method($body, array_merge($this->defaultFields, $fields));
    }

    /**
     * Make status check for status page.
     *
     * @param string $body
     * @param array $fields
     * @param string $rowPattern
     * @param string $header
     * @param string $footer
     * @param null|callable $nameTransformer
     * @param null|callable $valueTransformer
     * @param bool $startTimeTimestamp
     * @param bool $closingName
     */
    private function makeStatusCheck(
        string $body,
        array $fields,
        string $rowPattern,
        string $header = '',
        string $footer = '',
        $nameTransformer = null,
        $valueTransformer = null,
        bool $startTimeTimestamp = false,
        bool $closingName = false
    ) {

        if ($startTimeTimestamp && $fields['start time'][0] === '\\') {
            $fields['start time'] = '\d+';
        }
        $pattern = '|' . $header;
        foreach ($fields as $name => $value) {
            if ($nameTransformer) {
                $name = call_user_func($nameTransformer, $name);
            }
            if ($valueTransformer) {
                $value = call_user_func($valueTransformer, $value);
            }
            if ($closingName) {
                $pattern .= sprintf($rowPattern, $name, $value, $name);
            } else {
                $pattern .= sprintf($rowPattern, $name, $value);
            }
        }
        $pattern = rtrim($pattern, $rowPattern[strlen($rowPattern) - 1]);
        $pattern .= $footer . '|';

        if (!preg_match($pattern, $body)) {
            echo "ERROR: Expected body does not match pattern\n";
            echo "BODY:\n";
            var_dump($body);
            echo "PATTERN:\n";
            var_dump($pattern);
        }
    }

    /**
     * Check plain status page.
     *
     * @param string $body
     * @param array $fields
     */
    protected function checkStatusPlain(string $body, array $fields)
    {
        $this->makeStatusCheck($body, $fields, "%s:\s+%s\n");
    }

    /**
     * Check html status page.
     *
     * @param string $body
     * @param array $fields
     */
    protected function checkStatusHtml(string $body, array $fields)
    {
        $header = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" " .
            "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" .
            "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n" .
            "<head><title>" . self::HTML_TITLE . "</title></head>\n" .
            "<body>\n<table>\n";
        $footer = "\n</table>\n</body></html>";

        $this->makeStatusCheck(
            $body,
            $fields,
            "<tr><th>%s</th><td>%s</td></tr>\n",
            $header,
            $footer
        );
    }

    /**
     * Check xml status page.
     *
     * @param string $body
     * @param array $fields
     */
    protected function checkStatusXml(string $body, array $fields)
    {
        $this->makeStatusCheck(
            $body,
            $fields,
            "<%s>%s</%s>\n",
            "<\?xml version=\"1.0\" \?>\n<status>\n",
            "\n</status>",
            function ($name) {
                return str_replace(' ', '-', $name);
            },
            null,
            true,
            true
        );
    }

    /**
     * Check json status page.
     *
     * @param string $body
     * @param array $fields
     */
    protected function checkStatusJson(string $body, array $fields)
    {
        $this->makeStatusCheck(
            $body,
            $fields,
            '"%s":%s,',
            '{',
            '}',
            null,
            function ($value) {
                if (is_numeric($value) || $value === '\d+') {
                    return $value;
                }

                return '"' . $value . '"';
            },
            true
        );
    }

    /**
     * Check openmetrics status page.
     *
     * @param string $body
     * @param array $fields
     */
    protected function checkStatusOpenmetrics(string $body, array $fields)
    {
        $pattern = "|# HELP phpfpm_up Could pool " . $fields['pool'] . " using a " . $fields['process manager'] . " PM on PHP-FPM be reached\?\n" .
            "# TYPE phpfpm_up gauge\n" .
            "phpfpm_up 1\n" .
            "# HELP phpfpm_start_time When FPM has started\.\n" .
            "# TYPE phpfpm_start_time gauge\n" .
            "phpfpm_start_time \d+\n" .
            "# HELP phpfpm_start_since_total The number of seconds since FPM has started\.\n" .
            "# TYPE phpfpm_start_since_total counter\n" .
            "phpfpm_start_since_total " . $fields['start since'] . "\n" .
            "# HELP phpfpm_accepted_connections_total The number of requests accepted by the pool\.\n" .
            "# TYPE phpfpm_accepted_connections_total counter\n" .
            "phpfpm_accepted_connections_total " . $fields['accepted conn'] . "\n" .
            "# HELP phpfpm_listen_queue The number of requests in the queue of pending connections\.\n" .
            "# TYPE phpfpm_listen_queue gauge\n" .
            "phpfpm_listen_queue " . $fields['listen queue'] . "\n" .
            "# HELP phpfpm_max_listen_queue_total The maximum number of requests in the queue of pending connections since FPM has started\.\n" .
            "# TYPE phpfpm_max_listen_queue_total counter\n" .
            "phpfpm_max_listen_queue_total " . $fields['max listen queue'] . "\n" .
            "# TYPE phpfpm_listen_queue_length gauge\n" .
            "# HELP phpfpm_listen_queue_length The size of the socket queue of pending connections\.\n" .
            "phpfpm_listen_queue_length " . $fields['listen queue len'] . "\n" .
            "# HELP phpfpm_idle_processes The number of idle processes\.\n" .
            "# TYPE phpfpm_idle_processes gauge\n" .
            "phpfpm_idle_processes " . $fields['idle processes'] . "\n" .
            "# HELP phpfpm_active_processes The number of active processes\.\n" .
            "# TYPE phpfpm_active_processes gauge\n" .
            "phpfpm_active_processes " . $fields['active processes'] . "\n" .
            "# HELP phpfpm_total_processes The number of idle \+ active processes\.\n" .
            "# TYPE phpfpm_total_processes gauge\n" .
            "phpfpm_total_processes " . $fields['total processes'] . "\n" .
            "# HELP phpfpm_max_active_processes_total The maximum number of active processes since FPM has started\.\n" .
            "# TYPE phpfpm_max_active_processes_total counter\n" .
            "phpfpm_max_active_processes_total " . $fields['max active processes'] . "\n" .
            "# HELP phpfpm_max_children_reached_total The number of times, the process limit has been reached, when pm tries to start more children \(works only for pm 'dynamic' and 'ondemand'\)\.\n" .
            "# TYPE phpfpm_max_children_reached_total counter\n" .
            "phpfpm_max_children_reached_total " . $fields['max children reached'] . "\n" .
            "# HELP phpfpm_slow_requests_total The number of requests that exceeded your 'request_slowlog_timeout' value\.\n" .
            "# TYPE phpfpm_slow_requests_total counter\n" .
            "phpfpm_slow_requests_total " . $fields['slow requests'] . "|";

        if (!preg_match($pattern, $body)) {
            echo "ERROR: Expected body does not match pattern\n";
            echo "BODY:\n";
            var_dump($body);
            echo "PATTERN:\n";
            var_dump($pattern);
        }
    }
}