1{ config, lib, pkgs, options }:
2
3with lib;
4let
5 cfg = config.services.prometheus.exporters.pve;
6
7 # pve exporter requires a config file so create an empty one if configFile is not provided
8 emptyConfigFile = pkgs.writeTextFile {
9 name = "pve.yml";
10 text = "default:";
11 };
12
13 computedConfigFile = if cfg.configFile == null then emptyConfigFile else cfg.configFile;
14in
15{
16 port = 9221;
17 extraOpts = {
18 package = mkOption {
19 type = types.package;
20 default = pkgs.prometheus-pve-exporter;
21 defaultText = literalExpression "pkgs.prometheus-pve-exporter";
22 example = literalExpression "pkgs.prometheus-pve-exporter";
23 description = lib.mdDoc ''
24 The package to use for prometheus-pve-exporter
25 '';
26 };
27
28 environmentFile = mkOption {
29 type = with types; nullOr path;
30 default = null;
31 example = "/etc/prometheus-pve-exporter/pve.env";
32 description = lib.mdDoc ''
33 Path to the service's environment file. This path can either be a computed path in /nix/store or a path in the local filesystem.
34
35 The environment file should NOT be stored in /nix/store as it contains passwords and/or keys in plain text.
36
37 Environment reference: https://github.com/prometheus-pve/prometheus-pve-exporter#authentication
38 '';
39 };
40
41 configFile = mkOption {
42 type = with types; nullOr path;
43 default = null;
44 example = "/etc/prometheus-pve-exporter/pve.yml";
45 description = lib.mdDoc ''
46 Path to the service's config file. This path can either be a computed path in /nix/store or a path in the local filesystem.
47
48 The config file should NOT be stored in /nix/store as it will contain passwords and/or keys in plain text.
49
50 If both configFile and environmentFile are provided, the configFile option will be ignored.
51
52 Configuration reference: https://github.com/prometheus-pve/prometheus-pve-exporter/#authentication
53 '';
54 };
55
56 collectors = {
57 status = mkOption {
58 type = types.bool;
59 default = true;
60 description = lib.mdDoc ''
61 Collect Node/VM/CT status
62 '';
63 };
64 version = mkOption {
65 type = types.bool;
66 default = true;
67 description = lib.mdDoc ''
68 Collect PVE version info
69 '';
70 };
71 node = mkOption {
72 type = types.bool;
73 default = true;
74 description = lib.mdDoc ''
75 Collect PVE node info
76 '';
77 };
78 cluster = mkOption {
79 type = types.bool;
80 default = true;
81 description = lib.mdDoc ''
82 Collect PVE cluster info
83 '';
84 };
85 resources = mkOption {
86 type = types.bool;
87 default = true;
88 description = lib.mdDoc ''
89 Collect PVE resources info
90 '';
91 };
92 config = mkOption {
93 type = types.bool;
94 default = true;
95 description = lib.mdDoc ''
96 Collect PVE onboot status
97 '';
98 };
99 };
100 };
101 serviceOpts = {
102 serviceConfig = {
103 DynamicUser = cfg.environmentFile == null;
104 LoadCredential = "configFile:${computedConfigFile}";
105 ExecStart = ''
106 ${cfg.package}/bin/pve_exporter \
107 --${optionalString (!cfg.collectors.status) "no-"}collector.status \
108 --${optionalString (!cfg.collectors.version) "no-"}collector.version \
109 --${optionalString (!cfg.collectors.node) "no-"}collector.node \
110 --${optionalString (!cfg.collectors.cluster) "no-"}collector.cluster \
111 --${optionalString (!cfg.collectors.resources) "no-"}collector.resources \
112 --${optionalString (!cfg.collectors.config) "no-"}collector.config \
113 %d/configFile \
114 ${toString cfg.port} ${cfg.listenAddress}
115 '';
116 } // optionalAttrs (cfg.environmentFile != null) {
117 EnvironmentFile = cfg.environmentFile;
118 };
119 };
120}