]> arthur.barton.de Git - netdata.git/blob - src/proc_net_stat_synproxy.c
Merge pull request #528 from paulfantom/master
[netdata.git] / src / proc_net_stat_synproxy.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 #include "common.h"
8 #include "appconfig.h"
9 #include "procfile.h"
10 #include "rrd.h"
11 #include "plugin_proc.h"
12 #include "log.h"
13
14 #define RRD_TYPE_NET_STAT_NETFILTER                     "netfilter"
15 #define RRD_TYPE_NET_STAT_SYNPROXY                      "synproxy"
16 #define RRD_TYPE_NET_STAT_SYNPROXY_LEN          strlen(RRD_TYPE_NET_STAT_SYNPROXY)
17
18 int do_proc_net_stat_synproxy(int update_every, unsigned long long dt) {
19         static int do_entries = -1, do_cookies = -1, do_syns = -1, do_reopened = -1;
20         static procfile *ff = NULL;
21
22         if(do_entries == -1)    do_entries      = config_get_boolean_ondemand("plugin:proc:/proc/net/stat/synproxy", "SYNPROXY entries", CONFIG_ONDEMAND_ONDEMAND);
23         if(do_cookies == -1)    do_cookies      = config_get_boolean_ondemand("plugin:proc:/proc/net/stat/synproxy", "SYNPROXY cookies", CONFIG_ONDEMAND_ONDEMAND);
24         if(do_syns == -1)               do_syns         = config_get_boolean_ondemand("plugin:proc:/proc/net/stat/synproxy", "SYNPROXY SYN received", CONFIG_ONDEMAND_ONDEMAND);
25         if(do_reopened == -1)   do_reopened = config_get_boolean_ondemand("plugin:proc:/proc/net/stat/synproxy", "SYNPROXY connections reopened", CONFIG_ONDEMAND_ONDEMAND);
26
27         if(dt) {};
28
29         if(!ff) {
30                 char filename[FILENAME_MAX + 1];
31                 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/net/stat/synproxy");
32                 ff = procfile_open(config_get("plugin:proc:/proc/net/stat/synproxy", "filename to monitor", filename), " \t,:|", PROCFILE_FLAG_DEFAULT);
33         }
34         if(!ff) return 1;
35
36         ff = procfile_readall(ff);
37         if(!ff) return 0; // we return 0, so that we will retry to open it next time
38
39         // make sure we have 3 lines
40         size_t lines = procfile_lines(ff), l;
41         if(lines < 2) {
42                 error("/proc/net/stat/synproxy has %zu lines, expected no less than 2. Disabling it.", lines);
43                 return 1;
44         }
45
46         unsigned long long entries = 0, syn_received = 0, cookie_invalid = 0, cookie_valid = 0, cookie_retrans = 0, conn_reopened = 0;
47
48         // synproxy gives its values per CPU
49         for(l = 1; l < lines ;l++) {
50                 int words = procfile_linewords(ff, l);
51                 if(words < 6) continue;
52
53                 entries                 += strtoull(procfile_lineword(ff, l, 0), NULL, 16);
54                 syn_received    += strtoull(procfile_lineword(ff, l, 1), NULL, 16);
55                 cookie_invalid  += strtoull(procfile_lineword(ff, l, 2), NULL, 16);
56                 cookie_valid    += strtoull(procfile_lineword(ff, l, 3), NULL, 16);
57                 cookie_retrans  += strtoull(procfile_lineword(ff, l, 4), NULL, 16);
58                 conn_reopened   += strtoull(procfile_lineword(ff, l, 5), NULL, 16);
59         }
60
61         unsigned long long events = entries + syn_received + cookie_invalid + cookie_valid + cookie_retrans + conn_reopened;
62
63         RRDSET *st;
64
65         // --------------------------------------------------------------------
66
67         if((do_entries == CONFIG_ONDEMAND_ONDEMAND && events) || do_entries == CONFIG_ONDEMAND_YES) {
68                 do_entries = CONFIG_ONDEMAND_YES;
69
70                 st = rrdset_find(RRD_TYPE_NET_STAT_NETFILTER "." RRD_TYPE_NET_STAT_SYNPROXY "_entries");
71                 if(!st) {
72                         st = rrdset_create(RRD_TYPE_NET_STAT_NETFILTER, RRD_TYPE_NET_STAT_SYNPROXY "_entries", NULL, RRD_TYPE_NET_STAT_SYNPROXY, NULL, "SYNPROXY Entries Used", "entries", 1004, update_every, RRDSET_TYPE_LINE);
73
74                         rrddim_add(st, "entries", NULL, 1, 1, RRDDIM_ABSOLUTE);
75                 }
76                 else rrdset_next(st);
77
78                 rrddim_set(st, "entries", entries);
79                 rrdset_done(st);
80         }
81
82         // --------------------------------------------------------------------
83
84         if((do_syns == CONFIG_ONDEMAND_ONDEMAND && events) || do_syns == CONFIG_ONDEMAND_YES) {
85                 do_syns = CONFIG_ONDEMAND_YES;
86
87                 st = rrdset_find(RRD_TYPE_NET_STAT_NETFILTER "." RRD_TYPE_NET_STAT_SYNPROXY "_syn_received");
88                 if(!st) {
89                         st = rrdset_create(RRD_TYPE_NET_STAT_NETFILTER, RRD_TYPE_NET_STAT_SYNPROXY "_syn_received", NULL, RRD_TYPE_NET_STAT_SYNPROXY, NULL, "SYNPROXY SYN Packets received", "SYN/s", 1001, update_every, RRDSET_TYPE_LINE);
90
91                         rrddim_add(st, "received", NULL, 1, 1, RRDDIM_INCREMENTAL);
92                 }
93                 else rrdset_next(st);
94
95                 rrddim_set(st, "received", syn_received);
96                 rrdset_done(st);
97         }
98
99         // --------------------------------------------------------------------
100
101         if((do_reopened == CONFIG_ONDEMAND_ONDEMAND && events) || do_reopened == CONFIG_ONDEMAND_YES) {
102                 do_reopened = CONFIG_ONDEMAND_YES;
103
104                 st = rrdset_find(RRD_TYPE_NET_STAT_NETFILTER "." RRD_TYPE_NET_STAT_SYNPROXY "_conn_reopened");
105                 if(!st) {
106                         st = rrdset_create(RRD_TYPE_NET_STAT_NETFILTER, RRD_TYPE_NET_STAT_SYNPROXY "_conn_reopened", NULL, RRD_TYPE_NET_STAT_SYNPROXY, NULL, "SYNPROXY Connections Reopened", "connections/s", 1003, update_every, RRDSET_TYPE_LINE);
107
108                         rrddim_add(st, "reopened", NULL, 1, 1, RRDDIM_INCREMENTAL);
109                 }
110                 else rrdset_next(st);
111
112                 rrddim_set(st, "reopened", conn_reopened);
113                 rrdset_done(st);
114         }
115
116         // --------------------------------------------------------------------
117
118         if((do_cookies == CONFIG_ONDEMAND_ONDEMAND && events) || do_cookies == CONFIG_ONDEMAND_YES) {
119                 do_cookies = CONFIG_ONDEMAND_YES;
120
121                 st = rrdset_find(RRD_TYPE_NET_STAT_NETFILTER "." RRD_TYPE_NET_STAT_SYNPROXY "_cookies");
122                 if(!st) {
123                         st = rrdset_create(RRD_TYPE_NET_STAT_NETFILTER, RRD_TYPE_NET_STAT_SYNPROXY "_cookies", NULL, RRD_TYPE_NET_STAT_SYNPROXY, NULL, "SYNPROXY TCP Cookies", "cookies/s", 1002, update_every, RRDSET_TYPE_LINE);
124
125                         rrddim_add(st, "valid", NULL, 1, 1, RRDDIM_INCREMENTAL);
126                         rrddim_add(st, "invalid", NULL, -1, 1, RRDDIM_INCREMENTAL);
127                         rrddim_add(st, "retransmits", NULL, 1, 1, RRDDIM_INCREMENTAL);
128                 }
129                 else rrdset_next(st);
130
131                 rrddim_set(st, "valid", cookie_valid);
132                 rrddim_set(st, "invalid", cookie_invalid);
133                 rrddim_set(st, "retransmits", cookie_retrans);
134                 rrdset_done(st);
135         }
136
137         return 0;
138 }