]> granicus.if.org Git - icinga2/blobdiff - components/perfdata/graphitewriter.cpp
Remove the HostUnreachable state.
[icinga2] / components / perfdata / graphitewriter.cpp
index c82c703821c8143df90dc9f54040e010d0bfbcc3..f81d4c76cdb168b491e75abec78631388efc3055 100644 (file)
@@ -1,6 +1,6 @@
 /******************************************************************************
  * 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 "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();
@@ -66,7 +82,7 @@ void GraphiteWriter::ReconnectTimerHandler(void)
                        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>();
@@ -78,46 +94,68 @@ void GraphiteWriter::ReconnectTimerHandler(void)
        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;
 
-       /* TODO: sanitize host and service names */
-       String hostName = service->GetHost()->GetName();
-       String serviceName = service->GetShortName();   
+       Service::Ptr service = dynamic_pointer_cast<Service>(checkable);
+       Host::Ptr host;
 
+       if (service)
+               host = service->GetHost();
+       else
+               host = static_pointer_cast<Host>(checkable);
+
+       String hostName = host->GetName();
        SanitizeMetric(hostName);
-       SanitizeMetric(serviceName);
 
-       String prefix = "icinga." + hostName + "." + serviceName;
+       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());
+       }
 
-       /* basic metrics */
-       SendMetric(prefix, "current_attempt", service->GetCheckAttempt());
-       SendMetric(prefix, "max_check_attempts", service->GetMaxCheckAttempts());
-       SendMetric(prefix, "state_type", service->GetStateType());
-       SendMetric(prefix, "state", service->GetState());
+       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);
+}
 
-       Value pdv = cr->Get("performance_data");
+void GraphiteWriter::SendPerfdata(const String& prefix, const CheckResult::Ptr& cr)
+{
+       Value pdv = cr->GetPerformanceData();
 
        if (!pdv.IsObjectType<Dictionary>())
                return;
 
        Dictionary::Ptr perfdata = pdv;
 
-       String key;
-       Value value;
-       BOOST_FOREACH(boost::tie(key, value), perfdata) {
+       ObjectLock olock(perfdata);
+       BOOST_FOREACH(const Dictionary::Pair& kv, perfdata) {
                double valueNum;
 
-               if (!value.IsObjectType<PerfdataValue>())
-                       valueNum = value;
+               if (!kv.second.IsObjectType<PerfdataValue>())
+                       valueNum = kv.second;
                else
-                       valueNum = static_cast<PerfdataValue::Ptr>(value)->GetValue();
+                       valueNum = static_cast<PerfdataValue::Ptr>(kv.second)->GetValue();
+
+               String escaped_key = kv.first;
+               SanitizeMetric(escaped_key);
+               boost::algorithm::replace_all(escaped_key, "::", ".");
 
-               SendMetric(prefix, key, valueNum);
+               SendMetric(prefix, escaped_key, valueNum);
        }
 }
 
@@ -139,7 +177,7 @@ void GraphiteWriter::SendMetric(const String& prefix, const String& name, double
        } catch (const std::exception& ex) {
                std::ostringstream msgbuf;
                msgbuf << "Exception thrown while writing to the Graphite socket: " << std::endl
-                      << boost::diagnostic_information(ex);
+                      << DiagnosticInformation(ex);
 
                Log(LogCritical, "base", msgbuf.str());