From 66aa874f7dac652130d70515c36b8c9599a1a1a4 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Thu, 13 Feb 2014 19:23:38 +0100 Subject: [PATCH] Refactor ClusterCheckTask based on cluster status. Refs #5444 --- components/cluster/clusterchecktask.cpp | 67 +++++++++++++------------ components/cluster/clusterchecktask.h | 1 + components/cluster/clusterlistener.h | 4 +- 3 files changed, 38 insertions(+), 34 deletions(-) diff --git a/components/cluster/clusterchecktask.cpp b/components/cluster/clusterchecktask.cpp index 43c57315e..ff135a182 100644 --- a/components/cluster/clusterchecktask.cpp +++ b/components/cluster/clusterchecktask.cpp @@ -37,52 +37,55 @@ REGISTER_SCRIPTFUNCTION(ClusterCheck, &ClusterCheckTask::ScriptFunc); CheckResult::Ptr ClusterCheckTask::ScriptFunc(const Service::Ptr&) { - double interval = Utility::GetTime() - Application::GetStartTime(); - - if (interval > 60) - interval = 60; - - double count_endpoints = 0; - std::vector not_connected_endpoints; - std::vector connected_endpoints; - + Dictionary::Ptr status; BOOST_FOREACH(const ClusterListener::Ptr& cluster_listener, DynamicType::GetObjects()) { - String identity = cluster_listener->GetIdentity(); - - BOOST_FOREACH(const Endpoint::Ptr& endpoint, DynamicType::GetObjects()) { - count_endpoints++; - - if(!endpoint->IsConnected() && endpoint->GetName() != identity) - not_connected_endpoints.push_back(endpoint->GetName()); - else if(endpoint->IsConnected() && endpoint->GetName() != identity) - connected_endpoints.push_back(endpoint->GetName()); - } + /* XXX there's only one cluster listener */ + status = cluster_listener->GetClusterStatus(); } - std::sort(not_connected_endpoints.begin(), not_connected_endpoints.end()); - std::sort(connected_endpoints.begin(), connected_endpoints.end()); + String connected_endpoints = FormatArray(status->Get("conn_endpoints")); + String not_connected_endpoints = FormatArray(status->Get("not_conn_endpoints")); + + /* remove unneeded perfdata */ + status->Set("conn_endpoints", Empty); + status->Set("not_conn_endpoints", Empty); ServiceState state = StateOK; - String output = "Icinga 2 Cluster is running: Connected Endpoints: "+ Convert::ToString(connected_endpoints.size()) + " (" + - boost::algorithm::join(connected_endpoints, ",") + ")."; + String output = "Icinga 2 Cluster is running: Connected Endpoints: "+ Convert::ToString(status->Get("num_conn_endpoints")) + " (" + + connected_endpoints + ")."; - if (not_connected_endpoints.size() > 0) { + if (status->Get("num_not_conn_endpoints") > 0) { state = StateCritical; - output = "Icinga 2 Cluster Problem: " + Convert::ToString(not_connected_endpoints.size()) + - " Endpoints (" + boost::algorithm::join(not_connected_endpoints, ",") + ") not connected."; + output = "Icinga 2 Cluster Problem: " + Convert::ToString(status->Get("num_not_conn_endpoints")) + + " Endpoints (" + not_connected_endpoints + ") not connected."; } - Dictionary::Ptr perfdata = make_shared(); - perfdata->Set("num_endpoints", count_endpoints); - perfdata->Set("num_conn_endpoints", connected_endpoints.size()); - perfdata->Set("num_not_conn_endpoints", not_connected_endpoints.size()); - CheckResult::Ptr cr = make_shared(); cr->SetOutput(output); - cr->SetPerformanceData(perfdata); + cr->SetPerformanceData(status); cr->SetState(state); cr->SetCheckSource(IcingaApplication::GetInstance()->GetNodeName()); return cr; } +String ClusterCheckTask::FormatArray(const Array::Ptr& arr) +{ + bool first = true; + String str; + + if (arr) { + ObjectLock olock(arr); + BOOST_FOREACH(const Value& value, arr) { + if (first) + first = false; + else + str += ","; + + str += Convert::ToString(value); + } + } + + return str; +} + diff --git a/components/cluster/clusterchecktask.h b/components/cluster/clusterchecktask.h index 30fc6842d..d34ddd3a5 100644 --- a/components/cluster/clusterchecktask.h +++ b/components/cluster/clusterchecktask.h @@ -37,6 +37,7 @@ public: private: ClusterCheckTask(void); + static String FormatArray(const Array::Ptr& arr); }; } diff --git a/components/cluster/clusterlistener.h b/components/cluster/clusterlistener.h index 5f8e3bf40..cbcdd36b6 100644 --- a/components/cluster/clusterlistener.h +++ b/components/cluster/clusterlistener.h @@ -51,6 +51,8 @@ public: shared_ptr GetSSLContext(void) const; String GetClusterDir(void) const; + Dictionary::Ptr GetClusterStatus(void); + private: shared_ptr m_SSLContext; @@ -117,8 +119,6 @@ private: void PersistMessage(const Endpoint::Ptr& source, const Dictionary::Ptr& message); static void MessageExceptionHandler(boost::exception_ptr exp); - - Dictionary::Ptr GetClusterStatus(void); }; } -- 2.40.0