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"
28 #if HAVE_LIBIPTC_LIBIPTC_H
29 # include <libiptc/libiptc.h>
32 #if HAVE_LIBIPTC_LIBIPTC_H
33 # define IPTABLES_HAVE_READ 1
35 # define IPTABLES_HAVE_READ 0
38 #define MODULE_NAME "iptables"
42 * (Module-)Global variables
46 * Removed packet count for now, should have config option if you want to save
47 * them Although other collectd models don't seem to care much for options
48 * eitherway for what to log
50 /* Limit to ~125MByte/s (~1GBit/s) */
51 static data_source_t dsrc[1] =
53 {"value", DS_TYPE_COUNTER, 0.0, 134217728.0}
56 static data_set_t ipt_bytes_ds =
61 static data_set_t ipt_packets_ds =
63 "ipt_packets", 1, dsrc
66 #if IPTABLES_HAVE_READ
68 * Config format should be `Chain table chainname',
69 * e. g. `Chain mangle incoming'
71 static const char *config_keys[] =
76 static int config_keys_num = 1;
78 Each table/chain combo that will be queried goes into this list
80 #ifndef XT_TABLE_MAXNAMELEN
81 # define XT_TABLE_MAXNAMELEN 32
84 char table[XT_TABLE_MAXNAMELEN];
85 char chain[XT_TABLE_MAXNAMELEN];
100 static ip_chain_t **chain_list = NULL;
101 static int chain_num = 0;
103 static int iptables_config (const char *key, const char *value)
105 if (strcasecmp (key, "Chain") == 0)
107 ip_chain_t temp, *final, **list;
117 memset (&temp, 0, sizeof (temp));
119 value_copy = strdup (value);
120 if (value_copy == NULL)
123 ERROR ("strdup failed: %s",
124 sstrerror (errno, errbuf, sizeof (errbuf)));
128 /* Chain <table> <chain> [<comment|num> [name]] */
129 fields_num = strsplit (value_copy, fields, 4);
139 table_len = strlen (table);
140 if (table_len >= sizeof(temp.table))
142 ERROR ("Table `%s' too long.", table);
146 strncpy (temp.table, table, table_len);
147 temp.table[table_len] = '\0';
149 chain_len = strlen (chain);
150 if (chain_len >= sizeof(temp.chain))
152 ERROR ("Chain `%s' too long.", chain);
156 strncpy (temp.chain, chain, chain_len);
157 temp.chain[chain_len] = '\0';
161 char *comment = fields[2];
162 int rule = atoi (comment);
166 temp.rule.num = rule;
167 temp.rule_type = RTYPE_NUM;
171 strncpy (temp.rule.comment, comment,
172 sizeof (temp.rule.comment) - 1);
173 temp.rule_type = RTYPE_COMMENT;
178 temp.rule_type = RTYPE_COMMENT_ALL;
182 strncpy (temp.name, fields[3], sizeof (temp.name) - 1);
189 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
193 ERROR ("realloc failed: %s",
194 sstrerror (errno, errbuf, sizeof (errbuf)));
199 final = (ip_chain_t *) malloc( sizeof(temp) );
203 ERROR ("malloc failed: %s",
204 sstrerror (errno, errbuf, sizeof (errbuf)));
207 memcpy (final, &temp, sizeof (temp));
208 chain_list[chain_num] = final;
211 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
219 } /* int iptables_config */
220 #endif /* IPTABLES_HAVE_READ */
222 #if IPTABLES_HAVE_READ
223 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
224 static int submit_match (const struct ipt_entry_match *match,
225 const struct ipt_entry *entry,
226 const ip_chain_t *chain,
231 value_list_t vl = VALUE_LIST_INIT;
233 /* Select the rules to collect */
234 if (chain->rule_type == RTYPE_NUM)
236 if (chain->rule.num != rule_num)
241 if (strcmp (match->u.user.name, "comment") != 0)
243 if ((chain->rule_type == RTYPE_COMMENT)
244 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
250 vl.time = time (NULL);
251 strcpy (vl.host, hostname_g);
252 strcpy (vl.plugin, "iptables");
254 status = snprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
255 "%s-%s", chain->table, chain->chain);
256 if ((status >= sizeof (vl.plugin_instance)) || (status < 1))
259 if (chain->name[0] != '\0')
261 strncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
265 if (chain->rule_type == RTYPE_NUM)
266 snprintf (vl.type_instance, sizeof (vl.type_instance),
267 "%i", chain->rule.num);
269 strncpy (vl.type_instance, (char *) match->data,
270 sizeof (vl.type_instance));
272 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
274 values[0].counter = (counter_t) entry->counters.bcnt;
275 plugin_dispatch_values ("ipt_bytes", &vl);
277 values[0].counter = (counter_t) entry->counters.pcnt;
278 plugin_dispatch_values ("ipt_packets", &vl);
281 } /* void submit_match */
283 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) {
284 const struct ipt_entry *entry;
287 /* Find first rule for chain and use the iterate macro */
288 entry = iptc_first_rule( chain->chain, handle );
291 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
298 if (chain->rule_type == RTYPE_NUM)
300 submit_match (NULL, entry, chain, rule_num);
304 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
307 entry = iptc_next_rule( entry, handle );
309 } /* while (entry) */
313 static int iptables_read (void)
316 static complain_t complaint;
318 /* Init the iptc handle structure and query the correct table */
319 for (i = 0; i < chain_num; i++)
321 iptc_handle_t handle;
324 chain = chain_list[i];
327 DEBUG ("chain == NULL");
331 handle = iptc_init( chain->table );
334 DEBUG ("iptc_init (%s) failed: %s", chain->table, iptc_strerror (errno));
335 plugin_complain (LOG_ERR, &complaint, "iptc_init (%s) failed: %s",
336 chain->table, iptc_strerror (errno));
339 plugin_relief (LOG_INFO, &complaint, "iptc_init (%s) succeeded",
342 submit_chain (&handle, chain);
347 } /* int iptables_read */
349 static int iptables_shutdown (void)
353 for (i = 0; i < chain_num; i++)
354 sfree (chain_list[i]);
358 } /* int iptables_shutdown */
359 #endif /* IPTABLES_HAVE_READ */
361 void module_register (void)
363 plugin_register_data_set (&ipt_bytes_ds);
364 plugin_register_data_set (&ipt_packets_ds);
366 #if IPTABLES_HAVE_READ
367 plugin_register_config ("iptables", iptables_config,
368 config_keys, config_keys_num);
369 plugin_register_read ("iptables", iptables_read);
370 plugin_register_shutdown ("iptables", iptables_shutdown);
378 * vim:shiftwidth=4:softtabstop=4:tabstop=8