[3/3] collectd: Use libstatgrab to read disk stats

Message ID 20210708112005.22389-3-michael.tremer@ipfire.org
State Accepted
Commit a652d4187cb9b8b026e21951baffb0986f133f5c
Headers
Series [1/3] make.sh: Build libstatgrab before collectd |

Commit Message

Michael Tremer July 8, 2021, 11:20 a.m. UTC
  Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
---
 lfs/collectd                                  |   1 +
 ...collectd-4.10-drop-linux-disk-module.patch | 299 ++++++++++++++++++
 2 files changed, 300 insertions(+)
 create mode 100644 src/patches/collectd-4.10-drop-linux-disk-module.patch
  

Patch

diff --git a/lfs/collectd b/lfs/collectd
index 36b507868..09d8c6fcf 100644
--- a/lfs/collectd
+++ b/lfs/collectd
@@ -107,6 +107,7 @@  $(TARGET) : $(patsubst %,$(DIR_DL)/%,$(objects))
 	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd-4.10.9-xfs-compile-fix.patch
 	cd $(DIR_APP) && patch -p1 -i $(DIR_SRC)/src/patches/collectd-4.10-libiptc-build-fixes.patch
 	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/collectd-4.10.9-remove-checks-for-SENSORS_API_VERSION-upper-limit.patch
+	cd $(DIR_APP) && patch -Np1 < $(DIR_SRC)/src/patches/collectd-4.10-drop-linux-disk-module.patch
 	cd $(DIR_APP) && autoupdate -v configure.in
 	cd $(DIR_APP) && autoreconf -vfi
 	cd $(DIR_APP)/libltdl && autoreconf -vfi
