summaryrefslogtreecommitdiff
path: root/libadhocutil/nagios.cpp
blob: a995a1364d59857fdc54667d63b441918ded44ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "nagios.h"
#include "compileTimeFormatter.h"
#include "ctf-impl/printf-compat.h" // IWYU pragma: keep
#include <ctime>
#include <fstream> // IWYU pragma: keep
#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");

	/// LCOV_EXCL_START (calls real Nagios)
	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);
	}
	/// LCOV_EXCL_STOP

	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, static_cast<uint8_t>(code), output);
			command_file.flush();
		}
		return command_file.good();
	}
}