123ff5431c9d1b27eb27718d5a612c70243aee41
[collectd.git] / src / conntrack.c
1 /**
2  * collectd - src/conntrack.c
3  * Copyright (C) 2009  Tomasz Pala
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  *   Tomasz Pala <gotar at pld-linux.org>
20  * based on entropy.c by:
21  *   Florian octo Forster <octo at collectd.org>
22  **/
23
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27
28 #if !KERNEL_LINUX
29 # error "No applicable input method."
30 #endif
31
32 #define CONNTRACK_FILE "/proc/sys/net/netfilter/nf_conntrack_count"
33 #define CONNTRACK_MAX_FILE "/proc/sys/net/netfilter/nf_conntrack_max"
34 #define CONNTRACK_FILE_OLD "/proc/sys/net/ipv4/netfilter/ip_conntrack_count"
35 #define CONNTRACK_MAX_FILE_OLD "/proc/sys/net/ipv4/netfilter/ip_conntrack_max"
36
37 static const char *config_keys[] =
38 {
39         "OldFiles"
40 };
41 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
42 /*
43     Each table/chain combo that will be queried goes into this list
44 */
45
46 static int old_files = 0;
47
48 static int conntrack_config(const char *key, const char *value)
49 {
50     if (strcmp(key, "OldFiles") == 0)
51         old_files = 1;
52
53     return 0;
54 }
55
56 static void conntrack_submit (const char *type, const char *type_instance,
57                               value_t conntrack)
58 {
59         value_list_t vl = VALUE_LIST_INIT;
60
61         vl.values = &conntrack;
62         vl.values_len = 1;
63         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
64         sstrncpy (vl.plugin, "conntrack", sizeof (vl.plugin));
65         sstrncpy (vl.type, type, sizeof (vl.type));
66         if (type_instance != NULL)
67                 sstrncpy (vl.type_instance, type_instance,
68                           sizeof (vl.type_instance));
69
70         plugin_dispatch_values (&vl);
71 } /* static void conntrack_submit */
72
73 static int conntrack_read (void)
74 {
75         value_t conntrack, conntrack_max, conntrack_pct;
76         FILE *fh;
77         char buffer[64] = { 0 };
78         size_t buffer_len;
79
80         fh = fopen (old_files?CONNTRACK_FILE_OLD:CONNTRACK_FILE, "r");
81         if (fh == NULL)
82                 return (-1);
83
84         if (fgets (buffer, sizeof (buffer), fh) == NULL)
85         {
86                 fclose (fh);
87                 return (-1);
88         }
89         fclose (fh);
90
91         /* strip trailing newline. */
92         buffer_len = strlen (buffer);
93         while ((buffer_len > 0) && isspace ((int) buffer[buffer_len - 1]))
94         {
95                 buffer[buffer_len - 1] = 0;
96                 buffer_len--;
97         }
98
99         if (parse_value (buffer, &conntrack, DS_TYPE_GAUGE) != 0)
100                 return (-1);
101
102         conntrack_submit ("conntrack", NULL, conntrack);
103
104         fh = fopen (old_files?CONNTRACK_MAX_FILE_OLD:CONNTRACK_MAX_FILE, "r");
105         if (fh == NULL)
106                 return (-1);
107
108         memset (buffer, 0, sizeof (buffer));
109         if (fgets (buffer, sizeof (buffer), fh) == NULL)
110         {
111                 fclose (fh);
112                 return (-1);
113         }
114         fclose (fh);
115
116         /* strip trailing newline. */
117         buffer_len = strlen (buffer);
118         while ((buffer_len > 0) && isspace ((int) buffer[buffer_len - 1]))
119         {
120                 buffer[buffer_len - 1] = 0;
121                 buffer_len--;
122         }
123
124         if (parse_value (buffer, &conntrack_max, DS_TYPE_GAUGE) != 0)
125                 return (-1);
126
127         conntrack_submit ("conntrack", "max", conntrack_max);
128         conntrack_pct.gauge = (conntrack.gauge / conntrack_max.gauge) * 100;
129         conntrack_submit ("percent", "used", conntrack_pct);
130
131
132         return (0);
133 } /* static int conntrack_read */
134
135 void module_register (void)
136 {
137         plugin_register_config ("conntrack", conntrack_config,
138                             config_keys, config_keys_num);
139         plugin_register_read ("conntrack", conntrack_read);
140 } /* void module_register */