]> arthur.barton.de Git - netdata.git/blob - src/proc_stat.c
made /proc/stat guest_nice optional - it is not there on older systems
[netdata.git] / src / proc_stat.c
1 #include <inttypes.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "log.h"
7 #include "config.h"
8 #include "procfile.h"
9 #include "rrd.h"
10 #include "plugin_proc.h"
11
12 #define RRD_TYPE_STAT                           "cpu"
13 #define RRD_TYPE_STAT_LEN                       strlen(RRD_TYPE_STAT)
14
15 int do_proc_stat(int update_every, unsigned long long dt) {
16         static procfile *ff = NULL;
17         static int do_cpu = -1, do_cpu_cores = -1, do_interrupts = -1, do_context = -1, do_forks = -1, do_processes = -1;
18
19         if(do_cpu == -1)                do_cpu                  = config_get_boolean("plugin:proc:/proc/stat", "cpu utilization", 1);
20         if(do_cpu_cores == -1)  do_cpu_cores    = config_get_boolean("plugin:proc:/proc/stat", "per cpu core utilization", 1);
21         if(do_interrupts == -1) do_interrupts   = config_get_boolean("plugin:proc:/proc/stat", "cpu interrupts", 1);
22         if(do_context == -1)    do_context              = config_get_boolean("plugin:proc:/proc/stat", "context switches", 1);
23         if(do_forks == -1)              do_forks                = config_get_boolean("plugin:proc:/proc/stat", "processes started", 1);
24         if(do_processes == -1)  do_processes    = config_get_boolean("plugin:proc:/proc/stat", "processes running", 1);
25
26         if(dt) {};
27
28         if(!ff) ff = procfile_open("/proc/stat", " \t:");
29         if(!ff) return 1;
30
31         ff = procfile_readall(ff);
32         if(!ff) return 0; // we return 0, so that we will retry to open it next time
33
34         uint32_t lines = procfile_lines(ff), l;
35         uint32_t words;
36
37         unsigned long long processes = 0, running = 0 , blocked = 0;
38         RRD_STATS *st;
39
40         for(l = 0; l < lines ;l++) {
41                 if(strncmp(procfile_lineword(ff, l, 0), "cpu", 3) == 0) {
42                         words = procfile_linewords(ff, l);
43                         if(words < 10) {
44                                 error("Cannot read /proc/stat cpu line. Expected 10 params, read %d.", words);
45                                 continue;
46                         }
47
48                         char *id;
49                         unsigned long long user = 0, nice = 0, system = 0, idle = 0, iowait = 0, irq = 0, softirq = 0, steal = 0, guest = 0, guest_nice = 0;
50
51                         id                      = procfile_lineword(ff, l, 0);
52                         user            = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
53                         nice            = strtoull(procfile_lineword(ff, l, 2), NULL, 10);
54                         system          = strtoull(procfile_lineword(ff, l, 3), NULL, 10);
55                         idle            = strtoull(procfile_lineword(ff, l, 4), NULL, 10);
56                         iowait          = strtoull(procfile_lineword(ff, l, 5), NULL, 10);
57                         irq                     = strtoull(procfile_lineword(ff, l, 6), NULL, 10);
58                         softirq         = strtoull(procfile_lineword(ff, l, 7), NULL, 10);
59                         steal           = strtoull(procfile_lineword(ff, l, 8), NULL, 10);
60                         guest           = strtoull(procfile_lineword(ff, l, 9), NULL, 10);
61                         if(words >= 11) guest_nice      = strtoull(procfile_lineword(ff, l, 10), NULL, 10);
62
63                         char *title = "Core utilization";
64                         char *type = RRD_TYPE_STAT;
65                         long priority = 1000;
66                         int isthistotal = 0;
67                         if(strcmp(id, "cpu") == 0) {
68                                 isthistotal = 1;
69                                 title = "Total CPU utilization";
70                                 type = "system";
71                                 priority = 100;
72                         }
73
74                         if((isthistotal && do_cpu) || (!isthistotal && do_cpu_cores)) {
75                                 st = rrd_stats_find_bytype(type, id);
76                                 if(!st) {
77                                         st = rrd_stats_create(type, id, NULL, "cpu", title, "percentage", priority, update_every, CHART_TYPE_STACKED);
78
79                                         long multiplier = 1;
80                                         long divisor = 1; // sysconf(_SC_CLK_TCK);
81
82                                         rrd_stats_dimension_add(st, "guest_nice", NULL, multiplier, divisor, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL);
83                                         rrd_stats_dimension_add(st, "guest", NULL, multiplier, divisor, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL);
84                                         rrd_stats_dimension_add(st, "steal", NULL, multiplier, divisor, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL);
85                                         rrd_stats_dimension_add(st, "softirq", NULL, multiplier, divisor, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL);
86                                         rrd_stats_dimension_add(st, "irq", NULL, multiplier, divisor, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL);
87                                         rrd_stats_dimension_add(st, "user", NULL, multiplier, divisor, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL);
88                                         rrd_stats_dimension_add(st, "system", NULL, multiplier, divisor, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL);
89                                         rrd_stats_dimension_add(st, "nice", NULL, multiplier, divisor, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL);
90                                         rrd_stats_dimension_add(st, "iowait", NULL, multiplier, divisor, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL);
91
92                                         rrd_stats_dimension_add(st, "idle", NULL, multiplier, divisor, RRD_DIMENSION_PCENT_OVER_DIFF_TOTAL);
93                                         rrd_stats_dimension_hide(st, "idle");
94                                 }
95                                 else rrd_stats_next(st);
96
97                                 rrd_stats_dimension_set(st, "user", user);
98                                 rrd_stats_dimension_set(st, "nice", nice);
99                                 rrd_stats_dimension_set(st, "system", system);
100                                 rrd_stats_dimension_set(st, "idle", idle);
101                                 rrd_stats_dimension_set(st, "iowait", iowait);
102                                 rrd_stats_dimension_set(st, "irq", irq);
103                                 rrd_stats_dimension_set(st, "softirq", softirq);
104                                 rrd_stats_dimension_set(st, "steal", steal);
105                                 rrd_stats_dimension_set(st, "guest", guest);
106                                 rrd_stats_dimension_set(st, "guest_nice", guest_nice);
107                                 rrd_stats_done(st);
108                         }
109                 }
110                 else if(strcmp(procfile_lineword(ff, l, 0), "intr") == 0) {
111                         unsigned long long value = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
112
113                         // --------------------------------------------------------------------
114         
115                         if(do_interrupts) {
116                                 st = rrd_stats_find_bytype("system", "intr");
117                                 if(!st) {
118                                         st = rrd_stats_create("system", "intr", NULL, "cpu", "CPU Interrupts", "interrupts/s", 900, update_every, CHART_TYPE_LINE);
119                                         st->isdetail = 1;
120
121                                         rrd_stats_dimension_add(st, "interrupts", NULL, 1, 1 * update_every, RRD_DIMENSION_INCREMENTAL);
122                                 }
123                                 else rrd_stats_next(st);
124
125                                 rrd_stats_dimension_set(st, "interrupts", value);
126                                 rrd_stats_done(st);
127                         }
128                 }
129                 else if(strcmp(procfile_lineword(ff, l, 0), "ctxt") == 0) {
130                         unsigned long long value = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
131
132                         // --------------------------------------------------------------------
133         
134                         if(do_context) {
135                                 st = rrd_stats_find_bytype("system", "ctxt");
136                                 if(!st) {
137                                         st = rrd_stats_create("system", "ctxt", NULL, "cpu", "CPU Context Switches", "context switches/s", 800, update_every, CHART_TYPE_LINE);
138
139                                         rrd_stats_dimension_add(st, "switches", NULL, 1, 1 * update_every, RRD_DIMENSION_INCREMENTAL);
140                                 }
141                                 else rrd_stats_next(st);
142
143                                 rrd_stats_dimension_set(st, "switches", value);
144                                 rrd_stats_done(st);
145                         }
146                 }
147                 else if(!processes && strcmp(procfile_lineword(ff, l, 0), "processes") == 0) {
148                         processes = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
149                 }
150                 else if(!running && strcmp(procfile_lineword(ff, l, 0), "procs_running") == 0) {
151                         running = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
152                 }
153                 else if(!blocked && strcmp(procfile_lineword(ff, l, 0), "procs_blocked") == 0) {
154                         blocked = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
155                 }
156         }
157
158         // --------------------------------------------------------------------
159
160         if(do_forks) {
161                 st = rrd_stats_find_bytype("system", "forks");
162                 if(!st) {
163                         st = rrd_stats_create("system", "forks", NULL, "cpu", "New Processes", "processes/s", 700, update_every, CHART_TYPE_LINE);
164                         st->isdetail = 1;
165
166                         rrd_stats_dimension_add(st, "started", NULL, 1, 1 * update_every, RRD_DIMENSION_INCREMENTAL);
167                 }
168                 else rrd_stats_next(st);
169
170                 rrd_stats_dimension_set(st, "started", processes);
171                 rrd_stats_done(st);
172         }
173
174         // --------------------------------------------------------------------
175
176         if(do_processes) {
177                 st = rrd_stats_find_bytype("system", "processes");
178                 if(!st) {
179                         st = rrd_stats_create("system", "processes", NULL, "cpu", "Processes", "processes", 600, update_every, CHART_TYPE_LINE);
180
181                         rrd_stats_dimension_add(st, "running", NULL, 1, 1, RRD_DIMENSION_ABSOLUTE);
182                         rrd_stats_dimension_add(st, "blocked", NULL, -1, 1, RRD_DIMENSION_ABSOLUTE);
183                 }
184                 else rrd_stats_next(st);
185
186                 rrd_stats_dimension_set(st, "running", running);
187                 rrd_stats_dimension_set(st, "blocked", blocked);
188                 rrd_stats_done(st);
189         }
190
191         return 0;
192 }