1{ config, pkgs, lib, ... }:
2let cfg = config.services.victoriametrics; in
3{
4 options.services.victoriametrics = with lib; {
5 enable = mkEnableOption (lib.mdDoc "victoriametrics");
6 package = mkOption {
7 type = types.package;
8 default = pkgs.victoriametrics;
9 defaultText = literalExpression "pkgs.victoriametrics";
10 description = lib.mdDoc ''
11 The VictoriaMetrics distribution to use.
12 '';
13 };
14 listenAddress = mkOption {
15 default = ":8428";
16 type = types.str;
17 description = lib.mdDoc ''
18 The listen address for the http interface.
19 '';
20 };
21 retentionPeriod = mkOption {
22 type = types.int;
23 default = 1;
24 description = lib.mdDoc ''
25 Retention period in months.
26 '';
27 };
28 extraOptions = mkOption {
29 type = types.listOf types.str;
30 default = [];
31 description = lib.mdDoc ''
32 Extra options to pass to VictoriaMetrics. See the README:
33 <https://github.com/VictoriaMetrics/VictoriaMetrics/blob/master/README.md>
34 or {command}`victoriametrics -help` for more
35 information.
36 '';
37 };
38 };
39 config = lib.mkIf cfg.enable {
40 systemd.services.victoriametrics = {
41 description = "VictoriaMetrics time series database";
42 after = [ "network.target" ];
43 startLimitBurst = 5;
44 serviceConfig = {
45 Restart = "on-failure";
46 RestartSec = 1;
47 StateDirectory = "victoriametrics";
48 DynamicUser = true;
49 ExecStart = ''
50 ${cfg.package}/bin/victoria-metrics \
51 -storageDataPath=/var/lib/victoriametrics \
52 -httpListenAddr ${cfg.listenAddress} \
53 -retentionPeriod ${toString cfg.retentionPeriod} \
54 ${lib.escapeShellArgs cfg.extraOptions}
55 '';
56 # victoriametrics 1.59 with ~7GB of data seems to eventually panic when merging files and then
57 # begins restart-looping forever. Set LimitNOFILE= to a large number to work around this issue.
58 #
59 # panic: FATAL: unrecoverable error when merging small parts in the partition "/var/lib/victoriametrics/data/small/2021_08":
60 # cannot open source part for merging: cannot open values file in stream mode:
61 # cannot open file "/var/lib/victoriametrics/data/small/2021_08/[...]/values.bin":
62 # open /var/lib/victoriametrics/data/small/2021_08/[...]/values.bin: too many open files
63 LimitNOFILE = 1048576;
64 };
65 wantedBy = [ "multi-user.target" ];
66
67 postStart =
68 let
69 bindAddr = (lib.optionalString (lib.hasPrefix ":" cfg.listenAddress) "127.0.0.1") + cfg.listenAddress;
70 in
71 lib.mkBefore ''
72 until ${lib.getBin pkgs.curl}/bin/curl -s -o /dev/null http://${bindAddr}/ping; do
73 sleep 1;
74 done
75 '';
76 };
77 };
78}