]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/rec-carbon.cc
Merge pull request #3440 from pieterlexis/dnsdist-client-improvements
[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
13 void doCarbonDump(void*)
14 try
15 {
16 string hostname;
17 vector<string> carbonServers;
18
19 {
20 Lock l(&g_carbon_config_lock);
21 stringtok(carbonServers, arg()["carbon-server"], ", ");
22 hostname=arg()["carbon-ourname"];
23 }
24
25 if(carbonServers.empty())
26 return;
27
28 if(hostname.empty()) {
29 char tmp[80];
30 memset(tmp, 0, sizeof(tmp));
31 gethostname(tmp, sizeof(tmp));
32 char *p = strchr(tmp, '.');
33 if(p) *p=0;
34
35 hostname=tmp;
36 boost::replace_all(hostname, ".", "_");
37 }
38
39 string msg;
40 for(const auto& carbonServer: carbonServers) {
41 RecursorControlParser rcp; // inits if needed
42 ComboAddress remote(carbonServer, 2003);
43 Socket s(remote.sin4.sin_family, SOCK_STREAM);
44
45 s.setNonBlocking();
46 s.connect(remote); // we do the connect so the first attempt happens while we gather stats
47
48 if(msg.empty()) {
49 typedef map<string,string> all_t;
50 all_t all=getAllStatsMap();
51
52 ostringstream str;
53 time_t now=time(0);
54
55 for(const all_t::value_type& val : all) {
56 str<<"pdns."<<hostname<<".recursor."<<val.first<<' '<<val.second<<' '<<now<<"\r\n";
57 }
58 msg = str.str();
59 }
60
61 int ret=asendtcp(msg, &s); // this will actually do the right thing waiting on the connect
62 if(ret < 0)
63 L<<Logger::Warning<<"Error writing carbon data to "<<remote.toStringWithPort()<<": "<<strerror(errno)<<endl;
64 if(ret==0)
65 L<<Logger::Warning<<"Timeout connecting/writing carbon data to "<<remote.toStringWithPort()<<endl;
66 }
67 }
68 catch(PDNSException& e)
69 {
70 L<<Logger::Error<<"Error in carbon thread: "<<e.reason<<endl;
71 }
72 catch(std::exception& e)
73 {
74 L<<Logger::Error<<"Error in carbon thread: "<<e.what()<<endl;
75 }