From: Remi Gacogne Date: Wed, 2 Mar 2016 16:55:45 +0000 (+0100) Subject: dnsdist: Add support for multiple carbon servers X-Git-Tag: rec-4.0.0-alpha2~22^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3565320b4ebdbe00336f8ba56a564994a80a1018;p=pdns dnsdist: Add support for multiple carbon servers --- diff --git a/pdns/dnsdist-carbon.cc b/pdns/dnsdist-carbon.cc index ca6274eb2..1702c6109 100644 --- a/pdns/dnsdist-carbon.cc +++ b/pdns/dnsdist-carbon.cc @@ -21,77 +21,77 @@ try { auto localCarbon = g_carbon.getLocal(); for(int numloops=0;;++numloops) { - if(localCarbon->server == ComboAddress("0.0.0.0", 0)) { + if(localCarbon->servers.empty()) { sleep(1); continue; } if(numloops) sleep(localCarbon->interval); - try { - Socket s(localCarbon->server.sin4.sin_family, SOCK_STREAM); - - s.setNonBlocking(); - s.connect(localCarbon->server); // we do the connect so the attempt happens while we gather stats - - ostringstream str; - time_t now=time(0); - string hostname=localCarbon->ourname; - if(hostname.empty()) { - char tmp[80]; - memset(tmp, 0, sizeof(tmp)); - gethostname(tmp, sizeof(tmp)); - char *p = strchr(tmp, '.'); - if(p) *p=0; - hostname=tmp; - boost::replace_all(hostname, ".", "_"); - } - for(const auto& e : g_stats.entries) { - str<<"dnsdist."<(&e.second)) - str<<(*val)->load(); - else if (const auto& val = boost::get(&e.second)) - str<<**val; - else - str<<(*boost::get(&e.second))(e.first); - str<<' '<getName(); - boost::replace_all(serverName, ".", "_"); - const string base = "dnsdist." + hostname + ".main.servers." + serverName + "."; - str<queries.load() << " " << now << "\r\n"; - str<reuseds.load() << " " << now << "\r\n"; - str<latencyUsec/1000.0 << " " << now << "\r\n"; - str<sendErrors.load() << " " << now << "\r\n"; - str<outstanding.load() << " " << now << "\r\n"; - } - for(const auto& front : g_frontends) { - if (front->udpFD == -1 && front->tcpFD == -1) - continue; + string hostname=localCarbon->ourname; + if(hostname.empty()) { + char tmp[80]; + memset(tmp, 0, sizeof(tmp)); + gethostname(tmp, sizeof(tmp)); + char *p = strchr(tmp, '.'); + if(p) *p=0; + hostname=tmp; + boost::replace_all(hostname, ".", "_"); + } + for (auto server : localCarbon->servers) { + try { + Socket s(server.sin4.sin_family, SOCK_STREAM); + s.setNonBlocking(); + s.connect(server); // we do the connect so the attempt happens while we gather stats + ostringstream str; + time_t now=time(0); + for(const auto& e : g_stats.entries) { + str<<"dnsdist."<(&e.second)) + str<<(*val)->load(); + else if (const auto& val = boost::get(&e.second)) + str<<**val; + else + str<<(*boost::get(&e.second))(e.first); + str<<' '<getName(); + boost::replace_all(serverName, ".", "_"); + const string base = "dnsdist." + hostname + ".main.servers." + serverName + "."; + str<queries.load() << " " << now << "\r\n"; + str<reuseds.load() << " " << now << "\r\n"; + str<latencyUsec/1000.0 << " " << now << "\r\n"; + str<sendErrors.load() << " " << now << "\r\n"; + str<outstanding.load() << " " << now << "\r\n"; + } + for(const auto& front : g_frontends) { + if (front->udpFD == -1 && front->tcpFD == -1) + continue; - string frontName = front->local.toStringWithPort() + (front->udpFD >= 0 ? "_udp" : "_tcp"); - boost::replace_all(frontName, ".", "_"); - const string base = "dnsdist." + hostname + ".main.frontends." + frontName + "."; - str<queries.load() << " " << now << "\r\n"; - } - const string msg = str.str(); + string frontName = front->local.toStringWithPort() + (front->udpFD >= 0 ? "_udp" : "_tcp"); + boost::replace_all(frontName, ".", "_"); + const string base = "dnsdist." + hostname + ".main.frontends." + frontName + "."; + str<queries.load() << " " << now << "\r\n"; + } + const string msg = str.str(); - int ret = waitForRWData(s.getHandle(), false, 1 , 0); - if(ret <= 0 ) { - vinfolog("Unable to write data to carbon server on %s: %s", localCarbon->server.toStringWithPort(), (ret<0 ? strerror(errno) : "Timeout")); - continue; + int ret = waitForRWData(s.getHandle(), false, 1 , 0); + if(ret <= 0 ) { + vinfolog("Unable to write data to carbon server on %s: %s", server.toStringWithPort(), (ret<0 ? strerror(errno) : "Timeout")); + continue; + } + s.setBlocking(); + ret=writen2(s.getHandle(), msg.c_str(), msg.size()); + if(ret < 0) + warnlog("Error writing carbon data to %s: %s", server.toStringWithPort(), strerror(errno)); + if(ret==0) + warnlog("EOF writing carbon data to %s", server.toStringWithPort()); + } + catch(std::exception& e) { + warnlog("Problem sending carbon data: %s", e.what()); } - s.setBlocking(); - ret=writen2(s.getHandle(), msg.c_str(), msg.size()); - if(ret < 0) - warnlog("Error writing carbon data to %s: %s", localCarbon->server.toStringWithPort(), strerror(errno)); - if(ret==0) - warnlog("EOF writing carbon data to %s", localCarbon->server.toStringWithPort()); - } - catch(std::exception& e) { - warnlog("Problem sending carbon data: %s", e.what()); } } return 0; diff --git a/pdns/dnsdist-lua.cc b/pdns/dnsdist-lua.cc index 13fa1b9d3..b7f5a6b99 100644 --- a/pdns/dnsdist-lua.cc +++ b/pdns/dnsdist-lua.cc @@ -985,7 +985,7 @@ vector> setupLua(bool client, const std::string& confi boost::optional interval) { setLuaSideEffect(); auto ours = g_carbon.getCopy(); - ours.server=ComboAddress(address, 2003); + ours.servers.push_back(ComboAddress(address, 2003)); if(ourName) ours.ourname=*ourName; if(interval) diff --git a/pdns/dnsdist.hh b/pdns/dnsdist.hh index 0d5b8f7ab..d1c50f326 100644 --- a/pdns/dnsdist.hh +++ b/pdns/dnsdist.hh @@ -432,7 +432,7 @@ void removeServerFromPool(pools_t& pools, const string& poolName, std::shared_pt struct CarbonConfig { - ComboAddress server{"0.0.0.0", 0}; + vector servers; std::string ourname; unsigned int interval{30}; };