]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
ubi: add percent evaluation for the bad blocks
authorFlorian Eckert <fe@dev.tdt.de>
Fri, 12 Mar 2021 10:07:45 +0000 (11:07 +0100)
committerMatthias Runge <mrunge@matthias-runge.de>
Thu, 9 Sep 2021 05:41:41 +0000 (07:41 +0200)
So far the plugin contains the evaluation of the maximum erase count
from `/sys/class/ubi/ubi<x>/max_ec` and the evaluation of bad physical
erase block count from `/sys/class/ubi/ubi<x>/bad_peb_count`.

For each ubi device, the reserved physical erase blocks are also displayed.
The value is shown in `/sys/class/ubi/ubi<x>/reserved_for_bad`.

If the defekt erase blocks are now used with the reserved erase blocks,
we can calculate who many percent of the flash is defekt.

With this information we can now create a notify that is triggered at a
certain percentage.

Signed-off-by: Florian Eckert <fe@dev.tdt.de>
src/ubi.c

index 0474aa68ecc3c220b300b03165581550608b70fd..4d1f924d656138d2f2773624059c175d945843e6 100644 (file)
--- a/src/ubi.c
+++ b/src/ubi.c
@@ -35,6 +35,9 @@
 #define DEV_BAD_COUNT                                                          \
   "bad_peb_count" // Count of bad physical eraseblocks on the underlying MTD
                   // device.
+// Value reserved for bad block
+#define DEV_RESERVED_BAD_BLOCK "reserved_for_bad"
+
 #define MAXIMUM_ERASE "max_ec" // Current maximum erase counter value
 
 /*
@@ -141,6 +144,37 @@ static inline int ubi_read_max_ec(const char *dev_name) {
   return 0;
 } /* int ubi_read_max_ec */
 
+static inline int ubi_read_percent(const char *dev_name) {
+  int ret;
+  int bcount;
+  int bblock;
+
+  ret = ubi_read_dev_attr(dev_name, DEV_BAD_COUNT, &bcount);
+
+  if (ret != 0) {
+    ERROR(PLUGIN_NAME " : Unable to read bad_peb_count");
+    return -1;
+  }
+
+  ret = ubi_read_dev_attr(dev_name, DEV_RESERVED_BAD_BLOCK, &bblock);
+
+  if (ret != 0) {
+    ERROR(PLUGIN_NAME " : Unable to read reserved_for_bad");
+    return -1;
+  }
+
+  if (bblock == 0) {
+    ERROR(PLUGIN_NAME
+          " : Percentage value cannot be determined (reserved_for_bad = 0)");
+    return -2;
+  }
+
+  ubi_submit(dev_name, "percent",
+             (gauge_t)((float_t)bcount / (float_t)bblock * 100.0));
+
+  return 0;
+} /* int ubi_read_percent */
+
 static int ubi_read(void) {
   DIR *dir;
   struct dirent *dirent;
@@ -156,6 +190,7 @@ static int ubi_read(void) {
 
     ubi_read_dev_bad_count(dirent->d_name);
     ubi_read_max_ec(dirent->d_name);
+    ubi_read_percent(dirent->d_name);
   }
 
   closedir(dir);