]> granicus.if.org Git - pdns/commitdiff
Auth: add support for multiple carbon servers
authorPieter Lexis <pieter.lexis@powerdns.com>
Wed, 18 May 2016 11:54:25 +0000 (13:54 +0200)
committerPieter Lexis <pieter.lexis@powerdns.com>
Thu, 19 May 2016 08:30:33 +0000 (10:30 +0200)
Closes #3782
Closes #2093

pdns/auth-carbon.cc

index c270dbc97de335ad7e8c44b068e89f645c9501fc..c721cc0155e63aecdbe3f37fd82cfc7f81aa4360 100644 (file)
@@ -15,52 +15,49 @@ try
 {
   extern StatBag S;
 
-  for(int numloops=0;;++numloops) {
-    if(arg()["carbon-server"].empty()) {
+  string hostname=arg()["carbon-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, ".", "_");
+  }
+
+  vector<string> carbonServers;
+  stringtok(carbonServers, arg()["carbon-server"], ", ");
+
+  for(;;) {
+    if(carbonServers.empty()) {
       sleep(1);
       continue;
     }
-    if(numloops)
-      sleep(arg().asNum("carbon-interval"));
 
-    try {
-      ComboAddress remote(arg()["carbon-server"], 2003);
+    string msg;
+    vector<string> entries = S.getEntries();
+    ostringstream str;
+    time_t now=time(0);
+    for(const string& entry : entries) {
+      str<<"pdns."<<hostname<<".auth."<<entry<<' '<<S.read(entry)<<' '<<now<<"\r\n";
+    }
+    msg = str.str();
+
+    for (const auto& carbonServer : carbonServers) {
+      ComboAddress remote(carbonServer, 2003);
       Socket s(remote.sin4.sin_family, SOCK_STREAM);
-      
       s.setNonBlocking();
       s.connect(remote);  // we do the connect so the attempt happens while we gather stats
-      
-      vector<string> entries = S.getEntries();
-      
-      ostringstream str;
-      time_t now=time(0);
-      string hostname=arg()["carbon-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 string& entry :  entries) {
-       str<<"pdns."<<hostname<<".auth."<<entry<<' '<<S.read(entry)<<' '<<now<<"\r\n";
-      }
-      const string msg = str.str();
-      
-      int ret = waitForRWData(s.getHandle(), false, 1 , 0); 
-      if(ret <= 0 ) {
-       L<<Logger::Warning<<"Unable to write data to carbon server on "<<remote.toStringWithPort();
-       L<<": "<< (ret<0 ? strerror(errno) : "Timeout")<<endl;
-       continue;
+
+      try {
+        writen2WithTimeout(s.getHandle(), msg.c_str(), msg.length(), 2);
+      } catch (runtime_error &e){
+        L<<Logger::Warning<<"Unable to write data to carbon server at "<<remote.toStringWithPort()<<": "<<e.what()<<endl;
+        continue;
       }
-      s.setBlocking();
-      writen2(s.getHandle(), msg.c_str(), msg.size());
-    }
-    catch(std::exception& e) {
-      L<<Logger::Warning<<"Problem sending carbon data: "<<e.what()<<endl;
     }
+    sleep(arg().asNum("carbon-interval"));
   }
   return 0;
 }