From 361101a3017af79c0b01c35e72a57f7c589d065c Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 1 Feb 2020 15:12:00 +0000 Subject: Add Nagios passive check helpers --- libadhocutil/nagios.cpp | 30 ++++++++++++++++++++++++++++++ libadhocutil/nagios.h | 23 +++++++++++++++++++++++ libadhocutil/unittests/Jamfile.jam | 8 ++++++++ libadhocutil/unittests/testNagios.cpp | 16 ++++++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 libadhocutil/nagios.cpp create mode 100644 libadhocutil/nagios.h create mode 100644 libadhocutil/unittests/testNagios.cpp diff --git a/libadhocutil/nagios.cpp b/libadhocutil/nagios.cpp new file mode 100644 index 0000000..8bd7f62 --- /dev/null +++ b/libadhocutil/nagios.cpp @@ -0,0 +1,30 @@ +#include "nagios.h" +#include "ctf-impl/printf-compat.h" +#include +#include + +namespace AdHoc { + // [] PROCESS_SERVICE_CHECK_RESULT;;;; + AdHocFormatter(NagiosPassiveServiceCheck, "[%?] PROCESS_SERVICE_CHECK_RESULT;%?;%?;%d;%?\n"); + + bool + submitNagiosPassiveServiceCheck(const std::string_view & svc, NagiosStatusCode code, const std::string_view & output) + { + std::ofstream command_file("/var/nagios/rw/nagios.cmd"); + return submitNagiosPassiveServiceCheck(command_file, svc, code, output); + } + + bool + submitNagiosPassiveServiceCheck(std::ostream & command_file, const std::string_view & svc, + NagiosStatusCode code, const std::string_view & output) + { + if (command_file.good()) { + struct utsname buf; + uname(&buf); + NagiosPassiveServiceCheck::write(command_file, time(nullptr), buf.nodename, svc, (uint8_t)code, output); + command_file.flush(); + } + return command_file.good(); + } +} + diff --git a/libadhocutil/nagios.h b/libadhocutil/nagios.h new file mode 100644 index 0000000..144c27a --- /dev/null +++ b/libadhocutil/nagios.h @@ -0,0 +1,23 @@ +#ifndef NAGIOS_H +#define NAGIOS_H + +#include "visibility.h" +#include +#include + +namespace AdHoc { + enum class NagiosStatusCode : uint8_t { + OK = 0, + Warning = 1, + Critical = 2, + Unknown = 3, + }; + + DLL_PUBLIC bool submitNagiosPassiveServiceCheck(const std::string_view &, + NagiosStatusCode, const std::string_view &); + DLL_PUBLIC bool submitNagiosPassiveServiceCheck(std::ostream &, const std::string_view &, + NagiosStatusCode, const std::string_view &); +} + +#endif + diff --git a/libadhocutil/unittests/Jamfile.jam b/libadhocutil/unittests/Jamfile.jam index 0b749a8..7dd62fb 100644 --- a/libadhocutil/unittests/Jamfile.jam +++ b/libadhocutil/unittests/Jamfile.jam @@ -295,3 +295,11 @@ run boost_utf ; +run + testNagios.cpp + : : : + BOOST_TEST_DYN_LINK + ..//adhocutil + boost_utf + ; + diff --git a/libadhocutil/unittests/testNagios.cpp b/libadhocutil/unittests/testNagios.cpp new file mode 100644 index 0000000..7ef64dc --- /dev/null +++ b/libadhocutil/unittests/testNagios.cpp @@ -0,0 +1,16 @@ +#define BOOST_TEST_MODULE Nagios + +#include +#include + +BOOST_AUTO_TEST_CASE(test_write_cmd, * boost::unit_test::disabled()) +{ + BOOST_REQUIRE(AdHoc::submitNagiosPassiveServiceCheck("some service", AdHoc::NagiosStatusCode::OK, "bar")); +} + +BOOST_AUTO_TEST_CASE(test_fmtrite_cmd) +{ + std::stringstream strm; + BOOST_REQUIRE(AdHoc::submitNagiosPassiveServiceCheck(strm, "some service", AdHoc::NagiosStatusCode::OK, "bar")); +} + -- cgit v1.2.3