monitoring: optionify config

This commit is contained in:
Aaron Bieber 2023-04-07 21:47:00 -06:00
parent a1a8ad4ca5
commit 4f5203940a
No known key found for this signature in database

View File

@ -1,5 +1,7 @@
{ config, ... }: { config, lib, ... }:
with lib;
let let
cfg = config.services.xin-monitoring;
inherit (builtins) inherit (builtins)
readFile concatStringsSep attrValues mapAttrs replaceStrings; readFile concatStringsSep attrValues mapAttrs replaceStrings;
@ -10,7 +12,6 @@ let
if space usage > 90% then alert if space usage > 90% then alert
if inode usage > 90% then alert if inode usage > 90% then alert
'') fsList))); '') fsList)));
buildNginxChecker = vhostList: buildNginxChecker = vhostList:
(concatStringsSep "\n" (attrValues (mapAttrs (f: v: '' (concatStringsSep "\n" (attrValues (mapAttrs (f: v: ''
check host ${f} with address ${f} check host ${f} with address ${f}
@ -22,7 +23,6 @@ let
"" ""
} }
'') vhostList))); '') vhostList)));
nginxChecks = if nginxCfg.enable then nginxChecks = if nginxCfg.enable then
if config.networking.hostName == "h" then if config.networking.hostName == "h" then
(buildNginxChecker nginxCfg.virtualHosts) (buildNginxChecker nginxCfg.virtualHosts)
@ -32,7 +32,30 @@ let
""; "";
in { in {
config = { options = {
services.xin-monitoring = {
enable = mkOption {
type = types.bool;
default = true;
description = "Enable Monitoring";
};
fs = mkOption {
type = types.bool;
default = true;
description = ''
Create monitoring entry points from `config.fileSystems`.
'';
};
nginx = mkOption {
type = types.bool;
default = false;
description = ''
Create monitoring entry points from `services.nginx.virtualHosts`.
'';
};
};
};
config = mkIf cfg.enable {
sops.secrets = { sops.secrets = {
monit_cfg = { monit_cfg = {
sopsFile = config.xin-secrets.deploy; sopsFile = config.xin-secrets.deploy;
@ -42,8 +65,11 @@ in {
}; };
services.monit = { services.monit = {
enable = true; enable = true;
config = readFile ./monitrc + (buildFSChecker config.fileSystems) config = concatStrings [
+ nginxChecks; (readFile ./monitrc)
(optionalString cfg.fs (buildFSChecker config.fileSystems))
(optionalString cfg.nginx nginxChecks)
];
}; };
}; };
} }