forked from berthubert/simplomon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplomon.hh
175 lines (145 loc) · 3.45 KB
/
simplomon.hh
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
#pragma once
#include <mutex>
#include <string>
#include "record-types.hh"
#include "sclasses.hh"
#include "notifiers.hh"
#include "sol/sol.hpp"
#include <fmt/chrono.h>
using namespace std;
extern sol::state g_lua;
void initLua();
// sets alert status if there have been more than x alerts in y seconds
struct AlertFilter
{
explicit AlertFilter(int maxseconds=3600) : d_maxseconds(maxseconds) {}
void reportAlert(time_t t)
{
alerts.insert(t);
}
void reportAlert()
{
alerts.insert(time(nullptr));
}
bool shouldAlert(int numalerts, int numseconds)
{
time_t lim =time(nullptr) - d_maxseconds;
std::erase_if(alerts, [&](const auto& i) { return i < lim; });
lim = time(nullptr) - numseconds;
int count=0;
for(auto iter = alerts.lower_bound(lim); iter != alerts.end(); ++iter)
++count;
return count >= numalerts;
}
std::set<time_t> alerts;
int d_maxseconds;
};
struct CheckResult
{
CheckResult() {}
CheckResult(const char* reason) : d_reason(reason) {}
CheckResult(const std::string& reason) : d_reason(reason) {}
std::string d_reason;
};
class Checker
{
public:
Checker() {}
Checker(const Checker&) = delete;
virtual CheckResult perform() = 0;
CheckResult getStatus()
{
std::lock_guard<std::mutex> l(d_m);
return d_status;
}
void setStatus(const CheckResult& cr)
{
std::lock_guard<std::mutex> l(d_m);
d_status = cr;
}
AlertFilter d_af;
int d_minalerts=1;
int d_alertwindow = 60;
std::string d_alertedreason;
private:
CheckResult d_status;
std::mutex d_m;
};
class DNSChecker : public Checker
{
public:
DNSChecker(sol::table data);
CheckResult perform() override;
private:
ComboAddress d_nsip;
DNSName d_qname;
DNSType d_qtype;
std::set<std::string> d_acceptable;
bool d_rd = true;
};
class RRSIGChecker : public Checker
{
public:
RRSIGChecker(sol::table data);
CheckResult perform() override;
private:
ComboAddress d_nsip;
DNSName d_qname;
DNSType d_qtype;
int d_minDays=0;
};
class DNSSOAChecker : public Checker
{
public:
DNSSOAChecker(const std::string& doain,
const std::set<std::string>& servers);
DNSSOAChecker(sol::table data);
CheckResult perform() override;
private:
DNSName d_domain;
std::set<ComboAddress> d_servers;
};
class TCPPortClosedChecker : public Checker
{
public:
TCPPortClosedChecker(const std::set<std::string>& servers,
const std::set<int>& ports);
TCPPortClosedChecker(sol::table data);
CheckResult perform() override;
private:
std::set<ComboAddress> d_servers;
std::set<int> d_ports;
};
class HTTPSChecker : public Checker
{
public:
HTTPSChecker(sol::table data);
~HTTPSChecker()
{
}
CheckResult perform() override;
private:
std::string d_url;
int d_maxAgeMinutes = 0;
unsigned int d_minBytes = 0;
unsigned int d_minCertDays = 14;
std::optional<ComboAddress> d_serverIP;
};
class HTTPRedirChecker : public Checker
{
public:
HTTPRedirChecker(sol::table data);
CheckResult perform() override;
private:
std::string d_fromhostpart, d_frompath, d_tourl;
};
struct CheckerNotifierCombo
{
std::unique_ptr<Checker> checker;
std::vector<std::shared_ptr<Notifier>> notifiers;
};
extern std::vector<CheckerNotifierCombo> g_checkers;
void checkLuaTable(sol::table data,
const std::set<std::string>& mandatory,
const std::set<std::string>& opt = std::set<std::string>());
extern std::vector<std::shared_ptr<Notifier>> g_notifiers;