Skip to content

Commit b4ca102

Browse files
committed
add open port checker
1 parent 575071a commit b4ca102

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ rrsig{server="100.25.31.6", name="berthub.eu"}
128128
scaryports={25, 80, 110, 443, 3000, 3306, 5000, 5432, 8000, 8080, 8888}
129129
tcpportclosed{servers={"100.25.31.6"}, ports=scaryports}
130130

131+
tcpportopen{servers={"192.0.2.1"}, ports={80}}
132+
131133
-- Check if DNS is serving what it should be
132134
dns{server="100.25.31.6", name="berthub.eu", type="A",
133135
acceptable={"86.82.68.237", "217.100.190.174"}}
@@ -191,7 +193,6 @@ if they did not lead to notifications.
191193

192194
## Todo
193195

194-
* Generic port *open* test
195196
* HTTP *POST* support
196197
* HTTP JSON check
197198
* Performance tests ("average response time past hour > 100ms")

luabridge.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ void initLua()
100100
g_lua.set_function("tcpportclosed", [&](sol::table data) {
101101
g_checkers.emplace_back(make_unique<TCPPortClosedChecker>(data));
102102
});
103+
g_lua.set_function("tcpportopen", [&](sol::table data) {
104+
g_checkers.emplace_back(make_unique<TCPPortOpenChecker>(data));
105+
});
106+
103107
g_lua.set_function("dns", [&](sol::table data) {
104108
g_checkers.emplace_back(make_unique<DNSChecker>(data));
105109
});

netmon.cc

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,57 @@ CheckResult TCPPortClosedChecker::perform()
6060
return cr;
6161
}
6262

63+
TCPPortOpenChecker::TCPPortOpenChecker(sol::table data) : Checker(data)
64+
{
65+
checkLuaTable(data, {"servers", "ports"});
66+
for(const auto& s: data.get<vector<string>>("servers")) {
67+
d_servers.insert(ComboAddress(s));
68+
}
69+
for(int s: data.get<vector<int>>("ports")) {
70+
d_ports.insert(s);
71+
}
72+
}
73+
74+
75+
CheckResult TCPPortOpenChecker::perform()
76+
{
77+
CheckResult cr;
78+
79+
for(const auto& s : d_servers) {
80+
for(const auto& p : d_ports) {
81+
int ret=-1;
82+
ComboAddress rem=s;
83+
rem.setPort(p);
84+
85+
try {
86+
Socket sock(s.sin4.sin_family, SOCK_STREAM);
87+
SetNonBlocking(sock);
88+
//fmt::print("Going to connect to {}\n", rem.toStringWithPort());
89+
ret = SConnectWithTimeout(sock, rem, 1);
90+
}
91+
catch(exception& e) {
92+
// fmt::print("Could not connnect to TCP {}: {}\n",
93+
// rem.toStringWithPort(), e.what());
94+
cr.d_reasons[rem.toStringWithPort()].push_back(fmt::format("Unable to connect to TCP {}: {}",
95+
rem.toStringWithPort(), e.what()));
96+
97+
continue;
98+
}
99+
catch(...) {
100+
cr.d_reasons[rem.toStringWithPort()].push_back(fmt::format("Unable to connect to TCP {}",
101+
rem.toStringWithPort()));
102+
103+
continue;
104+
}
105+
if(ret < 0) {
106+
cr.d_reasons[rem.toStringWithPort()].push_back(fmt::format("Unable ot connect to TCP {}: ", rem.toStringWithPort(),
107+
strerror(errno)));
108+
}
109+
}
110+
}
111+
return cr;
112+
}
113+
63114

64115
// XXX needs switch to select IPv4 or IPv6 or happy eyeballs?
65116
HTTPSChecker::HTTPSChecker(sol::table data) : Checker(data)

simplomon.hh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,29 @@ private:
191191
std::set<int> d_ports;
192192
};
193193

194+
class TCPPortOpenChecker : public Checker
195+
{
196+
public:
197+
TCPPortOpenChecker(const std::set<std::string>& servers,
198+
const std::set<int>& ports);
199+
TCPPortOpenChecker(sol::table data);
200+
CheckResult perform() override;
201+
std::string getCheckerName() override { return "tcpportopen"; }
202+
std::string getDescription() override
203+
{
204+
std::vector<std::string> servers;
205+
for(const auto& s : d_servers) servers.push_back(s.toString());
206+
return fmt::format("TCP open check, servers {}, ports {}",
207+
servers, d_ports);
208+
}
209+
210+
211+
private:
212+
std::set<ComboAddress> d_servers;
213+
std::set<int> d_ports;
214+
};
215+
216+
194217

195218
class PINGChecker : public Checker
196219
{

0 commit comments

Comments
 (0)