10 #include "appconfig.h"
13 #include "plugin_proc.h"
15 #define RRD_TYPE_STAT "cpu"
16 #define RRD_TYPE_STAT_LEN strlen(RRD_TYPE_STAT)
18 int do_proc_stat(int update_every, unsigned long long dt) {
19 static procfile *ff = NULL;
20 static int do_cpu = -1, do_cpu_cores = -1, do_interrupts = -1, do_context = -1, do_forks = -1, do_processes = -1;
22 if(do_cpu == -1) do_cpu = config_get_boolean("plugin:proc:/proc/stat", "cpu utilization", 1);
23 if(do_cpu_cores == -1) do_cpu_cores = config_get_boolean("plugin:proc:/proc/stat", "per cpu core utilization", 1);
24 if(do_interrupts == -1) do_interrupts = config_get_boolean("plugin:proc:/proc/stat", "cpu interrupts", 1);
25 if(do_context == -1) do_context = config_get_boolean("plugin:proc:/proc/stat", "context switches", 1);
26 if(do_forks == -1) do_forks = config_get_boolean("plugin:proc:/proc/stat", "processes started", 1);
27 if(do_processes == -1) do_processes = config_get_boolean("plugin:proc:/proc/stat", "processes running", 1);
32 char filename[FILENAME_MAX + 1];
33 snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/stat");
34 ff = procfile_open(config_get("plugin:proc:/proc/stat", "filename to monitor", filename), " \t:", PROCFILE_FLAG_DEFAULT);
38 ff = procfile_readall(ff);
39 if(!ff) return 0; // we return 0, so that we will retry to open it next time
41 uint32_t lines = procfile_lines(ff), l;
44 unsigned long long processes = 0, running = 0 , blocked = 0;
47 for(l = 0; l < lines ;l++) {
48 if(strncmp(procfile_lineword(ff, l, 0), "cpu", 3) == 0) {
49 words = procfile_linewords(ff, l);
51 error("Cannot read /proc/stat cpu line. Expected 10 params, read %d.", words);
56 unsigned long long user = 0, nice = 0, system = 0, idle = 0, iowait = 0, irq = 0, softirq = 0, steal = 0, guest = 0, guest_nice = 0;
58 id = procfile_lineword(ff, l, 0);
59 user = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
60 nice = strtoull(procfile_lineword(ff, l, 2), NULL, 10);
61 system = strtoull(procfile_lineword(ff, l, 3), NULL, 10);
62 idle = strtoull(procfile_lineword(ff, l, 4), NULL, 10);
63 iowait = strtoull(procfile_lineword(ff, l, 5), NULL, 10);
64 irq = strtoull(procfile_lineword(ff, l, 6), NULL, 10);
65 softirq = strtoull(procfile_lineword(ff, l, 7), NULL, 10);
66 steal = strtoull(procfile_lineword(ff, l, 8), NULL, 10);
67 guest = strtoull(procfile_lineword(ff, l, 9), NULL, 10);
68 if(words >= 11) guest_nice = strtoull(procfile_lineword(ff, l, 10), NULL, 10);
70 char *title = "Core utilization";
71 char *type = RRD_TYPE_STAT;
72 char *context = "cpu.cpu";
73 char *family = "utilization";
77 if(strcmp(id, "cpu") == 0) {
80 title = "Total CPU utilization";
81 context = "system.cpu";
86 if((isthistotal && do_cpu) || (!isthistotal && do_cpu_cores)) {
87 st = rrdset_find_bytype(type, id);
89 st = rrdset_create(type, id, NULL, family, context, title, "percentage", priority, update_every, RRDSET_TYPE_STACKED);
92 long divisor = 1; // sysconf(_SC_CLK_TCK);
94 rrddim_add(st, "guest_nice", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
95 rrddim_add(st, "guest", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
96 rrddim_add(st, "steal", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
97 rrddim_add(st, "softirq", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
98 rrddim_add(st, "irq", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
99 rrddim_add(st, "user", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
100 rrddim_add(st, "system", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
101 rrddim_add(st, "nice", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
102 rrddim_add(st, "iowait", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
104 rrddim_add(st, "idle", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
105 rrddim_hide(st, "idle");
107 else rrdset_next(st);
109 rrddim_set(st, "user", user);
110 rrddim_set(st, "nice", nice);
111 rrddim_set(st, "system", system);
112 rrddim_set(st, "idle", idle);
113 rrddim_set(st, "iowait", iowait);
114 rrddim_set(st, "irq", irq);
115 rrddim_set(st, "softirq", softirq);
116 rrddim_set(st, "steal", steal);
117 rrddim_set(st, "guest", guest);
118 rrddim_set(st, "guest_nice", guest_nice);
122 else if(strcmp(procfile_lineword(ff, l, 0), "intr") == 0) {
123 unsigned long long value = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
125 // --------------------------------------------------------------------
128 st = rrdset_find_bytype("system", "intr");
130 st = rrdset_create("system", "intr", NULL, "interrupts", NULL, "CPU Interrupts", "interrupts/s", 900, update_every, RRDSET_TYPE_LINE);
133 rrddim_add(st, "interrupts", NULL, 1, 1, RRDDIM_INCREMENTAL);
135 else rrdset_next(st);
137 rrddim_set(st, "interrupts", value);
141 else if(strcmp(procfile_lineword(ff, l, 0), "ctxt") == 0) {
142 unsigned long long value = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
144 // --------------------------------------------------------------------
147 st = rrdset_find_bytype("system", "ctxt");
149 st = rrdset_create("system", "ctxt", NULL, "processes", NULL, "CPU Context Switches", "context switches/s", 800, update_every, RRDSET_TYPE_LINE);
151 rrddim_add(st, "switches", NULL, 1, 1, RRDDIM_INCREMENTAL);
153 else rrdset_next(st);
155 rrddim_set(st, "switches", value);
159 else if(!processes && strcmp(procfile_lineword(ff, l, 0), "processes") == 0) {
160 processes = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
162 else if(!running && strcmp(procfile_lineword(ff, l, 0), "procs_running") == 0) {
163 running = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
165 else if(!blocked && strcmp(procfile_lineword(ff, l, 0), "procs_blocked") == 0) {
166 blocked = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
170 // --------------------------------------------------------------------
173 st = rrdset_find_bytype("system", "forks");
175 st = rrdset_create("system", "forks", NULL, "processes", NULL, "Started Processes", "processes/s", 700, update_every, RRDSET_TYPE_LINE);
178 rrddim_add(st, "started", NULL, 1, 1, RRDDIM_INCREMENTAL);
180 else rrdset_next(st);
182 rrddim_set(st, "started", processes);
186 // --------------------------------------------------------------------
189 st = rrdset_find_bytype("system", "processes");
191 st = rrdset_create("system", "processes", NULL, "processes", NULL, "System Processes", "processes", 600, update_every, RRDSET_TYPE_LINE);
193 rrddim_add(st, "running", NULL, 1, 1, RRDDIM_ABSOLUTE);
194 rrddim_add(st, "blocked", NULL, -1, 1, RRDDIM_ABSOLUTE);
196 else rrdset_next(st);
198 rrddim_set(st, "running", running);
199 rrddim_set(st, "blocked", blocked);