1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "perfdata/opentsdbwriter.hpp"
4 #include "perfdata/opentsdbwriter-ti.cpp"
5 #include "icinga/service.hpp"
6 #include "icinga/checkcommand.hpp"
7 #include "icinga/macroprocessor.hpp"
8 #include "icinga/icingaapplication.hpp"
9 #include "icinga/compatutility.hpp"
10 #include "base/tcpsocket.hpp"
11 #include "base/configtype.hpp"
12 #include "base/objectlock.hpp"
13 #include "base/logger.hpp"
14 #include "base/convert.hpp"
15 #include "base/utility.hpp"
16 #include "base/perfdatavalue.hpp"
17 #include "base/application.hpp"
18 #include "base/stream.hpp"
19 #include "base/networkstream.hpp"
20 #include "base/exception.hpp"
21 #include "base/statsfunction.hpp"
22 #include <boost/algorithm/string.hpp>
23 #include <boost/algorithm/string/replace.hpp>
25 using namespace icinga;
27 REGISTER_TYPE(OpenTsdbWriter);
29 REGISTER_STATSFUNCTION(OpenTsdbWriter, &OpenTsdbWriter::StatsFunc);
32 * Enable HA capabilities once the config object is loaded.
34 void OpenTsdbWriter::OnConfigLoaded()
36 ObjectImpl<OpenTsdbWriter>::OnConfigLoaded();
39 Log(LogDebug, "OpenTsdbWriter")
40 << "HA functionality disabled. Won't pause connection: " << GetName();
42 SetHAMode(HARunEverywhere);
49 * Feature stats interface
51 * @param status Key value pairs for feature stats
53 void OpenTsdbWriter::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
57 for (const OpenTsdbWriter::Ptr& opentsdbwriter : ConfigType::GetObjectsByType<OpenTsdbWriter>()) {
58 nodes.emplace_back(opentsdbwriter->GetName(), new Dictionary({
59 { "connected", opentsdbwriter->GetConnected() }
63 status->Set("opentsdbwriter", new Dictionary(std::move(nodes)));
67 * Resume is equivalent to Start, but with HA capabilities to resume at runtime.
69 void OpenTsdbWriter::Resume()
71 ObjectImpl<OpenTsdbWriter>::Resume();
73 Log(LogInformation, "OpentsdbWriter")
74 << "'" << GetName() << "' resumed.";
76 m_ReconnectTimer = new Timer();
77 m_ReconnectTimer->SetInterval(10);
78 m_ReconnectTimer->OnTimerExpired.connect(std::bind(&OpenTsdbWriter::ReconnectTimerHandler, this));
79 m_ReconnectTimer->Start();
80 m_ReconnectTimer->Reschedule(0);
82 Service::OnNewCheckResult.connect(std::bind(&OpenTsdbWriter::CheckResultHandler, this, _1, _2));
86 * Pause is equivalent to Stop, but with HA capabilities to resume at runtime.
88 void OpenTsdbWriter::Pause()
90 m_ReconnectTimer.reset();
92 Log(LogInformation, "OpentsdbWriter")
93 << "'" << GetName() << "' paused.";
99 ObjectImpl<OpenTsdbWriter>::Pause();
103 * Reconnect handler called by the timer.
106 void OpenTsdbWriter::ReconnectTimerHandler()
111 SetShouldConnect(true);
116 Log(LogNotice, "OpenTsdbWriter")
117 << "Reconnect to OpenTSDB TSD on host '" << GetHost() << "' port '" << GetPort() << "'.";
120 * We're using telnet as input method. Future PRs may change this into using the HTTP API.
121 * http://opentsdb.net/docs/build/html/user_guide/writing/index.html#telnet
124 m_Stream = std::make_shared<AsioTcpStream>(IoEngine::Get().GetIoContext());
127 icinga::Connect(m_Stream->lowest_layer(), GetHost(), GetPort());
128 } catch (const std::exception& ex) {
129 Log(LogWarning, "OpenTsdbWriter")
130 << "Can't connect to OpenTSDB on host '" << GetHost() << "' port '" << GetPort() << ".'";
137 * Registered check result handler processing data.
138 * Calculates tags from the config.
140 * @param checkable Host/service object
141 * @param cr Check result
143 void OpenTsdbWriter::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
148 CONTEXT("Processing check result for '" + checkable->GetName() + "'");
150 if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !checkable->GetEnablePerfdata())
153 Service::Ptr service = dynamic_pointer_cast<Service>(checkable);
157 host = service->GetHost();
159 host = static_pointer_cast<Host>(checkable);
162 std::map<String, String> tags;
164 String escaped_hostName = EscapeTag(host->GetName());
165 tags["host"] = escaped_hostName;
167 double ts = cr->GetExecutionEnd();
170 String serviceName = service->GetShortName();
171 String escaped_serviceName = EscapeMetric(serviceName);
172 metric = "icinga.service." + escaped_serviceName;
174 SendMetric(checkable, metric + ".state", tags, service->GetState(), ts);
176 metric = "icinga.host";
177 SendMetric(checkable, metric + ".state", tags, host->GetState(), ts);
180 SendMetric(checkable, metric + ".state_type", tags, checkable->GetStateType(), ts);
181 SendMetric(checkable, metric + ".reachable", tags, checkable->IsReachable(), ts);
182 SendMetric(checkable, metric + ".downtime_depth", tags, checkable->GetDowntimeDepth(), ts);
183 SendMetric(checkable, metric + ".acknowledgement", tags, checkable->GetAcknowledgement(), ts);
185 SendPerfdata(checkable, metric, tags, cr, ts);
187 metric = "icinga.check";
190 tags["type"] = "service";
191 String serviceName = service->GetShortName();
192 String escaped_serviceName = EscapeTag(serviceName);
193 tags["service"] = escaped_serviceName;
195 tags["type"] = "host";
198 SendMetric(checkable, metric + ".current_attempt", tags, checkable->GetCheckAttempt(), ts);
199 SendMetric(checkable, metric + ".max_check_attempts", tags, checkable->GetMaxCheckAttempts(), ts);
200 SendMetric(checkable, metric + ".latency", tags, cr->CalculateLatency(), ts);
201 SendMetric(checkable, metric + ".execution_time", tags, cr->CalculateExecutionTime(), ts);
205 * Parse and send performance data metrics to OpenTSDB
207 * @param checkable Host/service object
208 * @param metric Full metric name
209 * @param tags Tag key pairs
210 * @param cr Check result containing performance data
211 * @param ts Timestamp when the check result was received
213 void OpenTsdbWriter::SendPerfdata(const Checkable::Ptr& checkable, const String& metric,
214 const std::map<String, String>& tags, const CheckResult::Ptr& cr, double ts)
216 Array::Ptr perfdata = cr->GetPerformanceData();
221 CheckCommand::Ptr checkCommand = checkable->GetCheckCommand();
223 ObjectLock olock(perfdata);
224 for (const Value& val : perfdata) {
225 PerfdataValue::Ptr pdv;
227 if (val.IsObjectType<PerfdataValue>())
231 pdv = PerfdataValue::Parse(val);
232 } catch (const std::exception&) {
233 Log(LogWarning, "OpenTsdbWriter")
234 << "Ignoring invalid perfdata for checkable '"
235 << checkable->GetName() << "' and command '"
236 << checkCommand->GetName() << "' with value: " << val;
241 String escaped_key = EscapeMetric(pdv->GetLabel());
242 boost::algorithm::replace_all(escaped_key, "::", ".");
244 SendMetric(checkable, metric + "." + escaped_key, tags, pdv->GetValue(), ts);
247 SendMetric(checkable, metric + "." + escaped_key + "_crit", tags, pdv->GetCrit(), ts);
249 SendMetric(checkable, metric + "." + escaped_key + "_warn", tags, pdv->GetWarn(), ts);
251 SendMetric(checkable, metric + "." + escaped_key + "_min", tags, pdv->GetMin(), ts);
253 SendMetric(checkable, metric + "." + escaped_key + "_max", tags, pdv->GetMax(), ts);
258 * Send given metric to OpenTSDB
260 * @param checkable Host/service object
261 * @param metric Full metric name
262 * @param tags Tag key pairs
263 * @param value Floating point metric value
264 * @param ts Timestamp where the metric was received from the check result
266 void OpenTsdbWriter::SendMetric(const Checkable::Ptr& checkable, const String& metric,
267 const std::map<String, String>& tags, double value, double ts)
269 String tags_string = "";
271 for (const Dictionary::Pair& tag : tags) {
272 tags_string += " " + tag.first + "=" + Convert::ToString(tag.second);
275 std::ostringstream msgbuf;
277 * must be (http://opentsdb.net/docs/build/html/user_guide/query/timeseries.html)
278 * put <metric> <timestamp> <value> <tagk1=tagv1[ tagk2=tagv2 ...tagkN=tagvN]>
279 * "tags" must include at least one tag, we use "host=HOSTNAME"
281 msgbuf << "put " << metric << " " << static_cast<long>(ts) << " " << Convert::ToString(value) << " " << tags_string;
283 Log(LogDebug, "OpenTsdbWriter")
284 << "Checkable '" << checkable->GetName() << "' adds to metric list: '" << msgbuf.str() << "'.";
286 /* do not send \n to debug log */
288 String put = msgbuf.str();
290 ObjectLock olock(this);
296 Log(LogDebug, "OpenTsdbWriter")
297 << "Checkable '" << checkable->GetName() << "' sending message '" << put << "'.";
299 boost::asio::write(*m_Stream, boost::asio::buffer(msgbuf.str()));
301 } catch (const std::exception& ex) {
302 Log(LogCritical, "OpenTsdbWriter")
303 << "Cannot write to TCP socket on host '" << GetHost() << "' port '" << GetPort() << "'.";
308 * Escape tags for OpenTSDB
309 * http://opentsdb.net/docs/build/html/user_guide/query/timeseries.html#precisions-on-metrics-and-tags
311 * @param str Tag name
312 * @return Escaped tag
314 String OpenTsdbWriter::EscapeTag(const String& str)
318 boost::replace_all(result, " ", "_");
319 boost::replace_all(result, "\\", "_");
325 * Escape metric name for OpenTSDB
326 * http://opentsdb.net/docs/build/html/user_guide/query/timeseries.html#precisions-on-metrics-and-tags
328 * @param str Metric name
329 * @return Escaped metric
331 String OpenTsdbWriter::EscapeMetric(const String& str)
335 boost::replace_all(result, " ", "_");
336 boost::replace_all(result, ".", "_");
337 boost::replace_all(result, "\\", "_");
338 boost::replace_all(result, ":", "_");