- int cpu;
- 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);
+ int cpu;
+ FILE *fh;
+ char buf[1024];
+
+ char *fields[11];
+ 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, STATIC_ARRAY_SIZE(fields));
+ if (numfields < 5)
+ continue;
+
+ cpu = atoi(fields[0] + 3);
+
+ /* Do not stage User and Nice immediately: we may need to alter them later: */
+ long long user_value = atoll(fields[1]);
+ long long nice_value = atoll(fields[2]);
+ cpu_stage(cpu, COLLECTD_CPU_STATE_SYSTEM, (derive_t)atoll(fields[3]), now);
+ cpu_stage(cpu, COLLECTD_CPU_STATE_IDLE, (derive_t)atoll(fields[4]), now);
+
+ if (numfields >= 8) {
+ cpu_stage(cpu, COLLECTD_CPU_STATE_WAIT, (derive_t)atoll(fields[5]), now);
+ cpu_stage(cpu, COLLECTD_CPU_STATE_INTERRUPT, (derive_t)atoll(fields[6]),
+ now);
+ cpu_stage(cpu, COLLECTD_CPU_STATE_SOFTIRQ, (derive_t)atoll(fields[7]),
+ now);