11 #include "plugin_proc.h"
13 #define RRD_TYPE_STAT "cpu"
14 #define RRD_TYPE_STAT_LEN strlen(RRD_TYPE_STAT)
16 int do_proc_stat(int update_every, unsigned long long dt) {
17 static procfile *ff = NULL;
18 static int do_cpu = -1, do_cpu_cores = -1, do_interrupts = -1, do_context = -1, do_forks = -1, do_processes = -1;
20 if(do_cpu == -1) do_cpu = config_get_boolean("plugin:proc:/proc/stat", "cpu utilization", 1);
21 if(do_cpu_cores == -1) do_cpu_cores = config_get_boolean("plugin:proc:/proc/stat", "per cpu core utilization", 1);
22 if(do_interrupts == -1) do_interrupts = config_get_boolean("plugin:proc:/proc/stat", "cpu interrupts", 1);
23 if(do_context == -1) do_context = config_get_boolean("plugin:proc:/proc/stat", "context switches", 1);
24 if(do_forks == -1) do_forks = config_get_boolean("plugin:proc:/proc/stat", "processes started", 1);
25 if(do_processes == -1) do_processes = config_get_boolean("plugin:proc:/proc/stat", "processes running", 1);
30 char filename[FILENAME_MAX + 1];
31 snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/stat");
32 ff = procfile_open(config_get("plugin:proc:/proc/stat", "filename to monitor", filename), " \t:", PROCFILE_FLAG_DEFAULT);
36 ff = procfile_readall(ff);
37 if(!ff) return 0; // we return 0, so that we will retry to open it next time
39 uint32_t lines = procfile_lines(ff), l;
42 unsigned long long processes = 0, running = 0 , blocked = 0;
45 for(l = 0; l < lines ;l++) {
46 if(strncmp(procfile_lineword(ff, l, 0), "cpu", 3) == 0) {
47 words = procfile_linewords(ff, l);
49 error("Cannot read /proc/stat cpu line. Expected 10 params, read %d.", words);
54 unsigned long long user = 0, nice = 0, system = 0, idle = 0, iowait = 0, irq = 0, softirq = 0, steal = 0, guest = 0, guest_nice = 0;
56 id = procfile_lineword(ff, l, 0);
57 user = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
58 nice = strtoull(procfile_lineword(ff, l, 2), NULL, 10);
59 system = strtoull(procfile_lineword(ff, l, 3), NULL, 10);
60 idle = strtoull(procfile_lineword(ff, l, 4), NULL, 10);
61 iowait = strtoull(procfile_lineword(ff, l, 5), NULL, 10);
62 irq = strtoull(procfile_lineword(ff, l, 6), NULL, 10);
63 softirq = strtoull(procfile_lineword(ff, l, 7), NULL, 10);
64 steal = strtoull(procfile_lineword(ff, l, 8), NULL, 10);
65 guest = strtoull(procfile_lineword(ff, l, 9), NULL, 10);
66 if(words >= 11) guest_nice = strtoull(procfile_lineword(ff, l, 10), NULL, 10);
68 char *title = "Core utilization";
69 char *type = RRD_TYPE_STAT;
72 if(strcmp(id, "cpu") == 0) {
74 title = "Total CPU utilization";
79 if((isthistotal && do_cpu) || (!isthistotal && do_cpu_cores)) {
80 st = rrdset_find_bytype(type, id);
82 st = rrdset_create(type, id, NULL, "cpu", title, "percentage", priority, update_every, RRDSET_TYPE_STACKED);
85 long divisor = 1; // sysconf(_SC_CLK_TCK);
87 rrddim_add(st, "guest_nice", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
88 rrddim_add(st, "guest", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
89 rrddim_add(st, "steal", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
90 rrddim_add(st, "softirq", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
91 rrddim_add(st, "irq", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
92 rrddim_add(st, "user", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
93 rrddim_add(st, "system", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
94 rrddim_add(st, "nice", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
95 rrddim_add(st, "iowait", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
97 rrddim_add(st, "idle", NULL, multiplier, divisor, RRDDIM_PCENT_OVER_DIFF_TOTAL);
98 rrddim_hide(st, "idle");
100 else rrdset_next(st);
102 rrddim_set(st, "user", user);
103 rrddim_set(st, "nice", nice);
104 rrddim_set(st, "system", system);
105 rrddim_set(st, "idle", idle);
106 rrddim_set(st, "iowait", iowait);
107 rrddim_set(st, "irq", irq);
108 rrddim_set(st, "softirq", softirq);
109 rrddim_set(st, "steal", steal);
110 rrddim_set(st, "guest", guest);
111 rrddim_set(st, "guest_nice", guest_nice);
115 else if(strcmp(procfile_lineword(ff, l, 0), "intr") == 0) {
116 unsigned long long value = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
118 // --------------------------------------------------------------------
121 st = rrdset_find_bytype("system", "intr");
123 st = rrdset_create("system", "intr", NULL, "cpu", "CPU Interrupts", "interrupts/s", 900, update_every, RRDSET_TYPE_LINE);
126 rrddim_add(st, "interrupts", NULL, 1, 1 * update_every, RRDDIM_INCREMENTAL);
128 else rrdset_next(st);
130 rrddim_set(st, "interrupts", value);
134 else if(strcmp(procfile_lineword(ff, l, 0), "ctxt") == 0) {
135 unsigned long long value = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
137 // --------------------------------------------------------------------
140 st = rrdset_find_bytype("system", "ctxt");
142 st = rrdset_create("system", "ctxt", NULL, "cpu", "CPU Context Switches", "context switches/s", 800, update_every, RRDSET_TYPE_LINE);
144 rrddim_add(st, "switches", NULL, 1, 1 * update_every, RRDDIM_INCREMENTAL);
146 else rrdset_next(st);
148 rrddim_set(st, "switches", value);
152 else if(!processes && strcmp(procfile_lineword(ff, l, 0), "processes") == 0) {
153 processes = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
155 else if(!running && strcmp(procfile_lineword(ff, l, 0), "procs_running") == 0) {
156 running = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
158 else if(!blocked && strcmp(procfile_lineword(ff, l, 0), "procs_blocked") == 0) {
159 blocked = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
163 // --------------------------------------------------------------------
166 st = rrdset_find_bytype("system", "forks");
168 st = rrdset_create("system", "forks", NULL, "cpu", "New Processes", "processes/s", 700, update_every, RRDSET_TYPE_LINE);
171 rrddim_add(st, "started", NULL, 1, 1 * update_every, RRDDIM_INCREMENTAL);
173 else rrdset_next(st);
175 rrddim_set(st, "started", processes);
179 // --------------------------------------------------------------------
182 st = rrdset_find_bytype("system", "processes");
184 st = rrdset_create("system", "processes", NULL, "cpu", "Processes", "processes", 600, update_every, RRDSET_TYPE_LINE);
186 rrddim_add(st, "running", NULL, 1, 1, RRDDIM_ABSOLUTE);
187 rrddim_add(st, "blocked", NULL, -1, 1, RRDDIM_ABSOLUTE);
189 else rrdset_next(st);
191 rrddim_set(st, "running", running);
192 rrddim_set(st, "blocked", blocked);