blob: d93cde0e9515bacdc8a9e83be9e0028663246a92 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
{
config,
pkgs,
lib,
...
}:
let
cfg = config.services.vmagent;
settingsFormat = pkgs.formats.yaml { };
startCLIList = [
"${cfg.package}/bin/vmagent"
]
++ lib.optionals (cfg.remoteWrite.url != null) [
"-remoteWrite.url=${cfg.remoteWrite.url}"
"-remoteWrite.tmpDataPath=%C/vmagent/remote_write_tmp"
]
++ lib.optional (
cfg.remoteWrite.basicAuthUsername != null
) "-remoteWrite.basicAuth.username=${cfg.remoteWrite.basicAuthUsername}"
++ lib.optional (
cfg.remoteWrite.basicAuthPasswordFile != null
) "-remoteWrite.basicAuth.passwordFile=\${CREDENTIALS_DIRECTORY}/remote_write_basic_auth_password"
++ cfg.extraArgs;
prometheusConfigYml = checkedConfig (
settingsFormat.generate "prometheusConfig.yaml" cfg.prometheusConfig
);
checkedConfig =
file:
if cfg.checkConfig then
pkgs.runCommand "checked-config" { nativeBuildInputs = [ cfg.package ]; } ''
ln -s ${file} $out
${lib.escapeShellArgs startCLIList} -promscrape.config=${file} -dryRun
''
else
file;
in
{
imports = [
(lib.mkRemovedOptionModule [
"services"
"vmagent"
"dataDir"
] "dataDir has been deprecated in favor of systemd provided CacheDirectory")
(lib.mkRemovedOptionModule [
"services"
"vmagent"
"user"
] "user has been deprecated in favor of systemd DynamicUser")
(lib.mkRemovedOptionModule [
"services"
"vmagent"
"group"
] "group has been deprecated in favor of systemd DynamicUser")
(lib.mkRenamedOptionModule
[ "services" "vmagent" "remoteWriteUrl" ]
[ "services" "vmagent" "remoteWrite" "url" ]
)
];
options.services.vmagent = {
enable = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
Whether to enable VictoriaMetrics's `vmagent`.
`vmagent` efficiently scrape metrics from Prometheus-compatible exporters
'';
};
package = lib.mkPackageOption pkgs "vmagent" { };
remoteWrite = {
url = lib.mkOption {
default = null;
type = lib.types.nullOr lib.types.str;
description = ''
Endpoint for prometheus compatible remote_write
'';
};
basicAuthUsername = lib.mkOption {
default = null;
type = lib.types.nullOr lib.types.str;
description = ''
Basic Auth username used to connect to remote_write endpoint
'';
};
basicAuthPasswordFile = lib.mkOption {
default = null;
type = lib.types.nullOr lib.types.str;
description = ''
File that contains the Basic Auth password used to connect to remote_write endpoint
'';
};
};
prometheusConfig = lib.mkOption {
type = lib.types.submodule { freeformType = settingsFormat.type; };
description = ''
Config for prometheus style metrics
'';
};
openFirewall = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
Whether to open the firewall for the default ports.
'';
};
extraArgs = lib.mkOption {
type = lib.types.listOf lib.types.str;
default = [ ];
description = ''
Extra args to pass to `vmagent`. See the docs:
<https://docs.victoriametrics.com/vmagent.html#advanced-usage>
or {command}`vmagent -help` for more information.
'';
};
checkConfig = lib.mkOption {
type = lib.types.bool;
default = true;
description = ''
Check configuration.
If you use credentials stored in external files (`environmentFile`, etc),
they will not be visible and it will report errors, despite a correct configuration.
'';
};
};
config = lib.mkIf cfg.enable {
networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [ 8429 ];
systemd.services.vmagent = {
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
description = "vmagent system service";
serviceConfig = {
DynamicUser = true;
User = "vmagent";
Group = "vmagent";
Type = "simple";
Restart = "on-failure";
CacheDirectory = "vmagent";
ExecStart = lib.escapeShellArgs (
startCLIList
++ lib.optionals (cfg.prometheusConfig != { }) [ "-promscrape.config=${prometheusConfigYml}" ]
);
LoadCredential = lib.optionals (cfg.remoteWrite.basicAuthPasswordFile != null) [
"remote_write_basic_auth_password:${cfg.remoteWrite.basicAuthPasswordFile}"
];
};
};
};
}
|