2 * collectd - src/iptables.c
3 * Copyright (C) 2007 Sjoerd van der Berg
4 * Copyright (C) 2007 Florian octo Forster
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 * Sjoerd van der Berg <harekiet at users.sourceforge.net>
22 * Florian Forster <octo at verplant.org>
28 #include "configfile.h"
31 # include "libiptc/libiptc.h"
33 # include <libiptc/libiptc.h>
37 * (Module-)Global variables
41 * Config format should be `Chain table chainname',
42 * e. g. `Chain mangle incoming'
44 static const char *config_keys[] =
49 static int config_keys_num = 1;
51 Each table/chain combo that will be queried goes into this list
53 #ifndef XT_TABLE_MAXNAMELEN
54 # define XT_TABLE_MAXNAMELEN 32
57 char table[XT_TABLE_MAXNAMELEN];
58 char chain[XT_TABLE_MAXNAMELEN];
73 static ip_chain_t **chain_list = NULL;
74 static int chain_num = 0;
76 static int iptables_config (const char *key, const char *value)
78 if (strcasecmp (key, "Chain") == 0)
80 ip_chain_t temp, *final, **list;
90 memset (&temp, 0, sizeof (temp));
92 value_copy = strdup (value);
93 if (value_copy == NULL)
96 ERROR ("strdup failed: %s",
97 sstrerror (errno, errbuf, sizeof (errbuf)));
101 /* Chain <table> <chain> [<comment|num> [name]] */
102 fields_num = strsplit (value_copy, fields, 4);
112 table_len = strlen (table) + 1;
113 if ((unsigned int)table_len > sizeof(temp.table))
115 ERROR ("Table `%s' too long.", table);
119 sstrncpy (temp.table, table, table_len);
121 chain_len = strlen (chain) + 1;
122 if ((unsigned int)chain_len > sizeof(temp.chain))
124 ERROR ("Chain `%s' too long.", chain);
128 sstrncpy (temp.chain, chain, chain_len);
132 char *comment = fields[2];
133 int rule = atoi (comment);
137 temp.rule.num = rule;
138 temp.rule_type = RTYPE_NUM;
142 temp.rule.comment = strdup (comment);
143 if (temp.rule.comment == NULL)
148 temp.rule_type = RTYPE_COMMENT;
153 temp.rule_type = RTYPE_COMMENT_ALL;
157 sstrncpy (temp.name, fields[3], sizeof (temp.name));
164 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
168 ERROR ("realloc failed: %s",
169 sstrerror (errno, errbuf, sizeof (errbuf)));
174 final = (ip_chain_t *) malloc( sizeof(temp) );
178 ERROR ("malloc failed: %s",
179 sstrerror (errno, errbuf, sizeof (errbuf)));
182 memcpy (final, &temp, sizeof (temp));
183 chain_list[chain_num] = final;
186 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
194 } /* int iptables_config */
196 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
197 static int submit_match (const struct ipt_entry_match *match,
198 const struct ipt_entry *entry,
199 const ip_chain_t *chain,
204 value_list_t vl = VALUE_LIST_INIT;
206 /* Select the rules to collect */
207 if (chain->rule_type == RTYPE_NUM)
209 if (chain->rule.num != rule_num)
214 if (strcmp (match->u.user.name, "comment") != 0)
216 if ((chain->rule_type == RTYPE_COMMENT)
217 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
223 vl.time = time (NULL);
224 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
225 sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
227 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
228 "%s-%s", chain->table, chain->chain);
229 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
232 if (chain->name[0] != '\0')
234 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
238 if (chain->rule_type == RTYPE_NUM)
239 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
240 "%i", chain->rule.num);
242 sstrncpy (vl.type_instance, (char *) match->data,
243 sizeof (vl.type_instance));
246 sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
247 values[0].counter = (counter_t) entry->counters.bcnt;
248 plugin_dispatch_values (&vl);
250 sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
251 values[0].counter = (counter_t) entry->counters.pcnt;
252 plugin_dispatch_values (&vl);
255 } /* void submit_match */
257 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) {
258 const struct ipt_entry *entry;
261 /* Find first rule for chain and use the iterate macro */
262 entry = iptc_first_rule( chain->chain, handle );
265 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
272 if (chain->rule_type == RTYPE_NUM)
274 submit_match (NULL, entry, chain, rule_num);
278 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
281 entry = iptc_next_rule( entry, handle );
283 } /* while (entry) */
287 static int iptables_read (void)
290 int num_failures = 0;
292 /* Init the iptc handle structure and query the correct table */
293 for (i = 0; i < chain_num; i++)
295 iptc_handle_t handle;
298 chain = chain_list[i];
301 DEBUG ("iptables plugin: chain == NULL");
305 handle = iptc_init (chain->table);
308 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
309 chain->table, iptc_strerror (errno));
314 submit_chain (&handle, chain);
316 } /* for (i = 0 .. chain_num) */
318 return ((num_failures < chain_num) ? 0 : -1);
319 } /* int iptables_read */
321 static int iptables_shutdown (void)
325 for (i = 0; i < chain_num; i++)
327 if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
329 sfree (chain_list[i]->rule.comment);
331 sfree (chain_list[i]);
336 } /* int iptables_shutdown */
338 void module_register (void)
340 plugin_register_config ("iptables", iptables_config,
341 config_keys, config_keys_num);
342 plugin_register_read ("iptables", iptables_read);
343 plugin_register_shutdown ("iptables", iptables_shutdown);
344 } /* void module_register */
347 * vim:shiftwidth=4:softtabstop=4:tabstop=8