b6aa938ea631f546fdceb221f254e36aaa9f48a1
[collectd.git] / src / iptables.c
1 /**
2  * collectd - src/iptables.c
3  * Copyright (C) 2007 Sjoerd van der Berg
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; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *  Sjoerd van der Berg <harekiet at users.sourceforge.net>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27 #include "utils_debug.h"
28
29 #if HAVE_LIBIPTC_LIBIPTC_H
30 # include <libiptc/libiptc.h>
31 #endif
32
33 #if HAVE_LIBIPTC_LIBIPTC_H
34 # define IPTABLES_HAVE_READ 1
35 #else
36 # define IPTABLES_HAVE_READ 0
37 #endif
38
39 #define MODULE_NAME "iptables"
40 #define BUFSIZE 512
41
42 /*
43  * (Module-)Global variables
44  */
45
46 /*
47  * Removed packet count for now, should have config option if you want to save
48  * them Although other collectd models don't seem to care much for options
49  * eitherway for what to log
50  */
51 /* Limit to ~125MByte/s (~1GBit/s) */
52 static data_source_t dsrc[1] =
53 {
54         {"value",  DS_TYPE_COUNTER, 0.0, 134217728.0}
55 };
56
57 static data_set_t ipt_bytes_ds =
58 {
59         "ipt_bytes", 1, dsrc
60 };
61
62 static data_set_t ipt_packets_ds =
63 {
64         "ipt_packets", 1, dsrc
65 };
66
67 #if IPTABLES_HAVE_READ
68 /*
69  * Config format should be `Chain table chainname',
70  * e. g. `Chain mangle incoming'
71  */
72 static const char *config_keys[] =
73 {
74         "Chain",
75         NULL
76 };
77 static int config_keys_num = 1;
78 /*
79     Each table/chain combo that will be queried goes into this list
80 */
81 #ifndef XT_TABLE_MAXNAMELEN
82 # define XT_TABLE_MAXNAMELEN 32
83 #endif
84 typedef struct {
85     char table[XT_TABLE_MAXNAMELEN];
86     char chain[XT_TABLE_MAXNAMELEN];
87     union
88     {
89         int   num;
90         char *comment;
91     } rule;
92     enum
93     {
94         RTYPE_NUM,
95         RTYPE_COMMENT,
96         RTYPE_COMMENT_ALL
97     } rule_type;
98     char name[64];
99 } ip_chain_t;
100
101 static ip_chain_t **chain_list = NULL;
102 static int chain_num = 0;
103
104 static int iptables_config (const char *key, const char *value)
105 {
106         if (strcasecmp (key, "Chain") == 0)
107         {
108                 ip_chain_t temp, *final, **list;
109                 char *table;
110                 int   table_len;
111                 char *chain;
112                 int   chain_len;
113
114                 char *value_copy;
115                 char *fields[4];
116                 int   fields_num;
117                 
118                 memset (&temp, 0, sizeof (temp));
119
120                 value_copy = strdup (value);
121                 if (value_copy == NULL)
122                 {
123                     ERROR ("strdup failed: %s", strerror (errno));
124                     return (1);
125                 }
126
127                 /* Chain <table> <chain> [<comment|num> [name]] */
128                 fields_num = strsplit (value_copy, fields, 4);
129                 if (fields_num < 2)
130                 {
131                     free (value_copy);
132                     return (1);
133                 }
134
135                 table = fields[0];
136                 chain = fields[1];
137
138                 table_len = strlen (table);
139                 if (table_len >= sizeof(temp.table))
140                 {
141                         ERROR ("Table `%s' too long.", table);
142                         free (value_copy);
143                         return (1);
144                 }
145                 strncpy (temp.table, table, table_len);
146                 temp.table[table_len] = '\0';
147
148                 chain_len = strlen (chain);
149                 if (chain_len >= sizeof(temp.chain))
150                 {
151                         ERROR ("Chain `%s' too long.", chain);
152                         free (value_copy);
153                         return (1);
154                 }
155                 strncpy (temp.chain, chain, chain_len);
156                 temp.chain[chain_len] = '\0'; 
157
158                 if (fields_num >= 3)
159                 {
160                     char *comment = fields[2];
161                     int   rule = atoi (comment);
162
163                     if (rule)
164                     {
165                         temp.rule.num = rule;
166                         temp.rule_type = RTYPE_NUM;
167                     }
168                     else
169                     {
170                         strncpy (temp.rule.comment, comment,
171                                 sizeof (temp.rule.comment) - 1);
172                         temp.rule_type = RTYPE_COMMENT;
173                     }
174                 }
175                 else
176                 {
177                     temp.rule_type = RTYPE_COMMENT_ALL;
178                 }
179
180                 if (fields_num >= 4)
181                     strncpy (temp.name, fields[3], sizeof (temp.name) - 1);
182
183                 free (value_copy);
184                 value_copy = NULL;
185                 table = NULL;
186                 chain = NULL;
187
188                 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
189                 if (list == NULL)
190                 {
191                         ERROR ("realloc failed: %s", strerror (errno));
192                         return (1);
193                 }
194
195                 chain_list = list;
196                 final = (ip_chain_t *) malloc( sizeof(temp) );
197                 if (final == NULL) 
198                 {
199                         ERROR ("malloc failed: %s", strerror (errno));
200                         return (1);
201                 }
202                 memcpy (final, &temp, sizeof (temp));
203                 chain_list[chain_num] = final;
204                 chain_num++;
205
206                 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
207         }
208         else 
209         {
210                 return (-1);
211         }
212
213         return (0);
214 } /* int iptables_config */
215 #endif /* IPTABLES_HAVE_READ */
216
217 #if IPTABLES_HAVE_READ
218 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
219 static int submit_match (const struct ipt_entry_match *match,
220                 const struct ipt_entry *entry,
221                 const ip_chain_t *chain,
222                 int rule_num) 
223 {
224     int status;
225     value_t values[1];
226     value_list_t vl = VALUE_LIST_INIT;
227
228     /* Select the rules to collect */
229     if (chain->rule_type == RTYPE_NUM)
230     {
231         if (chain->rule.num != rule_num)
232             return (0);
233     }
234     else
235     {
236         if (strcmp (match->u.user.name, "comment") != 0)
237             return (0);
238         if ((chain->rule_type == RTYPE_COMMENT)
239                 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
240             return (0);
241     }
242
243     vl.values = values;
244     vl.values_len = 1;
245     vl.time = time (NULL);
246     strcpy (vl.host, hostname_g);
247     strcpy (vl.plugin, "iptables");
248
249     status = snprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
250             "%s-%s", chain->table, chain->chain);
251     if ((status >= sizeof (vl.plugin_instance)) || (status < 1))
252         return (0);
253
254     if (chain->name[0] != '\0')
255     {
256         strncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
257     }
258     else
259     {
260         if (chain->rule_type == RTYPE_NUM)
261             snprintf (vl.type_instance, sizeof (vl.type_instance),
262                     "%i", chain->rule.num);
263         else
264             strncpy (vl.type_instance, (char *) match->data,
265                     sizeof (vl.type_instance));
266     }
267     vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
268
269     values[0].counter = (counter_t) entry->counters.bcnt;
270     plugin_dispatch_values ("ipt_bytes", &vl);
271
272     values[0].counter = (counter_t) entry->counters.pcnt;
273     plugin_dispatch_values ("ipt_packets", &vl);
274
275     return (0);
276 } /* void submit_match */
277
278 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) {
279     const struct ipt_entry *entry;
280     int rule_num;
281
282     /* Find first rule for chain and use the iterate macro */    
283     entry = iptc_first_rule( chain->chain, handle );
284     if (entry == NULL)
285     {
286         DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
287         return;
288     }
289
290     rule_num = 1;
291     while (entry)
292     {
293         if (chain->rule_type == RTYPE_NUM)
294         {
295             submit_match (NULL, entry, chain, rule_num);
296         }
297         else
298         {
299             IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
300         }
301
302         entry = iptc_next_rule( entry, handle );
303         rule_num++;
304     } /* while (entry) */
305 }
306
307
308 static int iptables_read (void)
309 {
310     int i;
311     static complain_t complaint;
312
313     /* Init the iptc handle structure and query the correct table */    
314     for (i = 0; i < chain_num; i++)
315     {
316         iptc_handle_t handle;
317         ip_chain_t *chain;
318         
319         chain = chain_list[i];
320         if (!chain)
321         {
322             DEBUG ("chain == NULL");
323             continue;
324         }
325
326         handle = iptc_init( chain->table );
327         if (!handle)
328         {
329             DEBUG ("iptc_init (%s) failed: %s", chain->table, iptc_strerror (errno));
330             plugin_complain (LOG_ERR, &complaint, "iptc_init (%s) failed: %s",
331                     chain->table, iptc_strerror (errno));
332             continue;
333         }
334         plugin_relief (LOG_INFO, &complaint, "iptc_init (%s) succeeded",
335                 chain->table);
336
337         submit_chain (&handle, chain);
338         iptc_free (&handle);
339     }
340
341     return (0);
342 } /* int iptables_read */
343
344 static int iptables_shutdown (void)
345 {
346     int i;
347
348     for (i = 0; i < chain_num; i++)
349         sfree (chain_list[i]);
350     sfree (chain_list);
351
352     return (0);
353 } /* int iptables_shutdown */
354 #endif /* IPTABLES_HAVE_READ */
355
356 void module_register (void)
357 {
358     plugin_register_data_set (&ipt_bytes_ds);
359     plugin_register_data_set (&ipt_packets_ds);
360
361 #if IPTABLES_HAVE_READ
362     plugin_register_config ("iptables", iptables_config,
363             config_keys, config_keys_num);
364     plugin_register_read ("iptables", iptables_read);
365     plugin_register_shutdown ("iptables", iptables_shutdown);
366 #endif
367 }
368
369 #undef BUFSIZE
370 #undef MODULE_NAME
371
372 /*
373  * vim:shiftwidth=4:softtabstop=4:tabstop=8
374  */