Skip to content
Open
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
8 changes: 8 additions & 0 deletions luabridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,12 @@ void initLua()
return make_shared<TelegramNotifier>(data);
});

// Opsgenie notifier
g_lua.set_function("addOpsgenieNotifier", [&](sol::table data) {
g_notifiers.emplace_back(make_shared<OpsgenieNotifier>(data));
return *g_notifiers.rbegin();
});
g_lua.set_function("createOpsgenieNotifier", [&](sol::table data) {
return make_shared<OpsgenieNotifier>(data);
});
}
76 changes: 72 additions & 4 deletions notifiers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,19 @@ void Notifier::bulkDone()
// fmt::print("There are {} results that used to be old enough & are gone now\n",
// diff.size());
for(const auto& str : diff) {
string desc = getAgeDesc(deltime[str]);
this->alert(fmt::format("🎉 after {}, the following alert is over: {}",
desc,
str));
this->resolve(str, deltime[str]);
}

d_prevOldEnough = d_oldEnough;
}

void Notifier::resolve(const std::string& message, const time_t ts)
{
string desc = getAgeDesc(ts);
this->alert(fmt::format("🎉 after {}, the following alert is over: {}",
message,
desc));
}

TelegramNotifier::TelegramNotifier(sol::table data) : Notifier(data)
{
Expand Down Expand Up @@ -268,3 +272,67 @@ void TelegramNotifier::alert(const std::string& message)
// fmt::print("{}\n", res->body);
}


OpsgenieNotifier::OpsgenieNotifier(sol::table data) : Notifier(data)
{
checkLuaTable(data, {"apikey"});
d_apikey = data.get<string>("apikey");
}

void OpsgenieNotifier::alert(const std::string& message)
{
httplib::Client cli("https://api.opsgenie.com");
auto field = "GenieKey " + d_apikey;
httplib::Headers headers = {
{"Authorization", std::move(field)}
};
nlohmann::json jsonRequest = {
{ "message", message },
};
std::string path;
path = "/v2/alerts";
auto response = cli.Post(path, headers, jsonRequest.dump(), "application/json");
if(!response) {
auto err = response.error();
throw std::runtime_error(fmt::format("\nCould not send post: {}", httplib::to_string(err)));
}
if(response->status != 202)
throw std::runtime_error(fmt::format("\nUnexpected response from Opsgenie, response = {}\n{}", response->status, response->body));
auto jsonResponse = nlohmann::json::parse(response->body);
const std::string requestId = jsonResponse["requestId"];

// now get the actual alert id
path = "/v2/alerts/requests/" + requestId;
response = cli.Get(path, headers);

// need to pull the request-id out of here!
if(!response) {
auto err = response.error();
throw std::runtime_error(fmt::format("\nCould not send post: {}", httplib::to_string(err)));
}
if(response->status != 200)
throw std::runtime_error(fmt::format("\nUnexpected response from Opsgenie, res = {}\n{}", response->status, response->body));
jsonResponse = nlohmann::json::parse(response->body);
const std::string alertId = jsonResponse["data"]["alertId"];
d_msg_to_alertid.insert({ message, alertId });
}

void OpsgenieNotifier::resolve(const std::string& message, const time_t ts)
{
auto alertIdIter = d_msg_to_alertid.find(message);
if (alertIdIter == d_msg_to_alertid.end()) {
throw std::runtime_error(fmt::format("\nCould not find alertId for message {} !?", message));
}
const std::string alertId = alertIdIter->second;

httplib::Client cli("https://api.opsgenie.com");
auto field = "GenieKey " + d_apikey;
httplib::Headers headers = {
{"Authorization", std::move(field)}
};
std::string path;
path = "/v2/alerts/" + alertId + "/close";
const std::string empty_body("{}");
// need post an empty json-object to this url, empty string is not accepted
cli.Post(path, headers, "{}", "application/json");
}
12 changes: 12 additions & 0 deletions notifiers.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public:
}
virtual ~Notifier() = default;
virtual void alert(const std::string& message) = 0;
virtual void resolve(const std::string& message, const time_t ts);
std::string getNotifierName() { return d_notifierName; }
void bulkAlert(const std::string& textBody);
void bulkDone();
Expand Down Expand Up @@ -96,3 +97,14 @@ public:
private:
std::string d_botid, d_apikey, d_chatid;
};

class OpsgenieNotifier : public Notifier
{
public:
OpsgenieNotifier(sol::table data);
void alert(const std::string& message) override;
void resolve(const std::string& message, const time_t ts) override;
private:
std::string d_apikey;
std::unordered_map<std::string, std::string> d_msg_to_alertid;
};