[collectd] CPU plugin patch

Oleg King king2 at kaluga.ru
Tue Apr 29 22:18:59 CEST 2008


Hi!

I wrote patch for cpu plugin that use libstatgrab to collect CPU
times. It collects info about all CPUs in system, but still reports
info as 'cpu-0' (I cannot find the way to report info as just 'cpu').

This patch will try to use libstatgrab before sysctlbyname, because
sysctlbyname can collect info for first CPU only. I think that info
about all CPUs is better than about first CPU only.

This is a patch:


diff -pur collectd-4.3.3/src/cpu.c collectd/src/cpu.c
--- collectd-4.3.3/src/cpu.c    2008-04-23 16:29:16.000000000 +0400
+++ collectd/src/cpu.c  2008-04-28 21:18:35.000000000 +0400
@@ -96,6 +96,10 @@ static kstat_t *ksp[MAX_NUMCPU];
 static int numcpu;
 /* #endif HAVE_LIBKSTAT */
 
+#elif defined(HAVE_LIBSTATGRAB)
+// do nothing, prevent from SYSCTLBYNAME if we have both
+/* #endif HAVE_LIBSTATGRAB */
+
 #elif defined(HAVE_SYSCTLBYNAME)
 static int numcpu;
 #endif /* HAVE_SYSCTLBYNAME */
@@ -137,6 +141,10 @@ static int init (void)
                        ksp[numcpu++] = ksp_chain;
 /* #endif HAVE_LIBKSTAT */
 
+#elif defined(HAVE_LIBSTATGRAB)
+// do nothing, prevent from SYSCTLBYNAME if we have both
+/* #endif HAVE_LIBSTATGRAB */
+
 #elif defined (HAVE_SYSCTLBYNAME)
        size_t numcpu_size;
 
@@ -350,10 +358,23 @@ static int cpu_read (void)
        }
 /* #endif defined(HAVE_LIBKSTAT) */
 
+#elif defined(HAVE_LIBSTATGRAB)
+        sg_cpu_percents *cs;
+
+        if ((cs = sg_get_cpu_percents ()) == NULL)
+                return (0);
+
+       submit (0, "user", cs->user);
+       submit (0, "nice", cs->nice);
+       submit (0, "idle", cs->idle);
+       submit (0, "kernel", cs->kernel);
+       submit (0, "iowait", cs->iowait);
+       submit (0, "swap", cs->swap);
+/* #endif HAVE_LIBSTATGRAB */
+
 #elif defined(HAVE_SYSCTLBYNAME)
        long cpuinfo[CPUSTATES]; 
        size_t cpuinfo_size;

-- 
WBR,
 Oleg                          mailto:king2 at kaluga.ru




More information about the collectd mailing list