|
| 1 | +import core.thread : Thread; |
| 2 | +import std.datetime : Duration, Clock, seconds; |
| 3 | +import std.format : format; |
| 4 | +import std.getopt : getopt, getOptConfig = config; |
| 5 | +import std.json : JSONValue, parseJSON, JSONOptions; |
| 6 | +import std.logger : infof, errorf, tracef, LogLevel; |
| 7 | +import std.random : uniform; |
| 8 | + |
| 9 | +import utils.json : toJSON; |
| 10 | + |
| 11 | +struct Params |
| 12 | +{ |
| 13 | + Duration minWaitTime; |
| 14 | + Duration maxWaitTime; |
| 15 | + Duration alertDuration; |
| 16 | + string url; |
| 17 | +} |
| 18 | + |
| 19 | +struct Alert |
| 20 | +{ |
| 21 | + string[string] labels; |
| 22 | + Annotation annotations; |
| 23 | + string startsAt; |
| 24 | + string endsAt; |
| 25 | + string generatorURL; |
| 26 | + |
| 27 | + struct Annotation |
| 28 | + { |
| 29 | + string alert_type; |
| 30 | + string title; |
| 31 | + string summary; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +int main(string[] args) |
| 36 | +{ |
| 37 | + LogLevel logLevel = LogLevel.info; |
| 38 | + string url; |
| 39 | + int minWaitTimeInSeconds = 3600; // 1 hour |
| 40 | + int maxWaitTimeInSeconds = 14400; // 4 hours |
| 41 | + int alertDurationInSeconds = 3600; // 1 hour |
| 42 | + |
| 43 | + try |
| 44 | + { |
| 45 | + args.getopt( |
| 46 | + getOptConfig.required, "url", &url, |
| 47 | + "min-wait-time", &minWaitTimeInSeconds, |
| 48 | + "max-wait-time", &maxWaitTimeInSeconds, |
| 49 | + "alert-duration", &alertDurationInSeconds, |
| 50 | + "log-level", &logLevel, |
| 51 | + ); |
| 52 | + |
| 53 | + setLogLevel(logLevel); |
| 54 | + |
| 55 | + executeAtRandomIntervals( |
| 56 | + Params( |
| 57 | + url: url, |
| 58 | + minWaitTime: minWaitTimeInSeconds.seconds(), |
| 59 | + maxWaitTime: maxWaitTimeInSeconds.seconds(), |
| 60 | + alertDuration: alertDurationInSeconds.seconds(), |
| 61 | + ) |
| 62 | + ); |
| 63 | + } |
| 64 | + catch (Exception e) |
| 65 | + { |
| 66 | + errorf("Exception: %s", e.message); |
| 67 | + return 1; |
| 68 | + } |
| 69 | + return 0; |
| 70 | +} |
| 71 | + |
| 72 | +auto getRandomDuration(Duration min, Duration max) => |
| 73 | + uniform(min.total!"seconds", max.total!"seconds").seconds; |
| 74 | + |
| 75 | +void executeAtRandomIntervals(Params params) |
| 76 | +{ |
| 77 | + with(params) while (true) |
| 78 | + { |
| 79 | + auto currentTime = Clock.currTime(); |
| 80 | + Duration randomDuration = getRandomDuration(minWaitTime, maxWaitTime); |
| 81 | + auto randomTime = currentTime + randomDuration; |
| 82 | + auto waitDuration = randomTime - currentTime; |
| 83 | + |
| 84 | + tracef("Wait before alert: ", waitDuration); |
| 85 | + |
| 86 | + if (waitDuration > 0.seconds) { |
| 87 | + Thread.sleep(waitDuration); |
| 88 | + } |
| 89 | + infof("Posting alert... "); |
| 90 | + postAlert(url, alertDuration); |
| 91 | + infof("Alert posted successfully."); |
| 92 | + |
| 93 | + Duration remainingTime = maxWaitTime - randomDuration; |
| 94 | + |
| 95 | + tracef("Will sleep: ", remainingTime); |
| 96 | + Thread.sleep(remainingTime); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +void postAlert(string alertManagerEndpoint, Duration alertDuration) |
| 101 | +{ |
| 102 | + string url = alertManagerEndpoint ~ "/api/v2/alerts"; |
| 103 | + |
| 104 | + postJson(url, [ |
| 105 | + Alert( |
| 106 | + startsAt: Clock.currTime.toUTC.toISOExtString(0), |
| 107 | + endsAt: (Clock.currTime + alertDuration).toUTC.toISOExtString(0), |
| 108 | + generatorURL: "http://localhost:9090", |
| 109 | + labels: [ |
| 110 | + "alertname": "Random alert", |
| 111 | + "severity": "critical", |
| 112 | + "environment": "staging", |
| 113 | + "job": "test-monitoring", |
| 114 | + ], |
| 115 | + annotations: Alert.Annotation( |
| 116 | + alert_type: "critical", |
| 117 | + title: "Write report", |
| 118 | + summary: "The alert was triggered at '%s'".format(Clock.currTime.toUTC) |
| 119 | + ) |
| 120 | + ) |
| 121 | + ]); |
| 122 | +} |
| 123 | + |
| 124 | +JSONValue postJson(T)(string url, T value) |
| 125 | +{ |
| 126 | + import std.net.curl : HTTP, post, HTTPStatusException; |
| 127 | + |
| 128 | + auto jsonRequest = value |
| 129 | + .toJSON |
| 130 | + .toPrettyString(JSONOptions.doNotEscapeSlashes); |
| 131 | + |
| 132 | + tracef("Sending request to '%s':\n%s", url, jsonRequest); |
| 133 | + |
| 134 | + auto http = HTTP(); |
| 135 | + http.addRequestHeader("Content-Type", "application/json"); |
| 136 | + |
| 137 | + auto response = post(url, jsonRequest, http); |
| 138 | + return response.parseJSON; |
| 139 | +} |
| 140 | + |
| 141 | +void setLogLevel(LogLevel l) |
| 142 | +{ |
| 143 | + import std.logger : globalLogLevel, sharedLog; |
| 144 | + globalLogLevel = l; |
| 145 | + (cast()sharedLog()).logLevel = l; |
| 146 | +} |
0 commit comments