]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/rec-carbon.cc
Merge pull request #5461 from rgacogne/rec-cache-index
[thirdparty/pdns.git] / pdns / rec-carbon.cc
CommitLineData
870a0fe4
AT
1#ifdef HAVE_CONFIG_H
2#include "config.h"
3#endif
2c78bd57 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"
77a6fa66 10#include "lock.hh"
fa8fd4d2 11
2c78bd57 12
13void doCarbonDump(void*)
14try
15{
71f46864 16 string hostname;
17 vector<string> carbonServers;
77a6fa66 18
19 {
20 Lock l(&g_carbon_config_lock);
71f46864 21 stringtok(carbonServers, arg()["carbon-server"], ", ");
77a6fa66 22 hostname=arg()["carbon-ourname"];
23 }
24
71f46864 25 if(carbonServers.empty())
2c78bd57 26 return;
27
cc08b5a9 28 if(hostname.empty()) {
29 char tmp[80];
30 memset(tmp, 0, sizeof(tmp));
31 gethostname(tmp, sizeof(tmp));
9a21d4b7 32 char *p = strchr(tmp, '.');
33 if(p) *p=0;
34
cc08b5a9 35 hostname=tmp;
46541751 36 boost::replace_all(hostname, ".", "_");
cc08b5a9 37 }
2c78bd57 38
b0b37121 39 registerAllStats();
71f46864 40 string msg;
41 for(const auto& carbonServer: carbonServers) {
71f46864 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 }
2c78bd57 67 }
68catch(PDNSException& e)
69{
70 L<<Logger::Error<<"Error in carbon thread: "<<e.reason<<endl;
71}
72catch(std::exception& e)
73{
74 L<<Logger::Error<<"Error in carbon thread: "<<e.what()<<endl;
75}