]>
Commit | Line | Data |
---|---|---|
12471842 PL |
1 | /* |
2 | * This file is part of PowerDNS or dnsdist. | |
3 | * Copyright -- PowerDNS.COM B.V. and its contributors | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of version 2 of the GNU General Public License as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * In addition, for the avoidance of any doubt, permission is granted to | |
10 | * link this program with OpenSSL and to (re)distribute the binaries | |
11 | * produced as the result of such linking. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
21 | */ | |
870a0fe4 AT |
22 | #ifdef HAVE_CONFIG_H |
23 | #include "config.h" | |
24 | #endif | |
952d3fcb | 25 | #include "statbag.hh" |
26 | #include "logger.hh" | |
519f5484 | 27 | #include "threadname.hh" |
952d3fcb | 28 | #include "iputils.hh" |
29 | #include "sstuff.hh" | |
30 | #include "arguments.hh" | |
31 | #include "common_startup.hh" | |
fa8fd4d2 | 32 | |
2449694c | 33 | #include "namespaces.hh" |
952d3fcb | 34 | |
35 | void* carbonDumpThread(void*) | |
36 | try | |
37 | { | |
519f5484 | 38 | setThreadName("pdns/carbonDump"); |
952d3fcb | 39 | extern StatBag S; |
40 | ||
b0d4aa0b | 41 | string namespace_name=arg()["carbon-namespace"]; |
486ce518 PL |
42 | string hostname=arg()["carbon-ourname"]; |
43 | if(hostname.empty()) { | |
44 | char tmp[80]; | |
45 | memset(tmp, 0, sizeof(tmp)); | |
46 | gethostname(tmp, sizeof(tmp)); | |
47 | char *p = strchr(tmp, '.'); | |
48 | if(p) *p=0; | |
49 | hostname=tmp; | |
50 | boost::replace_all(hostname, ".", "_"); | |
51 | } | |
f7a645ec | 52 | string instance_name=arg()["carbon-instance"]; |
486ce518 PL |
53 | |
54 | vector<string> carbonServers; | |
55 | stringtok(carbonServers, arg()["carbon-server"], ", "); | |
56 | ||
57 | for(;;) { | |
58 | if(carbonServers.empty()) { | |
952d3fcb | 59 | sleep(1); |
60 | continue; | |
61 | } | |
952d3fcb | 62 | |
486ce518 PL |
63 | string msg; |
64 | vector<string> entries = S.getEntries(); | |
65 | ostringstream str; | |
66 | time_t now=time(0); | |
67 | for(const string& entry : entries) { | |
b0d4aa0b | 68 | str<<namespace_name<<'.'<<hostname<<'.'<<instance_name<<'.'<<entry<<' '<<S.read(entry)<<' '<<now<<"\r\n"; |
486ce518 PL |
69 | } |
70 | msg = str.str(); | |
71 | ||
72 | for (const auto& carbonServer : carbonServers) { | |
73 | ComboAddress remote(carbonServer, 2003); | |
486ce518 PL |
74 | |
75 | try { | |
67180942 RG |
76 | Socket s(remote.sin4.sin_family, SOCK_STREAM); |
77 | s.setNonBlocking(); | |
78 | s.connect(remote, 2); | |
79 | ||
486ce518 PL |
80 | writen2WithTimeout(s.getHandle(), msg.c_str(), msg.length(), 2); |
81 | } catch (runtime_error &e){ | |
e6a9dde5 | 82 | g_log<<Logger::Warning<<"Unable to write data to carbon server at "<<remote.toStringWithPort()<<": "<<e.what()<<endl; |
486ce518 | 83 | continue; |
952d3fcb | 84 | } |
952d3fcb | 85 | } |
486ce518 | 86 | sleep(arg().asNum("carbon-interval")); |
952d3fcb | 87 | } |
88 | return 0; | |
89 | } | |
2449694c | 90 | catch(std::exception& e) |
952d3fcb | 91 | { |
e6a9dde5 | 92 | g_log<<Logger::Error<<"Carbon thread died: "<<e.what()<<endl; |
952d3fcb | 93 | return 0; |
94 | } | |
95 | catch(PDNSException& e) | |
96 | { | |
e6a9dde5 | 97 | g_log<<Logger::Error<<"Carbon thread died, PDNSException: "<<e.reason<<endl; |
952d3fcb | 98 | return 0; |
99 | } | |
100 | catch(...) | |
101 | { | |
e6a9dde5 | 102 | g_log<<Logger::Error<<"Carbon thread died"<<endl; |
952d3fcb | 103 | return 0; |
104 | } |