/******************************************************************************
* Icinga 2 *
- * Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
+ * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
#include "icinga/macroprocessor.h"
#include "icinga/icingaapplication.h"
#include "icinga/compatutility.h"
+#include "icinga/perfdatavalue.h"
#include "base/tcpsocket.h"
#include "base/dynamictype.h"
#include "base/objectlock.h"
#include "base/stream.h"
#include "base/networkstream.h"
#include "base/bufferedstream.h"
+#include "base/exception.h"
+#include "base/statsfunction.h"
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/foreach.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/replace.hpp>
-#include <boost/exception/diagnostic_information.hpp>
using namespace icinga;
REGISTER_TYPE(GraphiteWriter);
+REGISTER_STATSFUNCTION(GraphiteWriterStats, &GraphiteWriter::StatsFunc);
+
+Value GraphiteWriter::StatsFunc(Dictionary::Ptr& status, Dictionary::Ptr& perfdata)
+{
+ Dictionary::Ptr nodes = make_shared<Dictionary>();
+
+ BOOST_FOREACH(const GraphiteWriter::Ptr& graphitewriter, DynamicType::GetObjects<GraphiteWriter>()) {
+ nodes->Set(graphitewriter->GetName(), 1); //add more stats
+ }
+
+ status->Set("graphitewriter", nodes);
+
+ return 0;
+}
+
void GraphiteWriter::Start(void)
{
DynamicObject::Start();
return;
}
} catch (const std::exception& ex) {
- Log(LogWarning, "perfdata", "GraphiteWriter socket on host '" + GetHost() + "' port '" + GetPort() + "' gone. Attempting to reconnect.");
+ Log(LogWarning, "perfdata", "GraphiteWriter socket on host '" + GetHost() + "' port '" + GetPort() + "' gone. Attempting to reconnect.");
}
TcpSocket::Ptr socket = make_shared<TcpSocket>();
m_Stream = make_shared<BufferedStream>(net_stream);
}
-void GraphiteWriter::CheckResultHandler(const Service::Ptr& service, const Dictionary::Ptr& cr)
+void GraphiteWriter::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr)
{
- if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !service->GetEnablePerfdata())
+ if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !checkable->GetEnablePerfdata())
return;
- Host::Ptr host = service->GetHost();
+ Service::Ptr service = dynamic_pointer_cast<Service>(checkable);
+ Host::Ptr host;
- if (!host)
- return;
+ if (service)
+ host = service->GetHost();
+ else
+ host = static_pointer_cast<Host>(checkable);
- /* service metrics */
- std::vector<String> metrics;
- String metricName;
- Value metricValue;
-
- /* basic metrics */
- AddServiceMetric(metrics, service, "current_attempt", service->GetCheckAttempt());
- AddServiceMetric(metrics, service, "max_check_attempts", service->GetMaxCheckAttempts());
- AddServiceMetric(metrics, service, "state_type", service->GetStateType());
- AddServiceMetric(metrics, service, "state", service->GetState());
- AddServiceMetric(metrics, service, "latency", Service::CalculateLatency(cr));
- AddServiceMetric(metrics, service, "execution_time", Service::CalculateExecutionTime(cr));
-
- /* performance data metrics */
- String perfdata = CompatUtility::GetCheckResultPerfdata(cr);
-
- if (!perfdata.IsEmpty()) {
- perfdata.Trim();
-
- Log(LogDebug, "perfdata", "GraphiteWriter: Processing perfdata: '" + perfdata + "'.");
-
- /*
- * 'foo bar'=0;;; baz=0.0;;;
- * 'label'=value[UOM];[warn];[crit];[min];[max]
- */
- std::vector<String> tokens;
- boost::algorithm::split(tokens, perfdata, boost::is_any_of(" "));
-
- /* TODO deal with white spaces in single quoted labels: 'foo bar'=0;;; 'baz'=1.0;;;
- * 1. find first ', find second ' -> if no '=' in between, this is a label
- * 2. two single quotes define an escaped single quite
- * 3. warn/crit/min/max may be null and semicolon delimiter omitted
- * https://www.nagios-plugins.org/doc/guidelines.html#AEN200
- */
- BOOST_FOREACH(const String& token, tokens) {
- String metricKeyVal = token;
- metricKeyVal.Trim();
-
- std::vector<String> key_val;
- boost::algorithm::split(key_val, metricKeyVal, boost::is_any_of("="));
-
- if (key_val.size() == 0) {
- Log(LogWarning, "perfdata", "GraphiteWriter: Invalid performance data. No assignment operator found in :'" + metricKeyVal + "'.");
- return;
- }
-
- String metricName = key_val[0];
- metricName.Trim();
-
- if (key_val.size() == 1) {
- Log(LogWarning, "perfdata", "GraphiteWriter: Invalid performance data: '" + metricKeyVal + "' with key: '" + metricName + "'.");
- return;
- }
-
- String metricValues = key_val[1];
- metricValues.Trim();
-
- std::vector<String> perfdata_values;
- boost::algorithm::split(perfdata_values, metricValues, boost::is_any_of(";"));
-
- if (perfdata_values.size() == 0) {
- Log(LogWarning, "perfdata", "GraphiteWriter: Invalid performance data: '" + metricKeyVal +
- "' with key: '" + metricName + "' and values: '" + metricValues + "'.");
- return;
- }
-
- String metricValue = perfdata_values[0];
-
- metricValue.Trim();
- Log(LogDebug, "perfdata", "GraphiteWriter: Trimmed metric value: '" + metricValue + "'.");
-
- /* extract raw value (digit number digit as double) and uom
- * http://en.highscore.de/cpp/boost/stringhandling.html
- */
- String metricValueRaw = boost::algorithm::trim_right_copy_if(metricValue, (!boost::algorithm::is_digit() && !boost::algorithm::is_any_of(".,")));
- String metricValueUom = boost::algorithm::trim_left_copy_if(metricValue, (boost::algorithm::is_digit() || boost::algorithm::is_any_of(".,")));
-
- Log(LogDebug, "perfdata", "GraphiteWriter: Raw metric value: '" + metricValueRaw + "' with UOM: '" + metricValueUom + "'.");
-
- /* TODO: Normalize raw value based on UOM
- * a. empty - assume a number
- * b. 's' - seconds (us, ms)
- * c. '%' - percentage
- * d. 'B' - bytes (KB, MB, GB, TB)
- * e. 'c' - continous counter (snmp)
- */
-
- /* //TODO: Figure out how graphite handles warn/crit/min/max
- String metricValueWarn, metricValueCrit, metricValueMin, metricValueMax;
-
- if (perfdata_values.size() > 1)
- metricValueWarn = perfdata_values[1];
- if (perfdata_values.size() > 2)
- metricValueCrit = perfdata_values[2];
- if (perfdata_values.size() > 3)
- metricValueMin = perfdata_values[3];
- if (perfdata_values.size() > 4)
- metricValueMax = perfdata_values[4];
- */
-
- /* sanitize invalid metric characters */
- SanitizeMetric(metricName);
-
- AddServiceMetric(metrics, service, metricName, metricValueRaw);
- }
+ String hostName = host->GetName();
+ SanitizeMetric(hostName);
+
+ String prefix;
+
+ if (service) {
+ String serviceName = service->GetShortName();
+ SanitizeMetric(serviceName);
+ prefix = "icinga." + hostName + "." + serviceName;
+
+ SendMetric(prefix, "state", service->GetState());
+ } else {
+ prefix = "icinga." + hostName;
+
+ SendMetric(prefix, "state", host->GetState());
}
- SendMetrics(metrics);
+ SendMetric(prefix, "current_attempt", checkable->GetCheckAttempt());
+ SendMetric(prefix, "max_check_attempts", checkable->GetMaxCheckAttempts());
+ SendMetric(prefix, "state_type", checkable->GetStateType());
+ SendMetric(prefix, "reachable", checkable->IsReachable());
+ SendMetric(prefix, "latency", Service::CalculateLatency(cr));
+ SendMetric(prefix, "execution_time", Service::CalculateExecutionTime(cr));
+ SendPerfdata(prefix, cr);
}
-void GraphiteWriter::AddServiceMetric(std::vector<String>& metrics, const Service::Ptr& service, const String& name, const Value& value)
+void GraphiteWriter::SendPerfdata(const String& prefix, const CheckResult::Ptr& cr)
{
- /* TODO: sanitize host and service names */
- String hostName = service->GetHost()->GetName();
- String serviceName = service->GetShortName();
+ Value pdv = cr->GetPerformanceData();
- SanitizeMetric(hostName);
- SanitizeMetric(serviceName);
+ if (!pdv.IsObjectType<Dictionary>())
+ return;
- String metricPrefix = hostName + "." + serviceName;
- String graphitePrefix = "icinga";
+ Dictionary::Ptr perfdata = pdv;
- String metric = graphitePrefix + "." + metricPrefix + "." + name + " " + Convert::ToString(value) + " " + Convert::ToString(static_cast<long>(Utility::GetTime())) + "\n";
- Log(LogDebug, "perfdata", "GraphiteWriter: Add to metric list:'" + metric + "'.");
- metrics.push_back(metric);
+ ObjectLock olock(perfdata);
+ BOOST_FOREACH(const Dictionary::Pair& kv, perfdata) {
+ double valueNum;
+
+ if (!kv.second.IsObjectType<PerfdataValue>())
+ valueNum = kv.second;
+ else
+ valueNum = static_cast<PerfdataValue::Ptr>(kv.second)->GetValue();
+
+ String escaped_key = kv.first;
+ SanitizeMetric(escaped_key);
+ boost::algorithm::replace_all(escaped_key, "::", ".");
+
+ SendMetric(prefix, escaped_key, valueNum);
+ }
}
-void GraphiteWriter::SendMetrics(const std::vector<String>& metrics)
+void GraphiteWriter::SendMetric(const String& prefix, const String& name, double value)
{
- BOOST_FOREACH(const String& metric, metrics) {
- if (metric.IsEmpty())
- continue;
+ std::ostringstream msgbuf;
+ msgbuf << prefix << "." << name << " " << value << " " << static_cast<long>(Utility::GetTime()) << "\n";
- Log(LogDebug, "perfdata", "GraphiteWriter: Sending metric '" + metric + "'.");
+ String metric = msgbuf.str();
+ Log(LogDebug, "perfdata", "GraphiteWriter: Add to metric list:'" + metric + "'.");
- ObjectLock olock(this);
+ ObjectLock olock(this);
- if (!m_Stream)
- return;
+ if (!m_Stream)
+ return;
- try {
- m_Stream->Write(metric.CStr(), metric.GetLength());
- } catch (const std::exception& ex) {
- std::ostringstream msgbuf;
- msgbuf << "Exception thrown while writing to the Graphite socket: " << std::endl
- << boost::diagnostic_information(ex);
+ try {
+ m_Stream->Write(metric.CStr(), metric.GetLength());
+ } catch (const std::exception& ex) {
+ std::ostringstream msgbuf;
+ msgbuf << "Exception thrown while writing to the Graphite socket: " << std::endl
+ << DiagnosticInformation(ex);
- Log(LogCritical, "base", msgbuf.str());
+ Log(LogCritical, "base", msgbuf.str());
- m_Stream.reset();
- }
+ m_Stream.reset();
}
}
boost::replace_all(str, "\\", "_");
boost::replace_all(str, "/", "_");
}
-