netlink plugin: Use the parent's classid for `filter'.
[collectd.git] / src / netlink.c
1 /**
2  * collectd - src/netlink.c
3  * Copyright (C) 2007  Florian octo Forster
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; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
25
26 #include <asm/types.h>
27 #include <sys/socket.h>
28 #include <iproute/libnetlink.h>
29 #include <linux/netlink.h>
30 #include <linux/rtnetlink.h>
31 #include <linux/gen_stats.h>
32
33 #include <iproute/ll_map.h>
34
35 typedef struct ir_ignorelist_s
36 {
37   char *device;
38   char *type;
39   char *inst;
40   struct ir_ignorelist_s *next;
41 } ir_ignorelist_t;
42
43 static int ir_ignorelist_invert = 1;
44 static ir_ignorelist_t *ir_ignorelist_head = NULL;
45
46 static struct rtnl_handle rth;
47
48 static const char *config_keys[] =
49 {
50         "Interface",
51         "VerboseInterface",
52         "QDisc",
53         "Class",
54         "Filter",
55         "IgnoreSelected"
56 };
57 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
58
59 static int add_ignorelist (const char *dev, const char *type,
60     const char *inst)
61 {
62   ir_ignorelist_t *entry;
63
64   entry = (ir_ignorelist_t *) malloc (sizeof (ir_ignorelist_t));
65   if (entry == NULL)
66     return (-1);
67
68   memset (entry, '\0', sizeof (ir_ignorelist_t));
69
70   if (strcasecmp (dev, "All") != 0)
71   {
72     entry->device = strdup (dev);
73     if (entry->device == NULL)
74     {
75       sfree (entry);
76       return (-1);
77     }
78   }
79
80   entry->type = strdup (type);
81   if (entry->type == NULL)
82   {
83     sfree (entry->device);
84     sfree (entry);
85     return (-1);
86   }
87
88   if (inst != NULL)
89   {
90     entry->inst = strdup (inst);
91     if (entry->inst == NULL)
92     {
93       sfree (entry->type);
94       sfree (entry->device);
95       sfree (entry);
96       return (-1);
97     }
98   }
99
100   entry->next = ir_ignorelist_head;
101   ir_ignorelist_head = entry;
102
103   return (0);
104 } /* int add_ignorelist */
105
106 /* 
107  * Checks wether a data set should be ignored. Returns `true' is the value
108  * should be ignored, `false' otherwise.
109  */
110 static int check_ignorelist (const char *dev,
111     const char *type, const char *type_instance)
112 {
113   ir_ignorelist_t *i;
114
115   if (ir_ignorelist_head == NULL)
116     return (ir_ignorelist_invert ? 0 : 1);
117
118   for (i = ir_ignorelist_head; i != NULL; i = i->next)
119   {
120     if ((strcasecmp (i->device, dev) != 0)
121         || (strcasecmp (i->type, type) != 0))
122       continue;
123
124     if ((i->inst != NULL)
125         && ((type_instance == NULL)
126           || (strcasecmp (i->inst, type_instance) != 0)))
127       continue;
128
129     return (ir_ignorelist_invert ? 0 : 1);
130   } /* for i */
131
132   return (ir_ignorelist_invert);
133 } /* int check_ignorelist */
134
135 static void submit_one (const char *dev, const char *type,
136     const char *type_instance, counter_t value)
137 {
138   value_t values[1];
139   value_list_t vl = VALUE_LIST_INIT;
140
141   values[0].counter = value;
142
143   vl.values = values;
144   vl.values_len = 1;
145   vl.time = time (NULL);
146   strcpy (vl.host, hostname_g);
147   strcpy (vl.plugin, "netlink");
148   strncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
149
150   if (type_instance != NULL)
151     strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
152
153   plugin_dispatch_values (type, &vl);
154 } /* void submit_one */
155
156 static void submit_two (const char *dev, const char *type,
157     const char *type_instance,
158     counter_t rx, counter_t tx)
159 {
160   value_t values[2];
161   value_list_t vl = VALUE_LIST_INIT;
162
163   values[0].counter = rx;
164   values[1].counter = tx;
165
166   vl.values = values;
167   vl.values_len = 2;
168   vl.time = time (NULL);
169   strcpy (vl.host, hostname_g);
170   strcpy (vl.plugin, "netlink");
171   strncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
172
173   if (type_instance != NULL)
174     strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
175
176   plugin_dispatch_values (type, &vl);
177 } /* void submit_two */
178
179 static int link_filter (const struct sockaddr_nl *sa, struct nlmsghdr *nmh,
180     void *args)
181 {
182   struct ifinfomsg *msg;
183   int msg_len;
184   struct rtattr *attrs[IFLA_MAX + 1];
185   struct rtnl_link_stats *stats;
186
187   const char *dev;
188
189   if (nmh->nlmsg_type != RTM_NEWLINK)
190   {
191     ERROR ("netlink plugin: link_filter: Don't know how to handle type %i.\n",
192         nmh->nlmsg_type);
193     return (-1);
194   }
195
196   msg = NLMSG_DATA (nmh);
197
198   msg_len = nmh->nlmsg_len - sizeof (struct ifinfomsg);
199   if (msg_len < 0)
200   {
201     ERROR ("netlink plugin: link_filter: msg_len = %i < 0;\n", msg_len);
202     return (-1);
203   }
204
205   memset (attrs, '\0', sizeof (attrs));
206   if (parse_rtattr (attrs, IFLA_MAX, IFLA_RTA (msg), msg_len) != 0)
207   {
208     ERROR ("netlink plugin: link_filter: parse_rtattr failed.\n");
209     return (-1);
210   }
211
212   if (attrs[IFLA_STATS] == NULL)
213     return (-1);
214   stats = RTA_DATA (attrs[IFLA_STATS]);
215
216   if (attrs[IFLA_IFNAME] == NULL)
217   {
218     ERROR ("netlink plugin: link_filter: attrs[IFLA_IFNAME] == NULL\n");
219     return (-1);
220   }
221   dev = RTA_DATA (attrs[IFLA_IFNAME]);
222
223   if (check_ignorelist (dev, "interface", NULL) == 0)
224   {
225     submit_two (dev, "if_octets", NULL, stats->rx_bytes, stats->tx_bytes);
226     submit_two (dev, "if_packets", NULL, stats->rx_bytes, stats->tx_bytes);
227     submit_two (dev, "if_errors", NULL, stats->rx_bytes, stats->tx_bytes);
228   }
229   else
230   {
231     DEBUG ("netlink plugin: Ignoring %s/interface.", dev);
232   }
233
234   if (check_ignorelist (dev, "if_detail", NULL) == 0)
235   {
236     submit_two (dev, "if_dropped", NULL, stats->rx_bytes, stats->tx_bytes);
237     submit_one (dev, "if_multicast", NULL, stats->multicast);
238     submit_one (dev, "if_collisions", NULL, stats->collisions);
239
240     submit_one (dev, "if_rx_errors", "length", stats->rx_length_errors);
241     submit_one (dev, "if_rx_errors", "over", stats->rx_over_errors);
242     submit_one (dev, "if_rx_errors", "crc", stats->rx_crc_errors);
243     submit_one (dev, "if_rx_errors", "frame", stats->rx_frame_errors);
244     submit_one (dev, "if_rx_errors", "fifo", stats->rx_fifo_errors);
245     submit_one (dev, "if_rx_errors", "missed", stats->rx_missed_errors);
246
247     submit_one (dev, "if_tx_errors", "aborted", stats->tx_aborted_errors);
248     submit_one (dev, "if_tx_errors", "carrier", stats->tx_carrier_errors);
249     submit_one (dev, "if_tx_errors", "fifo", stats->tx_fifo_errors);
250     submit_one (dev, "if_tx_errors", "heartbeat", stats->tx_heartbeat_errors);
251     submit_one (dev, "if_tx_errors", "window", stats->tx_window_errors);
252   }
253   else
254   {
255     DEBUG ("netlink plugin: Ignoring %s/if_detail.", dev);
256   }
257
258   return (0);
259 } /* int link_filter */
260
261 static int qos_filter (const struct sockaddr_nl *sa, struct nlmsghdr *nmh,
262     void *args)
263 {
264   struct tcmsg *msg;
265   int msg_len;
266   struct rtattr *attrs[TCA_MAX + 1];
267
268   const char *dev;
269
270   /* char *type_instance; */
271   char *tc_type;
272   char tc_inst[DATA_MAX_NAME_LEN];
273
274   printf ("=== qos_filter ===\n");
275
276   if (nmh->nlmsg_type == RTM_NEWQDISC)
277     tc_type = "qdisc";
278   else if (nmh->nlmsg_type == RTM_NEWTCLASS)
279     tc_type = "class";
280   else if (nmh->nlmsg_type == RTM_NEWTFILTER)
281     tc_type = "filter";
282   else
283   {
284     ERROR ("netlink plugin: qos_filter: Don't know how to handle type %i.\n",
285         nmh->nlmsg_type);
286     return (-1);
287   }
288
289   msg = NLMSG_DATA (nmh);
290
291   msg_len = nmh->nlmsg_len - sizeof (struct tcmsg);
292   if (msg_len < 0)
293   {
294     ERROR ("netlink plugin: qos_filter: msg_len = %i < 0;\n", msg_len);
295     return (-1);
296   }
297
298   dev = ll_index_to_name (msg->tcm_ifindex);
299   if (dev == NULL)
300   {
301     ERROR ("netlink plugin: qos_filter: ll_index_to_name (%i) failed.\n",
302         msg->tcm_ifindex);
303     return (-1);
304   }
305
306   memset (attrs, '\0', sizeof (attrs));
307   if (parse_rtattr (attrs, TCA_MAX, TCA_RTA (msg), msg_len) != 0)
308   {
309     ERROR ("netlink plugin: qos_filter: parse_rtattr failed.\n");
310     return (-1);
311   }
312
313   if (attrs[TCA_KIND] == NULL)
314   {
315     ERROR ("netlink plugin: qos_filter: attrs[TCA_KIND] == NULL\n");
316     return (-1);
317   }
318
319   { /* The the ID */
320     uint32_t numberic_id;
321
322     numberic_id = msg->tcm_handle;
323     if (strcmp (tc_type, "filter") == 0)
324       numberic_id = msg->tcm_parent;
325
326     snprintf (tc_inst, sizeof (tc_inst), "%s-%x:%x",
327         (const char *) RTA_DATA (attrs[TCA_KIND]),
328         numberic_id >> 16,
329         numberic_id & 0x0000FFFF);
330     tc_inst[sizeof (tc_inst) - 1] = '\0';
331   }
332   
333   if (check_ignorelist (dev, tc_type, tc_inst))
334     return (0);
335
336   if (attrs[TCA_STATS2])
337   {
338     struct rtattr *attrs_stats[TCA_STATS_MAX + 1];
339
340     memset (attrs_stats, '\0', sizeof (attrs_stats));
341     parse_rtattr_nested (attrs_stats, TCA_STATS_MAX, attrs[TCA_STATS2]);
342
343     if (attrs_stats[TCA_STATS_BASIC])
344     {
345       struct gnet_stats_basic bs;
346       char type_instance[DATA_MAX_NAME_LEN];
347
348       snprintf (type_instance, sizeof (type_instance), "%s-%s",
349           tc_type, tc_inst);
350       type_instance[sizeof (type_instance) - 1] = '\0';
351
352       memset (&bs, '\0', sizeof (bs));
353       memcpy (&bs, RTA_DATA (attrs_stats[TCA_STATS_BASIC]),
354           MIN (RTA_PAYLOAD (attrs_stats[TCA_STATS_BASIC]), sizeof(bs)));
355
356       submit_one (dev, "ipt_octets", type_instance, bs.bytes);
357       submit_one (dev, "ipt_packets", type_instance, bs.packets);
358     }
359   }
360
361   return (0);
362 } /* int qos_filter */
363
364 static int ir_config (const char *key, const char *value)
365 {
366   char *new_val;
367   char *fields[8];
368   int fields_num;
369   int status = 1;
370
371   new_val = strdup (value);
372   if (new_val == NULL)
373     return (-1);
374
375   fields_num = strsplit (new_val, fields, STATIC_ARRAY_SIZE (fields));
376   if ((fields_num < 1) || (fields_num > 8))
377   {
378     sfree (new_val);
379     return (-1);
380   }
381
382   if ((strcasecmp (key, "Interface") == 0)
383       || (strcasecmp (key, "VerboseInterface") == 0))
384   {
385     if (fields_num != 1)
386     {
387       ERROR ("netlink plugin: Invalid number of fields for option "
388           "`%s'. Got %i, expected 1.", key, fields_num);
389       status = -1;
390     }
391     else
392     {
393       add_ignorelist (fields[0], "interface", NULL);
394       if (strcasecmp (key, "VerboseInterface") == 0)
395         add_ignorelist (fields[0], "if_detail", NULL);
396       status = 0;
397     }
398   }
399   else if ((strcasecmp (key, "QDisc") == 0)
400       || (strcasecmp (key, "Class") == 0)
401       || (strcasecmp (key, "Filter") == 0))
402   {
403     if ((fields_num < 1) || (fields_num > 2))
404     {
405       ERROR ("netlink plugin: Invalid number of fields for option "
406           "`%s'. Got %i, expected 1 or 2.", key, fields_num);
407       return (-1);
408     }
409     else
410     {
411       add_ignorelist (fields[0], key,
412           (fields_num == 2) ? fields[1] : NULL);
413       status = 0;
414     }
415   }
416   else if (strcasecmp (key, "IgnoreSelected"))
417   {
418     if (fields_num != 1)
419     {
420       ERROR ("netlink plugin: Invalid number of fields for option "
421           "`IgnoreSelected'. Got %i, expected 1.", fields_num);
422       status = -1;
423     }
424     else
425     {
426       if ((strcasecmp (fields[0], "yes") == 0)
427           || (strcasecmp (fields[0], "true") == 0)
428           || (strcasecmp (fields[0], "on") == 0))
429         ir_ignorelist_invert = 0;
430       else
431         ir_ignorelist_invert = 1;
432       status = 0;
433     }
434   }
435
436   sfree (new_val);
437
438   return (status);
439 } /* int ir_config */
440
441 static int ir_init (void)
442 {
443   memset (&rth, '\0', sizeof (rth));
444
445   if (rtnl_open (&rth, 0) != 0)
446   {
447     ERROR ("netlink plugin: print_stats: rtnl_open failed.\n");
448     return (-1);
449   }
450
451   if (ll_init_map (&rth) != 0)
452   {
453     ERROR ("netlink plugin: print_stats: ll_init_map failed.\n");
454     return (-1);
455   }
456
457   return (0);
458 } /* int ir_init */
459
460 static int ir_read (void)
461 {
462   struct ifinfomsg im;
463   struct tcmsg tm;
464
465   memset (&im, '\0', sizeof (im));
466   im.ifi_type = AF_UNSPEC;
467
468   memset (&tm, '\0', sizeof (tm));
469   tm.tcm_family = AF_UNSPEC;
470
471   if (rtnl_dump_request (&rth, RTM_GETLINK, &im, sizeof (im)) < 0)
472   {
473     ERROR ("netlink plugin: print_stats: rtnl_dump_request failed.\n");
474     return (-1);
475   }
476
477   if (rtnl_dump_filter (&rth, link_filter, /* arg1 = */ NULL,
478         NULL, NULL) != 0)
479   {
480     ERROR ("netlink plugin: print_stats: rtnl_dump_filter failed.\n");
481     return (-1);
482   }
483
484   /* Get QDisc stats */
485   if (rtnl_dump_request (&rth, RTM_GETQDISC, &tm, sizeof (tm)) < 0)
486   {
487     ERROR ("netlink plugin: print_stats: rtnl_dump_request failed.\n");
488     return (-1);
489   }
490
491   if (rtnl_dump_filter (&rth, qos_filter, /* arg1 = */ NULL,
492         NULL, NULL) != 0)
493   {
494     ERROR ("netlink plugin: print_stats: rtnl_dump_filter failed.\n");
495     return (-1);
496   }
497
498   /* Get Class stats */
499   if (rtnl_dump_request (&rth, RTM_GETTCLASS, &tm, sizeof (tm)) < 0)
500   {
501     ERROR ("netlink plugin: print_stats: rtnl_dump_request failed.\n");
502     return (-1);
503   }
504
505   if (rtnl_dump_filter (&rth, qos_filter, /* arg1 = */ NULL,
506         NULL, NULL) != 0)
507   {
508     ERROR ("netlink plugin: print_stats: rtnl_dump_filter failed.\n");
509     return (-1);
510   }
511
512   /* Get Filter stats */
513   if (rtnl_dump_request (&rth, RTM_GETTFILTER, &tm, sizeof (tm)) < 0)
514   {
515     ERROR ("netlink plugin: print_stats: rtnl_dump_request failed.\n");
516     return (-1);
517   }
518
519   if (rtnl_dump_filter (&rth, qos_filter, /* arg1 = */ NULL,
520         NULL, NULL) != 0)
521   {
522     ERROR ("netlink plugin: print_stats: rtnl_dump_filter failed.\n");
523     return (-1);
524   }
525
526
527   return (0);
528 } /* int print_stats */
529
530 static int ir_shutdown (void)
531 {
532   if ((rth.fd != 0) || (rth.seq != 0) || (rth.dump != 0))
533   {
534     rtnl_close(&rth);
535     memset (&rth, '\0', sizeof (rth));
536   }
537   
538   return (0);
539 } /* int ir_shutdown */
540
541 void module_register (void)
542 {
543   plugin_register_config ("netlink", ir_config, config_keys, config_keys_num);
544   plugin_register_init ("netlink", ir_init);
545   plugin_register_read ("netlink", ir_read);
546   plugin_register_shutdown ("netlink", ir_shutdown);
547 } /* void module_register */
548
549 /*
550  * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
551  */