Merge pull request #15235 from TDT-AG/pr/20210323-collectd-mod-ubi
authorHannu Nyman <hannu.nyman@iki.fi>
Wed, 7 Apr 2021 07:16:28 +0000 (10:16 +0300)
committerGitHub <noreply@github.com>
Wed, 7 Apr 2021 07:16:28 +0000 (10:16 +0300)
collectd: add bad blocks percent calculation for ubi plugin

utils/collectd/Makefile
utils/collectd/patches/934-ubi-prepare-read-for-percent.patch [new file with mode: 0644]
utils/collectd/patches/935-ubi-add-percent.patch [new file with mode: 0644]

index 3b09bb44fda17143943438b315cf0a56d894a376..52d523252e531b89e269ba95ae39da258f7f2491 100644 (file)
@@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
 
 PKG_NAME:=collectd
 PKG_VERSION:=5.12.0
-PKG_RELEASE:=8
+PKG_RELEASE:=9
 
 PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.bz2
 PKG_SOURCE_URL:=https://collectd.org/files/ \
diff --git a/utils/collectd/patches/934-ubi-prepare-read-for-percent.patch b/utils/collectd/patches/934-ubi-prepare-read-for-percent.patch
new file mode 100644 (file)
index 0000000..0277863
--- /dev/null
@@ -0,0 +1,66 @@
+--- a/src/ubi.c
++++ b/src/ubi.c
+@@ -84,9 +84,8 @@ static void ubi_submit(const char *dev_n
+   plugin_dispatch_values(&vl);
+ } /* void ubi_submit */
+-static int ubi_read_dev_attr(const char *dev_name, const char *attr) {
++static int ubi_read_dev_attr(const char *dev_name, const char *attr, int *value) {
+   FILE *f;
+-  int val;
+   char
+       str[sizeof(SYS_PATH) + strlen(dev_name) + sizeof("/") + strlen(attr) + 1];
+   int n;
+@@ -98,7 +97,7 @@ static int ubi_read_dev_attr(const char
+     return -1;
+   }
+-  n = fscanf(f, "%d", &val);
++  n = fscanf(f, "%d", value);
+   fclose(f);
+   if (n != 1) {
+@@ -106,17 +105,39 @@ static int ubi_read_dev_attr(const char
+     return -1;
+   }
+-  ubi_submit(dev_name, attr, (gauge_t)val);
+-
+   return 0;
+ } /* int ubi_read_dev_attr */
+ static inline int ubi_read_dev_bad_count(const char *dev_name) {
+-  return ubi_read_dev_attr(dev_name, DEV_BAD_COUNT);
++  int ret;
++  int value;
++
++  ret = ubi_read_dev_attr(dev_name, DEV_BAD_COUNT, &value);
++
++  if (ret != 0) {
++    ERROR(PLUGIN_NAME " : Unable to read bat_peb_count");
++    return -1;
++  }
++
++  ubi_submit(dev_name, DEV_BAD_COUNT, (gauge_t)value);
++
++  return 0;
+ } /* int ubi_read_dev_bad_count */
+ static inline int ubi_read_max_ec(const char *dev_name) {
+-  return ubi_read_dev_attr(dev_name, MAXIMUM_ERASE);
++  int ret;
++  int value;
++
++  ret = ubi_read_dev_attr(dev_name, MAXIMUM_ERASE, &value);
++
++  if (ret != 0) {
++    ERROR(PLUGIN_NAME " : Unable to read max_ec");
++    return -1;
++  }
++
++  ubi_submit(dev_name, MAXIMUM_ERASE, (gauge_t)value);
++
++  return 0;
+ } /* int ubi_read_max_ec */
+ static int ubi_read(void) {
diff --git a/utils/collectd/patches/935-ubi-add-percent.patch b/utils/collectd/patches/935-ubi-add-percent.patch
new file mode 100644 (file)
index 0000000..4736b85
--- /dev/null
@@ -0,0 +1,56 @@
+--- 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
+ /*
+@@ -140,6 +143,35 @@ static inline int ubi_read_max_ec(const
+   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;
+@@ -155,6 +187,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);