diff --git a/modules/random-alerts/default.nix b/modules/random-alerts/default.nix index 6baaacc3..b5ac764d 100644 --- a/modules/random-alerts/default.nix +++ b/modules/random-alerts/default.nix @@ -15,10 +15,12 @@ options.services.random-alerts = with lib; { enable = mkEnableOption (lib.mdDoc "Random Alerts"); args = { - url = mkOption { - type = types.str; - example = "http://localhost:9093"; - description = ''Alertmanager URL''; + urls = mkOption { + type = types.listOf types.str; + example = [ "http://localhost:9093" ]; + description = '' + A list of Alertmanager endpoint URLs used for sending alert notifications. + ''; }; min-wait-time = mkOption { @@ -73,7 +75,11 @@ sep: f: attrs: lib.concatStringsSep sep (lib.attrValues (lib.mapAttrs f attrs)); - args = concatMapAttrsStringSep " " (n: v: "--${n}=${toString v}") cfg.args; + filteredArgs = removeAttrs cfg.args [ "urls" ]; + filteredArgsToString = concatMapAttrsStringSep " " (n: v: "--${n}=${toString v}") filteredArgs; + urlsArg = " --urls=${lib.concatStringsSep "," cfg.args.urls} "; + args = filteredArgsToString + urlsArg; + in lib.mkIf cfg.enable { systemd.services.random-alerts = { diff --git a/packages/random-alerts/src/main.d b/packages/random-alerts/src/main.d index 3f41f241..e6d71439 100755 --- a/packages/random-alerts/src/main.d +++ b/packages/random-alerts/src/main.d @@ -1,7 +1,7 @@ import core.thread : Thread; import std.datetime : Duration, Clock, seconds, TimeOfDay; import std.format : format; -import std.getopt : getopt, getOptConfig = config; +import std.getopt : getopt, getOptConfig = config, arraySep; import std.json : JSONValue, parseJSON, JSONOptions; import std.logger : infof, errorf, tracef, LogLevel; import std.random : uniform; @@ -14,7 +14,7 @@ struct Params Duration minWaitTime; Duration maxWaitTime; Duration alertDuration; - string url; + string[] urls; TimeOfDay startTime; TimeOfDay endTime; } @@ -38,7 +38,7 @@ struct Alert int main(string[] args) { LogLevel logLevel = LogLevel.info; - string url; + string[] urls; string startTime = "00:00:00"; string endTime = "23:59:59"; uint minWaitTimeInSeconds = 3600; // 1 hour @@ -47,8 +47,9 @@ int main(string[] args) try { + arraySep = ","; args.getopt( - getOptConfig.required, "url", &url, + getOptConfig.required, "urls", &urls, "start-time", &startTime, "end-time", &endTime, "min-wait-time", &minWaitTimeInSeconds, @@ -61,9 +62,10 @@ int main(string[] args) setLogLevel(logLevel); + executeAtRandomIntervals( Params( - url: url, + urls: urls, startTime: TimeOfDay.fromISOExtString(startTime), endTime: TimeOfDay.fromISOExtString(endTime), minWaitTime: minWaitTimeInSeconds.seconds(), @@ -99,9 +101,12 @@ void executeAtRandomIntervals(Params params) if (currentTimeTOD >= startTime && currentTimeTOD <= endTime) { - infof("Posting alert... "); - postAlert(url, alertDuration); - infof("Alert posted successfully."); + foreach (url; urls) + { + infof("Posting alert on %s...", url); + postAlert(url, alertDuration); + infof("Alert posted successfully."); + } } else {