status.inc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace FPM;
  3. class Status
  4. {
  5. const HTML_TITLE = 'PHP-FPM Status Page';
  6. /**
  7. * @var array
  8. */
  9. private $contentTypes = [
  10. 'plain' => 'text/plain',
  11. 'html' => 'text/html',
  12. 'xml' => 'text/xml',
  13. 'json' => 'application/json',
  14. 'openmetrics' => 'application/openmetrics-text; version=1.0.0; charset=utf-8',
  15. ];
  16. /**
  17. * @var array
  18. */
  19. private $defaultFields = [
  20. 'pool' => '\w+',
  21. 'process manager' => '(static|dynamic|ondemand)',
  22. 'start time' => '\d+\/\w{3}\/\d{4}:\d{2}:\d{2}:\d{2}\s[+-]\d{4}',
  23. 'start since' => '\d+',
  24. 'accepted conn' => '\d+',
  25. 'listen queue' => '\d+',
  26. 'max listen queue' => '\d+',
  27. 'listen queue len' => '\d+',
  28. 'idle processes' => '\d+',
  29. 'active processes' => '\d+',
  30. 'total processes' => '\d+',
  31. 'max active processes' => '\d+',
  32. 'max children reached' => '\d+',
  33. 'slow requests' => '\d+',
  34. ];
  35. /**
  36. * Check status page.
  37. *
  38. * @param Response $response
  39. * @param array $fields
  40. * @param string $type
  41. * @throws \Exception
  42. */
  43. public function checkStatus(Response $response, array $fields, string $type)
  44. {
  45. if (!isset($this->contentTypes[$type])) {
  46. throw new \Exception('Invalid content type ' . $type);
  47. }
  48. $body = $response->getBody($this->contentTypes[$type]);
  49. if ($body === null) {
  50. return;
  51. }
  52. $method = "checkStatus" . ucfirst($type);
  53. $this->$method($body, array_merge($this->defaultFields, $fields));
  54. }
  55. /**
  56. * Make status check for status page.
  57. *
  58. * @param string $body
  59. * @param array $fields
  60. * @param string $rowPattern
  61. * @param string $header
  62. * @param string $footer
  63. * @param null|callable $nameTransformer
  64. * @param null|callable $valueTransformer
  65. * @param bool $startTimeTimestamp
  66. * @param bool $closingName
  67. */
  68. private function makeStatusCheck(
  69. string $body,
  70. array $fields,
  71. string $rowPattern,
  72. string $header = '',
  73. string $footer = '',
  74. $nameTransformer = null,
  75. $valueTransformer = null,
  76. bool $startTimeTimestamp = false,
  77. bool $closingName = false
  78. ) {
  79. if ($startTimeTimestamp && $fields['start time'][0] === '\\') {
  80. $fields['start time'] = '\d+';
  81. }
  82. $pattern = '(' . $header;
  83. foreach ($fields as $name => $value) {
  84. if ($nameTransformer) {
  85. $name = call_user_func($nameTransformer, $name);
  86. }
  87. if ($valueTransformer) {
  88. $value = call_user_func($valueTransformer, $value);
  89. }
  90. if ($closingName) {
  91. $pattern .= sprintf($rowPattern, $name, $value, $name);
  92. } else {
  93. $pattern .= sprintf($rowPattern, $name, $value);
  94. }
  95. }
  96. $pattern = rtrim($pattern, $rowPattern[strlen($rowPattern) - 1]);
  97. $pattern .= $footer . ')';
  98. if (!preg_match($pattern, $body)) {
  99. echo "ERROR: Expected body does not match pattern\n";
  100. echo "BODY:\n";
  101. var_dump($body);
  102. echo "PATTERN:\n";
  103. var_dump($pattern);
  104. }
  105. }
  106. /**
  107. * Check plain status page.
  108. *
  109. * @param string $body
  110. * @param array $fields
  111. */
  112. protected function checkStatusPlain(string $body, array $fields)
  113. {
  114. $this->makeStatusCheck($body, $fields, "%s:\s+%s\n");
  115. }
  116. /**
  117. * Check html status page.
  118. *
  119. * @param string $body
  120. * @param array $fields
  121. */
  122. protected function checkStatusHtml(string $body, array $fields)
  123. {
  124. $header = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" " .
  125. "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" .
  126. "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n" .
  127. "<head><title>" . self::HTML_TITLE . "</title></head>\n" .
  128. "<body>\n<table>\n";
  129. $footer = "\n</table>\n</body></html>";
  130. $this->makeStatusCheck(
  131. $body,
  132. $fields,
  133. "<tr><th>%s</th><td>%s</td></tr>\n",
  134. $header,
  135. $footer
  136. );
  137. }
  138. /**
  139. * Check xml status page.
  140. *
  141. * @param string $body
  142. * @param array $fields
  143. */
  144. protected function checkStatusXml(string $body, array $fields)
  145. {
  146. $this->makeStatusCheck(
  147. $body,
  148. $fields,
  149. "<%s>%s</%s>\n",
  150. "<\?xml version=\"1.0\" \?>\n<status>\n",
  151. "\n</status>",
  152. function ($name) {
  153. return str_replace(' ', '-', $name);
  154. },
  155. null,
  156. true,
  157. true
  158. );
  159. }
  160. /**
  161. * Check json status page.
  162. *
  163. * @param string $body
  164. * @param array $fields
  165. */
  166. protected function checkStatusJson(string $body, array $fields)
  167. {
  168. $this->makeStatusCheck(
  169. $body,
  170. $fields,
  171. '"%s":%s,',
  172. '{',
  173. '}',
  174. null,
  175. function ($value) {
  176. if (is_numeric($value) || $value === '\d+') {
  177. return $value;
  178. }
  179. return '"' . $value . '"';
  180. },
  181. true
  182. );
  183. }
  184. /**
  185. * Check openmetrics status page.
  186. *
  187. * @param string $body
  188. * @param array $fields
  189. */
  190. protected function checkStatusOpenmetrics(string $body, array $fields)
  191. {
  192. $pattern = "(# HELP phpfpm_up Could pool " . $fields['pool'] . " using a " . $fields['process manager'] . " PM on PHP-FPM be reached\?\n" .
  193. "# TYPE phpfpm_up gauge\n" .
  194. "phpfpm_up 1\n" .
  195. "# HELP phpfpm_start_since The number of seconds since FPM has started\.\n" .
  196. "# TYPE phpfpm_start_since counter\n" .
  197. "phpfpm_start_since " . $fields['start since'] . "\n" .
  198. "# HELP phpfpm_accepted_connections The number of requests accepted by the pool\.\n" .
  199. "# TYPE phpfpm_accepted_connections counter\n" .
  200. "phpfpm_accepted_connections " . $fields['accepted conn'] . "\n" .
  201. "# HELP phpfpm_listen_queue The number of requests in the queue of pending connections\.\n" .
  202. "# TYPE phpfpm_listen_queue gauge\n" .
  203. "phpfpm_listen_queue " . $fields['listen queue'] . "\n" .
  204. "# HELP phpfpm_max_listen_queue The maximum number of requests in the queue of pending connections since FPM has started\.\n" .
  205. "# TYPE phpfpm_max_listen_queue counter\n" .
  206. "phpfpm_max_listen_queue " . $fields['max listen queue'] . "\n" .
  207. "# TYPE phpfpm_listen_queue_length gauge\n" .
  208. "# HELP phpfpm_listen_queue_length The size of the socket queue of pending connections\.\n" .
  209. "phpfpm_listen_queue_length " . $fields['listen queue len'] . "\n" .
  210. "# HELP phpfpm_idle_processes The number of idle processes\.\n" .
  211. "# TYPE phpfpm_idle_processes gauge\n" .
  212. "phpfpm_idle_processes " . $fields['idle processes'] . "\n" .
  213. "# HELP phpfpm_active_processes The number of active processes\.\n" .
  214. "# TYPE phpfpm_active_processes gauge\n" .
  215. "phpfpm_active_processes " . $fields['active processes'] . "\n" .
  216. "# HELP phpfpm_total_processes The number of idle \+ active processes\.\n" .
  217. "# TYPE phpfpm_total_processes gauge\n" .
  218. "phpfpm_total_processes " . $fields['total processes'] . "\n" .
  219. "# HELP phpfpm_max_active_processes The maximum number of active processes since FPM has started\.\n" .
  220. "# TYPE phpfpm_max_active_processes counter\n" .
  221. "phpfpm_max_active_processes " . $fields['max active processes'] . "\n" .
  222. "# HELP phpfpm_max_children_reached 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" .
  223. "# TYPE phpfpm_max_children_reached counter\n" .
  224. "phpfpm_max_children_reached " . $fields['max children reached'] . "\n" .
  225. "# HELP phpfpm_slow_requests The number of requests that exceeded your 'request_slowlog_timeout' value\.\n" .
  226. "# TYPE phpfpm_slow_requests counter\n" .
  227. "phpfpm_slow_requests " . $fields['slow requests'] . "\n" .
  228. "# EOF)\n";
  229. if (!preg_match($pattern, $body)) {
  230. echo "ERROR: Expected body does not match pattern\n";
  231. echo "BODY:\n";
  232. var_dump($body);
  233. echo "PATTERN:\n";
  234. var_dump($pattern);
  235. }
  236. }
  237. }