Merge pull request #242365 from h7x4/add-mysqld-exporter

services/prometheus/exporters: add mysqld

Changed files
+104
nixos
doc
manual
release-notes
modules
services
monitoring
prometheus
tests
+2
nixos/doc/manual/release-notes/rl-2311.section.md
···
- [Anuko Time Tracker](https://github.com/anuko/timetracker), a simple, easy to use, open source time tracking system. Available as [services.anuko-time-tracker](#opt-services.anuko-time-tracker.enable).
+
- [Prometheus MySQL exporter](https://github.com/prometheus/mysqld_exporter), a MySQL server exporter for Prometheus. Available as [services.prometheus.exporters.mysqld](#opt-services.prometheus.exporters.mysqld.enable).
+
- [sitespeed-io](https://sitespeed.io), a tool that can generate metrics (timings, diagnostics) for websites. Available as [services.sitespeed-io](#opt-services.sitespeed-io.enable).
- [Jool](https://nicmx.github.io/Jool/en/index.html), an Open Source implementation of IPv4/IPv6 translation on Linux. Available as [networking.jool.enable](#opt-networking.jool.enable).
+7
nixos/modules/services/monitoring/prometheus/exporters.nix
···
"mikrotik"
"minio"
"modemmanager"
+
"mysqld"
"nextcloud"
"nginx"
"nginxlog"
···
message = ''
Please specify either 'services.prometheus.exporters.mail.configuration'
or 'services.prometheus.exporters.mail.configFile'.
+
'';
+
} {
+
assertion = cfg.mysqld.runAsLocalSuperUser -> config.services.mysql.enable;
+
message = ''
+
The exporter is configured to run as 'services.mysql.user', but
+
'services.mysql.enable' is set to false.
'';
} {
assertion = cfg.sql.enable -> (
+60
nixos/modules/services/monitoring/prometheus/exporters/mysqld.nix
···
+
{ config, lib, pkgs, options }:
+
let
+
cfg = config.services.prometheus.exporters.mysqld;
+
inherit (lib) types mkOption mdDoc mkIf mkForce cli concatStringsSep optionalString escapeShellArgs;
+
in {
+
port = 9104;
+
extraOpts = {
+
telemetryPath = mkOption {
+
type = types.str;
+
default = "/metrics";
+
description = mdDoc ''
+
Path under which to expose metrics.
+
'';
+
};
+
+
runAsLocalSuperUser = mkOption {
+
type = types.bool;
+
default = false;
+
description = mdDoc ''
+
Whether to run the exporter as {option}`services.mysql.user`.
+
'';
+
};
+
+
configFile = mkOption {
+
type = types.path;
+
example = "/var/lib/prometheus-mysqld-exporter.cnf";
+
description = mdDoc ''
+
Path to the services config file.
+
+
See <https://github.com/prometheus/mysqld_exporter#running> for more information about
+
the available options.
+
+
::: {.warn}
+
Please do not store this file in the nix store if you choose to include any credentials here,
+
as it would be world-readable.
+
:::
+
'';
+
};
+
};
+
+
serviceOpts = {
+
serviceConfig = {
+
DynamicUser = !cfg.runAsLocalSuperUser;
+
User = mkIf cfg.runAsLocalSuperUser (mkForce config.services.mysql.user);
+
LoadCredential = mkIf (cfg.configFile != null) (mkForce ("config:" + cfg.configFile));
+
ExecStart = concatStringsSep " " [
+
"${pkgs.prometheus-mysqld-exporter}/bin/mysqld_exporter"
+
"--web.listen-address=${cfg.listenAddress}:${toString cfg.port}"
+
"--web.telemetry-path=${cfg.telemetryPath}"
+
(optionalString (cfg.configFile != null) ''--config.my-cnf=''${CREDENTIALS_DIRECTORY}/config'')
+
(escapeShellArgs cfg.extraFlags)
+
];
+
RestrictAddressFamilies = [
+
# The exporter can be configured to talk to a local mysql server via a unix socket.
+
"AF_UNIX"
+
];
+
};
+
};
+
}
+
+35
nixos/tests/prometheus-exporters.nix
···
'';
};
+
mysqld = {
+
exporterConfig = {
+
enable = true;
+
runAsLocalSuperUser = true;
+
configFile = pkgs.writeText "test-prometheus-exporter-mysqld-config.my-cnf" ''
+
[client]
+
user = exporter
+
password = snakeoilpassword
+
'';
+
};
+
metricProvider = {
+
services.mysql = {
+
enable = true;
+
package = pkgs.mariadb;
+
initialScript = pkgs.writeText "mysql-init-script.sql" ''
+
CREATE USER 'exporter'@'localhost'
+
IDENTIFIED BY 'snakeoilpassword'
+
WITH MAX_USER_CONNECTIONS 3;
+
GRANT PROCESS, REPLICATION CLIENT, SLAVE MONITOR, SELECT ON *.* TO 'exporter'@'localhost';
+
'';
+
};
+
};
+
exporterTest = ''
+
wait_for_unit("prometheus-mysqld-exporter.service")
+
wait_for_open_port(9104)
+
wait_for_unit("mysql.service")
+
succeed("curl -sSf http://localhost:9104/metrics | grep 'mysql_up 1'")
+
systemctl("stop mysql.service")
+
succeed("curl -sSf http://localhost:9104/metrics | grep 'mysql_up 0'")
+
systemctl("start mysql.service")
+
wait_for_unit("mysql.service")
+
succeed("curl -sSf http://localhost:9104/metrics | grep 'mysql_up 1'")
+
'';
+
};
+
nextcloud = {
exporterConfig = {
enable = true;