diff --git a/src/patches/collectd-4.10-drop-linux-disk-module.patch b/src/patches/collectd-4.10-drop-linux-disk-module.patch
new file mode 100644
index 000000000..02e9543f1
--- /dev/null
+++ b/src/patches/collectd-4.10-drop-linux-disk-module.patch
@@ -0,0 +1,299 @@ 
+diff --git a/src/collectd.c b/src/collectd.c
+index 6b77d599..f17c19eb 100644
+--- a/src/collectd.c
++++ b/src/collectd.c
+@@ -294,7 +294,7 @@ static int do_init (void)
+ #endif
+ 
+ #if HAVE_LIBSTATGRAB
+-	if (sg_init ())
++	if (sg_init (0))
+ 	{
+ 		ERROR ("sg_init: %s", sg_str_error (sg_get_error ()));
+ 		return (-1);
+diff --git a/src/disk.c b/src/disk.c
+index 4a78f1bd..5f972aa5 100644
+--- a/src/disk.c
++++ b/src/disk.c
+@@ -77,34 +77,6 @@
+ static mach_port_t io_master_port = MACH_PORT_NULL;
+ /* #endif HAVE_IOKIT_IOKITLIB_H */
+ 
+-#elif KERNEL_LINUX
+-typedef struct diskstats
+-{
+-	char *name;
+-
+-	/* This overflows in roughly 1361 years */
+-	unsigned int poll_count;
+-
+-	counter_t read_sectors;
+-	counter_t write_sectors;
+-
+-	counter_t read_bytes;
+-	counter_t write_bytes;
+-
+-	counter_t read_ops;
+-	counter_t write_ops;
+-	counter_t read_time;
+-	counter_t write_time;
+-
+-	counter_t avg_read_time;
+-	counter_t avg_write_time;
+-
+-	struct diskstats *next;
+-} diskstats_t;
+-
+-static diskstats_t *disklist;
+-/* #endif KERNEL_LINUX */
+-
+ #elif HAVE_LIBKSTAT
+ #define MAX_NUMDISK 256
+ extern kstat_ctl_t *kc;
+@@ -182,10 +154,6 @@ static int disk_init (void)
+ 	}
+ /* #endif HAVE_IOKIT_IOKITLIB_H */
+ 
+-#elif KERNEL_LINUX
+-	/* do nothing */
+-/* #endif KERNEL_LINUX */
+-
+ #elif HAVE_LIBKSTAT
+ 	kstat_t *ksp_chain;
+ 
+@@ -235,16 +203,6 @@ static void disk_submit (const char *plugin_instance,
+ 	plugin_dispatch_values (&vl);
+ } /* void disk_submit */
+ 
+-#if KERNEL_LINUX
+-static counter_t disk_calc_time_incr (counter_t delta_time, counter_t delta_ops)
+-{
+-	double avg_time = ((double) delta_time) / ((double) delta_ops);
+-	double avg_time_incr = ((double) interval_g) * avg_time;
+-
+-	return ((counter_t) (avg_time_incr + .5));
+-}
+-#endif
+-
+ #if HAVE_IOKIT_IOKITLIB_H
+ static signed long long dict_get_value (CFDictionaryRef dict, const char *key)
+ {
+@@ -428,218 +386,6 @@ static int disk_read (void)
+ 	IOObjectRelease (disk_list);
+ /* #endif HAVE_IOKIT_IOKITLIB_H */
+ 
+-#elif KERNEL_LINUX
+-	FILE *fh;
+-	char buffer[1024];
+-	
+-	char *fields[32];
+-	int numfields;
+-	int fieldshift = 0;
+-
+-	int minor = 0;
+-
+-	counter_t read_sectors  = 0;
+-	counter_t write_sectors = 0;
+-
+-	counter_t read_ops      = 0;
+-	counter_t read_merged   = 0;
+-	counter_t read_time     = 0;
+-	counter_t write_ops     = 0;
+-	counter_t write_merged  = 0;
+-	counter_t write_time    = 0;
+-	int is_disk = 0;
+-
+-	diskstats_t *ds, *pre_ds;
+-
+-	if ((fh = fopen ("/proc/diskstats", "r")) == NULL)
+-	{
+-		fh = fopen ("/proc/partitions", "r");
+-		if (fh == NULL)
+-		{
+-			ERROR ("disk plugin: fopen (/proc/{diskstats,partitions}) failed.");
+-			return (-1);
+-		}
+-
+-		/* Kernel is 2.4.* */
+-		fieldshift = 1;
+-	}
+-
+-	while (fgets (buffer, sizeof (buffer), fh) != NULL)
+-	{
+-		char *disk_name;
+-
+-		numfields = strsplit (buffer, fields, 32);
+-
+-		if ((numfields != (14 + fieldshift)) && (numfields != 7))
+-			continue;
+-
+-		minor = atoll (fields[1]);
+-
+-		disk_name = fields[2 + fieldshift];
+-
+-		for (ds = disklist, pre_ds = disklist; ds != NULL; pre_ds = ds, ds = ds->next)
+-			if (strcmp (disk_name, ds->name) == 0)
+-				break;
+-
+-		if (ds == NULL)
+-		{
+-			if ((ds = (diskstats_t *) calloc (1, sizeof (diskstats_t))) == NULL)
+-				continue;
+-
+-			if ((ds->name = strdup (disk_name)) == NULL)
+-			{
+-				free (ds);
+-				continue;
+-			}
+-
+-			if (pre_ds == NULL)
+-				disklist = ds;
+-			else
+-				pre_ds->next = ds;
+-		}
+-
+-		is_disk = 0;
+-		if (numfields == 7)
+-		{
+-			/* Kernel 2.6, Partition */
+-			read_ops      = atoll (fields[3]);
+-			read_sectors  = atoll (fields[4]);
+-			write_ops     = atoll (fields[5]);
+-			write_sectors = atoll (fields[6]);
+-		}
+-		else if (numfields == (14 + fieldshift))
+-		{
+-			read_ops  =  atoll (fields[3 + fieldshift]);
+-			write_ops =  atoll (fields[7 + fieldshift]);
+-
+-			read_sectors  = atoll (fields[5 + fieldshift]);
+-			write_sectors = atoll (fields[9 + fieldshift]);
+-
+-			if ((fieldshift == 0) || (minor == 0))
+-			{
+-				is_disk = 1;
+-				read_merged  = atoll (fields[4 + fieldshift]);
+-				read_time    = atoll (fields[6 + fieldshift]);
+-				write_merged = atoll (fields[8 + fieldshift]);
+-				write_time   = atoll (fields[10+ fieldshift]);
+-			}
+-		}
+-		else
+-		{
+-			DEBUG ("numfields = %i; => unknown file format.", numfields);
+-			continue;
+-		}
+-
+-		{
+-			counter_t diff_read_sectors;
+-			counter_t diff_write_sectors;
+-
+-		/* If the counter wraps around, it's only 32 bits.. */
+-			if (read_sectors < ds->read_sectors)
+-				diff_read_sectors = 1 + read_sectors
+-					+ (UINT_MAX - ds->read_sectors);
+-			else
+-				diff_read_sectors = read_sectors - ds->read_sectors;
+-			if (write_sectors < ds->write_sectors)
+-				diff_write_sectors = 1 + write_sectors
+-					+ (UINT_MAX - ds->write_sectors);
+-			else
+-				diff_write_sectors = write_sectors - ds->write_sectors;
+-
+-			ds->read_bytes += 512 * diff_read_sectors;
+-			ds->write_bytes += 512 * diff_write_sectors;
+-			ds->read_sectors = read_sectors;
+-			ds->write_sectors = write_sectors;
+-		}
+-
+-		/* Calculate the average time an io-op needs to complete */
+-		if (is_disk)
+-		{
+-			counter_t diff_read_ops;
+-			counter_t diff_write_ops;
+-			counter_t diff_read_time;
+-			counter_t diff_write_time;
+-
+-			if (read_ops < ds->read_ops)
+-				diff_read_ops = 1 + read_ops
+-					+ (UINT_MAX - ds->read_ops);
+-			else
+-				diff_read_ops = read_ops - ds->read_ops;
+-			DEBUG ("disk plugin: disk_name = %s; read_ops = %llu; "
+-					"ds->read_ops = %llu; diff_read_ops = %llu;",
+-					disk_name,
+-					read_ops, ds->read_ops, diff_read_ops);
+-
+-			if (write_ops < ds->write_ops)
+-				diff_write_ops = 1 + write_ops
+-					+ (UINT_MAX - ds->write_ops);
+-			else
+-				diff_write_ops = write_ops - ds->write_ops;
+-
+-			if (read_time < ds->read_time)
+-				diff_read_time = 1 + read_time
+-					+ (UINT_MAX - ds->read_time);
+-			else
+-				diff_read_time = read_time - ds->read_time;
+-
+-			if (write_time < ds->write_time)
+-				diff_write_time = 1 + write_time
+-					+ (UINT_MAX - ds->write_time);
+-			else
+-				diff_write_time = write_time - ds->write_time;
+-
+-			if (diff_read_ops != 0)
+-				ds->avg_read_time += disk_calc_time_incr (
+-						diff_read_time, diff_read_ops);
+-			if (diff_write_ops != 0)
+-				ds->avg_write_time += disk_calc_time_incr (
+-						diff_write_time, diff_write_ops);
+-
+-			ds->read_ops = read_ops;
+-			ds->read_time = read_time;
+-			ds->write_ops = write_ops;
+-			ds->write_time = write_time;
+-		} /* if (is_disk) */
+-
+-		/* Don't write to the RRDs if we've just started.. */
+-		ds->poll_count++;
+-		if (ds->poll_count <= 2)
+-		{
+-			DEBUG ("disk plugin: (ds->poll_count = %i) <= "
+-					"(min_poll_count = 2); => Not writing.",
+-					ds->poll_count);
+-			continue;
+-		}
+-
+-		if ((read_ops == 0) && (write_ops == 0))
+-		{
+-			DEBUG ("disk plugin: ((read_ops == 0) && "
+-					"(write_ops == 0)); => Not writing.");
+-			continue;
+-		}
+-
+-		if ((ds->read_bytes != 0) || (ds->write_bytes != 0))
+-			disk_submit (disk_name, "disk_octets",
+-					ds->read_bytes, ds->write_bytes);
+-
+-		if ((ds->read_ops != 0) || (ds->write_ops != 0))
+-			disk_submit (disk_name, "disk_ops",
+-					read_ops, write_ops);
+-
+-		if ((ds->avg_read_time != 0) || (ds->avg_write_time != 0))
+-			disk_submit (disk_name, "disk_time",
+-					ds->avg_read_time, ds->avg_write_time);
+-
+-		if (is_disk)
+-		{
+-			disk_submit (disk_name, "disk_merged",
+-					read_merged, write_merged);
+-		} /* if (is_disk) */
+-	} /* while (fgets (buffer, sizeof (buffer), fh) != NULL) */
+-
+-	fclose (fh);
+-/* #endif defined(KERNEL_LINUX) */
+-
+ #elif HAVE_LIBKSTAT
+ # if HAVE_KSTAT_IO_T_WRITES && HAVE_KSTAT_IO_T_NWRITES && HAVE_KSTAT_IO_T_WTIME
+ #  define KIO_ROCTETS reads