From 8346c031fbf2be4822cb4b18eb7065d266d2368d Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 14 Jun 2012 11:23:25 +0200 Subject: [PATCH] Added checker component. --- components/checker/Makefile.am | 26 +++++ components/checker/checker.vcxproj | 92 +++++++++++++++++ components/checker/checkercomponent.cpp | 131 ++++++++++++++++++++++++ components/checker/checkercomponent.h | 61 +++++++++++ components/checker/i2-checker.h | 36 +++++++ 5 files changed, 346 insertions(+) create mode 100644 components/checker/Makefile.am create mode 100644 components/checker/checker.vcxproj create mode 100644 components/checker/checkercomponent.cpp create mode 100644 components/checker/checkercomponent.h create mode 100644 components/checker/i2-checker.h diff --git a/components/checker/Makefile.am b/components/checker/Makefile.am new file mode 100644 index 000000000..544845031 --- /dev/null +++ b/components/checker/Makefile.am @@ -0,0 +1,26 @@ +## Process this file with automake to produce Makefile.in + +pkglib_LTLIBRARIES = \ + checker.la + +checker_la_SOURCES = \ + checkercomponent.cpp \ + checkercomponent.h \ + i2-checker.h + +checker_la_CPPFLAGS = \ + $(BOOST_CPPFLAGS) \ + -I${top_srcdir}/base \ + -I${top_srcdir}/jsonrpc \ + -I${top_srcdir}/icinga + +checker_la_LDFLAGS = \ + $(BOOST_LDFLAGS) \ + -module \ + -no-undefined \ + @RELEASE_INFO@ \ + @VERSION_INFO@ + +checker_la_LIBADD = \ + ${top_builddir}/base/libbase.la \ + ${top_builddir}/icinga/libicinga.la diff --git a/components/checker/checker.vcxproj b/components/checker/checker.vcxproj new file mode 100644 index 000000000..6e894d9de --- /dev/null +++ b/components/checker/checker.vcxproj @@ -0,0 +1,92 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {38CE81CC-2660-4EF0-A936-4A337591DA3E} + Win32Proj + checker + + + + DynamicLibrary + true + MultiByte + + + DynamicLibrary + false + true + MultiByte + + + + + + + + + + + + + true + $(OutDir);$(LibraryPath) + $(SolutionDir)\base;$(SolutionDir)\jsonrpc;$(SolutionDir)\icinga;$(IncludePath) + + + false + $(OutDir);$(LibraryPath) + $(SolutionDir)\base;$(SolutionDir)\jsonrpc;$(SolutionDir)\icinga;$(IncludePath) + + + + + + Level3 + Disabled + WIN32;_DEBUG;_WINDOWS;_USRDLL;CHECKER_EXPORTS;%(PreprocessorDefinitions) + + + Windows + true + base.lib;dyn.lib;jsonrpc.lib;icinga.lib;%(AdditionalDependencies) + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_WINDOWS;_USRDLL;CHECKER_EXPORTS;%(PreprocessorDefinitions) + + + Windows + true + true + true + base.lib;dyn.lib;jsonrpc.lib;icinga.lib;%(AdditionalDependencies) + + + + + + + + + + + + + \ No newline at end of file diff --git a/components/checker/checkercomponent.cpp b/components/checker/checkercomponent.cpp new file mode 100644 index 000000000..56d194aa3 --- /dev/null +++ b/components/checker/checkercomponent.cpp @@ -0,0 +1,131 @@ +/****************************************************************************** + * 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 "i2-checker.h" + +using namespace icinga; + +string CheckerComponent::GetName(void) const +{ + return "configcomponent"; +} + +void CheckerComponent::Start(void) +{ + m_CheckerEndpoint = make_shared(); + m_CheckerEndpoint->RegisterTopicHandler("checker::AssignService", + bind_weak(&CheckerComponent::AssignServiceRequestHandler, shared_from_this())); + m_CheckerEndpoint->RegisterTopicHandler("checker::RevokeService", + bind_weak(&CheckerComponent::AssignServiceRequestHandler, shared_from_this())); + m_CheckerEndpoint->RegisterPublication("checker::CheckResult"); + GetEndpointManager()->RegisterEndpoint(m_CheckerEndpoint); + + RequestMessage rm; + rm.SetMethod("checker::AssignService"); + GetEndpointManager()->SendAPIMessage(m_CheckerEndpoint, rm, bind(&CheckerComponent::TestResponseHandler, this, _1)); + + // TODO: get rid of this + ConfigObject::GetAllObjects()->OnObjectAdded += bind_weak(&CheckerComponent::NewServiceHandler, shared_from_this()); + + m_CheckTimer = make_shared(); + m_CheckTimer->SetInterval(10); + m_CheckTimer->OnTimerExpired += bind_weak(&CheckerComponent::CheckTimerHandler, shared_from_this()); + m_CheckTimer->Start(); + + CheckTask::RegisterType("nagios", NagiosCheckTask::CreateTask); + + ConfigObject::TMap::Range range = ConfigObject::GetObjects("service"); + + for (ConfigObject::TMap::Iterator it = range.first; it != range.second; it++) { + Service svc(it->second); + CheckTask::Ptr ct = CheckTask::CreateTask(svc); + CheckResult cr = ct->Execute(); + } +} + +int CheckerComponent::TestResponseHandler(const NewResponseEventArgs& ea) +{ + return 0; +} + +void CheckerComponent::Stop(void) +{ + +} + +int CheckerComponent::NewServiceHandler(const ObjectSetEventArgs& ea) +{ + if (ea.Target->GetType() == "service") + m_Services.push(ea.Target); + + return 0; +} + +int CheckerComponent::CheckTimerHandler(const TimerEventArgs& ea) +{ + time_t now; + time(&now); + + if (m_Services.size() == 0) + return 0; + + for (;;) { + + Service service = m_Services.top(); + + if (service.GetNextCheck() > now) + break; + + CheckTask::Ptr ct = CheckTask::CreateTask(service); + CheckResult cr = ct->Execute(); + + m_Services.pop(); + service.SetNextCheck(now + service.GetCheckInterval()); + m_Services.push(service); + } + + /* adjust next call time for the check timer */ + Service service = m_Services.top(); + static_pointer_cast(ea.Source)->SetInterval(service.GetNextCheck() - now); + + return 0; +} + +int CheckerComponent::AssignServiceRequestHandler(const NewRequestEventArgs& nrea) +{ + string id; + if (!nrea.Request.GetID(&id)) + return 0; + + ResponseMessage rm; + rm.SetID(id); + + MessagePart result; + rm.SetResult(result); + GetEndpointManager()->SendUnicastMessage(m_CheckerEndpoint, nrea.Sender, rm); + + return 0; +} + +int CheckerComponent::RevokeServiceRequestHandler(const NewRequestEventArgs& nrea) +{ + return 0; +} + +EXPORT_COMPONENT(checker, CheckerComponent); diff --git a/components/checker/checkercomponent.h b/components/checker/checkercomponent.h new file mode 100644 index 000000000..b655496d6 --- /dev/null +++ b/components/checker/checkercomponent.h @@ -0,0 +1,61 @@ +/****************************************************************************** + * 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 CHECKERCOMPONENT_H +#define CHECKERCOMPONENT_H + +namespace icinga +{ + +struct ServiceNextCheckLessComparer +{ +public: + bool operator()(const Service& a, const Service& b) + { + return a.GetNextCheck() < b.GetNextCheck(); + } +}; + +/** + * @ingroup checker + */ +class CheckerComponent : public IcingaComponent +{ +public: + virtual string GetName(void) const; + virtual void Start(void); + virtual void Stop(void); + +private: + priority_queue, ServiceNextCheckLessComparer> m_Services; + Timer::Ptr m_CheckTimer; + VirtualEndpoint::Ptr m_CheckerEndpoint; + + int NewServiceHandler(const ObjectSetEventArgs& ea); + + int CheckTimerHandler(const TimerEventArgs& ea); + int TestResponseHandler(const NewResponseEventArgs& ea); + + int AssignServiceRequestHandler(const NewRequestEventArgs& nrea); + int RevokeServiceRequestHandler(const NewRequestEventArgs& nrea); +}; + +} + +#endif /* CHECKERCOMPONENT_H */ diff --git a/components/checker/i2-checker.h b/components/checker/i2-checker.h new file mode 100644 index 000000000..fc804edde --- /dev/null +++ b/components/checker/i2-checker.h @@ -0,0 +1,36 @@ +/****************************************************************************** + * 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 I2CHECKER_H +#define I2CHECKER_H + +/** + * @defgroup checker Checker component + * + * The Checker component executes service checks. + */ + +#include +#include + +#include + +#include "checkercomponent.h" + +#endif /* I2CHECKER_H */ -- 2.40.0