From 1921cfccd6d83edb15a04fdb143a3176fb96dd22 Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 30 Mar 2025 22:35:29 +0200 Subject: Always quote perfdata labels --- lib/perfdata.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/perfdata.c') diff --git a/lib/perfdata.c b/lib/perfdata.c index 661756c5..4f9c9558 100644 --- a/lib/perfdata.c +++ b/lib/perfdata.c @@ -33,7 +33,7 @@ char *pd_value_to_string(const mp_perfdata_value pd) { char *pd_to_string(mp_perfdata pd) { assert(pd.label != NULL); char *result = NULL; - asprintf(&result, "%s=", pd.label); + asprintf(&result, "'%s'=", pd.label); asprintf(&result, "%s%s", result, pd_value_to_string(pd.value)); -- cgit v1.2.3-74-g34f1 From 6e108cc25e75ec74013838620b26dabdd480718a Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 30 Mar 2025 22:36:07 +0200 Subject: Add more helpers to perfdata functions --- lib/perfdata.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/perfdata.h | 12 ++++++++ 2 files changed, 99 insertions(+) (limited to 'lib/perfdata.c') diff --git a/lib/perfdata.c b/lib/perfdata.c index 4f9c9558..1742342e 100644 --- a/lib/perfdata.c +++ b/lib/perfdata.c @@ -514,3 +514,90 @@ perfdata_value_parser_wrapper parse_pd_value(const char *input) { } return result; } + +mp_perfdata mp_set_pd_max_value(mp_perfdata perfdata, mp_perfdata_value value) { + perfdata.max = value; + perfdata.max_present = true; + return perfdata; +} + +mp_perfdata mp_set_pd_min_value(mp_perfdata perfdata, mp_perfdata_value value) { + perfdata.min = value; + perfdata.min_present = true; + return perfdata; +} + +double mp_get_pd_value(mp_perfdata_value value) { + assert(value.type != PD_TYPE_NONE); + switch (value.type) { + case PD_TYPE_DOUBLE: + return value.pd_double; + case PD_TYPE_INT: + return (double)value.pd_int; + case PD_TYPE_UINT: + return (double)value.pd_uint; + default: + return 0; // just to make the compiler happy + } +} + +mp_perfdata_value mp_pd_value_multiply(mp_perfdata_value left, mp_perfdata_value right) { + if (left.type == right.type) { + switch (left.type) { + case PD_TYPE_DOUBLE: + left.pd_double *= right.pd_double; + return left; + case PD_TYPE_INT: + left.pd_int *= right.pd_int; + return left; + case PD_TYPE_UINT: + left.pd_uint *= right.pd_uint; + return left; + default: + // what to here? + return left; + } + } + + // Different types, oh boy, just do the lazy thing for now and switch to double + switch (left.type) { + case PD_TYPE_INT: + left.pd_double = (double)left.pd_int; + left.type = PD_TYPE_DOUBLE; + break; + case PD_TYPE_UINT: + left.pd_double = (double)left.pd_uint; + left.type = PD_TYPE_DOUBLE; + break; + case PD_TYPE_DOUBLE: + default: + // already there + } + + switch (right.type) { + case PD_TYPE_INT: + right.pd_double = (double)right.pd_int; + right.type = PD_TYPE_DOUBLE; + break; + case PD_TYPE_UINT: + right.pd_double = (double)right.pd_uint; + right.type = PD_TYPE_DOUBLE; + break; + case PD_TYPE_DOUBLE: + default: + // already there + } + + left.pd_double *= right.pd_double; + return left; +} + +mp_range mp_range_multiply(mp_range range, mp_perfdata_value factor) { + if (!range.end_infinity) { + range.end = mp_pd_value_multiply(range.end, factor); + } + if (!range.start_infinity) { + range.start = mp_pd_value_multiply(range.start, factor); + } + return range; +} diff --git a/lib/perfdata.h b/lib/perfdata.h index 74583ee5..cb552678 100644 --- a/lib/perfdata.h +++ b/lib/perfdata.h @@ -171,6 +171,11 @@ mp_perfdata_value mp_create_pd_value_u_long(unsigned long); mp_perfdata_value mp_create_pd_value_long_long(long long); mp_perfdata_value mp_create_pd_value_u_long_long(unsigned long long); +mp_perfdata mp_set_pd_max_value(mp_perfdata perfdata, mp_perfdata_value value); +mp_perfdata mp_set_pd_min_value(mp_perfdata perfdata, mp_perfdata_value value); + +double mp_get_pd_value(mp_perfdata_value value); + /* * Free the memory used by a pd_list */ @@ -178,6 +183,13 @@ void pd_list_free(pd_list[1]); int cmp_perfdata_value(mp_perfdata_value, mp_perfdata_value); +// ================ +// Helper functions +// ================ + +mp_perfdata_value mp_pd_value_multiply(mp_perfdata_value left, mp_perfdata_value right); +mp_range mp_range_multiply(mp_range range, mp_perfdata_value factor); + // ================= // String formatters // ================= -- cgit v1.2.3-74-g34f1 From 430c641d9c6c3efb3fb0493b35e11dbc6ede2faa Mon Sep 17 00:00:00 2001 From: Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> Date: Sun, 30 Mar 2025 23:55:16 +0200 Subject: Try to circumvent some old compiler errors --- lib/perfdata.c | 6 ------ 1 file changed, 6 deletions(-) (limited to 'lib/perfdata.c') diff --git a/lib/perfdata.c b/lib/perfdata.c index 1742342e..f425ffcf 100644 --- a/lib/perfdata.c +++ b/lib/perfdata.c @@ -569,9 +569,6 @@ mp_perfdata_value mp_pd_value_multiply(mp_perfdata_value left, mp_perfdata_value left.pd_double = (double)left.pd_uint; left.type = PD_TYPE_DOUBLE; break; - case PD_TYPE_DOUBLE: - default: - // already there } switch (right.type) { @@ -583,9 +580,6 @@ mp_perfdata_value mp_pd_value_multiply(mp_perfdata_value left, mp_perfdata_value right.pd_double = (double)right.pd_uint; right.type = PD_TYPE_DOUBLE; break; - case PD_TYPE_DOUBLE: - default: - // already there } left.pd_double *= right.pd_double; -- cgit v1.2.3-74-g34f1