diff -Nurp -x lcc_features.h collectd-5.0.2/src/cpu.c collectd-5.0.2-new/src/cpu.c --- collectd-5.0.2/src/cpu.c 2012-01-22 09:10:04.000000000 -0200 +++ collectd-5.0.2-new/src/cpu.c 2012-05-09 16:03:59.000000000 -0300 @@ -252,7 +252,11 @@ static void submit (int cpu_num, const c vl.values_len = 1; sstrncpy (vl.host, hostname_g, sizeof (vl.host)); sstrncpy (vl.plugin, "cpu", sizeof (vl.plugin)); - ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), + if (cpu_num < 0) + ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), + "avg"); + else + ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance), "%i", cpu_num); sstrncpy (vl.type, "cpu", sizeof (vl.type)); sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); @@ -349,65 +353,6 @@ static int cpu_read (void) #endif /* PROCESSOR_TEMPERATURE */ } /* #endif PROCESSOR_CPU_LOAD_INFO */ - -#elif defined(KERNEL_LINUX) - int cpu; - derive_t user, nice, syst, idle; - derive_t wait, intr, sitr; /* sitr == soft interrupt */ - FILE *fh; - char buf[1024]; - - char *fields[9]; - int numfields; - - if ((fh = fopen ("/proc/stat", "r")) == NULL) - { - char errbuf[1024]; - ERROR ("cpu plugin: fopen (/proc/stat) failed: %s", - sstrerror (errno, errbuf, sizeof (errbuf))); - return (-1); - } - - while (fgets (buf, 1024, fh) != NULL) - { - if (strncmp (buf, "cpu", 3)) - continue; - if ((buf[3] < '0') || (buf[3] > '9')) - continue; - - numfields = strsplit (buf, fields, 9); - if (numfields < 5) - continue; - - cpu = atoi (fields[0] + 3); - user = atoll (fields[1]); - nice = atoll (fields[2]); - syst = atoll (fields[3]); - idle = atoll (fields[4]); - - submit (cpu, "user", user); - submit (cpu, "nice", nice); - submit (cpu, "system", syst); - submit (cpu, "idle", idle); - - if (numfields >= 8) - { - wait = atoll (fields[5]); - intr = atoll (fields[6]); - sitr = atoll (fields[7]); - - submit (cpu, "wait", wait); - submit (cpu, "interrupt", intr); - submit (cpu, "softirq", sitr); - - if (numfields >= 9) - submit (cpu, "steal", atoll (fields[8])); - } - } - - fclose (fh); -/* #endif defined(KERNEL_LINUX) */ - #elif defined(HAVE_LIBKSTAT) int cpu; derive_t user, syst, idle, wait; @@ -545,20 +490,109 @@ static int cpu_read (void) sg_cpu_stats *cs; cs = sg_get_cpu_stats (); + DEBUG("cpu plugin: Using libstatgrab to gather statistics"); + if (cs == NULL) { ERROR ("cpu plugin: sg_get_cpu_stats failed."); return (-1); } - submit (0, "idle", (derive_t) cs->idle); - submit (0, "nice", (derive_t) cs->nice); - submit (0, "swap", (derive_t) cs->swap); - submit (0, "system", (derive_t) cs->kernel); - submit (0, "user", (derive_t) cs->user); - submit (0, "wait", (derive_t) cs->iowait); + submit (-1, "idle", (derive_t) cs->idle); + submit (-1, "nice", (derive_t) cs->nice); + submit (-1, "swap", (derive_t) cs->swap); + submit (-1, "system", (derive_t) cs->kernel); + submit (-1, "user", (derive_t) cs->user); + submit (-1, "wait", (derive_t) cs->iowait); /* #endif HAVE_LIBSTATGRAB */ +#elif defined(KERNEL_LINUX) + int cpu; + derive_t user, nice, syst, idle; + derive_t wait, intr, sitr; /* sitr == soft interrupt */ + FILE *fh; + char buf[1024]; + + char *fields[9]; + int numfields; + int coren = sysconf(_SC_NPROCESSORS_ONLN); /* number of cores */ + + if (coren == -1) { /* sysconf has returned an error */ + char errbuf[1024]; + ERROR ("cpu plugin: can't get the number of cores: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + return (-1); + } + + + DEBUG ("cpu plugin: Reading /proc/stat for gather statistics"); + DEBUG ("cpu plugin: number of cpus found: %d", coren); + + if ((fh = fopen ("/proc/stat", "r")) == NULL) + { + char errbuf[1024]; + ERROR ("cpu plugin: fopen (/proc/stat) failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + return (-1); + } + + while (fgets (buf, 1024, fh) != NULL) + { + if (strncmp (buf, "cpu", 3)) + continue; + + numfields = strsplit (buf, fields, 9); + if (numfields < 5) + continue; + + if (!isdigit(fields[0][3])) { + DEBUG ("cpu plugin: gathering average"); + + cpu = -1; + user = atoll (fields[1]) / coren; + nice = atoll (fields[2]) / coren; + syst = atoll (fields[3]) / coren; + idle = atoll (fields[4]) / coren; + } else { + cpu = atoi (fields[0] + 3); + DEBUG ("cpu plugin: gathering cpu-%d", cpu); + user = atoll (fields[1]); + nice = atoll (fields[2]); + syst = atoll (fields[3]); + idle = atoll (fields[4]); + } + submit (cpu, "user", user); + submit (cpu, "nice", nice); + submit (cpu, "system", syst); + submit (cpu, "idle", idle); + + if (numfields >= 8) + { + if (cpu < 0) { + wait = atoll (fields[5]) / coren; + intr = atoll (fields[6]) / coren; + sitr = atoll (fields[7]) / coren; + } else { + wait = atoll (fields[5]); + intr = atoll (fields[6]); + sitr = atoll (fields[7]); + } + submit (cpu, "wait", wait); + submit (cpu, "interrupt", intr); + submit (cpu, "softirq", sitr); + + + } + if (numfields >= 9) { + if (cpu < 0) + submit (cpu, "steal", atoll (fields[8]) / coren); + else + submit (cpu, "steal", atoll (fields[8])); + } + } + + fclose (fh); +/* #endif defined(KERNEL_LINUX) */ #elif defined(HAVE_PERFSTAT) perfstat_id_t id; int i, cpus;