Transform output format to a global state

This commit removes the format parameter from the mp_check
object and creates a module global variable instead.
This prevents thread safe usage of different mp_check objects
which should likely not present a big problem for now.
The reason for this change is effectively the very same,
the format was lost if an exit was triggered by a signal
handler (timeout in this example).
This commit is contained in:
Lorenz Kästle 2025-02-25 17:20:05 +01:00
parent 3cbd84ce75
commit 72fd885f4f
4 changed files with 17 additions and 6 deletions

View file

@ -11,6 +11,9 @@
#include "perfdata.h"
#include "states.h"
// == Global variables
static mp_output_format output_format = MP_FORMAT_DEFAULT;
// == Prototypes ==
static char *fmt_subcheck_output(mp_output_format output_format, mp_subcheck check, unsigned int indentation);
static inline cJSON *json_serialize_subcheck(mp_subcheck subcheck);
@ -55,7 +58,6 @@ static inline char *fmt_subcheck_perfdata(mp_subcheck check) {
*/
mp_check mp_check_init(void) {
mp_check check = {0};
check.format = MP_FORMAT_DEFAULT;
return check;
}
@ -234,7 +236,7 @@ mp_state_enum mp_compute_check_state(const mp_check check) {
char *mp_fmt_output(mp_check check) {
char *result = NULL;
switch (check.format) {
switch (output_format) {
case MP_FORMAT_MULTI_LINE: {
if (check.summary == NULL) {
check.summary = get_subcheck_summary(check);
@ -482,7 +484,7 @@ void mp_print_output(mp_check check) { puts(mp_fmt_output(check)); }
*/
void mp_exit(mp_check check) {
mp_print_output(check);
if (check.format == MP_FORMAT_TEST_JSON) {
if (output_format == MP_FORMAT_TEST_JSON) {
exit(0);
}
@ -533,3 +535,7 @@ parsed_output_format mp_parse_output_format(char *format_string) {
return result;
}
void mp_set_format(mp_output_format format) { output_format = format; }
mp_output_format mp_get_format(void) { return output_format; }

View file

@ -35,6 +35,12 @@ typedef enum output_format {
#define MP_FORMAT_DEFAULT MP_FORMAT_MULTI_LINE
/*
* Format related functions
*/
void mp_set_format(mp_output_format format);
mp_output_format mp_get_format(void);
/*
* The main state object of a plugin. Exists only ONCE per plugin.
* This is the "root" of a tree of singular checks.
@ -42,7 +48,6 @@ typedef enum output_format {
* in the first layer of subchecks
*/
typedef struct {
mp_output_format format; // The output format
char *summary; // Overall summary, if not set a summary will be automatically generated
mp_subcheck_list *subchecks;
} mp_check;

View file

@ -77,7 +77,7 @@ int main(int argc, char **argv) {
mp_check overall = mp_check_init();
if (config.output_format_is_set) {
overall.format = config.output_format;
mp_set_format(config.output_format);
}
/* initialize alarm signal handling */

View file

@ -93,7 +93,7 @@ int main(int argc, char **argv) {
double percent_used;
mp_check overall = mp_check_init();
if (config.output_format_is_set) {
overall.format = config.output_format;
mp_set_format(config.output_format);
}
mp_subcheck sc1 = mp_subcheck_init();
sc1 = mp_set_subcheck_default_state(sc1, STATE_OK);