iptables plugin: Remove `iptables_init' because it's not needed..
[collectd.git] / src / iptables.c
1 /**
2  * collectd - src/iptables.c
3  * Copyright (C) 2007 Sjoerd van der Berg
4  *
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.
9  *
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.
14  *
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
18  *
19  * Authors:
20  *  Sjoerd van der Berg <harekiet at users.sourceforge.net>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27 #include "utils_debug.h"
28
29 #if HAVE_LIBIPTC_LIBIPTC_H
30 # include <libiptc/libiptc.h>
31 #endif
32
33 #if HAVE_LIBIPTC_LIBIPTC_H
34 # define IPTABLES_HAVE_READ 1
35 #else
36 # define IPTABLES_HAVE_READ 0
37 #endif
38
39 #define MODULE_NAME "iptables"
40 #define BUFSIZE 512
41
42 /*
43  * (Module-)Global variables
44  */
45
46 /*
47  * Removed packet count for now, should have config option if you want to save
48  * them Although other collectd models don't seem to care much for options
49  * eitherway for what to log
50  */
51 /* Limit to ~125MByte/s (~1GBit/s) */
52 static char *ds_def[] =
53 {
54         "DS:value:COUNTER:"COLLECTD_HEARTBEAT":0:134217728",
55         NULL
56 };
57 static int ds_num = 1;
58
59 #if IPTABLES_HAVE_READ
60 /*
61  * Config format should be `Chain table chainname',
62  * e. g. `Chain mangle incoming'
63  */
64 static char *config_keys[] =
65 {
66         "Chain",
67         NULL
68 };
69 static int config_keys_num = 1;
70 /*
71     Each table/chain combo that will be queried goes into this list
72 */
73 #ifndef XT_TABLE_MAXNAMELEN
74 # define XT_TABLE_MAXNAMELEN 32
75 #endif
76 typedef struct {
77     char table[XT_TABLE_MAXNAMELEN];
78     char chain[XT_TABLE_MAXNAMELEN];
79     union
80     {
81         int   num;
82         char *comment;
83     } rule;
84     enum
85     {
86         RTYPE_NUM,
87         RTYPE_COMMENT,
88         RTYPE_COMMENT_ALL
89     } rule_type;
90     char name[64];
91 } ip_chain_t;
92
93 static ip_chain_t **chain_list = NULL;
94 static int chain_num = 0;
95
96 static int iptables_config (char *key, char *value)
97 {
98         if (strcasecmp (key, "Chain") == 0)
99         {
100                 ip_chain_t temp, *final, **list;
101                 char *table;
102                 int   table_len;
103                 char *chain;
104                 int   chain_len;
105
106                 char *value_copy;
107                 char *fields[4];
108                 int   fields_num;
109                 
110                 memset (&temp, 0, sizeof (temp));
111
112                 value_copy = strdup (value);
113                 if (value_copy == NULL)
114                 {
115                     syslog (LOG_ERR, "strdup failed: %s", strerror (errno));
116                     return (1);
117                 }
118
119                 /* Chain <table> <chain> [<comment|num> [name]] */
120                 fields_num = strsplit (value_copy, fields, 4);
121                 if (fields_num < 2)
122                 {
123                     free (value_copy);
124                     return (1);
125                 }
126
127                 table = fields[0];
128                 chain = fields[1];
129
130                 table_len = strlen (table);
131                 if (table_len >= sizeof(temp.table))
132                 {
133                         syslog (LOG_ERR, "Table `%s' too long.", table);
134                         free (value_copy);
135                         return (1);
136                 }
137                 strncpy (temp.table, table, table_len);
138                 temp.table[table_len] = '\0';
139
140                 chain_len = strlen (chain);
141                 if (chain_len >= sizeof(temp.chain))
142                 {
143                         syslog (LOG_ERR, "Chain `%s' too long.", chain);
144                         free (value_copy);
145                         return (1);
146                 }
147                 strncpy (temp.chain, chain, chain_len);
148                 temp.chain[chain_len] = '\0'; 
149
150                 if (fields_num >= 3)
151                 {
152                     char *comment = fields[2];
153                     int   rule = atoi (comment);
154
155                     if (rule)
156                     {
157                         temp.rule.num = rule;
158                         temp.rule_type = RTYPE_NUM;
159                     }
160                     else
161                     {
162                         strncpy (temp.rule.comment, comment,
163                                 sizeof (temp.rule.comment) - 1);
164                         temp.rule_type = RTYPE_COMMENT;
165                     }
166                 }
167                 else
168                 {
169                     temp.rule_type = RTYPE_COMMENT_ALL;
170                 }
171
172                 if (fields_num >= 4)
173                     strncpy (temp.name, fields[3], sizeof (temp.name) - 1);
174
175                 free (value_copy);
176                 value_copy = NULL;
177                 table = NULL;
178                 chain = NULL;
179
180                 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
181                 if (list == NULL)
182                 {
183                         syslog (LOG_ERR, "realloc failed: %s", strerror (errno));
184                         return (1);
185                 }
186
187                 chain_list = list;
188                 final = (ip_chain_t *) malloc( sizeof(temp) );
189                 if (final == NULL) 
190                 {
191                         syslog (LOG_ERR, "malloc failed: %s", strerror (errno));
192                         return (1);
193                 }
194                 memcpy (final, &temp, sizeof (temp));
195                 chain_list[chain_num] = final;
196                 chain_num++;
197
198                 DBG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
199         }
200         else 
201         {
202                 return (-1);
203         }
204
205         return (0);
206 }
207 #endif /* IPTABLES_HAVE_READ */
208
209 static void iptables_write (char *host, char *orig_inst, char *val, char *type) 
210 {
211     char *table;
212     char *inst;
213     char file[256];
214     int status;
215
216     table = strdup (orig_inst);
217     if (table == NULL)
218         return;
219     inst = strchr (table, ',');
220     if (inst == NULL)
221     {
222         free (table);
223         return;
224     }
225
226     *inst = '\0';
227     inst++;
228     if (*inst == '\0')
229     {
230         free (table);
231         return;
232     }
233
234     status = snprintf (file, sizeof (file), "iptables-%s/%s-%s.rrd",
235             table, type, inst);
236     free (table);
237     if ((status >= sizeof (file)) || (status < 1))
238         return;
239
240     rrd_update_file (host, file, val, ds_def, ds_num);
241 } /* void iptables_write */
242
243 static void iptables_write_bytes (char *host, char *inst, char *val)
244 {
245     iptables_write (host, inst, val, "ipt_bytes");
246 }
247
248 static void iptables_write_packets (char *host, char *inst, char *val)
249 {
250     iptables_write (host, inst, val, "ipt_packets");
251 }
252
253 #if IPTABLES_HAVE_READ
254 static int submit_match (const struct ipt_entry_match *match,
255                 const struct ipt_entry *entry,
256                 const ip_chain_t *chain,
257                 int rule_num) 
258 {
259     char inst[64];
260     char value[64];
261     int status;
262
263     /* Only log rules that have a comment, although could probably also do
264      * numerical targets sometime */
265     if (chain->rule_type == RTYPE_NUM)
266     {
267         if (chain->rule.num != rule_num)
268             return (0);
269     }
270     else
271     {
272         if (strcmp (match->u.user.name, "comment") != 0)
273             return 0;
274         if ((chain->rule_type == RTYPE_COMMENT)
275                 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
276             return (0);
277     }
278
279     if (chain->name[0] != '\0')
280     {
281         status = snprintf (inst, sizeof (inst), "%s-%s,%s",
282                 chain->table, chain->chain, chain->name);
283     }
284     else
285     {
286         if (chain->rule_type == RTYPE_NUM)
287             status = snprintf (inst, sizeof (inst), "%s-%s,%i",
288                 chain->table, chain->chain, chain->rule.num);
289         else
290             status = snprintf (inst, sizeof (inst), "%s-%s,%s",
291                 chain->table, chain->chain, match->data);
292
293         if ((status >= sizeof (inst)) || (status < 1))
294             return (0);
295     }
296
297     status = snprintf (value, sizeof (value), "%u:%lld",
298             (unsigned int) curtime,
299             entry->counters.bcnt);
300     if ((status >= sizeof (value)) || (status < 1))
301         return 0;
302     plugin_submit ("ipt_bytes", inst, value);
303
304     status = snprintf (value, sizeof (value), "%u:%lld",
305             (unsigned int) curtime,
306             entry->counters.pcnt);
307     if ((status >= sizeof (value)) || (status < 1))
308         return 0;
309     plugin_submit ("ipt_packets", inst, value);
310
311     return 0;
312 } /* int submit_match */
313
314 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) {
315     const struct ipt_entry *entry;
316     int rule_num;
317
318     /* Find first rule for chain and use the iterate macro */    
319     entry = iptc_first_rule( chain->chain, handle );
320     if (entry == NULL)
321     {
322         DBG ("iptc_first_rule failed: %s", iptc_strerror (errno));
323         return;
324     }
325
326     rule_num = 1;
327     while (entry)
328     {
329         if (chain->rule_type == RTYPE_NUM)
330         {
331             submit_match (NULL, entry, chain, rule_num);
332         }
333         else
334         {
335             IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
336         }
337
338         entry = iptc_next_rule( entry, handle );
339         rule_num++;
340     } /* while (entry) */
341 }
342
343
344 static void iptables_read (void)
345 {
346     int i;
347     static complain_t complaint;
348
349     /* Init the iptc handle structure and query the correct table */    
350     for (i = 0; i < chain_num; i++)
351     {
352         iptc_handle_t handle;
353         ip_chain_t *chain;
354         
355         chain = chain_list[i];
356         if (!chain)
357         {
358             DBG ("chain == NULL");
359             continue;
360         }
361
362         handle = iptc_init( chain->table );
363         if (!handle)
364         {
365             DBG ("iptc_init (%s) failed: %s", chain->table, iptc_strerror (errno));
366             plugin_complain (LOG_ERR, &complaint, "iptc_init (%s) failed: %s",
367                     chain->table, iptc_strerror (errno));
368             continue;
369         }
370         plugin_relief (LOG_INFO, &complaint, "iptc_init (%s) succeeded",
371                 chain->table);
372
373         submit_chain (&handle, chain);
374         iptc_free (&handle);
375     }
376 }
377 #else /* !IPTABLES_HAVE_READ */
378 # define iptables_read NULL
379 #endif
380
381 void module_register (void)
382 {
383     plugin_register ("ipt_bytes", NULL, NULL, iptables_write_bytes);
384     plugin_register ("ipt_packets", NULL, NULL, iptables_write_packets);
385 #if IPTABLES_HAVE_READ
386     plugin_register (MODULE_NAME, NULL, iptables_read, NULL);
387     cf_register (MODULE_NAME, iptables_config, config_keys, config_keys_num);
388 #endif
389 }
390
391 #undef BUFSIZE
392 #undef MODULE_NAME
393
394 /*
395  * vim:shiftwidth=4:softtabstop=4:tabstop=8
396  */