]> granicus.if.org Git - icinga2/blobdiff - components/perfdata/graphitewriter.cpp
Remove the HostUnreachable state.
[icinga2] / components / perfdata / graphitewriter.cpp
index b2df6cf55b49ab2928d4084ae53c57fc9bb185cf..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                *
@@ -34,6 +34,7 @@
 #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>
@@ -44,6 +45,21 @@ 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,28 +94,47 @@ void GraphiteWriter::ReconnectTimerHandler(void)
        m_Stream = make_shared<BufferedStream>(net_stream);
 }
 
-void GraphiteWriter::CheckResultHandler(const Service::Ptr& service, const CheckResult::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;
 
-       /* 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, "state", service->GetState());
+       } else {
+               prefix = "icinga." + hostName;
+
+               SendMetric(prefix, "state", host->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);
+}
 
+void GraphiteWriter::SendPerfdata(const String& prefix, const CheckResult::Ptr& cr)
+{
        Value pdv = cr->GetPerformanceData();
 
        if (!pdv.IsObjectType<Dictionary>())