-
Notifications
You must be signed in to change notification settings - Fork 16
/
luabridge.cc
196 lines (157 loc) · 5.71 KB
/
luabridge.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <fmt/ranges.h>
#include "simplomon.hh"
#include "sol/sol.hpp"
#include <fmt/chrono.h>
using namespace std;
sol::state g_lua;
int g_intervalSeconds=60;
int g_maxWorkers = 16;
/* every checker has a table of properties, and you get an error if you put unexpected things in there.
DailyChime{utcHour=11}
DNSChecker{server="100.25.31.6", domain="berthub.eu", type="A", acceptable={"86.82.68.237", "217.100.190.174"}}
DNSChecker{server="10.0.0.1", domain="hubertnet.nl", type="MX", acceptable={"5 server.hubertnet.nl.", "10 ziggo.berthub.eu."}, minAlerts=3, alertWindow=180}
*/
/*
function that checks if mandatory fields are present, allows optional fields
and panics over other fields */
void checkLuaTable(sol::table data,
const set<string>& mandatory,
const set<string>& opt)
{
set<string> mand = mandatory;
data.for_each([&](sol::object key, sol::object value) {
string k = key.as<string>();
auto iter = mand.find(k);
if(iter != mand.end()) {
mand.erase(iter);
return;
}
if(!mandatory.count(k) && !opt.count(k)) {
if(k=="1")
throw std::runtime_error("This function requires a table {} as input, for example: f{param=1}");
throw std::runtime_error(fmt::format("Unknown parameter '{}' passed", k));
}
});
if(!mand.empty())
throw std::runtime_error(fmt::format("Missing mandatory fields '{}'", mand));
}
class DailyChimeChecker : public Checker
{
public:
DailyChimeChecker(sol::table data) : Checker(data)
{
checkLuaTable(data, {"utcHour"}, {"instance"});
d_utcHour = data["utcHour"];
d_instance = data.get_or("instance", getHostName());
}
CheckResult perform()
{
time_t now = time(nullptr);
now -= d_utcHour * 3600;
struct tm tm;
gmtime_r(&now, &tm);
return fmt::format("Your daily chime from {} for {:%Y-%m-%d}. This is not an alert.", d_instance, tm);
}
std::string getCheckerName() override { return "chime"; }
std::string getDescription() override
{
return fmt::format("Daily chime at {}:00 UTC", d_utcHour);
}
static std::string getHostName()
{
char tmp[HOST_NAME_MAX]={};
gethostname(tmp, sizeof(tmp) -1);
return tmp;
}
private:
int d_utcHour;
string d_instance;
};
void initLua()
{
g_lua.open_libraries(sol::lib::base, sol::lib::package);
g_lua.set_function("dailyChime", [&](sol::table data) {
g_checkers.emplace_back(make_unique<DailyChimeChecker>(data));
});
g_lua.set_function("https", [&](sol::table data) {
g_checkers.emplace_back(make_unique<HTTPSChecker>(data));
});
g_lua.set_function("dnssoa", [&](sol::table data) {
g_checkers.emplace_back(make_unique<DNSSOAChecker>(data));
});
g_lua.set_function("tcpportclosed", [&](sol::table data) {
g_checkers.emplace_back(make_unique<TCPPortClosedChecker>(data));
});
g_lua.set_function("dns", [&](sol::table data) {
g_checkers.emplace_back(make_unique<DNSChecker>(data));
});
g_lua.set_function("httpredir", [&](sol::table data) {
g_checkers.emplace_back(make_unique<HTTPRedirChecker>(data));
});
g_lua.set_function("rrsig", [&](sol::table data) {
g_checkers.emplace_back(make_unique<RRSIGChecker>(data));
});
g_lua.set_function("ping", [&](sol::table data) {
g_checkers.emplace_back(make_unique<PINGChecker>(data));
});
g_lua.set_function("prometheusExp", [&](sol::table data) {
g_checkers.emplace_back(make_unique<PrometheusChecker>(data));
});
g_lua.set_function("smtp", [&](sol::table data) {
g_checkers.emplace_back(make_unique<SMTPChecker>(data));
});
g_lua.set_function("imap", [&](sol::table data) {
g_checkers.emplace_back(make_unique<IMAPChecker>(data));
});
g_lua.set_function("addPushoverNotifier", [&](sol::table data) {
g_notifiers.emplace_back(make_shared<PushoverNotifier>(data));
return *g_notifiers.rbegin();
});
g_lua.set_function("createPushoverNotifier", [&](sol::table data) {
return make_shared<PushoverNotifier>(data);
});
g_lua.set_function("addNtfyNotifier", [&](sol::table data) {
g_notifiers.emplace_back(make_shared<NtfyNotifier>(data));
return *g_notifiers.rbegin();
});
g_lua.set_function("createNtfyNotifier", [&](sol::table data) {
return make_shared<NtfyNotifier>(data);
});
g_lua.set_function("addEmailNotifier", [&](sol::table data) {
g_notifiers.emplace_back(make_shared<EmailNotifier>(data));
return *g_notifiers.rbegin();
});
g_lua.set_function("createEmailNotifier", [&](sol::table data) {
return make_shared<EmailNotifier>(data);
});
g_lua.set_function("setNotifiers", [&](vector<shared_ptr<Notifier>> notifs) {
g_notifiers.resize(2); // need to keep the system notifiers
// fmt::print("Setting {} notifiers\n", notifs.size());
for(auto& n : notifs)
g_notifiers.push_back(n);
});
g_lua.set_function("intervalSeconds", [&](int seconds) {
g_intervalSeconds = seconds;
});
g_lua.set_function("maxWorkers", [&](int workers) {
if(workers <= 0)
throw std::runtime_error("maxWorkers must be a positive number");
g_maxWorkers = workers;
});
g_lua.set_function("Logger", [&](sol::table data) {
checkLuaTable(data, {"filename"});
g_sqlw = std::make_unique<SQLiteWriter>((string)data["filename"]);
});
g_lua.set_function("doIPv6", [&](bool ipv6) {
g_haveIPv6 = ipv6;
});
g_lua.set_function("Webserver", startWebService);
// Telegram notifier
g_lua.set_function("addTelegramNotifier", [&](sol::table data) {
g_notifiers.emplace_back(make_shared<TelegramNotifier>(data));
return *g_notifiers.rbegin();
});
g_lua.set_function("createTelegramNotifier", [&](sol::table data) {
return make_shared<TelegramNotifier>(data);
});
}