Skip to content

Commit

Permalink
Add Alertmanager
Browse files Browse the repository at this point in the history
* Configure Alertmanager to send webhooks to the `webhook` service.
* Connect Loki and Prometheus to Alertmanager.
* Add the Alertmanager as a datasource to Grafana

Limitations:

* The webhook only processes the first alert
  • Loading branch information
wagdav committed Jan 18, 2024
1 parent 936b561 commit 3e3a0a7
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 2 deletions.
2 changes: 2 additions & 0 deletions host-nuc.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{
imports = [
./hardware/nuc.nix
./modules/alertmanager.nix
./modules/cachix.nix
./modules/common.nix
./modules/consul/server.nix
Expand All @@ -15,6 +16,7 @@
./modules/remote-builder
./modules/traefik.nix
./modules/vpn.nix
./modules/webhook.nix
];

system.stateVersion = "22.05";
Expand Down
37 changes: 37 additions & 0 deletions modules/alertmanager.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{ config, ... }:

{
imports = [ ./consul-catalog.nix ];

services.prometheus.alertmanager = {
enable = true;
configuration = {
route.receiver = "webhook";
receivers = [
{
name = "webhook";
webhook_configs = [
{
url = "http://localhost:${toString config.services.webhook.port}/hooks/alertmanager";
}
];
}
];
};
};

services.consul.catalog = [
{
name = "alertmanager";
port = config.services.prometheus.alertmanager.port;
tags = (import ./lib/traefik.nix).tagsForHost "alertmanager";
check = {
name = "Health endpoint";
http = "http://localhost:${toString config.services.prometheus.alertmanager.port}/-/healthy";
interval = "10s";
};
}
];

networking.firewall.allowedTCPPorts = [ config.services.prometheus.alertmanager.port ];
}
2 changes: 1 addition & 1 deletion modules/grafana/dashboards/nixos.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"showLineNumbers": false,
"showMiniMap": false
},
"content": "* [Grafana](http://nuc:3000)\n* [Gitolite](http://nuc:8022)\n* [Node Exporter (nuc)](http://nuc:9100)\n* [Ntfy (nuc)](http://nuc:8080)\n* [Prometheus](http://nuc:9090)",
"content": "* [Alertmanager](http://nuc:9093)\n* [Grafana](http://nuc:3000)\n* [Gitolite](http://nuc:8022)\n* [Node Exporter (nuc)](http://nuc:9100)\n* [Ntfy (nuc)](http://nuc:8080)\n* [Prometheus](http://nuc:9090)",
"mode": "markdown"
},
"pluginVersion": "9.4.9",
Expand Down
7 changes: 7 additions & 0 deletions modules/grafana/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
type = "loki";
url = "http://loki.thewagner.home";
}
{
name = "Alertmanager";
type = "alertmanager";
url = "http://alertmanager.thewagner.home";
jsonData.implementation = "prometheus";
jsonData.handleGrafanaManagedAlerts = true;
}
];

dashboards.settings.providers = [
Expand Down
11 changes: 11 additions & 0 deletions modules/loki.nix
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ let
reject_old_samples = true;
reject_old_samples_max_age = "168h";
};

ruler = {
storage = {
type = "local";
local.directory = "/tmp/rules";
};
rule_path = "/tmp/scratch";
alertmanager_url = "http://alertmanager.thewagner.home";
ring.kvstore.store = "inmemory";
enable_api = true;
};
};

in
Expand Down
12 changes: 11 additions & 1 deletion modules/prometheus.nix
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,23 @@ let
}
];

alertmanagers = [
{
static_configs = [
{
targets = [ "alertmanager.thewagner.home" ];
}
];
}
];

in
{
imports = [ ./consul-catalog.nix ];

services.prometheus = {
enable = true;
inherit scrapeConfigs;
inherit alertmanagers scrapeConfigs;
};

services.consul.catalog = [
Expand Down
67 changes: 67 additions & 0 deletions modules/webhook.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{ config, pkgs, ... }:

let

command = pkgs.writeShellScriptBin "ntfy" ''
externalUrl=$1
status=$2
summary=$3
description=$4
if [ "$status" = "firing" ]; then
icon=rotating_light
else
icon=tada
fi
${pkgs.curl}/bin/curl \
-H "X-Tags: $icon" \
-H "Title: $summary" \
-H "Click: $externalUrl" \
-d "$description" \
"http://nuc:8080/home-thewagner-ec1"
'';

in

{
imports = [ ./consul-catalog.nix ];

services.webhook = {
enable = true;
hooks = {
alertmanager = {
execute-command = "${command}/bin/ntfy";
incoming-payload-content-type = "application/json";
pass-arguments-to-command = [
{
source = "payload";
name = "externalURL";
}
{
source = "payload";
name = "alerts.0.status";
}
{
source = "payload";
name = "alerts.0.annotations.summary";
}
{
source = "payload";
name = "alerts.0.annotations.description";
}
];
};
};
};

services.consul.catalog = [
{
name = "webhook";
port = config.services.webhook.port;
tags = (import ./lib/traefik.nix).tagsForHost "webhook";
}
];

networking.firewall.allowedTCPPorts = [ config.services.webhook.port ];
}
4 changes: 4 additions & 0 deletions router/config
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ while uci -q get dhcp.@cname[0]; do
uci delete dhcp.@cname[0]
done

uci add dhcp cname
uci set dhcp.@cname[-1].cname="alertmanager.thewagner.home"
uci set dhcp.@cname[-1].target="nuc.thewagner.home"

uci add dhcp cname
uci set dhcp.@cname[-1].cname="git.thewagner.home"
uci set dhcp.@cname[-1].target="nuc.thewagner.home"
Expand Down

0 comments on commit 3e3a0a7

Please sign in to comment.