Skip to content

Instantly share code, notes, and snippets.

@AllenJB
Created July 12, 2019 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AllenJB/ff15dc2e606c99e42a60743cf88fee92 to your computer and use it in GitHub Desktop.
Save AllenJB/ff15dc2e606c99e42a60743cf88fee92 to your computer and use it in GitHub Desktop.
PHP-FPM Pool Monitoring w/ Prometheus
<?php
declare(strict_types=1);
// Prometheus metrics exporter for PHP-FPM
header("Content-Type: text/plain; version=0.0.4");
$stats = null;
$protocol = ((!empty($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] !== "off") ? "https" : "http");
$result = file_get_contents($protocol ."://" . $_SERVER["SERVER_NAME"] . "/php-fpm-status?json&full");
if ($result !== false) {
$stats = @json_decode($result, true);
}
if (! is_array($stats)) {
exit();
}
$metrics = [
"uptime" => [
"type" => "counter",
"help" => "seconds since last restart",
"value" => ($stats["start since"] ?? null),
],
"accepted_connections_total" => [
"type" => "counter",
"help" => "Accepted connections",
"value" => ($stats["accepted conn"] ?? null),
],
"listen_queue_connections" => [
"type" => "gauge",
"help" => "Pending connections queue",
"value" => ($stats["listen queue"] ?? null),
],
"listen_queue_max_connections" => [
"type" => "counter",
"help" => "Max. pending connections queue",
"value" => ($stats["max listen queue"] ?? null),
],
"listen_queue_length" => [
"type" => "gauge",
"help" => "Listen queue length",
"value" => ($stats["listen queue len"] ?? null),
],
"active_processes" => [
"type" => "gauge",
"help" => "Active processes",
"value" => ($stats["active processes"] ?? null),
],
"idle_processes" => [
"type" => "gauge",
"help" => "Idle processes",
"value" => ($stats["idle processes"] ?? null),
],
"processes" => [
"type" => "gauge",
"help" => "Total processes",
"value" => ($stats["total processes"] ?? null),
],
"active_processes_max" => [
"type" => "counter",
"help" => "Max. seen processes",
"value" => ($stats["max active processes"] ?? null),
],
"max_children_reached_count" => [
"type" => "counter",
"help" => "Max. number of processes hit count",
"value" => ($stats["max children reached"] ?? null),
],
"slow_requests_total" => [
"type" => "counter",
"help" => "Slow requests",
"value" => ($stats["slow requests"] ?? null),
],
];
foreach ($metrics as $key => $metric) {
$key = "phpfpm_" . $key;
if ($metric["value"] === null) {
continue;
}
print "# HELP {$key} {$metric["help"]}\n";
print "# TYPE {$key} {$metric["type"]}\n";
print $key . " " . $metric["value"] . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment