Merge branch 'collectd-4.4' into collectd-4.5
[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   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
175   sstrncpy (vl.plugin, "netlink", sizeof (vl.plugin));
176   sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
177   sstrncpy (vl.type, type, sizeof (vl.type));
178
179   if (type_instance != NULL)
180     sstrncpy (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   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
199   sstrncpy (vl.plugin, "netlink", sizeof (vl.plugin));
200   sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
201   sstrncpy (vl.type, type, sizeof (vl.type));
202
203   if (type_instance != NULL)
204     sstrncpy (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     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     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     ssnprintf (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   }
405
406   DEBUG ("netlink plugin: qos_filter: got %s for %s (%i).",
407       tc_type, dev, msg->tcm_ifindex);
408   
409   if (check_ignorelist (dev, tc_type, tc_inst))
410     return (0);
411
412 #if HAVE_TCA_STATS2
413   if (attrs[TCA_STATS2])
414   {
415     struct rtattr *attrs_stats[TCA_STATS_MAX + 1];
416
417     memset (attrs_stats, '\0', sizeof (attrs_stats));
418     parse_rtattr_nested (attrs_stats, TCA_STATS_MAX, attrs[TCA_STATS2]);
419
420     if (attrs_stats[TCA_STATS_BASIC])
421     {
422       struct gnet_stats_basic bs;
423       char type_instance[DATA_MAX_NAME_LEN];
424
425       ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
426           tc_type, tc_inst);
427
428       memset (&bs, '\0', sizeof (bs));
429       memcpy (&bs, RTA_DATA (attrs_stats[TCA_STATS_BASIC]),
430           MIN (RTA_PAYLOAD (attrs_stats[TCA_STATS_BASIC]), sizeof(bs)));
431
432       submit_one (dev, "ipt_bytes", type_instance, bs.bytes);
433       submit_one (dev, "ipt_packets", type_instance, bs.packets);
434     }
435   }
436 #endif /* TCA_STATS2 */
437 #if HAVE_TCA_STATS && HAVE_TCA_STATS2
438   else
439 #endif
440 #if HAVE_TCA_STATS
441   if (attrs[TCA_STATS] != NULL)
442   {
443     struct tc_stats ts;
444     char type_instance[DATA_MAX_NAME_LEN];
445
446     ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
447         tc_type, tc_inst);
448
449     memset(&ts, '\0', sizeof (ts));
450     memcpy(&ts, RTA_DATA (attrs[TCA_STATS]),
451         MIN (RTA_PAYLOAD (attrs[TCA_STATS]), sizeof (ts)));
452
453     submit_one (dev, "ipt_bytes", type_instance, ts.bytes);
454     submit_one (dev, "ipt_packets", type_instance, ts.packets);
455   }
456 #endif /* TCA_STATS */
457 #if HAVE_TCA_STATS || HAVE_TCA_STATS2
458   else
459 #endif
460   {
461     DEBUG ("netlink plugin: qos_filter: Have neither TCA_STATS2 nor "
462         "TCA_STATS.");
463   }
464
465   return (0);
466 } /* int qos_filter */
467
468 static int ir_config (const char *key, const char *value)
469 {
470   char *new_val;
471   char *fields[8];
472   int fields_num;
473   int status = 1;
474
475   new_val = strdup (value);
476   if (new_val == NULL)
477     return (-1);
478
479   fields_num = strsplit (new_val, fields, STATIC_ARRAY_SIZE (fields));
480   if ((fields_num < 1) || (fields_num > 8))
481   {
482     sfree (new_val);
483     return (-1);
484   }
485
486   if ((strcasecmp (key, "Interface") == 0)
487       || (strcasecmp (key, "VerboseInterface") == 0))
488   {
489     if (fields_num != 1)
490     {
491       ERROR ("netlink plugin: Invalid number of fields for option "
492           "`%s'. Got %i, expected 1.", key, fields_num);
493       status = -1;
494     }
495     else
496     {
497       add_ignorelist (fields[0], "interface", NULL);
498       if (strcasecmp (key, "VerboseInterface") == 0)
499         add_ignorelist (fields[0], "if_detail", NULL);
500       status = 0;
501     }
502   }
503   else if ((strcasecmp (key, "QDisc") == 0)
504       || (strcasecmp (key, "Class") == 0)
505       || (strcasecmp (key, "Filter") == 0))
506   {
507     if ((fields_num < 1) || (fields_num > 2))
508     {
509       ERROR ("netlink plugin: Invalid number of fields for option "
510           "`%s'. Got %i, expected 1 or 2.", key, fields_num);
511       return (-1);
512     }
513     else
514     {
515       add_ignorelist (fields[0], key,
516           (fields_num == 2) ? fields[1] : NULL);
517       status = 0;
518     }
519   }
520   else if (strcasecmp (key, "IgnoreSelected") == 0)
521   {
522     if (fields_num != 1)
523     {
524       ERROR ("netlink plugin: Invalid number of fields for option "
525           "`IgnoreSelected'. Got %i, expected 1.", fields_num);
526       status = -1;
527     }
528     else
529     {
530       if ((strcasecmp (fields[0], "yes") == 0)
531           || (strcasecmp (fields[0], "true") == 0)
532           || (strcasecmp (fields[0], "on") == 0))
533         ir_ignorelist_invert = 0;
534       else
535         ir_ignorelist_invert = 1;
536       status = 0;
537     }
538   }
539
540   sfree (new_val);
541
542   return (status);
543 } /* int ir_config */
544
545 static int ir_init (void)
546 {
547   memset (&rth, '\0', sizeof (rth));
548
549   if (rtnl_open (&rth, 0) != 0)
550   {
551     ERROR ("netlink plugin: ir_init: rtnl_open failed.");
552     return (-1);
553   }
554
555   return (0);
556 } /* int ir_init */
557
558 static int ir_read (void)
559 {
560   struct ifinfomsg im;
561   struct tcmsg tm;
562   int ifindex;
563
564   static const int type_id[] = { RTM_GETQDISC, RTM_GETTCLASS, RTM_GETTFILTER };
565   static const char *type_name[] = { "qdisc", "class", "filter" };
566
567   memset (&im, '\0', sizeof (im));
568   im.ifi_type = AF_UNSPEC;
569
570   if (rtnl_dump_request (&rth, RTM_GETLINK, &im, sizeof (im)) < 0)
571   {
572     ERROR ("netlink plugin: ir_read: rtnl_dump_request failed.");
573     return (-1);
574   }
575
576   if (rtnl_dump_filter (&rth, link_filter, /* arg1 = */ NULL,
577         NULL, NULL) != 0)
578   {
579     ERROR ("netlink plugin: ir_read: rtnl_dump_filter failed.");
580     return (-1);
581   }
582
583   /* `link_filter' will update `iflist' which is used here to iterate over all
584    * interfaces. */
585   for (ifindex = 0; ifindex < iflist_len; ifindex++)
586   {
587     int type_index;
588
589     if (iflist[ifindex] == NULL)
590       continue;
591
592     for (type_index = 0; type_index < STATIC_ARRAY_SIZE (type_id); type_index++)
593     {
594       if (check_ignorelist (iflist[ifindex], type_name[type_index], NULL))
595       {
596         DEBUG ("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
597             "== TRUE", iflist[ifindex], type_name[type_index]);
598         continue;
599       }
600
601       DEBUG ("netlink plugin: ir_read: querying %s from %s (%i).",
602           type_name[type_index], iflist[ifindex], ifindex);
603
604       memset (&tm, '\0', sizeof (tm));
605       tm.tcm_family = AF_UNSPEC;
606       tm.tcm_ifindex = ifindex;
607
608       if (rtnl_dump_request (&rth, type_id[type_index], &tm, sizeof (tm)) < 0)
609       {
610         ERROR ("netlink plugin: ir_read: rtnl_dump_request failed.");
611         continue;
612       }
613
614       if (rtnl_dump_filter (&rth, qos_filter, (void *) &ifindex,
615             NULL, NULL) != 0)
616       {
617         ERROR ("netlink plugin: ir_read: rtnl_dump_filter failed.");
618         continue;
619       }
620     } /* for (type_index) */
621   } /* for (if_index) */
622
623   return (0);
624 } /* int ir_read */
625
626 static int ir_shutdown (void)
627 {
628   if ((rth.fd != 0) || (rth.seq != 0) || (rth.dump != 0))
629   {
630     rtnl_close(&rth);
631     memset (&rth, '\0', sizeof (rth));
632   }
633   
634   return (0);
635 } /* int ir_shutdown */
636
637 void module_register (void)
638 {
639   plugin_register_config ("netlink", ir_config, config_keys, config_keys_num);
640   plugin_register_init ("netlink", ir_init);
641   plugin_register_read ("netlink", ir_read);
642   plugin_register_shutdown ("netlink", ir_shutdown);
643 } /* void module_register */
644
645 /*
646  * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
647  */