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