summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-30 22:36:07 +0200
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-30 22:36:07 +0200
commit6e108cc25e75ec74013838620b26dabdd480718a (patch)
treead506a498f95813dec07119a9d8d4975eb28d2de
parent1921cfccd6d83edb15a04fdb143a3176fb96dd22 (diff)
downloadmonitoring-plugins-6e108cc25e75ec74013838620b26dabdd480718a.tar.gz
Add more helpers to perfdata functions
-rw-r--r--lib/perfdata.c87
-rw-r--r--lib/perfdata.h12
2 files changed, 99 insertions, 0 deletions
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) {
514 } 514 }
515 return result; 515 return result;
516} 516}
517
518mp_perfdata mp_set_pd_max_value(mp_perfdata perfdata, mp_perfdata_value value) {
519 perfdata.max = value;
520 perfdata.max_present = true;
521 return perfdata;
522}
523
524mp_perfdata mp_set_pd_min_value(mp_perfdata perfdata, mp_perfdata_value value) {
525 perfdata.min = value;
526 perfdata.min_present = true;
527 return perfdata;
528}
529
530double mp_get_pd_value(mp_perfdata_value value) {
531 assert(value.type != PD_TYPE_NONE);
532 switch (value.type) {
533 case PD_TYPE_DOUBLE:
534 return value.pd_double;
535 case PD_TYPE_INT:
536 return (double)value.pd_int;
537 case PD_TYPE_UINT:
538 return (double)value.pd_uint;
539 default:
540 return 0; // just to make the compiler happy
541 }
542}
543
544mp_perfdata_value mp_pd_value_multiply(mp_perfdata_value left, mp_perfdata_value right) {
545 if (left.type == right.type) {
546 switch (left.type) {
547 case PD_TYPE_DOUBLE:
548 left.pd_double *= right.pd_double;
549 return left;
550 case PD_TYPE_INT:
551 left.pd_int *= right.pd_int;
552 return left;
553 case PD_TYPE_UINT:
554 left.pd_uint *= right.pd_uint;
555 return left;
556 default:
557 // what to here?
558 return left;
559 }
560 }
561
562 // Different types, oh boy, just do the lazy thing for now and switch to double
563 switch (left.type) {
564 case PD_TYPE_INT:
565 left.pd_double = (double)left.pd_int;
566 left.type = PD_TYPE_DOUBLE;
567 break;
568 case PD_TYPE_UINT:
569 left.pd_double = (double)left.pd_uint;
570 left.type = PD_TYPE_DOUBLE;
571 break;
572 case PD_TYPE_DOUBLE:
573 default:
574 // already there
575 }
576
577 switch (right.type) {
578 case PD_TYPE_INT:
579 right.pd_double = (double)right.pd_int;
580 right.type = PD_TYPE_DOUBLE;
581 break;
582 case PD_TYPE_UINT:
583 right.pd_double = (double)right.pd_uint;
584 right.type = PD_TYPE_DOUBLE;
585 break;
586 case PD_TYPE_DOUBLE:
587 default:
588 // already there
589 }
590
591 left.pd_double *= right.pd_double;
592 return left;
593}
594
595mp_range mp_range_multiply(mp_range range, mp_perfdata_value factor) {
596 if (!range.end_infinity) {
597 range.end = mp_pd_value_multiply(range.end, factor);
598 }
599 if (!range.start_infinity) {
600 range.start = mp_pd_value_multiply(range.start, factor);
601 }
602 return range;
603}
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);
171mp_perfdata_value mp_create_pd_value_long_long(long long); 171mp_perfdata_value mp_create_pd_value_long_long(long long);
172mp_perfdata_value mp_create_pd_value_u_long_long(unsigned long long); 172mp_perfdata_value mp_create_pd_value_u_long_long(unsigned long long);
173 173
174mp_perfdata mp_set_pd_max_value(mp_perfdata perfdata, mp_perfdata_value value);
175mp_perfdata mp_set_pd_min_value(mp_perfdata perfdata, mp_perfdata_value value);
176
177double mp_get_pd_value(mp_perfdata_value value);
178
174/* 179/*
175 * Free the memory used by a pd_list 180 * Free the memory used by a pd_list
176 */ 181 */
@@ -178,6 +183,13 @@ void pd_list_free(pd_list[1]);
178 183
179int cmp_perfdata_value(mp_perfdata_value, mp_perfdata_value); 184int cmp_perfdata_value(mp_perfdata_value, mp_perfdata_value);
180 185
186// ================
187// Helper functions
188// ================
189
190mp_perfdata_value mp_pd_value_multiply(mp_perfdata_value left, mp_perfdata_value right);
191mp_range mp_range_multiply(mp_range range, mp_perfdata_value factor);
192
181// ================= 193// =================
182// String formatters 194// String formatters
183// ================= 195// =================