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

services/prometheus/exporters: add mysqld
This commit is contained in:
Franz Pletz 2023-08-25 23:56:22 +02:00 committed by GitHub
commit fe907aa37e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 104 additions and 0 deletions

View file

@ -30,6 +30,8 @@
- [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).

View file

@ -50,6 +50,7 @@ let
"mikrotik"
"minio"
"modemmanager"
"mysqld"
"nextcloud"
"nginx"
"nginxlog"
@ -296,6 +297,12 @@ in
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 -> (
(cfg.sql.configFile == null) != (cfg.sql.configuration == null)

View file

@ -0,0 +1,60 @@
{ 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"
];
};
};
}

View file

@ -716,6 +716,41 @@ let
'';
};
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;