summaryrefslogtreecommitdiff
path: root/libadhocutil/nagios.cpp
blob: 4350c35869ed2ffb4b754a875fec5b9029cf830c (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
36
#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();
	}
}