]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/auth-carbon.cc
Meson: Separate test files from common files
[thirdparty/pdns.git] / pdns / auth-carbon.cc
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 */
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 #include "statbag.hh"
26 #include "logger.hh"
27 #include "threadname.hh"
28 #include "iputils.hh"
29 #include "sstuff.hh"
30 #include "arguments.hh"
31 #include "auth-main.hh"
32
33 #include "namespaces.hh"
34
35 void carbonDumpThread()
36 try
37 {
38 setThreadName("pdns/carbonDump");
39 extern StatBag S;
40
41 string namespace_name=arg()["carbon-namespace"];
42 string hostname=arg()["carbon-ourname"];
43 if (hostname.empty()) {
44 try {
45 hostname = getCarbonHostName();
46 }
47 catch(const std::exception& e) {
48 throw std::runtime_error(std::string("The 'carbon-ourname' setting has not been set and we are unable to determine the system's hostname: ") + e.what());
49 }
50 }
51 string instance_name=arg()["carbon-instance"];
52
53 vector<string> carbonServers;
54 stringtok(carbonServers, arg()["carbon-server"], ", ");
55
56 for(;;) {
57 if(carbonServers.empty()) {
58 sleep(1);
59 continue;
60 }
61
62 string msg;
63 vector<string> entries = S.getEntries();
64 ostringstream str;
65 time_t now=time(nullptr);
66 for(const string& entry : entries) {
67 str<<namespace_name<<'.'<<hostname<<'.'<<instance_name<<'.'<<entry<<' '<<S.read(entry)<<' '<<now<<"\r\n";
68 }
69 msg = str.str();
70
71 for (const auto& carbonServer : carbonServers) {
72 ComboAddress remote(carbonServer, 2003);
73
74 try {
75 Socket s(remote.sin4.sin_family, SOCK_STREAM);
76 s.setNonBlocking();
77 s.connect(remote, 2);
78
79 writen2WithTimeout(s.getHandle(), msg.c_str(), msg.length(), timeval{2,0});
80 } catch (runtime_error &e){
81 g_log<<Logger::Warning<<"Unable to write data to carbon server at "<<remote.toStringWithPort()<<": "<<e.what()<<endl;
82 continue;
83 }
84 }
85 sleep(arg().asNum("carbon-interval"));
86 }
87 }
88 catch(std::exception& e)
89 {
90 g_log<<Logger::Error<<"Carbon thread died: "<<e.what()<<endl;
91 }
92 catch(PDNSException& e)
93 {
94 g_log<<Logger::Error<<"Carbon thread died, PDNSException: "<<e.reason<<endl;
95 }
96 catch(...)
97 {
98 g_log<<Logger::Error<<"Carbon thread died"<<endl;
99 }