1 /******************************************************************************
3 * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
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. *
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. *
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 ******************************************************************************/
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>
43 using namespace icinga;
45 REGISTER_TYPE(OpenTsdbWriter);
47 REGISTER_STATSFUNCTION(OpenTsdbWriter, &OpenTsdbWriter::StatsFunc);
49 void OpenTsdbWriter::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
51 Dictionary::Ptr nodes = new Dictionary();
53 for (const OpenTsdbWriter::Ptr& opentsdbwriter : ConfigType::GetObjectsByType<OpenTsdbWriter>()) {
54 nodes->Set(opentsdbwriter->GetName(), 1); //add more stats
57 status->Set("opentsdbwriter", nodes);
60 void OpenTsdbWriter::Start(bool runtimeCreated)
62 ObjectImpl<OpenTsdbWriter>::Start(runtimeCreated);
64 Log(LogInformation, "OpentsdbWriter")
65 << "'" << GetName() << "' started.";
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);
73 Service::OnNewCheckResult.connect(boost::bind(&OpenTsdbWriter::CheckResultHandler, this, _1, _2));
76 void OpenTsdbWriter::Stop(bool runtimeRemoved)
78 Log(LogInformation, "OpentsdbWriter")
79 << "'" << GetName() << "' stopped.";
81 ObjectImpl<OpenTsdbWriter>::Stop(runtimeRemoved);
84 void OpenTsdbWriter::ReconnectTimerHandler(void)
89 TcpSocket::Ptr socket = new TcpSocket();
91 Log(LogNotice, "OpenTsdbWriter")
92 << "Reconnect to OpenTSDB TSD on host '" << GetHost() << "' port '" << GetPort() << "'.";
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() << "'.";
102 m_Stream = new NetworkStream(socket);
105 void OpenTsdbWriter::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
107 CONTEXT("Processing check result for '" + checkable->GetName() + "'");
109 if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !checkable->GetEnablePerfdata())
112 Service::Ptr service = dynamic_pointer_cast<Service>(checkable);
116 host = service->GetHost();
118 host = static_pointer_cast<Host>(checkable);
121 std::map<String, String> tags;
123 String escaped_hostName = EscapeMetric(host->GetName());
124 tags["host"] = escaped_hostName;
126 double ts = cr->GetExecutionEnd();
129 String serviceName = service->GetShortName();
130 String escaped_serviceName = EscapeMetric(serviceName);
131 metric = "icinga.service." + escaped_serviceName;
133 SendMetric(metric + ".state", tags, service->GetState(), ts);
135 metric = "icinga.host";
136 SendMetric(metric + ".state", tags, host->GetState(), ts);
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);
144 SendPerfdata(metric, tags, cr, ts);
146 metric = "icinga.check";
149 tags["type"] = "service";
150 String serviceName = service->GetShortName();
151 String escaped_serviceName = EscapeTag(serviceName);
152 tags["service"] = escaped_serviceName;
154 tags["type"] = "host";
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);
163 void OpenTsdbWriter::SendPerfdata(const String& metric, const std::map<String, String>& tags, const CheckResult::Ptr& cr, double ts)
165 Array::Ptr perfdata = cr->GetPerformanceData();
170 ObjectLock olock(perfdata);
171 for (const Value& val : perfdata) {
172 PerfdataValue::Ptr pdv;
174 if (val.IsObjectType<PerfdataValue>())
178 pdv = PerfdataValue::Parse(val);
179 } catch (const std::exception&) {
180 Log(LogWarning, "OpenTsdbWriter")
181 << "Ignoring invalid perfdata value: " << val;
186 String escaped_key = EscapeMetric(pdv->GetLabel());
187 boost::algorithm::replace_all(escaped_key, "::", ".");
189 SendMetric(metric + "." + escaped_key, tags, pdv->GetValue(), ts);
192 SendMetric(metric + "." + escaped_key + "_crit", tags, pdv->GetCrit(), ts);
194 SendMetric(metric + "." + escaped_key + "_warn", tags, pdv->GetWarn(), ts);
196 SendMetric(metric + "." + escaped_key + "_min", tags, pdv->GetMin(), ts);
198 SendMetric(metric + "." + escaped_key + "_max", tags, pdv->GetMax(), ts);
202 void OpenTsdbWriter::SendMetric(const String& metric, const std::map<String, String>& tags, double value, double ts)
204 String tags_string = "";
205 for (const Dictionary::Pair& tag : tags) {
206 tags_string += " " + tag.first + "=" + Convert::ToString(tag.second);
209 std::ostringstream msgbuf;
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"
215 msgbuf << "put " << metric << " " << static_cast<long>(ts) << " " << Convert::ToString(value) << " " << tags_string;
217 Log(LogDebug, "OpenTsdbWriter")
218 << "Add to metric list:'" << msgbuf.str() << "'.";
220 /* do not send \n to debug log */
222 String put = msgbuf.str();
224 ObjectLock olock(this);
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() + "'.";
239 /* for metric and tag name rules, see
240 * http://opentsdb.net/docs/build/html/user_guide/writing.html#metrics-and-tags
242 String OpenTsdbWriter::EscapeTag(const String& str)
246 boost::replace_all(result, " ", "_");
247 boost::replace_all(result, "\\", "_");
252 String OpenTsdbWriter::EscapeMetric(const String& str)
256 boost::replace_all(result, " ", "_");
257 boost::replace_all(result, ".", "_");
258 boost::replace_all(result, "\\", "_");