]> granicus.if.org Git - icinga2/commitdiff
compatlog: add flapping messages
authorMichael Friedrich <michael.friedrich@netways.de>
Mon, 1 Jul 2013 12:29:07 +0000 (14:29 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Mon, 1 Jul 2013 12:44:56 +0000 (14:44 +0200)
refs #4360

12 files changed:
components/compat/compatlog.cpp
components/compat/compatlog.h
lib/icinga/Makefile.am
lib/icinga/externalcommandprocessor.cpp
lib/icinga/flappingmessage.cpp [new file with mode: 0644]
lib/icinga/flappingmessage.h [new file with mode: 0644]
lib/icinga/icinga.vcxproj
lib/icinga/icinga.vcxproj.filters
lib/icinga/service-check.cpp
lib/icinga/service-flapping.cpp
lib/icinga/service.cpp
lib/icinga/service.h

index 52fcfa06a629d2660c722a0a5699eda8bba69d04..d251975ad2f6e913781d1b3c109b9cab753ae059 100644 (file)
@@ -72,6 +72,7 @@ void CompatLog::Start(void)
            boost::bind(&CompatLog::NotificationSentRequestHandler, this, _3));
 
        Service::OnDowntimeChanged.connect(bind(&CompatLog::DowntimeHandler, this, _1, _2));
+       Service::OnFlappingChanged.connect(bind(&CompatLog::FlappingHandler, this, _1, _2));
 
        m_RotationTimer = boost::make_shared<Timer>();
        m_RotationTimer->OnTimerExpired.connect(boost::bind(&CompatLog::RotationTimerHandler, this));
@@ -357,6 +358,70 @@ void CompatLog::NotificationSentRequestHandler(const RequestMessage& request)
         }
 }
 
+/**
+ * @threadsafety Always.
+ */
+void CompatLog::FlappingHandler(const Service::Ptr& service, FlappingState flapping_state)
+{
+       Host::Ptr host = service->GetHost();
+
+       if (!host)
+               return;
+
+       String flapping_state_str;
+       String flapping_output;
+
+       switch (flapping_state) {
+               case FlappingStarted:
+                       flapping_output = "Service appears to have started flapping (00.0% change >= 00.0% threshold)";
+                       flapping_state_str = "STARTED";
+                       break;
+               case FlappingStopped:
+                       flapping_output = "Service appears to have stopped flapping (00.0% change < 00.1% threshold)";
+                       flapping_state_str = "STOPPED";
+                       break;
+               case FlappingDisabled:
+                       flapping_output = "Flap detection has been disabled";
+                       flapping_state_str = "DISABLED";
+                       break;
+               default:
+                       Log(LogCritical, "compat", "Unknown flapping state: " + Convert::ToString(flapping_state));
+                       return;
+       }
+
+        std::ostringstream msgbuf;
+        msgbuf << "SERVICE FLAPPING ALERT: "
+                << host->GetName() << ";"
+                << service->GetShortName() << ";"
+                << flapping_state_str << "; "
+                << flapping_output
+                << "";
+
+        {
+                ObjectLock oLock(this);
+                WriteLine(msgbuf.str());
+        }
+
+        if (service == host->GetHostCheckService()) {
+                std::ostringstream msgbuf;
+                msgbuf << "HOST FLAPPING ALERT: "
+                        << host->GetName() << ";"
+                        << flapping_state_str << "; "
+                        << flapping_output
+                        << "";
+
+                {
+                        ObjectLock oLock(this);
+                        WriteLine(msgbuf.str());
+                }
+        }
+
+        {
+                ObjectLock oLock(this);
+                Flush();
+        }
+
+}
 
 void CompatLog::WriteLine(const String& line)
 {
index 259842230796783676f9f9db0d079bd47cb0d6c7..cef9a2037fe0bd2b35c83469df9d357574221d4a 100644 (file)
@@ -66,6 +66,7 @@ private:
        void CheckResultRequestHandler(const RequestMessage& request);
        void NotificationSentRequestHandler(const RequestMessage& request);
        void DowntimeHandler(const Service::Ptr& service, DowntimeState downtime_state);
+       void FlappingHandler(const Service::Ptr& service, FlappingState flapping_state);
 
        Timer::Ptr m_RotationTimer;
        void RotationTimerHandler(void);
index 43a526a839e2eee13314fb7173e8e48f78333acf..9e2eeeb78713c8df8ad146cf4eb387cc73739987 100644 (file)
@@ -27,6 +27,8 @@ libicinga_la_SOURCES =  \
        eventcommand.h \
        externalcommandprocessor.cpp \
        externalcommandprocessor.h \
+       flappingmessage.cpp \
+       flappingmessage.h \
        host.cpp \
        hostgroup.cpp \
        hostgroup.h \
index ccc83279ab100638f831bf63afd955cb64a44175..aba9168c2932435888102c91ffb7ffa26581686e 100644 (file)
 #include "icinga/hostgroup.h"
 #include "icinga/servicegroup.h"
 #include "icinga/pluginchecktask.h"
+#include "icinga/flappingmessage.h"
 #include "base/convert.h"
 #include "base/logger_fwd.h"
 #include "base/objectlock.h"
 #include "base/application.h"
 #include "base/utility.h"
+#include "remoting/endpointmanager.h"
 #include <fstream>
 #include <boost/algorithm/string/classification.hpp>
 #include <boost/foreach.hpp>
@@ -1499,5 +1501,16 @@ void ExternalCommandProcessor::DisableSvcFlapping(double, const std::vector<Stri
                ObjectLock olock(service);
 
                service->SetEnableFlapping(false);
+
+               RequestMessage rm;
+               rm.SetMethod("icinga::Flapping");
+
+               FlappingMessage params;
+               params.SetService(service->GetName());
+               params.SetState(FlappingDisabled);
+
+               rm.SetParams(params);
+
+               EndpointManager::GetInstance()->SendMulticastMessage(rm);
        }
 }
diff --git a/lib/icinga/flappingmessage.cpp b/lib/icinga/flappingmessage.cpp
new file mode 100644 (file)
index 0000000..2b4bc43
--- /dev/null
@@ -0,0 +1,46 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012 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                *
+ * as published by the Free Software Foundation; either version 2             *
+ * of the License, or (at your option) any later version.                     *
+ *                                                                            *
+ * This program is distributed in the hope that it will be useful,            *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
+ * GNU General Public License for more details.                               *
+ *                                                                            *
+ * You should have received a copy of the GNU General Public License          *
+ * along with this program; if not, write to the Free Software Foundation     *
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
+ ******************************************************************************/
+
+#include "icinga/flappingmessage.h"
+
+using namespace icinga;
+
+String FlappingMessage::GetService(void) const
+{
+       String service;
+       Get("service", &service);
+       return service;
+}
+
+void FlappingMessage::SetService(const String& service)
+{
+       Set("service", service);
+}
+
+FlappingState FlappingMessage::GetState(void) const
+{
+       long state;
+       Get("state", &state);
+       return static_cast<FlappingState>(state);
+}
+
+void FlappingMessage::SetState(FlappingState state)
+{
+       Set("state", state);
+}
diff --git a/lib/icinga/flappingmessage.h b/lib/icinga/flappingmessage.h
new file mode 100644 (file)
index 0000000..59a586b
--- /dev/null
@@ -0,0 +1,50 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012 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                *
+ * as published by the Free Software Foundation; either version 2             *
+ * of the License, or (at your option) any later version.                     *
+ *                                                                            *
+ * This program is distributed in the hope that it will be useful,            *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
+ * GNU General Public License for more details.                               *
+ *                                                                            *
+ * You should have received a copy of the GNU General Public License          *
+ * along with this program; if not, write to the Free Software Foundation     *
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
+ ******************************************************************************/
+
+#ifndef FLAPPINGMESSAGE_H
+#define FLAPPINGMESSAGE_H
+
+#include "icinga/i2-icinga.h"
+#include "icinga/service.h"
+#include "remoting/messagepart.h"
+
+namespace icinga
+{
+
+/**
+ * A downtime message for a service.
+ *
+ * @ingroup icinga
+ */
+class I2_ICINGA_API FlappingMessage : public MessagePart
+{
+public:
+       FlappingMessage(void) : MessagePart() { }
+       explicit FlappingMessage(const MessagePart& message) : MessagePart(message) { }
+
+       String GetService(void) const;
+       void SetService(const String& service);
+
+       FlappingState GetState(void) const;
+       void SetState(FlappingState state);
+};
+
+}
+
+#endif /* FLAPPINGMESSAGE_H */
index bf5e5404d58c9192dea93e0f49fea06d17ba9995..7e078b8d061e20a3b9db00ea96a5295b1b62fe32 100644 (file)
@@ -24,6 +24,7 @@
     <ClCompile Include="cib.cpp" />
     <ClCompile Include="downtimemessage.cpp" />
     <ClCompile Include="externalcommandprocessor.cpp" />
+    <ClCompile Include="flappingmessage.cpp" />
     <ClCompile Include="host.cpp" />
     <ClCompile Include="hostgroup.cpp" />
     <ClCompile Include="icinga-type.cpp">
@@ -58,6 +59,7 @@
     <ClInclude Include="cib.h" />
     <ClInclude Include="downtimemessage.h" />
     <ClInclude Include="externalcommandprocessor.h" />
+    <ClInclude Include="flappingmessage.h" />
     <ClInclude Include="host.h" />
     <ClInclude Include="hostgroup.h" />
     <ClInclude Include="i2-icinga.h" />
index 01db5aacc6a26f4165a19f471fc41cd5d45c3755..f3bd9db108fbb1d5adb7c31742b55b6ac2d2d039 100644 (file)
@@ -37,6 +37,9 @@
     <ClCompile Include="downtimemessage.cpp">
       <Filter>Quelldateien</Filter>
     </ClCompile>
+    <ClCompile Include="flappingmessage.cpp">
+      <Filter>Quelldateien</Filter>
+    </ClCompile>
     <ClCompile Include="service-comment.cpp">
       <Filter>Quelldateien</Filter>
     </ClCompile>
     <ClInclude Include="downtimemessage.h">
       <Filter>Headerdateien</Filter>
     </ClInclude>
+    <ClInclude Include="flappingmessage.h">
+      <Filter>Headerdateien</Filter>
+    </ClInclude>
     <ClInclude Include="pluginnotificationtask.h">
       <Filter>Headerdateien</Filter>
     </ClInclude>
index ff787b827cacd94bef92f17511214a309ca6cd34..961c21d919f0aefec5b8b4923ea560482838b585 100644 (file)
@@ -21,6 +21,7 @@
 #include "icinga/checkcommand.h"
 #include "icinga/icingaapplication.h"
 #include "icinga/checkresultmessage.h"
+#include "icinga/flappingmessage.h"
 #include "icinga/cib.h"
 #include "remoting/endpointmanager.h"
 #include "base/dynamictype.h"
@@ -38,6 +39,7 @@ const double Service::CheckIntervalDivisor = 5.0;
 
 boost::signals2::signal<void (const Service::Ptr&)> Service::OnCheckerChanged;
 boost::signals2::signal<void (const Service::Ptr&)> Service::OnNextCheckChanged;
+boost::signals2::signal<void (const Service::Ptr&, FlappingState)> Service::OnFlappingChanged;
 
 CheckCommand::Ptr Service::GetCheckCommand(void) const
 {
@@ -480,10 +482,34 @@ void Service::ProcessCheckResult(const Dictionary::Ptr& cr)
        if (send_downtime_notification)
                RequestNotifications(in_downtime ? NotificationDowntimeStart : NotificationDowntimeEnd, cr);
 
-       if (!was_flapping && is_flapping)
+       if (!was_flapping && is_flapping) {
                RequestNotifications(NotificationFlappingStart, cr);
-       else if (was_flapping && !is_flapping)
+
+               RequestMessage rm;
+               rm.SetMethod("icinga::Flapping");
+
+               FlappingMessage params;
+               params.SetService(GetName());
+               params.SetState(FlappingStarted);
+
+               rm.SetParams(params);
+
+               EndpointManager::GetInstance()->SendMulticastMessage(rm);
+       }
+       else if (was_flapping && !is_flapping) {
                RequestNotifications(NotificationFlappingEnd, cr);
+
+               RequestMessage rm;
+               rm.SetMethod("icinga::Flapping");
+
+               FlappingMessage params;
+               params.SetService(GetName());
+               params.SetState(FlappingStopped);
+
+               rm.SetParams(params);
+
+               EndpointManager::GetInstance()->SendMulticastMessage(rm);
+       }
        else if (send_notification)
                RequestNotifications(recovery ? NotificationRecovery : NotificationProblem, cr);
 }
index 1c353d97f3ab00a4be0c91474361e17ebe8362f7..cdac8a3109436ff686f8c0dbfe68a8f864717af9 100644 (file)
@@ -18,6 +18,7 @@
  ******************************************************************************/
 
 #include "icinga/service.h"
+#include "icinga/flappingmessage.h"
 #include "base/dynamictype.h"
 #include "base/objectlock.h"
 #include "base/logger_fwd.h"
@@ -48,6 +49,18 @@ double Service::GetFlappingThreshold(void) const
                return m_FlappingThreshold;
 }
 
+void Service::FlappingRequestHandler(const RequestMessage& request)
+{
+       FlappingMessage params;
+       if (!request.GetParams(&params))
+               return;
+
+       String svcname = params.GetService();
+       Service::Ptr service = Service::GetByName(svcname);
+
+       OnFlappingChanged(service, params.GetState());
+}
+
 bool Service::GetEnableFlapping(void) const
 {
        if (m_EnableFlapping.IsEmpty())
index 2b8c9a38e6feb267aeb6216c1117e7ab3be835c1..5f04c44eff306c6fc2858c071f6c2ed467412c20 100644 (file)
@@ -111,6 +111,8 @@ void Service::Initialize(void)
        m_Endpoint = Endpoint::MakeEndpoint("service", false);
        m_Endpoint->RegisterTopicHandler("icinga::Downtime",
            boost::bind(&Service::DowntimeRequestHandler, _3));
+       m_Endpoint->RegisterTopicHandler("icinga::Flapping",
+           boost::bind(&Service::FlappingRequestHandler, _3));
 }
 
 void Service::OnRegistrationCompleted(void)
index 824036f6bdd558f428c6cc258c632313a178c1c4..8a43811865983d769a21b5f34c5c79ae461467ed 100644 (file)
@@ -73,6 +73,18 @@ enum DowntimeState
        DowntimeStopped = 2
 };
 
+/**
+ * The sate of service flapping.
+ *
+ * @ingroup icinga
+ */
+enum FlappingState
+{
+       FlappingStarted = 0,
+       FlappingDisabled = 1,
+       FlappingStopped = 2
+};
+
 class CheckCommand;
 class EventCommand;
 
@@ -196,6 +208,7 @@ public:
        static boost::signals2::signal<void (const Service::Ptr&)> OnCheckerChanged;
        static boost::signals2::signal<void (const Service::Ptr&)> OnNextCheckChanged;
        static boost::signals2::signal<void (const Service::Ptr&, DowntimeState)> OnDowntimeChanged;
+       static boost::signals2::signal<void (const Service::Ptr&, FlappingState)> OnFlappingChanged;
 
        virtual bool ResolveMacro(const String& macro, const Dictionary::Ptr& cr, String *result) const;
 
@@ -359,6 +372,8 @@ private:
        Attribute<long> m_FlappingNegative;
        Attribute<double> m_FlappingLastChange;
        Attribute<double> m_FlappingThreshold;
+
+       static void FlappingRequestHandler(const RequestMessage& request);
 };
 
 }