Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions modules/random-alerts/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 = {
Expand Down
21 changes: 13 additions & 8 deletions packages/random-alerts/src/main.d
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,7 +14,7 @@ struct Params
Duration minWaitTime;
Duration maxWaitTime;
Duration alertDuration;
string url;
string[] urls;
TimeOfDay startTime;
TimeOfDay endTime;
}
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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(),
Expand Down Expand Up @@ -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
{
Expand Down