Skip to content

Commit 1f0db17

Browse files
AleksPopov4PetarKirov
authored andcommitted
config(random-alerts): support multiple urls
1 parent 0e57767 commit 1f0db17

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

modules/random-alerts/default.nix

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
options.services.random-alerts = with lib; {
1616
enable = mkEnableOption (lib.mdDoc "Random Alerts");
1717
args = {
18-
url = mkOption {
19-
type = types.str;
20-
example = "http://localhost:9093";
21-
description = ''Alertmanager URL'';
18+
urls = mkOption {
19+
type = types.listOf types.str;
20+
example = [ "http://localhost:9093" ];
21+
description = ''
22+
A list of Alertmanager endpoint URLs used for sending alert notifications.
23+
'';
2224
};
2325

2426
min-wait-time = mkOption {
@@ -73,7 +75,11 @@
7375
sep: f: attrs:
7476
lib.concatStringsSep sep (lib.attrValues (lib.mapAttrs f attrs));
7577

76-
args = concatMapAttrsStringSep " " (n: v: "--${n}=${toString v}") cfg.args;
78+
filteredArgs = removeAttrs cfg.args [ "urls" ];
79+
filteredArgsToString = concatMapAttrsStringSep " " (n: v: "--${n}=${toString v}") filteredArgs;
80+
urlsArg = " --urls=${lib.concatStringsSep "," cfg.args.urls} ";
81+
args = filteredArgsToString + urlsArg;
82+
7783
in
7884
lib.mkIf cfg.enable {
7985
systemd.services.random-alerts = {

packages/random-alerts/src/main.d

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import core.thread : Thread;
22
import std.datetime : Duration, Clock, seconds, TimeOfDay;
33
import std.format : format;
4-
import std.getopt : getopt, getOptConfig = config;
4+
import std.getopt : getopt, getOptConfig = config, arraySep;
55
import std.json : JSONValue, parseJSON, JSONOptions;
66
import std.logger : infof, errorf, tracef, LogLevel;
77
import std.random : uniform;
@@ -14,7 +14,7 @@ struct Params
1414
Duration minWaitTime;
1515
Duration maxWaitTime;
1616
Duration alertDuration;
17-
string url;
17+
string[] urls;
1818
TimeOfDay startTime;
1919
TimeOfDay endTime;
2020
}
@@ -38,7 +38,7 @@ struct Alert
3838
int main(string[] args)
3939
{
4040
LogLevel logLevel = LogLevel.info;
41-
string url;
41+
string[] urls;
4242
string startTime = "00:00:00";
4343
string endTime = "23:59:59";
4444
uint minWaitTimeInSeconds = 3600; // 1 hour
@@ -47,8 +47,9 @@ int main(string[] args)
4747

4848
try
4949
{
50+
arraySep = ",";
5051
args.getopt(
51-
getOptConfig.required, "url", &url,
52+
getOptConfig.required, "urls", &urls,
5253
"start-time", &startTime,
5354
"end-time", &endTime,
5455
"min-wait-time", &minWaitTimeInSeconds,
@@ -61,9 +62,10 @@ int main(string[] args)
6162

6263
setLogLevel(logLevel);
6364

65+
6466
executeAtRandomIntervals(
6567
Params(
66-
url: url,
68+
urls: urls,
6769
startTime: TimeOfDay.fromISOExtString(startTime),
6870
endTime: TimeOfDay.fromISOExtString(endTime),
6971
minWaitTime: minWaitTimeInSeconds.seconds(),
@@ -99,9 +101,12 @@ void executeAtRandomIntervals(Params params)
99101

100102
if (currentTimeTOD >= startTime && currentTimeTOD <= endTime)
101103
{
102-
infof("Posting alert... ");
103-
postAlert(url, alertDuration);
104-
infof("Alert posted successfully.");
104+
foreach (url; urls)
105+
{
106+
infof("Posting alert on %s...", url);
107+
postAlert(url, alertDuration);
108+
infof("Alert posted successfully.");
109+
}
105110
}
106111
else
107112
{

0 commit comments

Comments
 (0)