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 #if IPTABLES_HAVE_READ
52 * Config format should be `Chain table chainname',
53 * e. g. `Chain mangle incoming'
55 static const char *config_keys[] =
60 static int config_keys_num = 1;
62 Each table/chain combo that will be queried goes into this list
64 #ifndef XT_TABLE_MAXNAMELEN
65 # define XT_TABLE_MAXNAMELEN 32
68 char table[XT_TABLE_MAXNAMELEN];
69 char chain[XT_TABLE_MAXNAMELEN];
84 static ip_chain_t **chain_list = NULL;
85 static int chain_num = 0;
87 static int iptables_config (const char *key, const char *value)
89 if (strcasecmp (key, "Chain") == 0)
91 ip_chain_t temp, *final, **list;
101 memset (&temp, 0, sizeof (temp));
103 value_copy = strdup (value);
104 if (value_copy == NULL)
107 ERROR ("strdup failed: %s",
108 sstrerror (errno, errbuf, sizeof (errbuf)));
112 /* Chain <table> <chain> [<comment|num> [name]] */
113 fields_num = strsplit (value_copy, fields, 4);
123 table_len = strlen (table);
124 if (table_len >= sizeof(temp.table))
126 ERROR ("Table `%s' too long.", table);
130 strncpy (temp.table, table, table_len);
131 temp.table[table_len] = '\0';
133 chain_len = strlen (chain);
134 if (chain_len >= sizeof(temp.chain))
136 ERROR ("Chain `%s' too long.", chain);
140 strncpy (temp.chain, chain, chain_len);
141 temp.chain[chain_len] = '\0';
145 char *comment = fields[2];
146 int rule = atoi (comment);
150 temp.rule.num = rule;
151 temp.rule_type = RTYPE_NUM;
155 strncpy (temp.rule.comment, comment,
156 sizeof (temp.rule.comment) - 1);
157 temp.rule_type = RTYPE_COMMENT;
162 temp.rule_type = RTYPE_COMMENT_ALL;
166 strncpy (temp.name, fields[3], sizeof (temp.name) - 1);
173 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
177 ERROR ("realloc failed: %s",
178 sstrerror (errno, errbuf, sizeof (errbuf)));
183 final = (ip_chain_t *) malloc( sizeof(temp) );
187 ERROR ("malloc failed: %s",
188 sstrerror (errno, errbuf, sizeof (errbuf)));
191 memcpy (final, &temp, sizeof (temp));
192 chain_list[chain_num] = final;
195 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
203 } /* int iptables_config */
204 #endif /* IPTABLES_HAVE_READ */
206 #if IPTABLES_HAVE_READ
207 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
208 static int submit_match (const struct ipt_entry_match *match,
209 const struct ipt_entry *entry,
210 const ip_chain_t *chain,
215 value_list_t vl = VALUE_LIST_INIT;
217 /* Select the rules to collect */
218 if (chain->rule_type == RTYPE_NUM)
220 if (chain->rule.num != rule_num)
225 if (strcmp (match->u.user.name, "comment") != 0)
227 if ((chain->rule_type == RTYPE_COMMENT)
228 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
234 vl.time = time (NULL);
235 strcpy (vl.host, hostname_g);
236 strcpy (vl.plugin, "iptables");
238 status = snprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
239 "%s-%s", chain->table, chain->chain);
240 if ((status >= sizeof (vl.plugin_instance)) || (status < 1))
243 if (chain->name[0] != '\0')
245 strncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
249 if (chain->rule_type == RTYPE_NUM)
250 snprintf (vl.type_instance, sizeof (vl.type_instance),
251 "%i", chain->rule.num);
253 strncpy (vl.type_instance, (char *) match->data,
254 sizeof (vl.type_instance));
256 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
258 values[0].counter = (counter_t) entry->counters.bcnt;
259 plugin_dispatch_values ("ipt_bytes", &vl);
261 values[0].counter = (counter_t) entry->counters.pcnt;
262 plugin_dispatch_values ("ipt_packets", &vl);
265 } /* void submit_match */
267 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) {
268 const struct ipt_entry *entry;
271 /* Find first rule for chain and use the iterate macro */
272 entry = iptc_first_rule( chain->chain, handle );
275 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
282 if (chain->rule_type == RTYPE_NUM)
284 submit_match (NULL, entry, chain, rule_num);
288 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
291 entry = iptc_next_rule( entry, handle );
293 } /* while (entry) */
297 static int iptables_read (void)
300 static complain_t complaint;
302 /* Init the iptc handle structure and query the correct table */
303 for (i = 0; i < chain_num; i++)
305 iptc_handle_t handle;
308 chain = chain_list[i];
311 DEBUG ("chain == NULL");
315 handle = iptc_init( chain->table );
318 DEBUG ("iptc_init (%s) failed: %s", chain->table, iptc_strerror (errno));
319 plugin_complain (LOG_ERR, &complaint, "iptc_init (%s) failed: %s",
320 chain->table, iptc_strerror (errno));
323 plugin_relief (LOG_INFO, &complaint, "iptc_init (%s) succeeded",
326 submit_chain (&handle, chain);
331 } /* int iptables_read */
333 static int iptables_shutdown (void)
337 for (i = 0; i < chain_num; i++)
339 sfree (chain_list[i]);
344 } /* int iptables_shutdown */
345 #endif /* IPTABLES_HAVE_READ */
347 void module_register (void)
349 #if IPTABLES_HAVE_READ
350 plugin_register_config ("iptables", iptables_config,
351 config_keys, config_keys_num);
352 plugin_register_read ("iptables", iptables_read);
353 plugin_register_shutdown ("iptables", iptables_shutdown);
355 } /* void module_register */
361 * vim:shiftwidth=4:softtabstop=4:tabstop=8