diff --git a/README.md b/README.md index 4b738ef..881279c 100644 --- a/README.md +++ b/README.md @@ -148,13 +148,17 @@ carries no additional information: * `ContactNotificationData` * `EventHandlerData` * `NotificationData` -* `StateChange` * `SystemCommandData` The reason is that the underlying naemon/nagios `nebstruct_*` structs simply have no `long_output` member for these events; only host and service checks have one. The duplication is kept for backwards compatibility, so that existing consumers do not break. -Do not read a separate long plugin output out of these five event types. +Do not read a separate long plugin output out of these four event types. + +`StateChange` is not in that list. Its `long_output` is the real long plugin output of the +host or service, read from the event's `object_ptr`, so it can differ from `output`. When +the object has no long output the field is an empty string, just like on host and service +checks. ### Compact JSON diff --git a/src/NagiosObject.h b/src/NagiosObject.h index cd8f591..3049c66 100644 --- a/src/NagiosObject.h +++ b/src/NagiosObject.h @@ -759,16 +759,19 @@ namespace statusengine { int last_state; int last_hard_state; + const char *long_output; if (stateChangeData->statechange_type == SERVICE_STATECHANGE) { auto tmp_service = reinterpret_cast(stateChangeData->object_ptr); last_state = tmp_service->last_state; last_hard_state = tmp_service->last_hard_state; + long_output = tmp_service->long_plugin_output; } else { auto tmp_host = reinterpret_cast(stateChangeData->object_ptr); last_state = tmp_host->last_state; last_hard_state = tmp_host->last_hard_state; + long_output = tmp_host->long_plugin_output; } NagiosObject statechange; @@ -776,10 +779,7 @@ namespace statusengine { statechange.SetData("host_name", stateChangeData->host_name); statechange.SetData("service_description", stateChangeData->service_description); statechange.SetData("output", nebmodule.EncodeString(stateChangeData->output)); - // Intentional: this nebstruct has no long_output member, only host and service - // checks do. long_output repeats output so the message format stays backwards - // compatible - see the "Message format" section in README.md. Not a typo. - statechange.SetData("long_output", nebmodule.EncodeString(stateChangeData->output)); + statechange.SetData("long_output", nebmodule.EncodeString(long_output)); statechange.SetData("statechange_type", stateChangeData->statechange_type); statechange.SetData("state", stateChangeData->state); statechange.SetData("state_type", stateChangeData->state_type); diff --git a/tests/test_messageformat.cpp b/tests/test_messageformat.cpp index 76cfbf7..182a980 100644 --- a/tests/test_messageformat.cpp +++ b/tests/test_messageformat.cpp @@ -53,6 +53,23 @@ namespace { return obj.ToString(); } + /// Value of a string field in the named sub object of a rendered message. + std::string SubObjectString(NagiosObject &obj, const char *subObject, const char *field) { + std::string rendered = Rendered(obj); + yyjson_doc *doc = yyjson_read(rendered.c_str(), rendered.length(), 0); + REQUIRE(doc != nullptr); + + yyjson_val *sub = yyjson_obj_get(yyjson_doc_get_root(doc), subObject); + REQUIRE(sub != nullptr); + yyjson_val *value = yyjson_obj_get(sub, field); + REQUIRE(value != nullptr); + REQUIRE(yyjson_is_str(value)); + + std::string result(yyjson_get_str(value), yyjson_get_len(value)); + yyjson_doc_free(doc); + return result; + } + /** * Compares a freshly built message against a recorded one: the outer envelope and the * named sub object have to carry exactly the same keys. Values are not compared, the @@ -182,6 +199,7 @@ TEST_CASE("state change message keeps its shape") { std::memset(&nagService, 0, sizeof(nagService)); nagService.last_state = 1; nagService.last_hard_state = 1; + nagService.long_plugin_output = Str("Your random value is: 158\nmore details"); nebstruct_statechange_data data; FillHeader(data, NEBTYPE_STATECHANGE_END); @@ -193,6 +211,44 @@ TEST_CASE("state change message keeps its shape") { NagiosStateChangeData msg(&data); CheckShape(msg, "statusngin_statechanges.json", "statechange"); + + // The long output comes from the service object, not from the short output. + CHECK(SubObjectString(msg, "statechange", "output") == "Your random value is: 158"); + CHECK(SubObjectString(msg, "statechange", "long_output") == + "Your random value is: 158\nmore details"); +} + +TEST_CASE("host state change message uses the host long output") { + host nagHost; + std::memset(&nagHost, 0, sizeof(nagHost)); + nagHost.long_plugin_output = Str("host long output"); + + nebstruct_statechange_data data; + FillHeader(data, NEBTYPE_STATECHANGE_END); + data.host_name = Str("localhost"); + data.statechange_type = HOST_STATECHANGE; + data.object_ptr = &nagHost; + data.output = Str("host short output"); + + NagiosStateChangeData msg(&data); + CHECK(SubObjectString(msg, "statechange", "long_output") == "host long output"); +} + +TEST_CASE("state change without long output reports an empty string") { + service nagService; + std::memset(&nagService, 0, sizeof(nagService)); + nagService.long_plugin_output = nullptr; + + nebstruct_statechange_data data; + FillHeader(data, NEBTYPE_STATECHANGE_END); + data.host_name = Str("localhost"); + data.service_description = Str("Flapping"); + data.statechange_type = SERVICE_STATECHANGE; + data.object_ptr = &nagService; + data.output = Str("short output only"); + + NagiosStateChangeData msg(&data); + CHECK(SubObjectString(msg, "statechange", "long_output").empty()); } TEST_CASE("log entry message keeps its shape") {