add phpfpm prometheus exporter

This commit is contained in:
ibizaman 2024-12-12 21:22:41 +01:00 committed by Pierre Penninckx
parent 3fc1e61b30
commit 9a7bcd374b
3 changed files with 85 additions and 0 deletions

View file

@ -1418,6 +1418,15 @@
"services-nextcloudserver-options-shb.nextcloud.phpFpmPoolSettings": [
"services-nextcloud.html#services-nextcloudserver-options-shb.nextcloud.phpFpmPoolSettings"
],
"services-nextcloudserver-options-shb.nextcloud.phpFpmPrometheusExporter": [
"services-nextcloud.html#services-nextcloudserver-options-shb.nextcloud.phpFpmPrometheusExporter"
],
"services-nextcloudserver-options-shb.nextcloud.phpFpmPrometheusExporter.enable": [
"services-nextcloud.html#services-nextcloudserver-options-shb.nextcloud.phpFpmPrometheusExporter.enable"
],
"services-nextcloudserver-options-shb.nextcloud.phpFpmPrometheusExporter.port": [
"services-nextcloud.html#services-nextcloudserver-options-shb.nextcloud.phpFpmPrometheusExporter.port"
],
"services-nextcloudserver-options-shb.nextcloud.port": [
"services-nextcloud.html#services-nextcloudserver-options-shb.nextcloud.port"
],

View file

@ -183,6 +183,27 @@ in
'';
};
phpFpmPrometheusExporter = lib.mkOption {
description = "Settings for exporting";
default = {};
type = lib.types.submodule {
options = {
enable = lib.mkOption {
description = "Enable export of php-fpm metrics to Prometheus.";
type = lib.types.bool;
default = true;
};
port = lib.mkOption {
description = "Port on which the exporter will listen.";
type = lib.types.port;
default = 8300;
};
};
};
};
apps = lib.mkOption {
description = ''
Applications to enable in Nextcloud. Enabling an application here will also configure
@ -749,6 +770,27 @@ in
systemd.services.nextcloud-setup.after = cfg.mountPointServices;
})
(lib.mkIf cfg.phpFpmPrometheusExporter.enable {
services.prometheus.exporters.php-fpm = {
enable = true;
user = "nginx";
port = cfg.phpFpmPrometheusExporter.port;
listenAddress = "127.0.0.1";
environmentFile = pkgs.writeText "phpFpmExporterEnvFiles" ''
PHP_FPM_SCRAPE_URI=unix://${config.services.phpfpm.pools.nextcloud.socket}
'';
};
services.prometheus.scrapeConfigs = [
{
job_name = "phpfpm-nextcloud";
static_configs = [{
targets = ["127.0.0.1:${toString cfg.phpFpmPrometheusExporter.port}"];
}];
}
];
})
(lib.mkIf (cfg.enable && cfg.apps.onlyoffice.enable) {
assertions = [
{

View file

@ -217,6 +217,24 @@ let
};
};
};
prometheus = { config, ... }: {
shb.nextcloud = {
phpFpmPrometheusExporter.enable = true;
};
};
prometheusTestScript = { nodes, ... }:
''
server.wait_for_open_unix_socket("${nodes.server.services.phpfpm.pools.nextcloud.socket}")
server.wait_for_open_port(${toString nodes.server.services.prometheus.exporters.php-fpm.port})
with subtest("prometheus"):
response = server.succeed(
"curl -sSf "
+ " http://localhost:${toString nodes.server.services.prometheus.exporters.php-fpm.port}/metrics"
)
print(response)
'';
in
{
basic = pkgs.testers.runNixOSTest {
@ -343,4 +361,20 @@ in
testScript = commonTestScript.access;
};
prometheus = pkgs.testers.runNixOSTest {
name = "nextcloud_prometheus";
nodes.server = { config, ... }: {
imports = [
base
basic
prometheus
];
};
nodes.client = {};
testScript = prometheusTestScript;
};
}