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 temp.rule.comment = strdup (comment);
156 if (temp.rule.comment == NULL)
161 temp.rule_type = RTYPE_COMMENT;
166 temp.rule_type = RTYPE_COMMENT_ALL;
170 strncpy (temp.name, fields[3], sizeof (temp.name) - 1);
177 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
181 ERROR ("realloc failed: %s",
182 sstrerror (errno, errbuf, sizeof (errbuf)));
187 final = (ip_chain_t *) malloc( sizeof(temp) );
191 ERROR ("malloc failed: %s",
192 sstrerror (errno, errbuf, sizeof (errbuf)));
195 memcpy (final, &temp, sizeof (temp));
196 chain_list[chain_num] = final;
199 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
207 } /* int iptables_config */
208 #endif /* IPTABLES_HAVE_READ */
210 #if IPTABLES_HAVE_READ
211 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
212 static int submit_match (const struct ipt_entry_match *match,
213 const struct ipt_entry *entry,
214 const ip_chain_t *chain,
219 value_list_t vl = VALUE_LIST_INIT;
221 /* Select the rules to collect */
222 if (chain->rule_type == RTYPE_NUM)
224 if (chain->rule.num != rule_num)
229 if (strcmp (match->u.user.name, "comment") != 0)
231 if ((chain->rule_type == RTYPE_COMMENT)
232 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
238 vl.time = time (NULL);
239 strcpy (vl.host, hostname_g);
240 strcpy (vl.plugin, "iptables");
242 status = snprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
243 "%s-%s", chain->table, chain->chain);
244 if ((status >= sizeof (vl.plugin_instance)) || (status < 1))
247 if (chain->name[0] != '\0')
249 strncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
253 if (chain->rule_type == RTYPE_NUM)
254 snprintf (vl.type_instance, sizeof (vl.type_instance),
255 "%i", chain->rule.num);
257 strncpy (vl.type_instance, (char *) match->data,
258 sizeof (vl.type_instance));
260 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
262 values[0].counter = (counter_t) entry->counters.bcnt;
263 plugin_dispatch_values ("ipt_bytes", &vl);
265 values[0].counter = (counter_t) entry->counters.pcnt;
266 plugin_dispatch_values ("ipt_packets", &vl);
269 } /* void submit_match */
271 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) {
272 const struct ipt_entry *entry;
275 /* Find first rule for chain and use the iterate macro */
276 entry = iptc_first_rule( chain->chain, handle );
279 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
286 if (chain->rule_type == RTYPE_NUM)
288 submit_match (NULL, entry, chain, rule_num);
292 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
295 entry = iptc_next_rule( entry, handle );
297 } /* while (entry) */
301 static int iptables_read (void)
304 static complain_t complaint;
306 /* Init the iptc handle structure and query the correct table */
307 for (i = 0; i < chain_num; i++)
309 iptc_handle_t handle;
312 chain = chain_list[i];
315 DEBUG ("chain == NULL");
319 handle = iptc_init( chain->table );
322 DEBUG ("iptc_init (%s) failed: %s", chain->table, iptc_strerror (errno));
323 plugin_complain (LOG_ERR, &complaint, "iptc_init (%s) failed: %s",
324 chain->table, iptc_strerror (errno));
327 plugin_relief (LOG_INFO, &complaint, "iptc_init (%s) succeeded",
330 submit_chain (&handle, chain);
335 } /* int iptables_read */
337 static int iptables_shutdown (void)
341 for (i = 0; i < chain_num; i++)
343 if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
345 sfree (chain_list[i]->rule.comment);
347 sfree (chain_list[i]);
352 } /* int iptables_shutdown */
353 #endif /* IPTABLES_HAVE_READ */
355 void module_register (void)
357 #if IPTABLES_HAVE_READ
358 plugin_register_config ("iptables", iptables_config,
359 config_keys, config_keys_num);
360 plugin_register_read ("iptables", iptables_read);
361 plugin_register_shutdown ("iptables", iptables_shutdown);
363 } /* void module_register */
369 * vim:shiftwidth=4:softtabstop=4:tabstop=8