about summary refs log tree commit diff
path: root/netif.c
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2024-06-09 21:44:04 +0200
committerLeah Neukirchen <leah@vuxu.org>2024-06-09 21:44:04 +0200
commit55362bf81505371c7faeb14c11ffa822ff8cfb25 (patch)
tree9480e7d966c53c2f72c19cf664b059feab91ced3 /netif.c
parent69192b99aa1ae122add5380416d73196b121f2b8 (diff)
downloadnano-exporter-55362bf81505371c7faeb14c11ffa822ff8cfb25.tar.gz
nano-exporter-55362bf81505371c7faeb14c11ffa822ff8cfb25.tar.xz
nano-exporter-55362bf81505371c7faeb14c11ffa822ff8cfb25.zip
netif: add node_network_up, avoid unnecessary file access
Diffstat (limited to 'netif.c')
-rw-r--r--netif.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/netif.c b/netif.c
index 1fe20b5..c732acc 100644
--- a/netif.c
+++ b/netif.c
@@ -33,16 +33,22 @@ static void netif_collect_dir(scrape_req *req, char *dir) {
     LABEL_END,
   };
 
-  if (read_file_at(dirfd, "carrier", buf, sizeof buf) > 0)
+  if (read_file_at(dirfd, "carrier", buf, sizeof buf) > 0) {
     scrape_write(req, "node_network_carrier", dev_label, atof(buf));
 
-  if (read_file_at(dirfd, "carrier_changes", buf, sizeof buf) > 0)
-    scrape_write(req, "node_network_carrier_changes_total", dev_label, atof(buf));
+    if (read_file_at(dirfd, "carrier_changes", buf, sizeof buf) > 0)
+      scrape_write(req, "node_network_carrier_changes_total", dev_label, atof(buf));
+  }
 
-  if (read_file_at(dirfd, "speed", buf, sizeof buf) > 0)
-    if (atof(buf) > 0)
+  if (read_file_at(dirfd, "operstate", buf, sizeof buf) > 0) {
+    scrape_write(req, "node_network_up", dev_label, (float)(strcmp(buf, "down") == 0));
+
+    if (strcmp(buf, "up") == 0 &&
+        read_file_at(dirfd, "speed", buf, sizeof buf) > 0 &&
+        atof(buf) > 0)
       scrape_write(req, "node_network_speed_bytes", dev_label,
           atof(buf) * 125000.0); /* Mbit -> bytes */
+  }
 
   close(dirfd);
 }