2 * collectd - src/iptables.c
3 * Copyright (C) 2007 Sjoerd van der Berg
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.
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.
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
20 * Sjoerd van der Berg <harekiet at users.sourceforge.net>
26 #include "configfile.h"
27 #include "utils_debug.h"
29 #if HAVE_LIBIPTC_LIBIPTC_H
30 # include <libiptc/libiptc.h>
33 #if HAVE_LIBIPTC_LIBIPTC_H
34 # define IPTABLES_HAVE_READ 1
36 # define IPTABLES_HAVE_READ 0
39 #define MODULE_NAME "iptables"
43 * (Module-)Global variables
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
51 /* Limit to ~125MByte/s (~1GBit/s) */
52 static data_source_t dsrc[1] =
54 {"value", DS_TYPE_COUNTER, 0.0, 134217728.0}
57 static data_set_t ipt_bytes_ds =
62 static data_set_t ipt_packets_ds =
64 "ipt_packets", 1, dsrc
67 #if IPTABLES_HAVE_READ
69 * Config format should be `Chain table chainname',
70 * e. g. `Chain mangle incoming'
72 static const char *config_keys[] =
77 static int config_keys_num = 1;
79 Each table/chain combo that will be queried goes into this list
81 #ifndef XT_TABLE_MAXNAMELEN
82 # define XT_TABLE_MAXNAMELEN 32
85 char table[XT_TABLE_MAXNAMELEN];
86 char chain[XT_TABLE_MAXNAMELEN];
101 static ip_chain_t **chain_list = NULL;
102 static int chain_num = 0;
104 static int iptables_config (const char *key, const char *value)
106 if (strcasecmp (key, "Chain") == 0)
108 ip_chain_t temp, *final, **list;
118 memset (&temp, 0, sizeof (temp));
120 value_copy = strdup (value);
121 if (value_copy == NULL)
123 ERROR ("strdup failed: %s", strerror (errno));
127 /* Chain <table> <chain> [<comment|num> [name]] */
128 fields_num = strsplit (value_copy, fields, 4);
138 table_len = strlen (table);
139 if (table_len >= sizeof(temp.table))
141 ERROR ("Table `%s' too long.", table);
145 strncpy (temp.table, table, table_len);
146 temp.table[table_len] = '\0';
148 chain_len = strlen (chain);
149 if (chain_len >= sizeof(temp.chain))
151 ERROR ("Chain `%s' too long.", chain);
155 strncpy (temp.chain, chain, chain_len);
156 temp.chain[chain_len] = '\0';
160 char *comment = fields[2];
161 int rule = atoi (comment);
165 temp.rule.num = rule;
166 temp.rule_type = RTYPE_NUM;
170 strncpy (temp.rule.comment, comment,
171 sizeof (temp.rule.comment) - 1);
172 temp.rule_type = RTYPE_COMMENT;
177 temp.rule_type = RTYPE_COMMENT_ALL;
181 strncpy (temp.name, fields[3], sizeof (temp.name) - 1);
188 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
191 ERROR ("realloc failed: %s", strerror (errno));
196 final = (ip_chain_t *) malloc( sizeof(temp) );
199 ERROR ("malloc failed: %s", strerror (errno));
202 memcpy (final, &temp, sizeof (temp));
203 chain_list[chain_num] = final;
206 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
214 } /* int iptables_config */
215 #endif /* IPTABLES_HAVE_READ */
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,
226 value_list_t vl = VALUE_LIST_INIT;
228 /* Select the rules to collect */
229 if (chain->rule_type == RTYPE_NUM)
231 if (chain->rule.num != rule_num)
236 if (strcmp (match->u.user.name, "comment") != 0)
238 if ((chain->rule_type == RTYPE_COMMENT)
239 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
245 vl.time = time (NULL);
246 strcpy (vl.host, hostname_g);
247 strcpy (vl.plugin, "iptables");
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))
254 if (chain->name[0] != '\0')
256 strncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
260 if (chain->rule_type == RTYPE_NUM)
261 snprintf (vl.type_instance, sizeof (vl.type_instance),
262 "%i", chain->rule.num);
264 strncpy (vl.type_instance, (char *) match->data,
265 sizeof (vl.type_instance));
267 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
269 values[0].counter = (counter_t) entry->counters.bcnt;
270 plugin_dispatch_values ("ipt_bytes", &vl);
272 values[0].counter = (counter_t) entry->counters.pcnt;
273 plugin_dispatch_values ("ipt_packets", &vl);
276 } /* void submit_match */
278 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) {
279 const struct ipt_entry *entry;
282 /* Find first rule for chain and use the iterate macro */
283 entry = iptc_first_rule( chain->chain, handle );
286 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
293 if (chain->rule_type == RTYPE_NUM)
295 submit_match (NULL, entry, chain, rule_num);
299 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
302 entry = iptc_next_rule( entry, handle );
304 } /* while (entry) */
308 static int iptables_read (void)
311 static complain_t complaint;
313 /* Init the iptc handle structure and query the correct table */
314 for (i = 0; i < chain_num; i++)
316 iptc_handle_t handle;
319 chain = chain_list[i];
322 DEBUG ("chain == NULL");
326 handle = iptc_init( chain->table );
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));
334 plugin_relief (LOG_INFO, &complaint, "iptc_init (%s) succeeded",
337 submit_chain (&handle, chain);
342 } /* int iptables_read */
344 static int iptables_shutdown (void)
348 for (i = 0; i < chain_num; i++)
349 sfree (chain_list[i]);
353 } /* int iptables_shutdown */
354 #endif /* IPTABLES_HAVE_READ */
356 void module_register (void)
358 plugin_register_data_set (&ipt_bytes_ds);
359 plugin_register_data_set (&ipt_packets_ds);
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);
373 * vim:shiftwidth=4:softtabstop=4:tabstop=8