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