10 #include "appconfig.h"
13 #include "plugin_proc.h"
16 #define MAX_INTERRUPTS 256
17 #define MAX_INTERRUPT_CPUS 256
18 #define MAX_INTERRUPT_NAME 50
23 char name[MAX_INTERRUPT_NAME + 1];
24 unsigned long long value[MAX_INTERRUPT_CPUS];
25 unsigned long long total;
28 int do_proc_interrupts(int update_every, unsigned long long dt) {
29 static procfile *ff = NULL;
30 static int cpus = -1, do_per_core = -1;
34 if(do_per_core == -1) do_per_core = config_get_boolean("plugin:proc:/proc/interrupts", "interrupts per core", 1);
37 char filename[FILENAME_MAX + 1];
38 snprintf(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/interrupts");
39 ff = procfile_open(config_get("plugin:proc:/proc/interrupts", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
43 ff = procfile_readall(ff);
44 if(!ff) return 0; // we return 0, so that we will retry to open it next time
46 uint32_t lines = procfile_lines(ff), l;
47 uint32_t words = procfile_linewords(ff, 0), w;
50 error("Cannot read /proc/interrupts, zero lines reported.");
54 // find how many CPUs are there
57 for(w = 0; w < words ; w++) {
58 if(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0)
62 if(cpus > MAX_INTERRUPT_CPUS) cpus = MAX_INTERRUPT_CPUS;
66 error("PLUGIN: PROC_INTERRUPTS: Cannot find the number of CPUs in /proc/interrupts");
70 // allocate the size we need;
71 struct interrupt irrs[lines];
74 // loop through all lines
75 for(l = 1; l < lines ;l++) {
76 struct interrupt *irr = &irrs[l];
80 words = procfile_linewords(ff, l);
83 irr->id = procfile_lineword(ff, l, 0);
84 if(!irr->id || !irr->id[0]) continue;
86 int idlen = strlen(irr->id);
87 if(irr->id[idlen - 1] == ':')
88 irr->id[idlen - 1] = '\0';
91 for(c = 0; c < cpus ;c++) {
92 if((c + 1) < (int)words)
93 irr->value[c] = strtoull(procfile_lineword(ff, l, (uint32_t)(c + 1)), NULL, 10);
97 irr->total += irr->value[c];
100 if(isdigit(irr->id[0]) && (uint32_t)(cpus + 2) < words) {
101 strncpy(irr->name, procfile_lineword(ff, l, words - 1), MAX_INTERRUPT_NAME);
102 irr->name[MAX_INTERRUPT_NAME] = '\0';
103 int nlen = strlen(irr->name);
104 if(nlen < (MAX_INTERRUPT_NAME-1)) {
105 irr->name[nlen] = '_';
106 strncpy(&irr->name[nlen + 1], irr->id, MAX_INTERRUPT_NAME - nlen);
107 irr->name[MAX_INTERRUPT_NAME] = '\0';
111 strncpy(irr->name, irr->id, MAX_INTERRUPT_NAME);
112 irr->name[MAX_INTERRUPT_NAME] = '\0';
120 // --------------------------------------------------------------------
122 st = rrdset_find_bytype("system", "interrupts");
124 st = rrdset_create("system", "interrupts", NULL, "interrupts", NULL, "System interrupts", "interrupts/s", 1000, update_every, RRDSET_TYPE_STACKED);
126 for(l = 0; l < lines ;l++) {
127 if(!irrs[l].used) continue;
128 rrddim_add(st, irrs[l].id, irrs[l].name, 1, 1, RRDDIM_INCREMENTAL);
131 else rrdset_next(st);
133 for(l = 0; l < lines ;l++) {
134 if(!irrs[l].used) continue;
135 rrddim_set(st, irrs[l].id, irrs[l].total);
142 for(c = 0; c < cpus ; c++) {
144 snprintf(id, 256, "cpu%d_interrupts", c);
146 st = rrdset_find_bytype("cpu", id);
148 char name[256], title[256];
149 snprintf(name, 256, "cpu%d_interrupts", c);
150 snprintf(title, 256, "CPU%d Interrupts", c);
151 st = rrdset_create("cpu", id, name, "interrupts", "cpu.interrupts", title, "interrupts/s", 2000 + c, update_every, RRDSET_TYPE_STACKED);
153 for(l = 0; l < lines ;l++) {
154 if(!irrs[l].used) continue;
155 rrddim_add(st, irrs[l].id, irrs[l].name, 1, 1, RRDDIM_INCREMENTAL);
158 else rrdset_next(st);
160 for(l = 0; l < lines ;l++) {
161 if(!irrs[l].used) continue;
162 rrddim_set(st, irrs[l].id, irrs[l].value[c]);