mirror of
https://github.com/SebastianStork/nixos-config.git
synced 2026-03-22 22:29:06 +01:00
prometheus, alertmanager: Init modules
This commit is contained in:
parent
58202b18bf
commit
9e79934cf2
3 changed files with 197 additions and 0 deletions
100
modules/nixos/services/prometheus.nix
Normal file
100
modules/nixos/services/prometheus.nix
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
{
|
||||
config,
|
||||
pkgs,
|
||||
lib,
|
||||
allHosts,
|
||||
...
|
||||
}:
|
||||
let
|
||||
cfg = config.custom.services.prometheus;
|
||||
in
|
||||
{
|
||||
options.custom.services.prometheus = {
|
||||
enable = lib.mkEnableOption "";
|
||||
domain = lib.mkOption {
|
||||
type = lib.types.nonEmptyStr;
|
||||
default = "";
|
||||
};
|
||||
port = lib.mkOption {
|
||||
type = lib.types.port;
|
||||
default = 9090;
|
||||
};
|
||||
};
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
services.prometheus = {
|
||||
enable = true;
|
||||
stateDir = "prometheus";
|
||||
|
||||
listenAddress = "localhost";
|
||||
inherit (cfg) port;
|
||||
webExternalUrl = "https://${cfg.domain}";
|
||||
|
||||
globalConfig = {
|
||||
scrape_interval = "30s";
|
||||
external_labels.monitor = "global";
|
||||
};
|
||||
|
||||
alertmanagers = lib.singleton {
|
||||
scheme = "https";
|
||||
static_configs = lib.singleton {
|
||||
targets =
|
||||
allHosts
|
||||
|> lib.attrValues
|
||||
|> lib.map (host: host.config.custom.services.alertmanager)
|
||||
|> lib.filter (alertmanager: alertmanager.enable)
|
||||
|> lib.map (alertmanager: alertmanager.domain);
|
||||
};
|
||||
};
|
||||
|
||||
scrapeConfigs = [
|
||||
{
|
||||
job_name = "prometheus";
|
||||
static_configs = lib.singleton {
|
||||
targets =
|
||||
allHosts
|
||||
|> lib.attrValues
|
||||
|> lib.map (host: host.config.custom.services.prometheus)
|
||||
|> lib.filter (prometheus: prometheus.enable)
|
||||
|> lib.map (prometheus: prometheus.domain);
|
||||
};
|
||||
}
|
||||
{
|
||||
job_name = "alertmanager";
|
||||
static_configs = lib.singleton {
|
||||
targets =
|
||||
allHosts
|
||||
|> lib.attrValues
|
||||
|> lib.map (host: host.config.custom.services.alertmanager)
|
||||
|> lib.filter (alertmanager: alertmanager.enable)
|
||||
|> lib.map (alertmanager: alertmanager.domain);
|
||||
};
|
||||
}
|
||||
];
|
||||
|
||||
ruleFiles =
|
||||
{
|
||||
groups = lib.singleton {
|
||||
name = "InstanceDown";
|
||||
rules = lib.singleton {
|
||||
alert = "InstanceDown";
|
||||
expr = "up == 0";
|
||||
for = "2m";
|
||||
labels.severity = "critical";
|
||||
annotations.summary = "Instance {{ $labels.instance }} down";
|
||||
};
|
||||
};
|
||||
}
|
||||
|> lib.strings.toJSON
|
||||
|> pkgs.writeText "prometheus-instance-down-rule"
|
||||
|> toString
|
||||
|> lib.singleton;
|
||||
};
|
||||
|
||||
custom = {
|
||||
services.caddy.virtualHosts.${cfg.domain}.port = cfg.port;
|
||||
|
||||
persistence.directories = [ "/var/lib/${config.services.prometheus.stateDir}" ];
|
||||
};
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue