]> granicus.if.org Git - icinga2/commitdiff
add downtime message (started,stopped,cancelled) and compat logger
authorMichael Friedrich <michael.friedrich@netways.de>
Fri, 28 Jun 2013 11:40:01 +0000 (13:40 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Fri, 28 Jun 2013 11:40:01 +0000 (13:40 +0200)
refs #3985
refs #2750

components/compat/compatlog.cpp
components/compat/compatlog.h
lib/icinga/Makefile.am
lib/icinga/downtimemessage.cpp [new file with mode: 0644]
lib/icinga/downtimemessage.h [new file with mode: 0644]
lib/icinga/icinga.vcxproj
lib/icinga/icinga.vcxproj.filters
lib/icinga/service-downtime.cpp
lib/icinga/service.h

index 7d74fefe908929b97d4cb27cc5ac6a868fedae39..7bb1c74c513a918c9eb78b75e87b2016b8f92d75 100644 (file)
@@ -19,6 +19,7 @@
 
 #include "compat/compatlog.h"
 #include "icinga/checkresultmessage.h"
+#include "icinga/downtimemessage.h"
 #include "icinga/service.h"
 #include "icinga/macroprocessor.h"
 #include "config/configcompilercontext.h"
@@ -64,6 +65,8 @@ void CompatLog::Start(void)
        m_Endpoint = Endpoint::MakeEndpoint("compatlog_" + GetName(), false);
        m_Endpoint->RegisterTopicHandler("checker::CheckResult",
            boost::bind(&CompatLog::CheckResultRequestHandler, this, _3));
+       m_Endpoint->RegisterTopicHandler("icinga::Downtime",
+           boost::bind(&CompatLog::DowntimeRequestHandler, this, _3));
 
        m_RotationTimer = boost::make_shared<Timer>();
        m_RotationTimer->OnTimerExpired.connect(boost::bind(&CompatLog::RotationTimerHandler, this));
@@ -197,6 +200,78 @@ void CompatLog::CheckResultRequestHandler(const RequestMessage& request)
        }
 }
 
+/**
+ * @threadsafety Always.
+ */
+void CompatLog::DowntimeRequestHandler(const RequestMessage& request)
+{
+       DowntimeMessage params;
+       if (!request.GetParams(&params))
+               return;
+
+       String svcname = params.GetService();
+       Service::Ptr service = Service::GetByName(svcname);
+
+       Host::Ptr host = service->GetHost();
+
+       if (!host)
+               return;
+
+       DowntimeState downtime_state = params.GetState();
+       String downtime_state_str;
+       String downtime_output;
+
+       switch (downtime_state) {
+               case DowntimeStarted:
+                       downtime_output = "Service has entered a period of scheduled downtime.";
+                       downtime_state_str = "STARTED";
+                       break;
+               case DowntimeStopped:
+                       downtime_output = "Service has exited from a period of scheduled downtime.";
+                       downtime_state_str = "STOPPED";
+                       break;
+               case DowntimeCancelled:
+                       downtime_output = "Scheduled downtime for service has been cancelled.";
+                       downtime_state_str = "CANCELLED";
+                       break;
+               default:
+                       Log(LogCritical, "compat", "Unknown downtime state: " + Convert::ToString(downtime_state));
+                       return;
+       }
+
+       std::ostringstream msgbuf;
+       msgbuf << "SERVICE DOWNTIME ALERT: "
+               << host->GetName() << ";"
+               << service->GetShortName() << ";"
+               << downtime_state_str << "; "
+               << downtime_output
+               << "";
+
+       {
+               ObjectLock oLock(this);
+               WriteLine(msgbuf.str());
+       }
+
+       if (service == host->GetHostCheckService()) {
+               std::ostringstream msgbuf;
+               msgbuf << "HOST DOWNTIME ALERT: "
+                       << host->GetName() << ";"
+                       << downtime_state_str << "; "
+                       << downtime_output
+                       << "";
+
+               {
+                       ObjectLock oLock(this);
+                       WriteLine(msgbuf.str());
+               }
+       }
+
+       {
+               ObjectLock oLock(this);
+               Flush();
+       }
+}
+
 void CompatLog::WriteLine(const String& line)
 {
        ASSERT(OwnsLock());
index d07a17850f08a6b5aa2b3d2c4f77f7a0614a27e2..519e7af41b64d161af155fa2b12bcb5854c6e532 100644 (file)
@@ -63,6 +63,7 @@ private:
 
        Endpoint::Ptr m_Endpoint;
        void CheckResultRequestHandler(const RequestMessage& request);
+       void DowntimeRequestHandler(const RequestMessage& request);
 
        Timer::Ptr m_RotationTimer;
        void RotationTimerHandler(void);
index 93d3b782b04567065369c61470189fb46cc199c5..4234db6306be14c4e346a3b40b990ee19fe3fbeb 100644 (file)
@@ -21,6 +21,8 @@ libicinga_la_SOURCES =  \
        cib.h \
        command.cpp \
        command.h \
+       downtimemessage.cpp \
+       downtimemessage.h \
        eventcommand.cpp \
        eventcommand.h \
        externalcommandprocessor.cpp \
diff --git a/lib/icinga/downtimemessage.cpp b/lib/icinga/downtimemessage.cpp
new file mode 100644 (file)
index 0000000..1a5b496
--- /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/downtimemessage.h"
+
+using namespace icinga;
+
+String DowntimeMessage::GetService(void) const
+{
+       String service;
+       Get("service", &service);
+       return service;
+}
+
+void DowntimeMessage::SetService(const String& service)
+{
+       Set("service", service);
+}
+
+DowntimeState DowntimeMessage::GetState(void) const
+{
+       long state;
+       Get("state", &state);
+       return static_cast<DowntimeState>(state);
+}
+
+void DowntimeMessage::SetState(DowntimeState state)
+{
+       Set("state", state);
+}
diff --git a/lib/icinga/downtimemessage.h b/lib/icinga/downtimemessage.h
new file mode 100644 (file)
index 0000000..85d0212
--- /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 DOWNTIMEMESSAGE_H
+#define DOWNTIMEMESSAGE_H
+
+#include "icinga/i2-icinga.h"
+#include "icinga/service.h"
+#include "remoting/messagepart.h"
+
+namespace icinga
+{
+
+/**
+ * A state change message for a service.
+ *
+ * @ingroup icinga
+ */
+class I2_ICINGA_API DowntimeMessage : public MessagePart
+{
+public:
+       DowntimeMessage(void) : MessagePart() { }
+       explicit DowntimeMessage(const MessagePart& message) : MessagePart(message) { }
+
+       String GetService(void) const;
+       void SetService(const String& service);
+
+       DowntimeState GetState(void) const;
+       void SetState(DowntimeState state);
+};
+
+}
+
+#endif /* DOWNTIMEMESSAGE_H */
index 160863db0901ae8c62ce11669c11c24e17e79b99..310925b45059217bafc077af2e1760746f50c89b 100644 (file)
@@ -22,6 +22,7 @@
     <ClCompile Include="api.cpp" />
     <ClCompile Include="checkresultmessage.cpp" />
     <ClCompile Include="cib.cpp" />
+    <ClCompile Include="downtimemessage.cpp" />
     <ClCompile Include="externalcommandprocessor.cpp" />
     <ClCompile Include="host.cpp" />
     <ClCompile Include="hostgroup.cpp" />
@@ -54,6 +55,7 @@
     <ClInclude Include="api.h" />
     <ClInclude Include="checkresultmessage.h" />
     <ClInclude Include="cib.h" />
+    <ClInclude Include="downtimemessage.h" />
     <ClInclude Include="externalcommandprocessor.h" />
     <ClInclude Include="host.h" />
     <ClInclude Include="hostgroup.h" />
index 94a44f1e8735697002aa1db43e51c238bf8dcedf..f170ee25f497876f1a82dcb7d2cd49cebe46e5de 100644 (file)
@@ -34,6 +34,9 @@
     <ClCompile Include="checkresultmessage.cpp">
       <Filter>Quelldateien</Filter>
     </ClCompile>
+    <ClCompile Include="downtimemessage.cpp">
+      <Filter>Quelldateien</Filter>
+    </ClCompile>
     <ClCompile Include="service-comment.cpp">
       <Filter>Quelldateien</Filter>
     </ClCompile>
     <ClInclude Include="checkresultmessage.h">
       <Filter>Headerdateien</Filter>
     </ClInclude>
+    <ClInclude Include="downtimemessage.h">
+      <Filter>Headerdateien</Filter>
+    </ClInclude>
     <ClInclude Include="pluginnotificationtask.h">
       <Filter>Headerdateien</Filter>
     </ClInclude>
       <Filter>Quelldateien</Filter>
     </CustomBuild>
   </ItemGroup>
-</Project>
\ No newline at end of file
+</Project>
index 43345fca3cebafaeabe71544e47baddde5a278ca..a3c18c0cb9a423f64330a85aa87acd7e256f9dd4 100644 (file)
@@ -18,6 +18,8 @@
  ******************************************************************************/
 
 #include "icinga/service.h"
+#include "icinga/downtimemessage.h"
+#include "remoting/endpointmanager.h"
 #include "base/dynamictype.h"
 #include "base/objectlock.h"
 #include "base/logger_fwd.h"
@@ -127,7 +129,20 @@ void Service::RemoveDowntime(const String& id)
 
        {
                ObjectLock olock(owner);
+
                downtimes->Remove(id);
+
+               RequestMessage rm;
+               rm.SetMethod("icinga::Downtime");
+
+               DowntimeMessage params;
+               params.SetService(owner->GetName());
+               params.SetState(DowntimeCancelled);
+
+               rm.SetParams(params);
+
+               EndpointManager::GetInstance()->SendMulticastMessage(rm);
+
                owner->Touch("downtimes");
        }
 }
