Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PluginNotificationTask::ScriptFunc(): on Linux truncate output and comment #9887

Merged
merged 1 commit into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion lib/methods/pluginnotificationtask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@
#include "base/process.hpp"
#include "base/convert.hpp"

#ifdef __linux__
# include <linux/binfmts.h>
# include <unistd.h>

# ifndef PAGE_SIZE
// MAX_ARG_STRLEN is a multiple of PAGE_SIZE which is missing
# define PAGE_SIZE getpagesize()
# endif /* PAGE_SIZE */

// Make e.g. the $host.output$ itself even 10% shorter to leave enough room
// for e.g. --host-output= as in --host-output=$host.output$, but without int overflow
const static auto l_MaxOutLen = MAX_ARG_STRLEN - MAX_ARG_STRLEN / 10u;
#endif /* __linux__ */

using namespace icinga;

REGISTER_FUNCTION_NONCONST(Internal, PluginNotification, &PluginNotificationTask::ScriptFunc, "notification:user:cr:itype:author:comment:resolvedMacros:useResolvedMacros");
Expand All @@ -33,7 +47,11 @@ void PluginNotificationTask::ScriptFunc(const Notification::Ptr& notification,
Dictionary::Ptr notificationExtra = new Dictionary({
{ "type", Notification::NotificationTypeToStringCompat(type) }, //TODO: Change that to our types.
{ "author", author },
#ifdef __linux__
{ "comment", comment.SubStr(0, l_MaxOutLen) }
#else /* __linux__ */
{ "comment", comment }
#endif /* __linux__ */
});

Host::Ptr host;
Expand All @@ -48,8 +66,35 @@ void PluginNotificationTask::ScriptFunc(const Notification::Ptr& notification,
resolvers.emplace_back("user", user);
resolvers.emplace_back("notification", notificationExtra);
resolvers.emplace_back("notification", notification);
if (service)

if (service) {
#ifdef __linux__
auto cr (service->GetLastCheckResult());

if (cr) {
auto output (cr->GetOutput());

if (output.GetLength() > l_MaxOutLen) {
resolvers.emplace_back("service", new Dictionary({{"output", output.SubStr(0, l_MaxOutLen)}}));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's Linux, the macro resolver key service is set twice? Is it safe to rely on the vector element insertion order and expect this element to be found before the actual service object?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

notification is already set twice, on all platforms.

}
}
#endif /* __linux__ */

resolvers.emplace_back("service", service);
}

#ifdef __linux__
auto hcr (host->GetLastCheckResult());

if (hcr) {
auto output (hcr->GetOutput());

if (output.GetLength() > l_MaxOutLen) {
resolvers.emplace_back("host", new Dictionary({{"output", output.SubStr(0, l_MaxOutLen)}}));
}
}
#endif /* __linux__ */

resolvers.emplace_back("host", host);
resolvers.emplace_back("command", commandObj);

Expand Down
3 changes: 3 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ set(base_test_SOURCES
icinga-macros.cpp
icinga-notification.cpp
icinga-perfdata.cpp
methods-pluginnotificationtask.cpp
remote-configpackageutility.cpp
remote-url.cpp
${base_OBJS}
$<TARGET_OBJECTS:config>
$<TARGET_OBJECTS:remote>
$<TARGET_OBJECTS:icinga>
$<TARGET_OBJECTS:methods>
)

if(ICINGA2_UNITY_BUILD)
Expand Down Expand Up @@ -161,6 +163,7 @@ add_boost_test(base
icinga_perfdata/multi
icinga_perfdata/scientificnotation
icinga_perfdata/parse_edgecases
methods_pluginnotificationtask/truncate_long_output
remote_configpackageutility/ValidateName
remote_url/id_and_path
remote_url/parameters
Expand Down
88 changes: 88 additions & 0 deletions test/methods-pluginnotificationtask.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* Icinga 2 | (c) 2023 Icinga GmbH | GPLv2+ */

#include "base/array.hpp"
#include "icinga/checkresult.hpp"
#include "icinga/host.hpp"
#include "icinga/notification.hpp"
#include "icinga/notificationcommand.hpp"
#include "icinga/service.hpp"
#include "icinga/user.hpp"
#include "methods/pluginnotificationtask.hpp"
#include <BoostTestTargetConfig.h>
#include <future>

using namespace icinga;

BOOST_AUTO_TEST_SUITE(methods_pluginnotificationtask)

BOOST_AUTO_TEST_CASE(truncate_long_output)
{
#ifdef __linux__
Host::Ptr h = new Host();
CheckResult::Ptr hcr = new CheckResult();
CheckResult::Ptr scr = new CheckResult();
Service::Ptr s = new Service();
User::Ptr u = new User();
NotificationCommand::Ptr nc = new NotificationCommand();
Notification::Ptr n = new Notification();
String placeHolder (1024 * 1024, 'x');
std::promise<String> promise;
auto future (promise.get_future());

hcr->SetOutput("H" + placeHolder + "h", true);
scr->SetOutput("S" + placeHolder + "s", true);

h->SetName("example.com", true);
h->SetLastCheckResult(hcr, true);
h->Register();

s->SetHostName("example.com", true);
s->SetShortName("disk", true);
s->SetLastCheckResult(scr, true);
s->OnAllConfigLoaded(); // link Host

nc->SetCommandLine(
new Array({
"echo",
"host_output=$host.output$",
"service_output=$service.output$",
"notification_comment=$notification.comment$",
"output=$output$",
"comment=$comment$"
}),
true
);

nc->SetName("mail", true);
nc->Register();

n->SetFieldByName("host_name", "example.com", false, DebugInfo());
n->SetFieldByName("service_name", "disk", false, DebugInfo());
n->SetFieldByName("command", "mail", false, DebugInfo());
n->OnAllConfigLoaded(); // link Service

Checkable::ExecuteCommandProcessFinishedHandler = [&promise](const Value&, const ProcessResult& pr) {
promise.set_value(pr.Output);
};

PluginNotificationTask::ScriptFunc(n, u, nullptr, NotificationCustom, "jdoe", "C" + placeHolder + "c", nullptr, false);
future.wait();

Checkable::ExecuteCommandProcessFinishedHandler = nullptr;
h->Unregister();
nc->Unregister();

auto output (future.get());

BOOST_CHECK(output.Contains("host_output=Hx"));
BOOST_CHECK(!output.Contains("xh"));
BOOST_CHECK(output.Contains("x service_output=Sx"));
BOOST_CHECK(!output.Contains("xs"));
BOOST_CHECK(output.Contains("x notification_comment=Cx"));
BOOST_CHECK(!output.Contains("xc"));
BOOST_CHECK(output.Contains("x output=Sx"));
BOOST_CHECK(output.Contains("x comment=Cx"));
#endif /* __linux__ */
}

BOOST_AUTO_TEST_SUITE_END()
Loading