eea65baa2864c229cc03d136b9fd8300c283e5e6
[collectd.git] / src / synproxy.c
1 /**
2  * collectd - src/synproxy.c
3  * Copyright (C) 2017 Marek Becka
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Marek Becka <https://github.com/marekbecka>
20  **/
21
22 #include "collectd.h"
23
24 #include "common.h"
25 #include "plugin.h"
26
27 #if !KERNEL_LINUX
28 #error "No applicable input method."
29 #endif
30
31 static const char *synproxy_stat_path = "/proc/net/stat/synproxy";
32
33 static const char *column_names[] = {
34   "entries", "syn_received", "invalid", "valid", "retransmission", "reopened"
35 };
36 static const char *column_types[] = {
37   "current_connections", "synproxy_connections", "synproxy_cookies", 
38   "synproxy_cookies", "synproxy_cookies", "synproxy_connections"
39 };
40
41 static void synproxy_submit(value_t *results) {
42   value_list_t vl = VALUE_LIST_INIT;
43
44   for (unsigned n = 1; n < 6; n++) {
45     vl.values = &results[n];
46     vl.values_len = 1;
47
48     sstrncpy(vl.plugin, "synproxy", sizeof(vl.plugin));
49     sstrncpy(vl.type, column_types[n], sizeof(vl.type));
50     sstrncpy(vl.type_instance, column_names[n], sizeof(vl.type_instance));
51
52     plugin_dispatch_values(&vl);
53   } 
54 }
55
56 static int synproxy_read(void) {
57   FILE *fh;
58   char buf[1024];
59   value_t results[6];
60   int is_header = 1, status = 0;
61
62   fh = fopen(synproxy_stat_path, "r");
63   if (fh == NULL) {
64     ERROR("synproxy plugin: unable to open %s", synproxy_stat_path);
65     return -1;
66   }
67
68   memset(results, 0, sizeof(results));
69
70   while (fgets(buf, sizeof(buf), fh) != NULL) {
71     int numfields;
72     char *fields[6], *endprt;
73
74     if (is_header) {
75       is_header = 0;
76       continue;
77     }
78
79     numfields = strsplit(buf, fields, STATIC_ARRAY_SIZE(fields));
80     if (numfields != 6) {
81       ERROR("synproxy plugin: unexpected number of columns in %s", 
82              synproxy_stat_path);
83       status = -1;
84       break;
85     }
86
87     /* 1st column (entries) is hardcoded to 0 in kernel code */
88     for (unsigned n = 1; n < 6; n++) {
89       char *endptr = NULL;
90       errno = 0;
91
92       results[n].derive += strtoull(fields[n], &endprt, 16);
93       if ((endptr == fields[n]) || errno != 0) {
94         ERROR("synproxy plugin: unable to parse value: %s", fields[n]);
95         status = -1;
96         goto err_close;
97       }
98     }
99   }
100
101 err_close:
102   fclose(fh);
103
104   if (status == 0) {
105     synproxy_submit(results);
106   }
107
108   return status;  
109 }
110
111 void module_register(void) {
112   plugin_register_read("synproxy", synproxy_read);
113 } /* void module_register */
114