diff --git a/docs/redirects.json b/docs/redirects.json index 0045280..03a843c 100644 --- a/docs/redirects.json +++ b/docs/redirects.json @@ -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" ], diff --git a/modules/services/nextcloud-server.nix b/modules/services/nextcloud-server.nix index 8945913..6c55fef 100644 --- a/modules/services/nextcloud-server.nix +++ b/modules/services/nextcloud-server.nix @@ -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 = [ { diff --git a/test/services/nextcloud.nix b/test/services/nextcloud.nix index 017e1b9..6833f72 100644 --- a/test/services/nextcloud.nix +++ b/test/services/nextcloud.nix @@ -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; + }; }