Added "type" to the value_list_t struct.
[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
29 #include <linux/netlink.h>
30 #include <linux/rtnetlink.h>
31 #if HAVE_LINUX_GEN_STATS_H
32 # include <linux/gen_stats.h>
33 #endif
34 #if HAVE_LINUX_PKT_SCHED_H
35 # include <linux/pkt_sched.h>
36 #endif
37
38 #if HAVE_LIBNETLINK_H
39 # include <libnetlink.h>
40 #elif HAVE_IPROUTE_LIBNETLINK_H
41 # include <iproute/libnetlink.h>
42 #elif HAVE_LINUX_LIBNETLINK_H
43 # include <linux/libnetlink.h>
44 #endif
45
46 typedef struct ir_ignorelist_s
47 {
48   char *device;
49   char *type;
50   char *inst;
51   struct ir_ignorelist_s *next;
52 } ir_ignorelist_t;
53
54 static int ir_ignorelist_invert = 1;
55 static ir_ignorelist_t *ir_ignorelist_head = NULL;
56
57 static struct rtnl_handle rth;
58
59 static char **iflist = NULL;
60 static size_t iflist_len = 0;
61
62 static const char *config_keys[] =
63 {
64         "Interface",
65         "VerboseInterface",
66         "QDisc",
67         "Class",
68         "Filter",
69         "IgnoreSelected"
70 };
71 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
72
73 static int add_ignorelist (const char *dev, const char *type,
74     const char *inst)
75 {
76   ir_ignorelist_t *entry;
77
78   entry = (ir_ignorelist_t *) malloc (sizeof (ir_ignorelist_t));
79   if (entry == NULL)
80     return (-1);
81
82   memset (entry, '\0', sizeof (ir_ignorelist_t));
83
84   if (strcasecmp (dev, "All") != 0)
85   {
86     entry->device = strdup (dev);
87     if (entry->device == NULL)
88     {
89       sfree (entry);
90       return (-1);
91     }
92   }
93
94   entry->type = strdup (type);
95   if (entry->type == NULL)
96   {
97     sfree (entry->device);
98     sfree (entry);
99     return (-1);
100   }
101
102   if (inst != NULL)
103   {
104     entry->inst = strdup (inst);
105     if (entry->inst == NULL)
106     {
107       sfree (entry->type);
108       sfree (entry->device);
109       sfree (entry);
110       return (-1);
111     }
112   }
113
114   entry->next = ir_ignorelist_head;
115   ir_ignorelist_head = entry;
116
117   return (0);
118 } /* int add_ignorelist */
119
120 /* 
121  * Checks wether a data set should be ignored. Returns `true' is the value
122  * should be ignored, `false' otherwise.
123  */
124 static int check_ignorelist (const char *dev,
125     const char *type, const char *type_instance)
126 {
127   ir_ignorelist_t *i;
128
129   assert ((dev != NULL) && (type != NULL));
130
131   if (ir_ignorelist_head == NULL)
132     return (ir_ignorelist_invert ? 0 : 1);
133
134   for (i = ir_ignorelist_head; i != NULL; i = i->next)
135   {
136     /* i->device == NULL  =>  match all devices */
137     if ((i->device != NULL)
138         && (strcasecmp (i->device, dev) != 0))
139       continue;
140
141     if (strcasecmp (i->type, type) != 0)
142       continue;
143
144     if ((i->inst != NULL) && (type_instance != NULL)
145         && (strcasecmp (i->inst, type_instance) != 0))
146       continue;
147
148     DEBUG ("netlink plugin: check_ignorelist: "
149         "(dev = %s; type = %s; inst = %s) matched "
150         "(dev = %s; type = %s; inst = %s)",
151         dev, type,
152         type_instance == NULL ? "(nil)" : type_instance,
153         i->device == NULL ? "(nil)" : i->device,
154         i->type,
155         i->inst == NULL ? "(nil)" : i->inst);
156
157     return (ir_ignorelist_invert ? 0 : 1);
158   } /* for i */
159
160   return (ir_ignorelist_invert);
161 } /* int check_ignorelist */
162
163 static void submit_one (const char *dev, const char *type,
164     const char *type_instance, counter_t value)
165 {
166   value_t values[1];
167   value_list_t vl = VALUE_LIST_INIT;
168
169   values[0].counter = value;
170
171   vl.values = values;
172   vl.values_len = 1;
173   vl.time = time (NULL);
174   strcpy (vl.host, hostname_g);
175   strcpy (vl.plugin, "netlink");
176   strncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
177   strncpy (vl.type, type, sizeof (vl.type));
178
179   if (type_instance != NULL)
180     strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
181
182   plugin_dispatch_values (&vl);
183 } /* void submit_one */
184
185 static void submit_two (const char *dev, const char *type,
186     const char *type_instance,
187     counter_t rx, counter_t tx)
188 {
189   value_t values[2];
190   value_list_t vl = VALUE_LIST_INIT;
191
192   values[0].counter = rx;
193   values[1].counter = tx;
194
195   vl.values = values;
196   vl.values_len = 2;
197   vl.time = time (NULL);
198   strcpy (vl.host, hostname_g);
199   strcpy (vl.plugin, "netlink");
200   strncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
201   strncpy (vl.type, type, sizeof (vl.type));
202
203   if (type_instance != NULL)
204     strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
205
206   plugin_dispatch_values (&vl);
207 } /* void submit_two */
208
209 static int link_filter (const struct sockaddr_nl *sa,
210     const struct nlmsghdr *nmh, void *args)
211 {
212   struct ifinfomsg *msg;
213   int msg_len;
214   struct rtattr *attrs[IFLA_MAX + 1];
215   struct rtnl_link_stats *stats;
216
217   const char *dev;
218
219   if (nmh->nlmsg_type != RTM_NEWLINK)
220   {
221     ERROR ("netlink plugin: link_filter: Don't know how to handle type %i.",
222         nmh->nlmsg_type);
223     return (-1);
224   }
225
226   msg = NLMSG_DATA (nmh);
227
228   msg_len = nmh->nlmsg_len - sizeof (struct ifinfomsg);
229   if (msg_len < 0)
230   {
231     ERROR ("netlink plugin: link_filter: msg_len = %i < 0;", msg_len);
232     return (-1);
233   }
234
235   memset (attrs, '\0', sizeof (attrs));
236   if (parse_rtattr (attrs, IFLA_MAX, IFLA_RTA (msg), msg_len) != 0)
237   {
238     ERROR ("netlink plugin: link_filter: parse_rtattr failed.");
239     return (-1);
240   }
241
242   if (attrs[IFLA_IFNAME] == NULL)
243   {
244     ERROR ("netlink plugin: link_filter: attrs[IFLA_IFNAME] == NULL");
245     return (-1);
246   }
247   dev = RTA_DATA (attrs[IFLA_IFNAME]);
248
249   /* Update the `iflist'. It's used to know which interfaces exist and query
250    * them later for qdiscs and classes. */
251   if (msg->ifi_index >= iflist_len)
252   {
253     char **temp;
254
255     temp = (char **) realloc (iflist, (msg->ifi_index + 1) * sizeof (char *));
256     if (temp == NULL)
257     {
258       ERROR ("netlink plugin: link_filter: realloc failed.");
259       return (-1);
260     }
261
262     memset (temp + iflist_len, '\0',
263         (msg->ifi_index + 1 - iflist_len) * sizeof (char *));
264     iflist = temp;
265     iflist_len = msg->ifi_index + 1;
266   }
267   if ((iflist[msg->ifi_index] == NULL)
268       || (strcmp (iflist[msg->ifi_index], dev) != 0))
269   {
270     sfree (iflist[msg->ifi_index]);
271     iflist[msg->ifi_index] = strdup (dev);
272   }
273
274   if (attrs[IFLA_STATS] == NULL)
275   {
276     DEBUG ("netlink plugin: link_filter: No statistics for interface %s.", dev);
277     return (0);
278   }
279   stats = RTA_DATA (attrs[IFLA_STATS]);
280
281   if (check_ignorelist (dev, "interface", NULL) == 0)
282   {
283     submit_two (dev, "if_octets", NULL, stats->rx_bytes, stats->tx_bytes);
284     submit_two (dev, "if_packets", NULL, stats->rx_packets, stats->tx_packets);
285     submit_two (dev, "if_errors", NULL, stats->rx_errors, stats->tx_errors);
286   }
287   else
288   {
289     DEBUG ("netlink plugin: Ignoring %s/interface.", dev);
290   }
291
292   if (check_ignorelist (dev, "if_detail", NULL) == 0)
293   {
294     submit_two (dev, "if_dropped", NULL, stats->rx_dropped, stats->tx_dropped);
295     submit_one (dev, "if_multicast", NULL, stats->multicast);
296     submit_one (dev, "if_collisions", NULL, stats->collisions);
297
298     submit_one (dev, "if_rx_errors", "length", stats->rx_length_errors);
299     submit_one (dev, "if_rx_errors", "over", stats->rx_over_errors);
300     submit_one (dev, "if_rx_errors", "crc", stats->rx_crc_errors);
301     submit_one (dev, "if_rx_errors", "frame", stats->rx_frame_errors);
302     submit_one (dev, "if_rx_errors", "fifo", stats->rx_fifo_errors);
303     submit_one (dev, "if_rx_errors", "missed", stats->rx_missed_errors);
304
305     submit_one (dev, "if_tx_errors", "aborted", stats->tx_aborted_errors);
306     submit_one (dev, "if_tx_errors", "carrier", stats->tx_carrier_errors);
307     submit_one (dev, "if_tx_errors", "fifo", stats->tx_fifo_errors);
308     submit_one (dev, "if_tx_errors", "heartbeat", stats->tx_heartbeat_errors);
309     submit_one (dev, "if_tx_errors", "window", stats->tx_window_errors);
310   }
311   else
312   {
313     DEBUG ("netlink plugin: Ignoring %s/if_detail.", dev);
314   }
315
316   return (0);
317 } /* int link_filter */
318
319 static int qos_filter (const struct sockaddr_nl *sa,
320     const struct nlmsghdr *nmh, void *args)
321 {
322   struct tcmsg *msg;
323   int msg_len;
324   struct rtattr *attrs[TCA_MAX + 1];
325
326   int wanted_ifindex = *((int *) args);
327
328   const char *dev;
329
330   /* char *type_instance; */
331   char *tc_type;
332   char tc_inst[DATA_MAX_NAME_LEN];
333
334   if (nmh->nlmsg_type == RTM_NEWQDISC)
335     tc_type = "qdisc";
336   else if (nmh->nlmsg_type == RTM_NEWTCLASS)
337     tc_type = "class";
338   else if (nmh->nlmsg_type == RTM_NEWTFILTER)
339     tc_type = "filter";
340   else
341   {
342     ERROR ("netlink plugin: qos_filter: Don't know how to handle type %i.",
343         nmh->nlmsg_type);
344     return (-1);
345   }
346
347   msg = NLMSG_DATA (nmh);
348
349   msg_len = nmh->nlmsg_len - sizeof (struct tcmsg);
350   if (msg_len < 0)
351   {
352     ERROR ("netlink plugin: qos_filter: msg_len = %i < 0;", msg_len);
353     return (-1);
354   }
355
356   if (msg->tcm_ifindex != wanted_ifindex)
357   {
358     DEBUG ("netlink plugin: qos_filter: Got %s for interface #%i, "
359         "but expected #%i.",
360         tc_type, msg->tcm_ifindex, wanted_ifindex);
361     return (0);
362   }
363
364   if (msg->tcm_ifindex >= iflist_len)
365   {
366     ERROR ("netlink plugin: qos_filter: msg->tcm_ifindex = %i "
367         ">= iflist_len = %i",
368         msg->tcm_ifindex, iflist_len);
369     return (-1);
370   }
371
372   dev = iflist[msg->tcm_ifindex];
373   if (dev == NULL)
374   {
375     ERROR ("netlink plugin: qos_filter: iflist[%i] == NULL",
376         msg->tcm_ifindex);
377     return (-1);
378   }
379
380   memset (attrs, '\0', sizeof (attrs));
381   if (parse_rtattr (attrs, TCA_MAX, TCA_RTA (msg), msg_len) != 0)
382   {
383     ERROR ("netlink plugin: qos_filter: parse_rtattr failed.");
384     return (-1);
385   }
386
387   if (attrs[TCA_KIND] == NULL)
388   {
389     ERROR ("netlink plugin: qos_filter: attrs[TCA_KIND] == NULL");
390     return (-1);
391   }
392
393   { /* The the ID */
394     uint32_t numberic_id;
395
396     numberic_id = msg->tcm_handle;
397     if (strcmp (tc_type, "filter") == 0)
398       numberic_id = msg->tcm_parent;
399
400     snprintf (tc_inst, sizeof (tc_inst), "%s-%x:%x",
401         (const char *) RTA_DATA (attrs[TCA_KIND]),
402         numberic_id >> 16,
403         numberic_id & 0x0000FFFF);
404     tc_inst[sizeof (tc_inst) - 1] = '\0';
405   }
406
407   DEBUG ("netlink plugin: qos_filter: got %s for %s (%i).",
408       tc_type, dev, msg->tcm_ifindex);
409   
410   if (check_ignorelist (dev, tc_type, tc_inst))
411     return (0);
412
413 #if HAVE_TCA_STATS2
414   if (attrs[TCA_STATS2])
415   {
416     struct rtattr *attrs_stats[TCA_STATS_MAX + 1];
417
418     memset (attrs_stats, '\0', sizeof (attrs_stats));
419     parse_rtattr_nested (attrs_stats, TCA_STATS_MAX, attrs[TCA_STATS2]);
420
421     if (attrs_stats[TCA_STATS_BASIC])
422     {
423       struct gnet_stats_basic bs;
424       char type_instance[DATA_MAX_NAME_LEN];
425
426       snprintf (type_instance, sizeof (type_instance), "%s-%s",
427           tc_type, tc_inst);
428       type_instance[sizeof (type_instance) - 1] = '\0';
429
430       memset (&bs, '\0', sizeof (bs));
431       memcpy (&bs, RTA_DATA (attrs_stats[TCA_STATS_BASIC]),
432           MIN (RTA_PAYLOAD (attrs_stats[TCA_STATS_BASIC]), sizeof(bs)));
433
434       submit_one (dev, "ipt_bytes", type_instance, bs.bytes);
435       submit_one (dev, "ipt_packets", type_instance, bs.packets);
436     }
437   }
438 #endif /* TCA_STATS2 */
439 #if HAVE_TCA_STATS && HAVE_TCA_STATS2
440   else
441 #endif
442 #if HAVE_TCA_STATS
443   if (attrs[TCA_STATS] != NULL)
444   {
445     struct tc_stats ts;
446     char type_instance[DATA_MAX_NAME_LEN];
447
448     snprintf (type_instance, sizeof (type_instance), "%s-%s",
449         tc_type, tc_inst);
450     type_instance[sizeof (type_instance) - 1] = '\0';
451
452     memset(&ts, '\0', sizeof (ts));
453     memcpy(&ts, RTA_DATA (attrs[TCA_STATS]),
454         MIN (RTA_PAYLOAD (attrs[TCA_STATS]), sizeof (ts)));
455
456     submit_one (dev, "ipt_bytes", type_instance, ts.bytes);
457     submit_one (dev, "ipt_packets", type_instance, ts.packets);
458   }
459 #endif /* TCA_STATS */
460 #if HAVE_TCA_STATS || HAVE_TCA_STATS2
461   else
462 #endif
463   {
464     DEBUG ("netlink plugin: qos_filter: Have neither TCA_STATS2 nor "
465         "TCA_STATS.");
466   }
467
468   return (0);
469 } /* int qos_filter */
470
471 static int ir_config (const char *key, const char *value)
472 {
473   char *new_val;
474   char *fields[8];
475   int fields_num;
476   int status = 1;
477
478   new_val = strdup (value);
479   if (new_val == NULL)
480     return (-1);
481
482   fields_num = strsplit (new_val, fields, STATIC_ARRAY_SIZE (fields));
483   if ((fields_num < 1) || (fields_num > 8))
484   {
485     sfree (new_val);
486     return (-1);
487   }
488
489   if ((strcasecmp (key, "Interface") == 0)
490       || (strcasecmp (key, "VerboseInterface") == 0))
491   {
492     if (fields_num != 1)
493     {
494       ERROR ("netlink plugin: Invalid number of fields for option "
495           "`%s'. Got %i, expected 1.", key, fields_num);
496       status = -1;
497     }
498     else
499     {
500       add_ignorelist (fields[0], "interface", NULL);
501       if (strcasecmp (key, "VerboseInterface") == 0)
502         add_ignorelist (fields[0], "if_detail", NULL);
503       status = 0;
504     }
505   }
506   else if ((strcasecmp (key, "QDisc") == 0)
507       || (strcasecmp (key, "Class") == 0)
508       || (strcasecmp (key, "Filter") == 0))
509   {
510     if ((fields_num < 1) || (fields_num > 2))
511     {
512       ERROR ("netlink plugin: Invalid number of fields for option "
513           "`%s'. Got %i, expected 1 or 2.", key, fields_num);
514       return (-1);
515     }
516     else
517     {
518       add_ignorelist (fields[0], key,
519           (fields_num == 2) ? fields[1] : NULL);
520       status = 0;
521     }
522   }
523   else if (strcasecmp (key, "IgnoreSelected") == 0)
524   {
525     if (fields_num != 1)
526     {
527       ERROR ("netlink plugin: Invalid number of fields for option "
528           "`IgnoreSelected'. Got %i, expected 1.", fields_num);
529       status = -1;
530     }
531     else
532     {
533       if ((strcasecmp (fields[0], "yes") == 0)
534           || (strcasecmp (fields[0], "true") == 0)
535           || (strcasecmp (fields[0], "on") == 0))
536         ir_ignorelist_invert = 0;
537       else
538         ir_ignorelist_invert = 1;
539       status = 0;
540     }
541   }
542
543   sfree (new_val);
544
545   return (status);
546 } /* int ir_config */
547
548 static int ir_init (void)
549 {
550   memset (&rth, '\0', sizeof (rth));
551
552   if (rtnl_open (&rth, 0) != 0)
553   {
554     ERROR ("netlink plugin: ir_init: rtnl_open failed.");
555     return (-1);
556   }
557
558   return (0);
559 } /* int ir_init */
560
561 static int ir_read (void)
562 {
563   struct ifinfomsg im;
564   struct tcmsg tm;
565   int ifindex;
566
567   static const int type_id[] = { RTM_GETQDISC, RTM_GETTCLASS, RTM_GETTFILTER };
568   static const char *type_name[] = { "qdisc", "class", "filter" };
569
570   memset (&im, '\0', sizeof (im));
571   im.ifi_type = AF_UNSPEC;
572
573   if (rtnl_dump_request (&rth, RTM_GETLINK, &im, sizeof (im)) < 0)
574   {
575     ERROR ("netlink plugin: ir_read: rtnl_dump_request failed.");
576     return (-1);
577   }
578
579   if (rtnl_dump_filter (&rth, link_filter, /* arg1 = */ NULL,
580         NULL, NULL) != 0)
581   {
582     ERROR ("netlink plugin: ir_read: rtnl_dump_filter failed.");
583     return (-1);
584   }
585
586   /* `link_filter' will update `iflist' which is used here to iterate over all
587    * interfaces. */
588   for (ifindex = 0; ifindex < iflist_len; ifindex++)
589   {
590     int type_index;
591
592     if (iflist[ifindex] == NULL)
593       continue;
594
595     for (type_index = 0; type_index < STATIC_ARRAY_SIZE (type_id); type_index++)
596     {
597       if (check_ignorelist (iflist[ifindex], type_name[type_index], NULL))
598       {
599         DEBUG ("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
600             "== TRUE", iflist[ifindex], type_name[type_index]);
601         continue;
602       }
603
604       DEBUG ("netlink plugin: ir_read: querying %s from %s (%i).",
605           type_name[type_index], iflist[ifindex], ifindex);
606
607       memset (&tm, '\0', sizeof (tm));
608       tm.tcm_family = AF_UNSPEC;
609       tm.tcm_ifindex = ifindex;
610
611       if (rtnl_dump_request (&rth, type_id[type_index], &tm, sizeof (tm)) < 0)
612       {
613         ERROR ("netlink plugin: ir_read: rtnl_dump_request failed.");
614         continue;
615       }
616
617       if (rtnl_dump_filter (&rth, qos_filter, (void *) &ifindex,
618             NULL, NULL) != 0)
619       {
620         ERROR ("netlink plugin: ir_read: rtnl_dump_filter failed.");
621         continue;
622       }
623     } /* for (type_index) */
624   } /* for (if_index) */
625
626   return (0);
627 } /* int ir_read */
628
629 static int ir_shutdown (void)
630 {
631   if ((rth.fd != 0) || (rth.seq != 0) || (rth.dump != 0))
632   {
633     rtnl_close(&rth);
634     memset (&rth, '\0', sizeof (rth));
635   }
636   
637   return (0);
638 } /* int ir_shutdown */
639
640 void module_register (void)
641 {
642   plugin_register_config ("netlink", ir_config, config_keys, config_keys_num);
643   plugin_register_init ("netlink", ir_init);
644   plugin_register_read ("netlink", ir_read);
645   plugin_register_shutdown ("netlink", ir_shutdown);
646 } /* void module_register */
647
648 /*
649  * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
650  */