2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
29 #include "namespaces.hh"
31 #include "threadname.hh"
33 GlobalStateHolder
<vector
<CarbonConfig
> > g_carbon
;
34 static time_t s_start
=time(0);
35 uint64_t uptimeOfProcess(const std::string
& str
)
37 return time(0) - s_start
;
40 void carbonDumpThread()
43 setThreadName("dnsdist/carbon");
44 auto localCarbon
= g_carbon
.getLocal();
45 for(int numloops
=0;;++numloops
) {
46 if(localCarbon
->empty()) {
50 /* this is wrong, we use the interval of the first server
51 for every single one of them */
53 const unsigned int interval
= localCarbon
->at(0).interval
;
57 for (const auto& conf
: *localCarbon
) {
58 const auto& server
= conf
.server
;
59 const std::string
& namespace_name
= conf
.namespace_name
;
60 std::string hostname
= conf
.ourname
;
61 if(hostname
.empty()) {
63 memset(tmp
, 0, sizeof(tmp
));
64 gethostname(tmp
, sizeof(tmp
));
65 char *p
= strchr(tmp
, '.');
68 boost::replace_all(hostname
, ".", "_");
70 const std::string
& instance_name
= conf
.instance_name
;
73 Socket
s(server
.sin4
.sin_family
, SOCK_STREAM
);
75 s
.connect(server
); // we do the connect so the attempt happens while we gather stats
78 for(const auto& e
: g_stats
.entries
) {
79 str
<<namespace_name
<<"."<<hostname
<<"."<<instance_name
<<"."<<e
.first
<<' ';
80 if(const auto& val
= boost::get
<DNSDistStats::stat_t
*>(&e
.second
))
82 else if (const auto& dval
= boost::get
<double*>(&e
.second
))
85 str
<<(*boost::get
<DNSDistStats::statfunction_t
>(&e
.second
))(e
.first
);
86 str
<<' '<<now
<<"\r\n";
88 auto states
= g_dstates
.getLocal();
89 for(const auto& state
: *states
) {
90 string serverName
= state
->name
.empty() ? (state
->remote
.toString() + ":" + std::to_string(state
->remote
.getPort())) : state
->getName();
91 boost::replace_all(serverName
, ".", "_");
92 const string base
= namespace_name
+ "." + hostname
+ "." + instance_name
+ ".servers." + serverName
+ ".";
93 str
<<base
<<"queries" << ' ' << state
->queries
.load() << " " << now
<< "\r\n";
94 str
<<base
<<"drops" << ' ' << state
->reuseds
.load() << " " << now
<< "\r\n";
95 str
<<base
<<"latency" << ' ' << (state
->availability
!= DownstreamState::Availability::Down
? state
->latencyUsec
/1000.0 : 0) << " " << now
<< "\r\n";
96 str
<<base
<<"senderrors" << ' ' << state
->sendErrors
.load() << " " << now
<< "\r\n";
97 str
<<base
<<"outstanding" << ' ' << state
->outstanding
.load() << " " << now
<< "\r\n";
98 str
<<base
<<"tcpdiedsendingquery" << ' '<< state
->tcpDiedSendingQuery
.load() << " " << now
<< "\r\n";
99 str
<<base
<<"tcpdiedreaddingresponse" << ' '<< state
->tcpDiedReadingResponse
.load() << " " << now
<< "\r\n";
100 str
<<base
<<"tcpgaveup" << ' '<< state
->tcpGaveUp
.load() << " " << now
<< "\r\n";
101 str
<<base
<<"tcpreadimeouts" << ' '<< state
->tcpReadTimeouts
.load() << " " << now
<< "\r\n";
102 str
<<base
<<"tcpwritetimeouts" << ' '<< state
->tcpWriteTimeouts
.load() << " " << now
<< "\r\n";
103 str
<<base
<<"tcpcurrentconnections" << ' '<< state
->tcpCurrentConnections
.load() << " " << now
<< "\r\n";
104 str
<<base
<<"tcpavgqueriesperconnection" << ' '<< state
->tcpAvgQueriesPerConnection
.load() << " " << now
<< "\r\n";
105 str
<<base
<<"tcpavgconnectionduration" << ' '<< state
->tcpAvgConnectionDuration
.load() << " " << now
<< "\r\n";
107 for(const auto& front
: g_frontends
) {
108 if (front
->udpFD
== -1 && front
->tcpFD
== -1)
111 string frontName
= front
->local
.toString() + ":" + std::to_string(front
->local
.getPort()) + (front
->udpFD
>= 0 ? "_udp" : "_tcp");
112 boost::replace_all(frontName
, ".", "_");
113 const string base
= namespace_name
+ "." + hostname
+ "." + instance_name
+ ".frontends." + frontName
+ ".";
114 str
<<base
<<"queries" << ' ' << front
->queries
.load() << " " << now
<< "\r\n";
115 str
<<base
<<"tcpdiedreadingquery" << ' '<< front
->tcpDiedReadingQuery
.load() << " " << now
<< "\r\n";
116 str
<<base
<<"tcpdiedsendingresponse" << ' '<< front
->tcpDiedSendingResponse
.load() << " " << now
<< "\r\n";
117 str
<<base
<<"tcpgaveup" << ' '<< front
->tcpGaveUp
.load() << " " << now
<< "\r\n";
118 str
<<base
<<"tcpclientimeouts" << ' '<< front
->tcpClientTimeouts
.load() << " " << now
<< "\r\n";
119 str
<<base
<<"tcpdownstreamtimeouts" << ' '<< front
->tcpDownstreamTimeouts
.load() << " " << now
<< "\r\n";
120 str
<<base
<<"tcpcurrentconnections" << ' '<< front
->tcpCurrentConnections
.load() << " " << now
<< "\r\n";
121 str
<<base
<<"tcpavgqueriesperconnection" << ' '<< front
->tcpAvgQueriesPerConnection
.load() << " " << now
<< "\r\n";
122 str
<<base
<<"tcpavgconnectionduration" << ' '<< front
->tcpAvgConnectionDuration
.load() << " " << now
<< "\r\n";
124 auto localPools
= g_pools
.getLocal();
125 for (const auto& entry
: *localPools
) {
126 string poolName
= entry
.first
;
127 boost::replace_all(poolName
, ".", "_");
128 if (poolName
.empty()) {
129 poolName
= "_default_";
131 const string base
= namespace_name
+ "." + hostname
+ "." + instance_name
+ ".pools." + poolName
+ ".";
132 const std::shared_ptr
<ServerPool
> pool
= entry
.second
;
133 str
<<base
<<"servers" << " " << pool
->countServers(false) << " " << now
<< "\r\n";
134 str
<<base
<<"servers-up" << " " << pool
->countServers(true) << " " << now
<< "\r\n";
135 if (pool
->packetCache
!= nullptr) {
136 const auto& cache
= pool
->packetCache
;
137 str
<<base
<<"cache-size" << " " << cache
->getMaxEntries() << " " << now
<< "\r\n";
138 str
<<base
<<"cache-entries" << " " << cache
->getEntriesCount() << " " << now
<< "\r\n";
139 str
<<base
<<"cache-hits" << " " << cache
->getHits() << " " << now
<< "\r\n";
140 str
<<base
<<"cache-misses" << " " << cache
->getMisses() << " " << now
<< "\r\n";
141 str
<<base
<<"cache-deferred-inserts" << " " << cache
->getDeferredInserts() << " " << now
<< "\r\n";
142 str
<<base
<<"cache-deferred-lookups" << " " << cache
->getDeferredLookups() << " " << now
<< "\r\n";
143 str
<<base
<<"cache-lookup-collisions" << " " << cache
->getLookupCollisions() << " " << now
<< "\r\n";
144 str
<<base
<<"cache-insert-collisions" << " " << cache
->getInsertCollisions() << " " << now
<< "\r\n";
145 str
<<base
<<"cache-ttl-too-shorts" << " " << cache
->getTTLTooShorts() << " " << now
<< "\r\n";
149 #ifdef HAVE_DNS_OVER_HTTPS
151 const string base
= "dnsdist." + hostname
+ ".main.doh.";
152 for(const auto& doh
: g_dohlocals
) {
153 string name
= doh
->d_local
.toStringWithPort();
154 boost::replace_all(name
, ".", "_");
155 boost::replace_all(name
, ":", "_");
156 boost::replace_all(name
, "[", "_");
157 boost::replace_all(name
, "]", "_");
159 vector
<pair
<const char*, const std::atomic
<uint64_t>&>> v
{
160 {"http-connects", doh
->d_httpconnects
},
161 {"http1-queries", doh
->d_http1queries
},
162 {"http2-queries", doh
->d_http2queries
},
163 {"tls10-queries", doh
->d_tls10queries
},
164 {"tls11-queries", doh
->d_tls11queries
},
165 {"tls12-queries", doh
->d_tls12queries
},
166 {"tls13-queries", doh
->d_tls13queries
},
167 {"tls-unknown-queries", doh
->d_tlsUnknownqueries
},
168 {"get-queries", doh
->d_getqueries
},
169 {"post-queries", doh
->d_postqueries
},
170 {"bad-requests", doh
->d_badrequests
},
171 {"error-responses", doh
->d_errorresponses
},
172 {"valid-responses", doh
->d_validresponses
}
175 for(const auto& item
: v
) {
176 str
<<base
<<name
<<"."<<item
.first
<< " " << item
.second
<< " " << now
<<"\r\n";
180 #endif /* HAVE_DNS_OVER_HTTPS */
183 WriteLock
wl(&g_qcount
.queryLock
);
185 for(auto &record
: g_qcount
.records
) {
186 qname
= record
.first
;
187 boost::replace_all(qname
, ".", "_");
188 str
<<"dnsdist.querycount." << qname
<< ".queries " << record
.second
<< " " << now
<< "\r\n";
190 g_qcount
.records
.clear();
193 const string msg
= str
.str();
195 int ret
= waitForRWData(s
.getHandle(), false, 1 , 0);
197 vinfolog("Unable to write data to carbon server on %s: %s", server
.toStringWithPort(), (ret
<0 ? strerror(errno
) : "Timeout"));
201 writen2(s
.getHandle(), msg
.c_str(), msg
.size());
203 catch(std::exception
& e
) {
204 warnlog("Problem sending carbon data: %s", e
.what());
209 catch(std::exception
& e
)
211 errlog("Carbon thread died: %s", e
.what());
213 catch(PDNSException
& e
)
215 errlog("Carbon thread died, PDNSException: %s", e
.reason
);
219 errlog("Carbon thread died");