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;
74 if(strcmp(id, "cpu") == 0) {
76 title = "Total CPU utilization";
81 if((isthistotal && do_cpu) || (!isthistotal && do_cpu_cores)) {
82 st = rrdset_find_bytype(type, id);
84 st = rrdset_create(type, id, NULL, "cpu", title, "percentage", priority, update_every, RRDSET_TYPE_STACKED);
87 long divisor = 1; // sysconf(_SC_CLK_TCK);
89 rrddim_add(st, "guest_nice", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
90 rrddim_add(st, "guest", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
91 rrddim_add(st, "steal", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
92 rrddim_add(st, "softirq", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
93 rrddim_add(st, "irq", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
94 rrddim_add(st, "user", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
95 rrddim_add(st, "system", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
96 rrddim_add(st, "nice", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
97 rrddim_add(st, "iowait", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
99 rrddim_add(st, "idle", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
100 rrddim_hide(st, "idle");
102 else rrdset_next(st);
104 rrddim_set(st, "user", user);
105 rrddim_set(st, "nice", nice);
106 rrddim_set(st, "system", system);
107 rrddim_set(st, "idle", idle);
108 rrddim_set(st, "iowait", iowait);
109 rrddim_set(st, "irq", irq);
110 rrddim_set(st, "softirq", softirq);
111 rrddim_set(st, "steal", steal);
112 rrddim_set(st, "guest", guest);
113 rrddim_set(st, "guest_nice", guest_nice);
117 else if(strcmp(procfile_lineword(ff, l, 0), "intr") == 0) {
118 unsigned long long value = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
120 // --------------------------------------------------------------------
123 st = rrdset_find_bytype("system", "intr");
125 st = rrdset_create("system", "intr", NULL, "cpu", "CPU Interrupts", "interrupts/s", 900, update_every, RRDSET_TYPE_LINE);
128 rrddim_add(st, "interrupts", NULL, 1, 1 * update_every, RRDDIM_INCREMENTAL);
130 else rrdset_next(st);
132 rrddim_set(st, "interrupts", value);
136 else if(strcmp(procfile_lineword(ff, l, 0), "ctxt") == 0) {
137 unsigned long long value = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
139 // --------------------------------------------------------------------
142 st = rrdset_find_bytype("system", "ctxt");
144 st = rrdset_create("system", "ctxt", NULL, "cpu", "CPU Context Switches", "context switches/s", 800, update_every, RRDSET_TYPE_LINE);
146 rrddim_add(st, "switches", NULL, 1, 1 * update_every, RRDDIM_INCREMENTAL);
148 else rrdset_next(st);
150 rrddim_set(st, "switches", value);
154 else if(!processes && strcmp(procfile_lineword(ff, l, 0), "processes") == 0) {
155 processes = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
157 else if(!running && strcmp(procfile_lineword(ff, l, 0), "procs_running") == 0) {
158 running = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
160 else if(!blocked && strcmp(procfile_lineword(ff, l, 0), "procs_blocked") == 0) {
161 blocked = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
165 // --------------------------------------------------------------------
168 st = rrdset_find_bytype("system", "forks");
170 st = rrdset_create("system", "forks", NULL, "cpu", "New Processes", "processes/s", 700, update_every, RRDSET_TYPE_LINE);
173 rrddim_add(st, "started", NULL, 1, 1 * update_every, RRDDIM_INCREMENTAL);
175 else rrdset_next(st);
177 rrddim_set(st, "started", processes);
181 // --------------------------------------------------------------------
184 st = rrdset_find_bytype("system", "processes");
186 st = rrdset_create("system", "processes", NULL, "cpu", "Processes", "processes", 600, update_every, RRDSET_TYPE_LINE);
188 rrddim_add(st, "running", NULL, 1, 1, RRDDIM_ABSOLUTE);
189 rrddim_add(st, "blocked", NULL, -1, 1, RRDDIM_ABSOLUTE);
191 else rrdset_next(st);
193 rrddim_set(st, "running", running);
194 rrddim_set(st, "blocked", blocked);