From 33af04a05397c0a45ead1ff990edbc862bfc5e0a Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sun, 17 Dec 2023 13:47:44 +0100 Subject: [PATCH] src/daemon/unit.[ch]: Add utility for determining the unit. --- Makefile.am | 2 ++ src/daemon/unit.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++ src/daemon/unit.h | 44 +++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 src/daemon/unit.c create mode 100644 src/daemon/unit.h diff --git a/Makefile.am b/Makefile.am index 8ef7bee06..4c650ae1f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -252,6 +252,8 @@ collectd_SOURCES = \ src/daemon/resource.h \ src/daemon/types_list.c \ src/daemon/types_list.h \ + src/daemon/unit.c \ + src/daemon/unit.h \ src/daemon/utils_cache.c \ src/daemon/utils_cache.h \ src/daemon/utils_complain.c \ diff --git a/src/daemon/unit.c b/src/daemon/unit.c new file mode 100644 index 000000000..60ccc879d --- /dev/null +++ b/src/daemon/unit.c @@ -0,0 +1,70 @@ +/** + * collectd - src/daemon/unit.c + * Copyright (C) 2023 Florian "octo" Forster + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Authors: + * Florian octo Forster + **/ + +#include "collectd.h" +#include "daemon/unit.h" + +static bool string_has_suffix(char const *s, char const *suffix) { + size_t s_len = strlen(s); + size_t suffix_len = strlen(suffix); + + if (s_len < suffix_len) { + return false; + } + + s += (s_len - suffix_len); + return strcmp(s, suffix) == 0; +} + +static char const *default_unit_static(metric_family_t const *fam) { + if (string_has_suffix(fam->name, ".utilization")) { + return "1"; + } + if (string_has_suffix(fam->name, ".time")) { + return "s"; + } + if (string_has_suffix(fam->name, ".io")) { + return "By"; + } + if (string_has_suffix(fam->name, ".operations")) { + return "{operation}"; + } + + return NULL; +} + +char *default_unit(metric_family_t const *fam) { + if (fam->unit != NULL) { + return fam->unit; + } + + char const *unit = default_unit_static(fam); + if (unit == NULL) { + return NULL; + } + + return strdup(unit); +} diff --git a/src/daemon/unit.h b/src/daemon/unit.h new file mode 100644 index 000000000..0afe324f4 --- /dev/null +++ b/src/daemon/unit.h @@ -0,0 +1,44 @@ +/** + * collectd - src/daemon/unit.h + * Copyright (C) 2023 Florian "octo" Forster + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + * + * Authors: + * Florian octo Forster + **/ + +#ifndef DAEMON_UNIT_H +#define DAEMON_UNIT_H 1 + +#include "daemon/metric.h" + +/* default_unit tries to guess a metric family's unit. + * + * If fam->unit is not NULL, that pointer is returned. Otherwise, the function + * tries to heuristically determine a unit for th metric family. If successful, + * a new string is allocated on the heap and returned. This string must be freed + * using free(). If unsuccessful, NULL is returned. + * + * This is designed to be used like this: + * fam->unit = default_unit(fam); + */ +char *default_unit(metric_family_t const *fam); + +#endif -- 2.47.2