@@ -180,6 +195,17 @@ void Service::TriggerDowntime(const String& id)
                TriggerDowntime(tid);
        }
 
+       RequestMessage rm;
+       rm.SetMethod("icinga::Downtime");
+
+       DowntimeMessage params;
+       params.SetService(owner->GetName());
+       params.SetState(DowntimeStarted);
+
+       rm.SetParams(params);
+
+       EndpointManager::GetInstance()->SendMulticastMessage(rm);
+
        owner->Touch("downtimes");
 }
 
@@ -339,6 +365,17 @@ void Service::RemoveExpiredDowntimes(void)
 
        if (!expiredDowntimes.empty()) {
                BOOST_FOREACH(const String& id, expiredDowntimes) {
+                       RequestMessage rm;
+                       rm.SetMethod("icinga::Downtime");
+
+                       DowntimeMessage params;
+                       params.SetService(GetName());
+                       params.SetState(DowntimeStopped);
+
+                       rm.SetParams(params);
+
+                       EndpointManager::GetInstance()->SendMulticastMessage(rm);
+
                        downtimes->Remove(id);
                }
 
index 349002396d58ebe5a6e8ae42fd6bcaa741da743e..cb0b96424db794298f49c732e5a50ef31e26ef32 100644 (file)
@@ -57,6 +57,18 @@ enum CommentType
        CommentAcknowledgement = 4
 };
 
+/**
+ * The state of a service downtime.
+ *
+ * @ingroup icinga
+ */
+enum DowntimeState
+{
+       DowntimeStarted = 0,
+       DowntimeCancelled = 1,
+       DowntimeStopped = 2
+};
+
 class CheckCommand;
 class EventCommand;