2022-06-16 17:23:12 +00:00
|
|
|
{ lib, pkgs, config, generators, ... }:
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services.grafana-agent;
|
|
|
|
settingsFormat = pkgs.formats.yaml { };
|
|
|
|
configFile = settingsFormat.generate "grafana-agent.yaml" cfg.settings;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
meta = {
|
2022-06-26 10:26:21 +00:00
|
|
|
maintainers = with maintainers; [ flokli zimbatm ];
|
2022-06-16 17:23:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
options.services.grafana-agent = {
|
2022-09-09 14:08:57 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "grafana-agent");
|
2022-06-16 17:23:12 +00:00
|
|
|
|
2024-01-02 11:29:13 +00:00
|
|
|
package = mkPackageOption pkgs "grafana-agent" { };
|
2022-06-16 17:23:12 +00:00
|
|
|
|
|
|
|
credentials = mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-06-16 17:23:12 +00:00
|
|
|
Credentials to load at service startup. Keys that are UPPER_SNAKE will be loaded as env vars. Values are absolute paths to the credentials.
|
|
|
|
'';
|
|
|
|
type = types.attrsOf types.str;
|
|
|
|
default = { };
|
|
|
|
|
|
|
|
example = {
|
|
|
|
logs_remote_write_password = "/run/keys/grafana_agent_logs_remote_write_password";
|
|
|
|
LOGS_REMOTE_WRITE_URL = "/run/keys/grafana_agent_logs_remote_write_url";
|
|
|
|
LOGS_REMOTE_WRITE_USERNAME = "/run/keys/grafana_agent_logs_remote_write_username";
|
|
|
|
metrics_remote_write_password = "/run/keys/grafana_agent_metrics_remote_write_password";
|
|
|
|
METRICS_REMOTE_WRITE_URL = "/run/keys/grafana_agent_metrics_remote_write_url";
|
|
|
|
METRICS_REMOTE_WRITE_USERNAME = "/run/keys/grafana_agent_metrics_remote_write_username";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-05-24 13:37:59 +00:00
|
|
|
extraFlags = mkOption {
|
|
|
|
type = with types; listOf str;
|
|
|
|
default = [ ];
|
|
|
|
example = [ "-enable-features=integrations-next" "-disable-reporting" ];
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Extra command-line flags passed to {command}`grafana-agent`.
|
|
|
|
|
|
|
|
See <https://grafana.com/docs/agent/latest/static/configuration/flags/>
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-06-16 17:23:12 +00:00
|
|
|
settings = mkOption {
|
2022-08-21 13:32:41 +00:00
|
|
|
description = lib.mdDoc ''
|
2023-05-24 13:37:59 +00:00
|
|
|
Configuration for {command}`grafana-agent`.
|
2022-06-16 17:23:12 +00:00
|
|
|
|
2023-05-24 13:37:59 +00:00
|
|
|
See <https://grafana.com/docs/agent/latest/configuration/>
|
2022-06-16 17:23:12 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
type = types.submodule {
|
|
|
|
freeformType = settingsFormat.type;
|
|
|
|
};
|
|
|
|
|
2022-08-12 12:06:08 +00:00
|
|
|
default = { };
|
2022-12-17 10:02:37 +00:00
|
|
|
defaultText = lib.literalExpression ''
|
|
|
|
{
|
|
|
|
metrics = {
|
|
|
|
wal_directory = "\''${STATE_DIRECTORY}";
|
|
|
|
global.scrape_interval = "5s";
|
|
|
|
};
|
|
|
|
integrations = {
|
|
|
|
agent.enabled = true;
|
|
|
|
agent.scrape_integration = true;
|
|
|
|
node_exporter.enabled = true;
|
|
|
|
};
|
|
|
|
}
|
2022-08-12 12:06:08 +00:00
|
|
|
'';
|
2022-06-16 17:23:12 +00:00
|
|
|
example = {
|
2022-06-26 10:26:21 +00:00
|
|
|
metrics.global.remote_write = [{
|
|
|
|
url = "\${METRICS_REMOTE_WRITE_URL}";
|
|
|
|
basic_auth.username = "\${METRICS_REMOTE_WRITE_USERNAME}";
|
|
|
|
basic_auth.password_file = "\${CREDENTIALS_DIRECTORY}/metrics_remote_write_password";
|
|
|
|
}];
|
|
|
|
logs.configs = [{
|
2022-06-16 17:23:12 +00:00
|
|
|
name = "default";
|
|
|
|
scrape_configs = [
|
|
|
|
{
|
|
|
|
job_name = "journal";
|
|
|
|
journal = {
|
|
|
|
max_age = "12h";
|
|
|
|
labels.job = "systemd-journal";
|
|
|
|
};
|
|
|
|
relabel_configs = [
|
|
|
|
{
|
|
|
|
source_labels = [ "__journal__systemd_unit" ];
|
|
|
|
target_label = "systemd_unit";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
source_labels = [ "__journal__hostname" ];
|
|
|
|
target_label = "nodename";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
source_labels = [ "__journal_syslog_identifier" ];
|
|
|
|
target_label = "syslog_identifier";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
];
|
|
|
|
positions.filename = "\${STATE_DIRECTORY}/loki_positions.yaml";
|
|
|
|
clients = [{
|
|
|
|
url = "\${LOGS_REMOTE_WRITE_URL}";
|
|
|
|
basic_auth.username = "\${LOGS_REMOTE_WRITE_USERNAME}";
|
|
|
|
basic_auth.password_file = "\${CREDENTIALS_DIRECTORY}/logs_remote_write_password";
|
|
|
|
}];
|
|
|
|
}];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2022-08-12 12:06:08 +00:00
|
|
|
services.grafana-agent.settings = {
|
|
|
|
# keep this in sync with config.services.grafana-agent.settings.defaultText.
|
|
|
|
metrics = {
|
|
|
|
wal_directory = mkDefault "\${STATE_DIRECTORY}";
|
|
|
|
global.scrape_interval = mkDefault "5s";
|
|
|
|
};
|
|
|
|
integrations = {
|
|
|
|
agent.enabled = mkDefault true;
|
|
|
|
agent.scrape_integration = mkDefault true;
|
|
|
|
node_exporter.enabled = mkDefault true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-06-16 17:23:12 +00:00
|
|
|
systemd.services.grafana-agent = {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
script = ''
|
|
|
|
set -euo pipefail
|
|
|
|
shopt -u nullglob
|
|
|
|
|
|
|
|
# Load all credentials into env if they are in UPPER_SNAKE form.
|
|
|
|
if [[ -n "''${CREDENTIALS_DIRECTORY:-}" ]]; then
|
|
|
|
for file in "$CREDENTIALS_DIRECTORY"/*; do
|
|
|
|
key=$(basename "$file")
|
|
|
|
if [[ $key =~ ^[A-Z0-9_]+$ ]]; then
|
|
|
|
echo "Environ $key"
|
|
|
|
export "$key=$(< "$file")"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
# We can't use Environment=HOSTNAME=%H, as it doesn't include the domain part.
|
|
|
|
export HOSTNAME=$(< /proc/sys/kernel/hostname)
|
|
|
|
|
2023-05-24 13:37:59 +00:00
|
|
|
exec ${lib.getExe cfg.package} -config.expand-env -config.file ${configFile} ${escapeShellArgs cfg.extraFlags}
|
2022-06-16 17:23:12 +00:00
|
|
|
'';
|
|
|
|
serviceConfig = {
|
|
|
|
Restart = "always";
|
|
|
|
DynamicUser = true;
|
|
|
|
RestartSec = 2;
|
|
|
|
SupplementaryGroups = [
|
|
|
|
# allow to read the systemd journal for loki log forwarding
|
|
|
|
"systemd-journal"
|
|
|
|
];
|
|
|
|
StateDirectory = "grafana-agent";
|
|
|
|
LoadCredential = lib.mapAttrsToList (key: value: "${key}:${value}") cfg.credentials;
|
|
|
|
Type = "simple";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|