summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2020-02-01 15:12:00 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2020-02-02 17:13:24 +0000
commit361101a3017af79c0b01c35e72a57f7c589d065c (patch)
treeabcf6a79775c522be802dae2ce1170b7bfa27a71
parentAdd a pkgconfig file (diff)
downloadlibadhocutil-361101a3017af79c0b01c35e72a57f7c589d065c.tar.bz2
libadhocutil-361101a3017af79c0b01c35e72a57f7c589d065c.tar.xz
libadhocutil-361101a3017af79c0b01c35e72a57f7c589d065c.zip
Add Nagios passive check helperslibadhocutil-0.7.7
-rw-r--r--libadhocutil/nagios.cpp30
-rw-r--r--libadhocutil/nagios.h23
-rw-r--r--libadhocutil/unittests/Jamfile.jam8
-rw-r--r--libadhocutil/unittests/testNagios.cpp16
4 files changed, 77 insertions, 0 deletions
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 <fstream>
+#include <sys/utsname.h>
+
+namespace AdHoc {
+ // [<timestamp>] PROCESS_SERVICE_CHECK_RESULT;<host_name>;<svc_description>;<return_code>;<plugin_output>
+ 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 <cstdint>
+#include <string_view>
+
+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
<library>boost_utf
;
+run
+ testNagios.cpp
+ : : :
+ <define>BOOST_TEST_DYN_LINK
+ <library>..//adhocutil
+ <library>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 <boost/test/unit_test.hpp>
+#include <nagios.h>
+
+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"));
+}
+