]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/rec-carbon.cc
updated KSK and ZSK Rollover procedures, small fixes in Algorithm Rollover procedure
[thirdparty/pdns.git] / pdns / rec-carbon.cc
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4 #include "mtasker.hh"
5 #include "syncres.hh"
6 #include "rec_channel.hh"
7 #include "iputils.hh"
8 #include "logger.hh"
9 #include "arguments.hh"
10 #include "lock.hh"
11
12 GlobalStateHolder<CarbonConfig> g_carbonConfig;
13
14 void doCarbonDump(void*)
15 {
16 try {
17 static thread_local auto configHolder = g_carbonConfig.getLocal();
18
19 auto config = *configHolder;
20 if (config.servers.empty()) {
21 return;
22 }
23
24 if (config.namespace_name.empty()) {
25 config.namespace_name = "pdns";
26 }
27
28 if (config.hostname.empty()) {
29 try {
30 config.hostname = getCarbonHostName();
31 }
32 catch (const std::exception& e) {
33 throw std::runtime_error(std::string("The 'carbon-ourname' setting has not been set and we are unable to determine the system's hostname: ") + e.what());
34 }
35 }
36 if (config.instance_name.empty()) {
37 config.instance_name = "recursor";
38 }
39
40 registerAllStats();
41 PacketBuffer msg;
42 for (const auto& carbonServer : config.servers) {
43 ComboAddress remote(carbonServer, 2003);
44 Socket s(remote.sin4.sin_family, SOCK_STREAM);
45 s.setNonBlocking();
46 std::shared_ptr<TLSCtx> tlsCtx{nullptr};
47 const struct timeval timeout
48 {
49 g_networkTimeoutMsec / 1000, static_cast<suseconds_t>(g_networkTimeoutMsec) % 1000 * 1000
50 };
51 auto handler = std::make_shared<TCPIOHandler>("", false, s.releaseHandle(), timeout, tlsCtx, time(nullptr));
52 handler->tryConnect(SyncRes::s_tcp_fast_open_connect, remote); // we do the connect so the first attempt happens while we gather stats
53
54 if (msg.empty()) {
55 auto all = getAllStatsMap(StatComponent::Carbon);
56
57 ostringstream str;
58 time_t now = time(0);
59
60 for (const auto& val : all) {
61 str << config.namespace_name << '.' << config.hostname << '.' << config.instance_name << '.' << val.first << ' ' << val.second.d_value << ' ' << now << "\r\n";
62 }
63 const string& x = str.str();
64 msg.insert(msg.end(), x.cbegin(), x.cend());
65 }
66
67 auto ret = asendtcp(msg, handler); // this will actually do the right thing waiting on the connect
68 if (ret == LWResult::Result::Timeout) {
69 g_log << Logger::Warning << "Timeout connecting/writing carbon data to " << remote.toStringWithPort() << endl;
70 }
71 else if (ret != LWResult::Result::Success) {
72 g_log << Logger::Warning << "Error writing carbon data to " << remote.toStringWithPort() << ": " << stringerror() << endl;
73 }
74 handler->close();
75 }
76 }
77 catch (const PDNSException& e) {
78 g_log << Logger::Error << "Error in carbon thread: " << e.reason << endl;
79 }
80 catch (const std::exception& e) {
81 g_log << Logger::Error << "Error in carbon thread: " << e.what() << endl;
82 }
83 catch (...) {
84 g_log << Logger::Error << "Unknown error in carbon thread" << endl;
85 }
86 }