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>
33 * (Module-)Global variables
37 * Config format should be `Chain table chainname',
38 * e. g. `Chain mangle incoming'
40 static const char *config_keys[] =
45 static int config_keys_num = 1;
47 Each table/chain combo that will be queried goes into this list
49 #ifndef XT_TABLE_MAXNAMELEN
50 # define XT_TABLE_MAXNAMELEN 32
53 char table[XT_TABLE_MAXNAMELEN];
54 char chain[XT_TABLE_MAXNAMELEN];
69 static ip_chain_t **chain_list = NULL;
70 static int chain_num = 0;
72 static int iptables_config (const char *key, const char *value)
74 if (strcasecmp (key, "Chain") == 0)
76 ip_chain_t temp, *final, **list;
86 memset (&temp, 0, sizeof (temp));
88 value_copy = strdup (value);
89 if (value_copy == NULL)
92 ERROR ("strdup failed: %s",
93 sstrerror (errno, errbuf, sizeof (errbuf)));
97 /* Chain <table> <chain> [<comment|num> [name]] */
98 fields_num = strsplit (value_copy, fields, 4);
108 table_len = strlen (table);
109 if ((unsigned int)table_len >= sizeof(temp.table))
111 ERROR ("Table `%s' too long.", table);
115 sstrncpy (temp.table, table, table_len);
117 chain_len = strlen (chain);
118 if ((unsigned int)chain_len >= sizeof(temp.chain))
120 ERROR ("Chain `%s' too long.", chain);
124 sstrncpy (temp.chain, chain, chain_len);
128 char *comment = fields[2];
129 int rule = atoi (comment);
133 temp.rule.num = rule;
134 temp.rule_type = RTYPE_NUM;
138 temp.rule.comment = strdup (comment);
139 if (temp.rule.comment == NULL)
144 temp.rule_type = RTYPE_COMMENT;
149 temp.rule_type = RTYPE_COMMENT_ALL;
153 sstrncpy (temp.name, fields[3], sizeof (temp.name));
160 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
164 ERROR ("realloc failed: %s",
165 sstrerror (errno, errbuf, sizeof (errbuf)));
170 final = (ip_chain_t *) malloc( sizeof(temp) );
174 ERROR ("malloc failed: %s",
175 sstrerror (errno, errbuf, sizeof (errbuf)));
178 memcpy (final, &temp, sizeof (temp));
179 chain_list[chain_num] = final;
182 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
190 } /* int iptables_config */
192 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
193 static int submit_match (const struct ipt_entry_match *match,
194 const struct ipt_entry *entry,
195 const ip_chain_t *chain,
200 value_list_t vl = VALUE_LIST_INIT;
202 /* Select the rules to collect */
203 if (chain->rule_type == RTYPE_NUM)
205 if (chain->rule.num != rule_num)
210 if (strcmp (match->u.user.name, "comment") != 0)
212 if ((chain->rule_type == RTYPE_COMMENT)
213 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
219 vl.time = time (NULL);
220 strcpy (vl.host, hostname_g);
221 strcpy (vl.plugin, "iptables");
223 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
224 "%s-%s", chain->table, chain->chain);
225 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
228 if (chain->name[0] != '\0')
230 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
234 if (chain->rule_type == RTYPE_NUM)
235 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
236 "%i", chain->rule.num);
238 sstrncpy (vl.type_instance, (char *) match->data,
239 sizeof (vl.type_instance));
242 strcpy (vl.type, "ipt_bytes");
243 values[0].counter = (counter_t) entry->counters.bcnt;
244 plugin_dispatch_values (&vl);
246 strcpy (vl.type, "ipt_packets");
247 values[0].counter = (counter_t) entry->counters.pcnt;
248 plugin_dispatch_values (&vl);
251 } /* void submit_match */
253 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) {
254 const struct ipt_entry *entry;
257 /* Find first rule for chain and use the iterate macro */
258 entry = iptc_first_rule( chain->chain, handle );
261 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
268 if (chain->rule_type == RTYPE_NUM)
270 submit_match (NULL, entry, chain, rule_num);
274 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
277 entry = iptc_next_rule( entry, handle );
279 } /* while (entry) */
283 static int iptables_read (void)
286 int num_failures = 0;
288 /* Init the iptc handle structure and query the correct table */
289 for (i = 0; i < chain_num; i++)
291 iptc_handle_t handle;
294 chain = chain_list[i];
297 DEBUG ("iptables plugin: chain == NULL");
301 handle = iptc_init (chain->table);
304 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
305 chain->table, iptc_strerror (errno));
310 submit_chain (&handle, chain);
312 } /* for (i = 0 .. chain_num) */
314 return ((num_failures < chain_num) ? 0 : -1);
315 } /* int iptables_read */
317 static int iptables_shutdown (void)
321 for (i = 0; i < chain_num; i++)
323 if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
325 sfree (chain_list[i]->rule.comment);
327 sfree (chain_list[i]);
332 } /* int iptables_shutdown */
334 void module_register (void)
336 plugin_register_config ("iptables", iptables_config,
337 config_keys, config_keys_num);
338 plugin_register_read ("iptables", iptables_read);
339 plugin_register_shutdown ("iptables", iptables_shutdown);
340 } /* void module_register */
343 * vim:shiftwidth=4:softtabstop=4:tabstop=8