]> arthur.barton.de Git - netdata.git/blob - src/proc_net_stat_conntrack.c
Merge pull request #1165 from kesslerm/fix/python-plugin-crash
[netdata.git] / src / proc_net_stat_conntrack.c
1 #include "common.h"
2
3 #define RRD_TYPE_NET_STAT_NETFILTER     "netfilter"
4 #define RRD_TYPE_NET_STAT_CONNTRACK     "conntrack"
5
6 int do_proc_net_stat_conntrack(int update_every, unsigned long long dt) {
7     (void)dt;
8
9     static procfile *ff = NULL;
10     static int do_sockets = -1, do_new = -1, do_changes = -1, do_expect = -1, do_search = -1, do_errors = -1;
11
12     if(unlikely(do_sockets == -1)) {
13         do_sockets = config_get_boolean("plugin:proc:/proc/net/stat/nf_conntrack", "netfilter connections", 1);
14         do_new = config_get_boolean("plugin:proc:/proc/net/stat/nf_conntrack", "netfilter new connections", 1);
15         do_changes = config_get_boolean("plugin:proc:/proc/net/stat/nf_conntrack", "netfilter connection changes", 1);
16         do_expect = config_get_boolean("plugin:proc:/proc/net/stat/nf_conntrack", "netfilter connection expectations", 1);
17         do_search = config_get_boolean("plugin:proc:/proc/net/stat/nf_conntrack", "netfilter connection searches", 1);
18         do_errors = config_get_boolean("plugin:proc:/proc/net/stat/nf_conntrack", "netfilter errors", 1);
19     }
20
21     if(unlikely(!ff)) {
22         char filename[FILENAME_MAX + 1];
23         snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/net/stat/nf_conntrack");
24         ff = procfile_open(config_get("plugin:proc:/proc/net/stat/nf_conntrack", "filename to monitor", filename), " \t:", PROCFILE_FLAG_DEFAULT);
25         if(unlikely(!ff))
26             return 1;
27     }
28
29     ff = procfile_readall(ff);
30     if(unlikely(!ff))
31         return 0; // we return 0, so that we will retry to open it next time
32
33     uint32_t lines = procfile_lines(ff), l;
34
35     unsigned long long aentries = 0, asearched = 0, afound = 0, anew = 0, ainvalid = 0, aignore = 0, adelete = 0, adelete_list = 0,
36         ainsert = 0, ainsert_failed = 0, adrop = 0, aearly_drop = 0, aicmp_error = 0, aexpect_new = 0, aexpect_create = 0, aexpect_delete = 0, asearch_restart = 0;
37
38     for(l = 1; l < lines ;l++) {
39         uint32_t words = procfile_linewords(ff, l);
40         if(unlikely(words < 17)) {
41             if(unlikely(words)) error("Cannot read /proc/net/stat/nf_conntrack line. Expected 17 params, read %u.", words);
42             continue;
43         }
44
45         unsigned long long tentries = 0, tsearched = 0, tfound = 0, tnew = 0, tinvalid = 0, tignore = 0, tdelete = 0, tdelete_list = 0, tinsert = 0, tinsert_failed = 0, tdrop = 0, tearly_drop = 0, ticmp_error = 0, texpect_new = 0, texpect_create = 0, texpect_delete = 0, tsearch_restart = 0;
46
47         tentries        = strtoull(procfile_lineword(ff, l, 0), NULL, 16);
48         tsearched       = strtoull(procfile_lineword(ff, l, 1), NULL, 16);
49         tfound          = strtoull(procfile_lineword(ff, l, 2), NULL, 16);
50         tnew            = strtoull(procfile_lineword(ff, l, 3), NULL, 16);
51         tinvalid        = strtoull(procfile_lineword(ff, l, 4), NULL, 16);
52         tignore         = strtoull(procfile_lineword(ff, l, 5), NULL, 16);
53         tdelete         = strtoull(procfile_lineword(ff, l, 6), NULL, 16);
54         tdelete_list    = strtoull(procfile_lineword(ff, l, 7), NULL, 16);
55         tinsert         = strtoull(procfile_lineword(ff, l, 8), NULL, 16);
56         tinsert_failed  = strtoull(procfile_lineword(ff, l, 9), NULL, 16);
57         tdrop           = strtoull(procfile_lineword(ff, l, 10), NULL, 16);
58         tearly_drop     = strtoull(procfile_lineword(ff, l, 11), NULL, 16);
59         ticmp_error     = strtoull(procfile_lineword(ff, l, 12), NULL, 16);
60         texpect_new     = strtoull(procfile_lineword(ff, l, 13), NULL, 16);
61         texpect_create  = strtoull(procfile_lineword(ff, l, 14), NULL, 16);
62         texpect_delete  = strtoull(procfile_lineword(ff, l, 15), NULL, 16);
63         tsearch_restart = strtoull(procfile_lineword(ff, l, 16), NULL, 16);
64
65         if(unlikely(!aentries)) aentries =  tentries;
66
67         // sum all the cpus together
68         asearched           += tsearched;       // conntrack.search
69         afound              += tfound;          // conntrack.search
70         anew                += tnew;            // conntrack.new
71         ainvalid            += tinvalid;        // conntrack.new
72         aignore             += tignore;         // conntrack.new
73         adelete             += tdelete;         // conntrack.changes
74         adelete_list        += tdelete_list;    // conntrack.changes
75         ainsert             += tinsert;         // conntrack.changes
76         ainsert_failed      += tinsert_failed;  // conntrack.errors
77         adrop               += tdrop;           // conntrack.errors
78         aearly_drop         += tearly_drop;     // conntrack.errors
79         aicmp_error         += ticmp_error;     // conntrack.errors
80         aexpect_new         += texpect_new;     // conntrack.expect
81         aexpect_create      += texpect_create;  // conntrack.expect
82         aexpect_delete      += texpect_delete;  // conntrack.expect
83         asearch_restart     += tsearch_restart; // conntrack.search
84     }
85
86     RRDSET *st;
87
88     // --------------------------------------------------------------------
89
90     if(do_sockets) {
91         st = rrdset_find(RRD_TYPE_NET_STAT_NETFILTER "." RRD_TYPE_NET_STAT_CONNTRACK "_sockets");
92         if(unlikely(!st)) {
93             st = rrdset_create(RRD_TYPE_NET_STAT_NETFILTER, RRD_TYPE_NET_STAT_CONNTRACK "_sockets", NULL, RRD_TYPE_NET_STAT_CONNTRACK, NULL, "Connection Tracker Connections", "active connections", 3000, update_every, RRDSET_TYPE_LINE);
94
95             rrddim_add(st, "connections", NULL, 1, 1, RRDDIM_ABSOLUTE);
96         }
97         else rrdset_next(st);
98
99         rrddim_set(st, "connections", aentries);
100         rrdset_done(st);
101     }
102
103     // --------------------------------------------------------------------
104
105     if(do_new) {
106         st = rrdset_find(RRD_TYPE_NET_STAT_NETFILTER "." RRD_TYPE_NET_STAT_CONNTRACK "_new");
107         if(unlikely(!st)) {
108             st = rrdset_create(RRD_TYPE_NET_STAT_NETFILTER, RRD_TYPE_NET_STAT_CONNTRACK "_new", NULL, RRD_TYPE_NET_STAT_CONNTRACK, NULL, "Connection Tracker New Connections", "connections/s", 3001, update_every, RRDSET_TYPE_LINE);
109
110             rrddim_add(st, "new", NULL, 1, 1, RRDDIM_INCREMENTAL);
111             rrddim_add(st, "ignore", NULL, -1, 1, RRDDIM_INCREMENTAL);
112             rrddim_add(st, "invalid", NULL, -1, 1, RRDDIM_INCREMENTAL);
113         }
114         else rrdset_next(st);
115
116         rrddim_set(st, "new", anew);
117         rrddim_set(st, "ignore", aignore);
118         rrddim_set(st, "invalid", ainvalid);
119         rrdset_done(st);
120     }
121
122     // --------------------------------------------------------------------
123
124     if(do_changes) {
125         st = rrdset_find(RRD_TYPE_NET_STAT_NETFILTER "." RRD_TYPE_NET_STAT_CONNTRACK "_changes");
126         if(unlikely(!st)) {
127             st = rrdset_create(RRD_TYPE_NET_STAT_NETFILTER, RRD_TYPE_NET_STAT_CONNTRACK "_changes", NULL, RRD_TYPE_NET_STAT_CONNTRACK, NULL, "Connection Tracker Changes", "changes/s", 3002, update_every, RRDSET_TYPE_LINE);
128             st->isdetail = 1;
129
130             rrddim_add(st, "inserted", NULL, 1, 1, RRDDIM_INCREMENTAL);
131             rrddim_add(st, "deleted", NULL, -1, 1, RRDDIM_INCREMENTAL);
132             rrddim_add(st, "delete_list", NULL, -1, 1, RRDDIM_INCREMENTAL);
133         }
134         else rrdset_next(st);
135
136         rrddim_set(st, "inserted", ainsert);
137         rrddim_set(st, "deleted", adelete);
138         rrddim_set(st, "delete_list", adelete_list);
139         rrdset_done(st);
140     }
141
142     // --------------------------------------------------------------------
143
144     if(do_expect) {
145         st = rrdset_find(RRD_TYPE_NET_STAT_NETFILTER "." RRD_TYPE_NET_STAT_CONNTRACK "_expect");
146         if(unlikely(!st)) {
147             st = rrdset_create(RRD_TYPE_NET_STAT_NETFILTER, RRD_TYPE_NET_STAT_CONNTRACK "_expect", NULL, RRD_TYPE_NET_STAT_CONNTRACK, NULL, "Connection Tracker Expectations", "expectations/s", 3003, update_every, RRDSET_TYPE_LINE);
148             st->isdetail = 1;
149
150             rrddim_add(st, "created", NULL, 1, 1, RRDDIM_INCREMENTAL);
151             rrddim_add(st, "deleted", NULL, -1, 1, RRDDIM_INCREMENTAL);
152             rrddim_add(st, "new", NULL, 1, 1, RRDDIM_INCREMENTAL);
153         }
154         else rrdset_next(st);
155
156         rrddim_set(st, "created", aexpect_create);
157         rrddim_set(st, "deleted", aexpect_delete);
158         rrddim_set(st, "new", aexpect_new);
159         rrdset_done(st);
160     }
161
162     // --------------------------------------------------------------------
163
164     if(do_search) {
165         st = rrdset_find(RRD_TYPE_NET_STAT_NETFILTER "." RRD_TYPE_NET_STAT_CONNTRACK "_search");
166         if(unlikely(!st)) {
167             st = rrdset_create(RRD_TYPE_NET_STAT_NETFILTER, RRD_TYPE_NET_STAT_CONNTRACK "_search", NULL, RRD_TYPE_NET_STAT_CONNTRACK, NULL, "Connection Tracker Searches", "searches/s", 3010, update_every, RRDSET_TYPE_LINE);
168             st->isdetail = 1;
169
170             rrddim_add(st, "searched", NULL, 1, 1, RRDDIM_INCREMENTAL);
171             rrddim_add(st, "restarted", NULL, -1, 1, RRDDIM_INCREMENTAL);
172             rrddim_add(st, "found", NULL, 1, 1, RRDDIM_INCREMENTAL);
173         }
174         else rrdset_next(st);
175
176         rrddim_set(st, "searched", asearched);
177         rrddim_set(st, "restarted", asearch_restart);
178         rrddim_set(st, "found", afound);
179         rrdset_done(st);
180     }
181
182     // --------------------------------------------------------------------
183
184     if(do_errors) {
185         st = rrdset_find(RRD_TYPE_NET_STAT_NETFILTER "." RRD_TYPE_NET_STAT_CONNTRACK "_errors");
186         if(unlikely(!st)) {
187             st = rrdset_create(RRD_TYPE_NET_STAT_NETFILTER, RRD_TYPE_NET_STAT_CONNTRACK "_errors", NULL, RRD_TYPE_NET_STAT_CONNTRACK, NULL, "Connection Tracker Errors", "events/s", 3005, update_every, RRDSET_TYPE_LINE);
188             st->isdetail = 1;
189
190             rrddim_add(st, "icmp_error", NULL, 1, 1, RRDDIM_INCREMENTAL);
191             rrddim_add(st, "insert_failed", NULL, -1, 1, RRDDIM_INCREMENTAL);
192             rrddim_add(st, "drop", NULL, -1, 1, RRDDIM_INCREMENTAL);
193             rrddim_add(st, "early_drop", NULL, -1, 1, RRDDIM_INCREMENTAL);
194         }
195         else rrdset_next(st);
196
197         rrddim_set(st, "icmp_error", aicmp_error);
198         rrddim_set(st, "insert_failed", ainsert_failed);
199         rrddim_set(st, "drop", adrop);
200         rrddim_set(st, "early_drop", aearly_drop);
201         rrdset_done(st);
202     }
203
204     return 0;
205 }