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"
29 # include "libiptc/libiptc.h"
31 # include <libiptc/libiptc.h>
35 * (Module-)Global variables
39 * Config format should be `Chain table chainname',
40 * e. g. `Chain mangle incoming'
42 static const char *config_keys[] =
47 static int config_keys_num = 1;
49 Each table/chain combo that will be queried goes into this list
51 #ifndef XT_TABLE_MAXNAMELEN
52 # define XT_TABLE_MAXNAMELEN 32
55 char table[XT_TABLE_MAXNAMELEN];
56 char chain[XT_TABLE_MAXNAMELEN];
71 static ip_chain_t **chain_list = NULL;
72 static int chain_num = 0;
74 static int iptables_config (const char *key, const char *value)
76 if (strcasecmp (key, "Chain") == 0)
78 ip_chain_t temp, *final, **list;
88 memset (&temp, 0, sizeof (temp));
90 value_copy = strdup (value);
91 if (value_copy == NULL)
94 ERROR ("strdup failed: %s",
95 sstrerror (errno, errbuf, sizeof (errbuf)));
99 /* Chain <table> <chain> [<comment|num> [name]] */
100 fields_num = strsplit (value_copy, fields, 4);
110 table_len = strlen (table);
111 if ((unsigned int)table_len >= sizeof(temp.table))
113 ERROR ("Table `%s' too long.", table);
117 strncpy (temp.table, table, table_len);
118 temp.table[table_len] = '\0';
120 chain_len = strlen (chain);
121 if ((unsigned int)chain_len >= sizeof(temp.chain))
123 ERROR ("Chain `%s' too long.", chain);
127 strncpy (temp.chain, chain, chain_len);
128 temp.chain[chain_len] = '\0';
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 strncpy (temp.name, fields[3], sizeof (temp.name) - 1);
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 = snprintf (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 strncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
238 if (chain->rule_type == RTYPE_NUM)
239 snprintf (vl.type_instance, sizeof (vl.type_instance),
240 "%i", chain->rule.num);
242 strncpy (vl.type_instance, (char *) match->data,
243 sizeof (vl.type_instance));
245 vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
247 values[0].counter = (counter_t) entry->counters.bcnt;
248 plugin_dispatch_values ("ipt_bytes", &vl);
250 values[0].counter = (counter_t) entry->counters.pcnt;
251 plugin_dispatch_values ("ipt_packets", &vl);
254 } /* void submit_match */
256 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) {
257 const struct ipt_entry *entry;
260 /* Find first rule for chain and use the iterate macro */
261 entry = iptc_first_rule( chain->chain, handle );
264 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
271 if (chain->rule_type == RTYPE_NUM)
273 submit_match (NULL, entry, chain, rule_num);
277 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
280 entry = iptc_next_rule( entry, handle );
282 } /* while (entry) */
286 static int iptables_read (void)
289 int num_failures = 0;
291 /* Init the iptc handle structure and query the correct table */
292 for (i = 0; i < chain_num; i++)
294 iptc_handle_t handle;
297 chain = chain_list[i];
300 DEBUG ("iptables plugin: chain == NULL");
304 handle = iptc_init (chain->table);
307 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
308 chain->table, iptc_strerror (errno));
313 submit_chain (&handle, chain);
315 } /* for (i = 0 .. chain_num) */
317 return ((num_failures < chain_num) ? 0 : -1);
318 } /* int iptables_read */
320 static int iptables_shutdown (void)
324 for (i = 0; i < chain_num; i++)
326 if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
328 sfree (chain_list[i]->rule.comment);
330 sfree (chain_list[i]);
335 } /* int iptables_shutdown */
337 void module_register (void)
339 plugin_register_config ("iptables", iptables_config,
340 config_keys, config_keys_num);
341 plugin_register_read ("iptables", iptables_read);
342 plugin_register_shutdown ("iptables", iptables_shutdown);
343 } /* void module_register */
346 * vim:shiftwidth=4:softtabstop=4:tabstop=8