Skip to content
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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions src/NagiosObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -759,27 +759,27 @@ namespace statusengine {

int last_state;
int last_hard_state;
const char *long_output;

if (stateChangeData->statechange_type == SERVICE_STATECHANGE) {
auto tmp_service = reinterpret_cast<service *>(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<host *>(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;

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);
Expand Down
56 changes: 56 additions & 0 deletions tests/test_messageformat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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") {
Expand Down
Loading