From 1cd8a25ab2cb1bf5a093a7df262a019dc9254084 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Mon, 8 Aug 2016 12:51:20 +0200 Subject: [PATCH] Add the "exception" check command refs #10075 --- doc/7-icinga-template-library.md | 6 ++++ itl/command-icinga.conf | 4 +++ lib/methods/CMakeLists.txt | 2 +- lib/methods/exceptionchecktask.cpp | 41 ++++++++++++++++++++++++++ lib/methods/exceptionchecktask.hpp | 46 ++++++++++++++++++++++++++++++ lib/methods/methods-itl.conf | 4 +++ 6 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 lib/methods/exceptionchecktask.cpp create mode 100644 lib/methods/exceptionchecktask.hpp diff --git a/doc/7-icinga-template-library.md b/doc/7-icinga-template-library.md index cc0e519ef..c491b6e73 100644 --- a/doc/7-icinga-template-library.md +++ b/doc/7-icinga-template-library.md @@ -79,6 +79,12 @@ and adds the check source to the check output. For test and demo purposes only. The `random` check command does not support any vars. +### exception + +Check command for the built-in `exception` check. This check throws an exception. + +For test and demo purposes only. The `exception` check command does not support +any vars. # Plugin Check Commands diff --git a/itl/command-icinga.conf b/itl/command-icinga.conf index 5b9e3b86f..9070f9f36 100644 --- a/itl/command-icinga.conf +++ b/itl/command-icinga.conf @@ -34,3 +34,7 @@ object CheckCommand "cluster-zone" { object CheckCommand "random" { import "random-check-command" } + +object CheckCommand "exception" { + import "exception-check-command" +} diff --git a/lib/methods/CMakeLists.txt b/lib/methods/CMakeLists.txt index 64aa5343a..6666ab474 100644 --- a/lib/methods/CMakeLists.txt +++ b/lib/methods/CMakeLists.txt @@ -24,7 +24,7 @@ else() endif() set(methods_SOURCES - clusterchecktask.cpp clusterzonechecktask.cpp + clusterchecktask.cpp clusterzonechecktask.cpp exceptionchecktask.cpp icingachecktask.cpp methods-itl.cpp nullchecktask.cpp nulleventtask.cpp pluginchecktask.cpp plugineventtask.cpp pluginnotificationtask.cpp randomchecktask.cpp timeperiodtask.cpp ${WindowsSources} diff --git a/lib/methods/exceptionchecktask.cpp b/lib/methods/exceptionchecktask.cpp new file mode 100644 index 000000000..f9a3516a9 --- /dev/null +++ b/lib/methods/exceptionchecktask.cpp @@ -0,0 +1,41 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012-2016 Icinga Development Team (https://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 _WIN32 +# include +#endif /* _WIN32 */ +#include "methods/exceptionchecktask.hpp" +#include "base/utility.hpp" +#include "base/convert.hpp" +#include "base/function.hpp" +#include "base/logger.hpp" +#include "base/exception.hpp" + +using namespace icinga; + +REGISTER_SCRIPTFUNCTION(ExceptionCheck, &ExceptionCheckTask::ScriptFunc); + +void ExceptionCheckTask::ScriptFunc(const Checkable::Ptr& service, const CheckResult::Ptr& cr, + const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros) +{ + if (resolvedMacros && !useResolvedMacros) + return; + + BOOST_THROW_EXCEPTION(ScriptError("Test") << boost::errinfo_api_function("Test")); +} diff --git a/lib/methods/exceptionchecktask.hpp b/lib/methods/exceptionchecktask.hpp new file mode 100644 index 000000000..a677e4c7f --- /dev/null +++ b/lib/methods/exceptionchecktask.hpp @@ -0,0 +1,46 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012-2016 Icinga Development Team (https://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 EXCEPTIONCHECKTASK_H +#define EXCEPTIONCHECKTASK_H + +#include "icinga/service.hpp" +#include "base/dictionary.hpp" + +namespace icinga +{ + +/** + * Test class for additional check types. Implements the "exception" check type. + * + * @ingroup methods + */ +class ExceptionCheckTask +{ +public: + static void ScriptFunc(const Checkable::Ptr& service, const CheckResult::Ptr& cr, + const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros); + +private: + ExceptionCheckTask(void); +}; + +} + +#endif /* EXCEPTIONCHECKTASK_H */ diff --git a/lib/methods/methods-itl.conf b/lib/methods/methods-itl.conf index 90039f6a5..3255be313 100644 --- a/lib/methods/methods-itl.conf +++ b/lib/methods/methods-itl.conf @@ -48,3 +48,7 @@ template EventCommand "plugin-event-command" { template CheckCommand "random-check-command" { execute = RandomCheck } + +template CheckCommand "exception-check-command" { + execute = ExceptionCheck +}