]>
Commit | Line | Data |
---|---|---|
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 | |
13 | void doCarbonDump(void*) | |
14 | try | |
15 | { | |
71f46864 | 16 | string hostname; |
b0d4aa0b G |
17 | string instance_name; |
18 | string namespace_name; | |
71f46864 | 19 | vector<string> carbonServers; |
77a6fa66 | 20 | |
21 | { | |
22 | Lock l(&g_carbon_config_lock); | |
71f46864 | 23 | stringtok(carbonServers, arg()["carbon-server"], ", "); |
b0d4aa0b | 24 | namespace_name=arg()["carbon-namespace"]; |
77a6fa66 | 25 | hostname=arg()["carbon-ourname"]; |
b0d4aa0b | 26 | instance_name=arg()["carbon-instance"]; |
77a6fa66 | 27 | } |
28 | ||
71f46864 | 29 | if(carbonServers.empty()) |
2c78bd57 | 30 | return; |
31 | ||
b0d4aa0b G |
32 | if(namespace_name.empty()) { |
33 | namespace_name="pdns"; | |
34 | } | |
cc08b5a9 | 35 | if(hostname.empty()) { |
36 | char tmp[80]; | |
37 | memset(tmp, 0, sizeof(tmp)); | |
38 | gethostname(tmp, sizeof(tmp)); | |
9a21d4b7 | 39 | char *p = strchr(tmp, '.'); |
40 | if(p) *p=0; | |
41 | ||
cc08b5a9 | 42 | hostname=tmp; |
46541751 | 43 | boost::replace_all(hostname, ".", "_"); |
cc08b5a9 | 44 | } |
b0d4aa0b G |
45 | if(instance_name.empty()) { |
46 | instance_name="recursor"; | |
47 | } | |
2c78bd57 | 48 | |
b0b37121 | 49 | registerAllStats(); |
71f46864 | 50 | string msg; |
51 | for(const auto& carbonServer: carbonServers) { | |
71f46864 | 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; | |
72259676 | 60 | all_t all=getAllStatsMap(StatComponent::Carbon); |
71f46864 | 61 | |
62 | ostringstream str; | |
63 | time_t now=time(0); | |
64 | ||
65 | for(const all_t::value_type& val : all) { | |
b0d4aa0b | 66 | str<<namespace_name<<'.'<<hostname<<'.'<<instance_name<<'.'<<val.first<<' '<<val.second<<' '<<now<<"\r\n"; |
71f46864 | 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) | |
e6a9dde5 | 73 | g_log<<Logger::Warning<<"Error writing carbon data to "<<remote.toStringWithPort()<<": "<<strerror(errno)<<endl; |
71f46864 | 74 | if(ret==0) |
e6a9dde5 | 75 | g_log<<Logger::Warning<<"Timeout connecting/writing carbon data to "<<remote.toStringWithPort()<<endl; |
71f46864 | 76 | } |
2c78bd57 | 77 | } |
78 | catch(PDNSException& e) | |
79 | { | |
e6a9dde5 | 80 | g_log<<Logger::Error<<"Error in carbon thread: "<<e.reason<<endl; |
2c78bd57 | 81 | } |
82 | catch(std::exception& e) | |
83 | { | |
e6a9dde5 | 84 | g_log<<Logger::Error<<"Error in carbon thread: "<<e.what()<<endl; |
2c78bd57 | 85 | } |
2cfc20bd | 86 | catch(...) |
87 | { | |
e6a9dde5 | 88 | g_log<<Logger::Error<<"Unknown error in carbon thread"<<endl; |
2cfc20bd | 89 | } |