use generic types in synproxy plugin and code refactoring
[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 #define SYNPROXY_FIELDS 6
32
33 static const char *synproxy_stat_path = "/proc/net/stat/synproxy";
34
35 static const char *column_names[SYNPROXY_FIELDS] = {
36   "entries", "syn_received", "invalid", "valid", "retransmission", "reopened"
37 };
38 static const char *column_types[SYNPROXY_FIELDS] = {
39   "current_connections", "connections", "cookies", "cookies", "cookies", 
40   "connections"
41 };
42
43 static void synproxy_submit(value_t *results) {
44   value_list_t vl = VALUE_LIST_INIT;
45
46   /* 1st column (entries) is hardcoded to 0 in kernel code */
47   for (size_t n = 1; n < SYNPROXY_FIELDS; n++) {
48     vl.values = &results[n];
49     vl.values_len = 1;
50
51     sstrncpy(vl.plugin, "synproxy", sizeof(vl.plugin));
52     sstrncpy(vl.type, column_types[n], sizeof(vl.type));
53     sstrncpy(vl.type_instance, column_names[n], sizeof(vl.type_instance));
54
55     plugin_dispatch_values(&vl);
56   } 
57 }
58
59 static int synproxy_read(void) {
60   char buf[1024];
61   value_t results[SYNPROXY_FIELDS];
62   int is_header = 1, status = 0;
63
64   FILE *fh = fopen(synproxy_stat_path, "r");
65   if (fh == NULL) {
66     ERROR("synproxy plugin: unable to open %s", synproxy_stat_path);
67     return -1;
68   }
69
70   memset(results, 0, sizeof(results));
71
72   while (fgets(buf, sizeof(buf), fh) != NULL) {
73     char *fields[SYNPROXY_FIELDS], *endprt;
74
75     if (is_header) {
76       is_header = 0;
77       continue;
78     }
79
80     int numfields = strsplit(buf, fields, STATIC_ARRAY_SIZE(fields));
81     if (numfields != SYNPROXY_FIELDS) {
82       ERROR("synproxy plugin: unexpected number of columns in %s", 
83              synproxy_stat_path);
84       status = -1;
85       break;
86     }
87
88     /* 1st column (entries) is hardcoded to 0 in kernel code */
89     for (size_t n = 1; n < SYNPROXY_FIELDS; n++) {
90       char *endptr = NULL;
91       errno = 0;
92
93       results[n].derive += strtoull(fields[n], &endprt, 16);
94       if ((endptr == fields[n]) || errno != 0) {
95         ERROR("synproxy plugin: unable to parse value: %s", fields[n]);
96         fclose(fh);
97         return -1;
98       }
99     }
100   }
101
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