]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
src/daemon/unit.[ch]: Add utility for determining the unit.
authorFlorian Forster <octo@collectd.org>
Sun, 17 Dec 2023 12:47:44 +0000 (13:47 +0100)
committerFlorian Forster <octo@collectd.org>
Thu, 28 Dec 2023 19:50:08 +0000 (20:50 +0100)
Makefile.am
src/daemon/unit.c [new file with mode: 0644]
src/daemon/unit.h [new file with mode: 0644]

index 8ef7bee06e900ff939bd96d345f35ba9b0dbe80e..4c650ae1f76a6c0bed7039d360e98b01e23dd853 100644 (file)
@@ -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 (file)
index 0000000..60ccc87
--- /dev/null
@@ -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 <octo at collectd.org>
+ **/
+
+#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 (file)
index 0000000..0afe324
--- /dev/null
@@ -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 <octo at collectd.org>
+ **/
+
+#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