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
1 change: 1 addition & 0 deletions html/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ td {
border: 1px solid var(--clr-primary-fg);
padding: var(--size-xs);
vertical-align: top;
white-space: pre-wrap;
}

th {
Expand Down
3 changes: 3 additions & 0 deletions luabridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ void initLua()
g_checkers.emplace_back(make_unique<PrometheusChecker>(data));
});

g_lua.set_function("external", [&](sol::table data) {
g_checkers.emplace_back(make_unique<ExternalChecker>(data));
});

g_lua.set_function("smtp", [&](sol::table data) {
g_checkers.emplace_back(make_unique<SMTPChecker>(data));
Expand Down
9 changes: 9 additions & 0 deletions manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ dnssoa{domain="berthub.eu", servers= nameservers}
dnssoa{domain="hubertnet.nl", servers= nameservers}
```

## external

Execute some external program, and check whether the output matches a regex and/or a return code:

```lua
external{cmd="do-thing", regex="success"}
external{cmd="do-other-thing", rc=0}
```

## httpredir

Does the http redirect work? TBC. Example:
Expand Down
51 changes: 51 additions & 0 deletions netmon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,54 @@ CheckResult PINGChecker::perform()
}
return ret;
}


// Based on: https://stackoverflow.com/questions/478898/how-do-i-execute-a-command-and-get-the-output-of-the-command-within-c-using-po
std::pair<int, std::string> exec(const char* cmd) {
char buffer[128];
std::string result = "";
int rc;
FILE* pipe = popen(cmd, "r");
if (!pipe) return std::pair<int, std::string> (255, "");
try {
while (fgets(buffer, sizeof buffer, pipe) != NULL) {
result += buffer;
}
} catch (...) {
pclose(pipe);
throw;
}
rc = pclose(pipe) / 256;
std::pair<int, std::string> ret(rc, result);
return ret;
}

ExternalChecker::ExternalChecker(sol::table data) : Checker(data)
{
checkLuaTable(data, {"cmd"}, {"regex", "rc"});

d_cmd = data.get<string>("cmd");
d_exp = data.get_or<string>("regex", "");
d_rc = data.get_or("rc", -1);
}

CheckResult ExternalChecker::perform()
{
std::pair<int, std::string> output = exec(d_cmd.c_str());

d_results.clear();
d_results[d_cmd]["rc"] = output.first;
d_results[d_cmd]["output"] = output.second;

if (d_exp != "") {
if (!std::regex_search(output.second, std::regex(d_exp))) {
return fmt::format("External check \"{}\" against \"{}\" failed, actual output: \"{}\"", d_cmd, d_exp, output.second);
}
}
if (d_rc != -1) {
if (output.first != d_rc) {
return fmt::format("External check \"{}\" expected rc \"{}\", received: \"{}\"", d_cmd, d_rc, output.first);
}
}
return "";
}
18 changes: 18 additions & 0 deletions simplomon.hh
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,24 @@ private:
};


class ExternalChecker : public Checker
{
public:
ExternalChecker(sol::table data);
CheckResult perform() override;
std::string getCheckerName() override { return "external"; }
std::string getDescription() override
{
return fmt::format("External check {}", d_cmd);
}

private:
std::string d_cmd;
std::string d_exp;
int d_rc;
};


class HTTPSChecker : public Checker
{
public:
Expand Down