]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/rec-carbon.cc
Merge remote-tracking branch 'origin/master' into rec-edsn-unaligned-test
[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 string instance_name;
18 string namespace_name;
19 vector<string> carbonServers;
20
21 {
22 Lock l(&g_carbon_config_lock);
23 stringtok(carbonServers, arg()["carbon-server"], ", ");
24 namespace_name=arg()["carbon-namespace"];
25 hostname=arg()["carbon-ourname"];
26 instance_name=arg()["carbon-instance"];
27 }
28
29 if(carbonServers.empty())
30 return;
31
32 if(namespace_name.empty()) {
33 namespace_name="pdns";
34 }
35 if(hostname.empty()) {
36 char tmp[80];
37 memset(tmp, 0, sizeof(tmp));
38 gethostname(tmp, sizeof(tmp));
39 char *p = strchr(tmp, '.');
40 if(p) *p=0;
41
42 hostname=tmp;
43 boost::replace_all(hostname, ".", "_");
44 }
45 if(instance_name.empty()) {
46 instance_name="recursor";
47 }
48
49 registerAllStats();
50 string msg;
51 for(const auto& carbonServer: carbonServers) {
52 ComboAddress remote(carbonServer, 2003);
53 Socket s(remote.sin4.sin_family, SOCK_STREAM);
54
55 s.setNonBlocking();
56 s.connect(remote); // we do the connect so the first attempt happens while we gather stats
57
58 if(msg.empty()) {
59 typedef map<string,string> all_t;
60 all_t all=getAllStatsMap(StatComponent::Carbon);
61
62 ostringstream str;
63 time_t now=time(0);
64
65 for(const all_t::value_type& val : all) {
66 str<<namespace_name<<'.'<<hostname<<'.'<<instance_name<<'.'<<val.first<<' '<<val.second<<' '<<now<<"\r\n";
67 }
68 msg = str.str();
69 }
70
71 int ret=asendtcp(msg, &s); // this will actually do the right thing waiting on the connect
72 if(ret < 0)
73 g_log<<Logger::Warning<<"Error writing carbon data to "<<remote.toStringWithPort()<<": "<<strerror(errno)<<endl;
74 if(ret==0)
75 g_log<<Logger::Warning<<"Timeout connecting/writing carbon data to "<<remote.toStringWithPort()<<endl;
76 }
77 }
78 catch(PDNSException& e)
79 {
80 g_log<<Logger::Error<<"Error in carbon thread: "<<e.reason<<endl;
81 }
82 catch(std::exception& e)
83 {
84 g_log<<Logger::Error<<"Error in carbon thread: "<<e.what()<<endl;
85 }
86 catch(...)
87 {
88 g_log<<Logger::Error<<"Unknown error in carbon thread"<<endl;
89 }