- 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[11];
+ int numfields;
+
+ if ((fh = fopen("/proc/stat", "r")) == NULL) {
+ ERROR("cpu plugin: fopen (/proc/stat) failed: %s", STRERRNO);
+ 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);
+ }
+
+ if (numfields >= 9) { /* Steal (since Linux 2.6.11) */
+ cpu_stage(cpu, COLLECTD_CPU_STATE_STEAL, (derive_t)atoll(fields[8]), now);
+ }
+
+ if (numfields >= 10) { /* Guest (since Linux 2.6.24) */
+ if (report_guest) {
+ long long value = atoll(fields[9]);
+ cpu_stage(cpu, COLLECTD_CPU_STATE_GUEST, (derive_t)value, now);
+ /* Guest is included in User; optionally subtract Guest from User: */
+ if (subtract_guest) {
+ user_value -= value;
+ if (user_value < 0)
+ user_value = 0;
+ }
+ }
+ }
+
+ if (numfields >= 11) { /* Guest_nice (since Linux 2.6.33) */
+ if (report_guest) {
+ long long value = atoll(fields[10]);
+ cpu_stage(cpu, COLLECTD_CPU_STATE_GUEST_NICE, (derive_t)value, now);
+ /* Guest_nice is included in Nice; optionally subtract Guest_nice from
+ Nice: */
+ if (subtract_guest) {
+ nice_value -= value;
+ if (nice_value < 0)
+ nice_value = 0;
+ }
+ }
+ }
+
+ /* Eventually stage User and Nice: */
+ cpu_stage(cpu, COLLECTD_CPU_STATE_USER, (derive_t)user_value, now);
+ cpu_stage(cpu, COLLECTD_CPU_STATE_NICE, (derive_t)nice_value, now);
+ }
+ fclose(fh);