Skip to content

Commit

Permalink
add phpfpm prometheus exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
ibizaman authored and ibizaman committed Dec 23, 2024
1 parent 3fc1e61 commit 9a7bcd3
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/redirects.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
],
Expand Down
42 changes: 42 additions & 0 deletions modules/services/nextcloud-server.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = [
{
Expand Down
34 changes: 34 additions & 0 deletions test/services/nextcloud.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
};
}

0 comments on commit 9a7bcd3

Please sign in to comment.