- 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);
- }
-
- 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);
-
- cpu_stage (cpu, COLLECTD_CPU_STATE_USER, (derive_t) atoll(fields[1]), now);
- cpu_stage (cpu, COLLECTD_CPU_STATE_NICE, (derive_t) atoll(fields[2]), now);
- 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);
-
- if (numfields >= 9)
- cpu_stage (cpu, COLLECTD_CPU_STATE_STEAL, (derive_t) atoll(fields[8]), now);
- }
- }
- fclose (fh);
+ 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;
+ }
+
+ 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);
+
+ cpu_stage(cpu, COLLECTD_CPU_STATE_USER, (derive_t)atoll(fields[1]), now);
+ cpu_stage(cpu, COLLECTD_CPU_STATE_NICE, (derive_t)atoll(fields[2]), now);
+ 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);
+
+ if (numfields >= 9)
+ cpu_stage(cpu, COLLECTD_CPU_STATE_STEAL, (derive_t)atoll(fields[8]),
+ now);
+ }
+ }
+ fclose(fh);