about summary refs log tree commit diff
path: root/hwmon.c
diff options
context:
space:
mode:
authorHeikki Kallasjoki <kallasjoki@google.com>2018-11-15 23:31:31 +0000
committerHeikki Kallasjoki <kallasjoki@google.com>2018-11-15 23:31:31 +0000
commitbd4e9df0668f883705eac840a74ce9659376dabd (patch)
treed64dc9576c80f5bfbe421e02da994deb6b0b1368 /hwmon.c
parentc73aca3d51a9ce915b089d92ea9a43ec49ef63f0 (diff)
downloadnano-exporter-bd4e9df0668f883705eac840a74ce9659376dabd.tar.gz
nano-exporter-bd4e9df0668f883705eac840a74ce9659376dabd.tar.xz
nano-exporter-bd4e9df0668f883705eac840a74ce9659376dabd.zip
Improve naming of hwmon devices.
Diffstat (limited to 'hwmon.c')
-rw-r--r--hwmon.c64
1 files changed, 51 insertions, 13 deletions
diff --git a/hwmon.c b/hwmon.c
index 70c3129..11b6b87 100644
--- a/hwmon.c
+++ b/hwmon.c
@@ -116,6 +116,56 @@ static const struct metric_data metrics[] = {
   { .prefix = 0 },
 };
 
+static void hwmon_name(const char *path, char *dst, size_t dst_len) {
+  char buf[BUF_SIZE];
+  FILE *f;
+  size_t len;
+
+  // if the path is a symlink to "../../devices/X/Y/...", use X/Y as the name,
+  // except if it is "virtual/hwmon"
+
+  len = readlink(path, buf, sizeof buf);
+  if (len > 14 && memcmp(buf, "../../devices/", 14) == 0) {
+    char *start = buf + 14;
+    char *end = strchr(start, '/');
+    if (end)
+      end = strchr(end + 1, '/');
+    if (end)
+      *end = '\0';
+    if (strcmp(start, "virtual/hwmon") != 0) {
+      snprintf(dst, dst_len, "%s", start);
+      return;
+    }
+  }
+
+  // try to use the 'name' file
+
+  snprintf(buf, sizeof buf, "%s/name", path);
+  f = fopen(buf, "r");
+  if (f) {
+    bool found = false;
+    if (fgets(buf, sizeof buf, f)) {
+      len = strlen(buf);
+      if (len > 0 && buf[len-1] == '\n') {
+        len--;
+        buf[len] = '\0';
+      }
+      if (len > 0) {
+        snprintf(dst, dst_len, "hwmon/%s", buf);
+        found = true;
+      }
+    }
+    fclose(f);
+    if (found)
+      return;
+  }
+
+  // give up, just call this unknown
+
+  // TODO: add an index
+  snprintf(dst, dst_len, "unknown");
+}
+
 static void hwmon_collect(scrape_req *req, void *ctx) {
   // buffers
 
@@ -130,7 +180,6 @@ static void hwmon_collect(scrape_req *req, void *ctx) {
 
   char path[BUF_SIZE];
   char buf[BUF_SIZE];
-  size_t len;
 
   FILE *f;
 
@@ -148,18 +197,7 @@ static void hwmon_collect(scrape_req *req, void *ctx) {
       continue;
     snprintf(path, sizeof path, "/sys/class/hwmon/%s", dent->d_name);
 
-    len = readlink(path, buf, sizeof buf);
-    if (len > 14 && memcmp(buf, "../../devices/", 14) == 0) {
-      char *start = buf + 14;
-      char *end = strchr(start, '/');
-      if (end)
-        end = strchr(end + 1, '/');
-      if (end)
-        *end = '\0';
-      snprintf(chip_label, sizeof chip_label, "%s", start);
-    } else {
-      snprintf(chip_label, sizeof chip_label, "unknown");
-    }
+    hwmon_name(path, chip_label, sizeof chip_label);
 
     DIR *dir = opendir(path);
     if (!dir)