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 sstrncpy (temp.table, table, table_len);
119 chain_len = strlen (chain);
120 if ((unsigned int)chain_len >= sizeof(temp.chain))
122 ERROR ("Chain `%s' too long.", chain);
126 sstrncpy (temp.chain, chain, chain_len);
130 char *comment = fields[2];
131 int rule = atoi (comment);
135 temp.rule.num = rule;
136 temp.rule_type = RTYPE_NUM;
140 temp.rule.comment = strdup (comment);
141 if (temp.rule.comment == NULL)
146 temp.rule_type = RTYPE_COMMENT;
151 temp.rule_type = RTYPE_COMMENT_ALL;
155 sstrncpy (temp.name, fields[3], sizeof (temp.name));
162 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
166 ERROR ("realloc failed: %s",
167 sstrerror (errno, errbuf, sizeof (errbuf)));
172 final = (ip_chain_t *) malloc( sizeof(temp) );
176 ERROR ("malloc failed: %s",
177 sstrerror (errno, errbuf, sizeof (errbuf)));
180 memcpy (final, &temp, sizeof (temp));
181 chain_list[chain_num] = final;
184 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
192 } /* int iptables_config */
194 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
195 static int submit_match (const struct ipt_entry_match *match,
196 const struct ipt_entry *entry,
197 const ip_chain_t *chain,
202 value_list_t vl = VALUE_LIST_INIT;
204 /* Select the rules to collect */
205 if (chain->rule_type == RTYPE_NUM)
207 if (chain->rule.num != rule_num)
212 if (strcmp (match->u.user.name, "comment") != 0)
214 if ((chain->rule_type == RTYPE_COMMENT)
215 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
221 vl.time = time (NULL);
222 strcpy (vl.host, hostname_g);
223 strcpy (vl.plugin, "iptables");
225 status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
226 "%s-%s", chain->table, chain->chain);
227 if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
230 if (chain->name[0] != '\0')
232 sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
236 if (chain->rule_type == RTYPE_NUM)
237 ssnprintf (vl.type_instance, sizeof (vl.type_instance),
238 "%i", chain->rule.num);
240 sstrncpy (vl.type_instance, (char *) match->data,
241 sizeof (vl.type_instance));
244 strcpy (vl.type, "ipt_bytes");
245 values[0].counter = (counter_t) entry->counters.bcnt;
246 plugin_dispatch_values (&vl);
248 strcpy (vl.type, "ipt_packets");
249 values[0].counter = (counter_t) entry->counters.pcnt;
250 plugin_dispatch_values (&vl);
253 } /* void submit_match */
255 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) {
256 const struct ipt_entry *entry;
259 /* Find first rule for chain and use the iterate macro */
260 entry = iptc_first_rule( chain->chain, handle );
263 DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
270 if (chain->rule_type == RTYPE_NUM)
272 submit_match (NULL, entry, chain, rule_num);
276 IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
279 entry = iptc_next_rule( entry, handle );
281 } /* while (entry) */
285 static int iptables_read (void)
288 int num_failures = 0;
290 /* Init the iptc handle structure and query the correct table */
291 for (i = 0; i < chain_num; i++)
293 iptc_handle_t handle;
296 chain = chain_list[i];
299 DEBUG ("iptables plugin: chain == NULL");
303 handle = iptc_init (chain->table);
306 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
307 chain->table, iptc_strerror (errno));
312 submit_chain (&handle, chain);
314 } /* for (i = 0 .. chain_num) */
316 return ((num_failures < chain_num) ? 0 : -1);
317 } /* int iptables_read */
319 static int iptables_shutdown (void)
323 for (i = 0; i < chain_num; i++)
325 if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
327 sfree (chain_list[i]->rule.comment);
329 sfree (chain_list[i]);
334 } /* int iptables_shutdown */
336 void module_register (void)
338 plugin_register_config ("iptables", iptables_config,
339 config_keys, config_keys_num);
340 plugin_register_read ("iptables", iptables_read);
341 plugin_register_shutdown ("iptables", iptables_shutdown);
342 } /* void module_register */
345 * vim:shiftwidth=4:softtabstop=4:tabstop=8