{
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;
}