]> granicus.if.org Git - icinga2/blob - lib/perfdata/opentsdbwriter.cpp
Move PerfdataValue() class into base library
[icinga2] / lib / perfdata / opentsdbwriter.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "perfdata/opentsdbwriter.hpp"
21 #include "perfdata/opentsdbwriter.tcpp"
22 #include "icinga/service.hpp"
23 #include "icinga/macroprocessor.hpp"
24 #include "icinga/icingaapplication.hpp"
25 #include "icinga/compatutility.hpp"
26 #include "base/tcpsocket.hpp"
27 #include "base/configtype.hpp"
28 #include "base/objectlock.hpp"
29 #include "base/logger.hpp"
30 #include "base/convert.hpp"
31 #include "base/utility.hpp"
32 #include "base/perfdatavalue.hpp"
33 #include "base/application.hpp"
34 #include "base/stream.hpp"
35 #include "base/networkstream.hpp"
36 #include "base/exception.hpp"
37 #include "base/statsfunction.hpp"
38 #include <boost/algorithm/string.hpp>
39 #include <boost/algorithm/string/classification.hpp>
40 #include <boost/algorithm/string/split.hpp>
41 #include <boost/algorithm/string/replace.hpp>
42
43 using namespace icinga;
44
45 REGISTER_TYPE(OpenTsdbWriter);
46
47 REGISTER_STATSFUNCTION(OpenTsdbWriter, &OpenTsdbWriter::StatsFunc);
48
49 void OpenTsdbWriter::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
50 {
51         Dictionary::Ptr nodes = new Dictionary();
52
53         for (const OpenTsdbWriter::Ptr& opentsdbwriter : ConfigType::GetObjectsByType<OpenTsdbWriter>()) {
54                 nodes->Set(opentsdbwriter->GetName(), 1); //add more stats
55         }
56
57         status->Set("opentsdbwriter", nodes);
58 }
59
60 void OpenTsdbWriter::Start(bool runtimeCreated)
61 {
62         ObjectImpl<OpenTsdbWriter>::Start(runtimeCreated);
63
64         Log(LogInformation, "OpentsdbWriter")
65             << "'" << GetName() << "' started.";
66
67         m_ReconnectTimer = new Timer();
68         m_ReconnectTimer->SetInterval(10);
69         m_ReconnectTimer->OnTimerExpired.connect(boost::bind(&OpenTsdbWriter::ReconnectTimerHandler, this));
70         m_ReconnectTimer->Start();
71         m_ReconnectTimer->Reschedule(0);
72
73         Service::OnNewCheckResult.connect(boost::bind(&OpenTsdbWriter::CheckResultHandler, this, _1, _2));
74 }
75
76 void OpenTsdbWriter::Stop(bool runtimeRemoved)
77 {
78         Log(LogInformation, "OpentsdbWriter")
79             << "'" << GetName() << "' stopped.";
80
81         ObjectImpl<OpenTsdbWriter>::Stop(runtimeRemoved);
82 }
83
84 void OpenTsdbWriter::ReconnectTimerHandler(void)
85 {
86         if (m_Stream)
87                 return;
88
89         TcpSocket::Ptr socket = new TcpSocket();
90
91         Log(LogNotice, "OpenTsdbWriter")
92                 << "Reconnect to OpenTSDB TSD on host '" << GetHost() << "' port '" << GetPort() << "'.";
93
94         try {
95                 socket->Connect(GetHost(), GetPort());
96         } catch (std::exception&) {
97                 Log(LogCritical, "OpenTsdbWriter")
98                         << "Can't connect to OpenTSDB TSD on host '" << GetHost() << "' port '" << GetPort() << "'.";
99                 return;
100         }
101
102         m_Stream = new NetworkStream(socket);
103 }
104
105 void OpenTsdbWriter::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
106 {
107         CONTEXT("Processing check result for '" + checkable->GetName() + "'");
108
109         if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !checkable->GetEnablePerfdata())
110                 return;
111
112         Service::Ptr service = dynamic_pointer_cast<Service>(checkable);
113         Host::Ptr host;
114
115         if (service)
116                 host = service->GetHost();
117         else
118                 host = static_pointer_cast<Host>(checkable);
119
120         String metric;
121         std::map<String, String> tags;
122
123         String escaped_hostName = EscapeMetric(host->GetName());
124         tags["host"] = escaped_hostName;
125
126         double ts = cr->GetExecutionEnd();
127
128         if (service) {
129                 String serviceName = service->GetShortName();
130                 String escaped_serviceName = EscapeMetric(serviceName);
131                 metric = "icinga.service." + escaped_serviceName;
132
133                 SendMetric(metric + ".state", tags, service->GetState(), ts);
134         } else {
135                 metric = "icinga.host";
136                 SendMetric(metric + ".state", tags, host->GetState(), ts);
137         }
138
139         SendMetric(metric + ".state_type", tags, checkable->GetStateType(), ts);
140         SendMetric(metric + ".reachable", tags, checkable->IsReachable(), ts);
141         SendMetric(metric + ".downtime_depth", tags, checkable->GetDowntimeDepth(), ts);
142         SendMetric(metric + ".acknowledgement", tags, checkable->GetAcknowledgement(), ts);
143
144         SendPerfdata(metric, tags, cr, ts);
145
146         metric = "icinga.check";
147
148         if (service) {
149                 tags["type"] = "service";
150                 String serviceName = service->GetShortName();
151                 String escaped_serviceName = EscapeTag(serviceName);
152                 tags["service"] = escaped_serviceName;
153         } else {
154                 tags["type"] = "host";
155         }
156
157         SendMetric(metric + ".current_attempt", tags, checkable->GetCheckAttempt(), ts);
158         SendMetric(metric + ".max_check_attempts", tags, checkable->GetMaxCheckAttempts(), ts);
159         SendMetric(metric + ".latency", tags, cr->CalculateLatency(), ts);
160         SendMetric(metric + ".execution_time", tags, cr->CalculateExecutionTime(), ts);
161 }
162
163 void OpenTsdbWriter::SendPerfdata(const String& metric, const std::map<String, String>& tags, const CheckResult::Ptr& cr, double ts)
164 {
165         Array::Ptr perfdata = cr->GetPerformanceData();
166
167         if (!perfdata)
168                 return;
169
170         ObjectLock olock(perfdata);
171         for (const Value& val : perfdata) {
172                 PerfdataValue::Ptr pdv;
173
174                 if (val.IsObjectType<PerfdataValue>())
175                         pdv = val;
176                 else {
177                         try {
178                                 pdv = PerfdataValue::Parse(val);
179                         } catch (const std::exception&) {
180                                 Log(LogWarning, "OpenTsdbWriter")
181                                         << "Ignoring invalid perfdata value: " << val;
182                                 continue;
183                         }
184                 }
185
186                 String escaped_key = EscapeMetric(pdv->GetLabel());
187                 boost::algorithm::replace_all(escaped_key, "::", ".");
188
189                 SendMetric(metric + "." + escaped_key, tags, pdv->GetValue(), ts);
190
191                 if (pdv->GetCrit())
192                         SendMetric(metric + "." + escaped_key + "_crit", tags, pdv->GetCrit(), ts);
193                 if (pdv->GetWarn())
194                         SendMetric(metric + "." + escaped_key + "_warn", tags, pdv->GetWarn(), ts);
195                 if (pdv->GetMin())
196                         SendMetric(metric + "." + escaped_key + "_min", tags, pdv->GetMin(), ts);
197                 if (pdv->GetMax())
198                         SendMetric(metric + "." + escaped_key + "_max", tags, pdv->GetMax(), ts);
199         }
200 }
201
202 void OpenTsdbWriter::SendMetric(const String& metric, const std::map<String, String>& tags, double value, double ts)
203 {
204         String tags_string = "";
205         for (const Dictionary::Pair& tag : tags) {
206                 tags_string += " " + tag.first + "=" + Convert::ToString(tag.second);
207         }
208
209         std::ostringstream msgbuf;
210         /*
211          * must be (http://opentsdb.net/docs/build/html/user_guide/writing.html)
212          * put <metric> <timestamp> <value> <tagk1=tagv1[ tagk2=tagv2 ...tagkN=tagvN]>
213          * "tags" must include at least one tag, we use "host=HOSTNAME"
214          */
215         msgbuf << "put " << metric << " " << static_cast<long>(ts) << " " << Convert::ToString(value) << " " << tags_string;
216
217         Log(LogDebug, "OpenTsdbWriter")
218                 << "Add to metric list:'" << msgbuf.str() << "'.";
219
220         /* do not send \n to debug log */
221         msgbuf << "\n";
222         String put = msgbuf.str();
223
224         ObjectLock olock(this);
225
226         if (!m_Stream)
227                 return;
228
229         try {
230                 m_Stream->Write(put.CStr(), put.GetLength());
231         } catch (const std::exception& ex) {
232                 Log(LogCritical, "OpenTsdbWriter")
233                         << "Cannot write to OpenTSDB TSD on host '" << GetHost() << "' port '" << GetPort() + "'.";
234
235                 m_Stream.reset();
236         }
237 }
238
239 /* for metric and tag name rules, see
240  * http://opentsdb.net/docs/build/html/user_guide/writing.html#metrics-and-tags
241  */
242 String OpenTsdbWriter::EscapeTag(const String& str)
243 {
244         String result = str;
245
246         boost::replace_all(result, " ", "_");
247         boost::replace_all(result, "\\", "_");
248
249         return result;
250 }
251
252 String OpenTsdbWriter::EscapeMetric(const String& str)
253 {
254         String result = str;
255
256         boost::replace_all(result, " ", "_");
257         boost::replace_all(result, ".", "_");
258         boost::replace_all(result, "\\", "_");
259
260         return result;
261 }