1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
{ lib, ... }:
let
ports = {
grafana-to-ntfy = 8080;
ntfy-sh = 8081;
grafana = 3000;
alertmanager = 9093;
};
ntfyTopic = "grafana-alerts";
in
{
name = "grafana-to-ntfy";
meta.maintainers = with lib.maintainers; [ kittyandrew ];
nodes.machine = {
services.grafana-to-ntfy = {
enable = true;
settings = {
ntfyUrl = "http://127.0.0.1:${toString ports.ntfy-sh}/${ntfyTopic}";
port = ports.grafana-to-ntfy;
address = "127.0.0.1";
};
};
services.ntfy-sh = {
enable = true;
settings = {
listen-http = "127.0.0.1:${toString ports.ntfy-sh}";
base-url = "http://127.0.0.1:${toString ports.ntfy-sh}";
};
};
services.grafana = {
enable = true;
settings = {
server.http_port = ports.grafana;
server.http_addr = "127.0.0.1";
security.admin_user = "admin";
security.admin_password = "admin";
security.secret_key = "test-only-dummy-key";
};
provision.alerting = {
contactPoints.settings = {
apiVersion = 1;
contactPoints = [
{
orgId = 1;
name = "grafana-to-ntfy";
receivers = [
{
uid = "cp_webhook";
type = "webhook";
disableResolveMessage = false;
settings = {
url = "http://127.0.0.1:${toString ports.grafana-to-ntfy}";
httpMethod = "POST";
};
}
];
}
];
};
policies.settings = {
apiVersion = 1;
policies = [
{
orgId = 1;
receiver = "grafana-to-ntfy";
group_by = [ "..." ];
group_wait = "0s";
group_interval = "1s";
repeat_interval = "1h";
}
];
};
};
};
services.prometheus.alertmanager = {
enable = true;
listenAddress = "127.0.0.1";
port = ports.alertmanager;
configuration = {
route = {
receiver = "grafana-to-ntfy";
group_by = [ "..." ];
group_wait = "0s";
group_interval = "1s";
repeat_interval = "2h";
};
receivers = [
{
name = "grafana-to-ntfy";
webhook_configs = [
{ url = "http://127.0.0.1:${toString ports.grafana-to-ntfy}"; }
];
}
];
};
};
};
interactive.nodes.machine = {
services.grafana-to-ntfy.settings.address = lib.mkForce "0.0.0.0";
services.grafana.settings.server.http_addr = lib.mkForce "0.0.0.0";
services.prometheus.alertmanager.listenAddress = lib.mkForce "0.0.0.0";
services.ntfy-sh.settings.listen-http = lib.mkForce "0.0.0.0:${toString ports.ntfy-sh}";
networking.firewall.enable = false;
virtualisation.forwardPorts = lib.mapAttrsToList (_: port: {
from = "host";
host = { inherit port; };
guest = { inherit port; };
}) ports;
};
testScript = ''
import json
machine.wait_for_unit("grafana-to-ntfy.service")
machine.wait_for_unit("ntfy-sh.service")
machine.wait_for_unit("grafana.service")
machine.wait_for_unit("alertmanager.service")
machine.wait_for_open_port(${toString ports.grafana-to-ntfy})
machine.wait_for_open_port(${toString ports.ntfy-sh})
machine.wait_for_open_port(${toString ports.grafana})
machine.wait_for_open_port(${toString ports.alertmanager})
with subtest("Health endpoint returns 200"):
machine.succeed("curl -sf http://127.0.0.1:${toString ports.grafana-to-ntfy}/health")
with subtest("Alertmanager alert arrives at ntfy"):
machine.succeed(
"curl -sf http://127.0.0.1:${toString ports.alertmanager}/api/v2/alerts"
" -X POST -H 'Content-Type: application/json'"
" -d '[{\"labels\": {\"alertname\": \"TestAlertFromAM\"}}]'"
)
# grep makes wait_until_succeeds retry: ntfy returns 200 with empty body when no messages exist
resp = machine.wait_until_succeeds(
"curl -sf 'http://127.0.0.1:${toString ports.ntfy-sh}/${ntfyTopic}/json?poll=1'"
" | grep '\"title\":\"Alertmanager\"'"
)
msg = json.loads(resp.strip())
assert msg["title"] == "Alertmanager", f"Expected title 'Alertmanager', got '{msg['title']}'"
assert "warning" in msg["tags"], f"Expected 'warning' in tags, got {msg['tags']}"
assert "firing" in msg["tags"], f"Expected 'firing' in tags, got {msg['tags']}"
with subtest("Grafana alert arrives at ntfy"):
machine.succeed(
"curl -sf http://127.0.0.1:${toString ports.grafana}/apis/notifications.alerting.grafana.app/v1beta1/namespaces/default/receivers/-/test"
" -u admin:admin"
" -X POST -H 'Content-Type: application/json'"
""" -d '{"alert": {"labels": {"alertname": "test-alert"}, "annotations": {}}, "integration": {"type": "webhook", "settings": {"url": "http://127.0.0.1:${toString ports.grafana-to-ntfy}", "httpMethod": "POST"}}}'"""
)
# grep ensures we wait for the Grafana message specifically (see above)
resp = machine.wait_until_succeeds(
"curl -sf 'http://127.0.0.1:${toString ports.ntfy-sh}/${ntfyTopic}/json?poll=1'"
" | grep 'FIRING'"
)
msg = json.loads(resp.strip())
assert "[FIRING:1]" in msg["title"], f"Expected Grafana title with '[FIRING:1]', got '{msg['title']}'"
assert "warning" in msg["tags"], f"Expected 'warning' in tags, got {msg['tags']}"
assert "firing" in msg["tags"], f"Expected 'firing' in tags, got {msg['tags']}"
'';
}
|