2 * collectd - src/netlink.c
3 * Copyright (C) 2007 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
26 #include <asm/types.h>
27 #include <sys/socket.h>
29 #include <linux/netlink.h>
30 #include <linux/rtnetlink.h>
31 #if HAVE_LINUX_GEN_STATS_H
32 # include <linux/gen_stats.h>
34 #if HAVE_LINUX_PKT_SCHED_H
35 # include <linux/pkt_sched.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>
46 typedef struct ir_ignorelist_s
51 struct ir_ignorelist_s *next;
54 static int ir_ignorelist_invert = 1;
55 static ir_ignorelist_t *ir_ignorelist_head = NULL;
57 static struct rtnl_handle rth;
59 static char **iflist = NULL;
60 static size_t iflist_len = 0;
62 static const char *config_keys[] =
71 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
73 static int add_ignorelist (const char *dev, const char *type,
76 ir_ignorelist_t *entry;
78 entry = (ir_ignorelist_t *) malloc (sizeof (ir_ignorelist_t));
82 memset (entry, '\0', sizeof (ir_ignorelist_t));
84 if (strcasecmp (dev, "All") != 0)
86 entry->device = strdup (dev);
87 if (entry->device == NULL)
94 entry->type = strdup (type);
95 if (entry->type == NULL)
97 sfree (entry->device);
104 entry->inst = strdup (inst);
105 if (entry->inst == NULL)
108 sfree (entry->device);
114 entry->next = ir_ignorelist_head;
115 ir_ignorelist_head = entry;
118 } /* int add_ignorelist */
121 * Checks wether a data set should be ignored. Returns `true' is the value
122 * should be ignored, `false' otherwise.
124 static int check_ignorelist (const char *dev,
125 const char *type, const char *type_instance)
129 assert ((dev != NULL) && (type != NULL));
131 if (ir_ignorelist_head == NULL)
132 return (ir_ignorelist_invert ? 0 : 1);
134 for (i = ir_ignorelist_head; i != NULL; i = i->next)
136 /* i->device == NULL => match all devices */
137 if ((i->device != NULL)
138 && (strcasecmp (i->device, dev) != 0))
141 if (strcasecmp (i->type, type) != 0)
144 if ((i->inst != NULL) && (type_instance != NULL)
145 && (strcasecmp (i->inst, type_instance) != 0))
148 DEBUG ("netlink plugin: check_ignorelist: "
149 "(dev = %s; type = %s; inst = %s) matched "
150 "(dev = %s; type = %s; inst = %s)",
152 type_instance == NULL ? "(nil)" : type_instance,
153 i->device == NULL ? "(nil)" : i->device,
155 i->inst == NULL ? "(nil)" : i->inst);
157 return (ir_ignorelist_invert ? 0 : 1);
160 return (ir_ignorelist_invert);
161 } /* int check_ignorelist */
163 static void submit_one (const char *dev, const char *type,
164 const char *type_instance, counter_t value)
167 value_list_t vl = VALUE_LIST_INIT;
169 values[0].counter = value;
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));
179 if (type_instance != NULL)
180 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
182 plugin_dispatch_values (&vl);
183 } /* void submit_one */
185 static void submit_two (const char *dev, const char *type,
186 const char *type_instance,
187 counter_t rx, counter_t tx)
190 value_list_t vl = VALUE_LIST_INIT;
192 values[0].counter = rx;
193 values[1].counter = tx;
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));
203 if (type_instance != NULL)
204 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
206 plugin_dispatch_values (&vl);
207 } /* void submit_two */
209 static int link_filter (const struct sockaddr_nl *sa,
210 struct nlmsghdr *nmh, void *args)
212 struct ifinfomsg *msg;
214 struct rtattr *attrs[IFLA_MAX + 1];
215 struct rtnl_link_stats *stats;
219 if (nmh->nlmsg_type != RTM_NEWLINK)
221 ERROR ("netlink plugin: link_filter: Don't know how to handle type %i.",
226 msg = NLMSG_DATA (nmh);
228 msg_len = nmh->nlmsg_len - sizeof (struct ifinfomsg);
231 ERROR ("netlink plugin: link_filter: msg_len = %i < 0;", msg_len);
235 memset (attrs, '\0', sizeof (attrs));
236 if (parse_rtattr (attrs, IFLA_MAX, IFLA_RTA (msg), msg_len) != 0)
238 ERROR ("netlink plugin: link_filter: parse_rtattr failed.");
242 if (attrs[IFLA_IFNAME] == NULL)
244 ERROR ("netlink plugin: link_filter: attrs[IFLA_IFNAME] == NULL");
247 dev = RTA_DATA (attrs[IFLA_IFNAME]);
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)
255 temp = (char **) realloc (iflist, (msg->ifi_index + 1) * sizeof (char *));
258 ERROR ("netlink plugin: link_filter: realloc failed.");
262 memset (temp + iflist_len, '\0',
263 (msg->ifi_index + 1 - iflist_len) * sizeof (char *));
265 iflist_len = msg->ifi_index + 1;
267 if ((iflist[msg->ifi_index] == NULL)
268 || (strcmp (iflist[msg->ifi_index], dev) != 0))
270 sfree (iflist[msg->ifi_index]);
271 iflist[msg->ifi_index] = strdup (dev);
274 if (attrs[IFLA_STATS] == NULL)
276 DEBUG ("netlink plugin: link_filter: No statistics for interface %s.", dev);
279 stats = RTA_DATA (attrs[IFLA_STATS]);
281 if (check_ignorelist (dev, "interface", NULL) == 0)
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);
289 DEBUG ("netlink plugin: Ignoring %s/interface.", dev);
292 if (check_ignorelist (dev, "if_detail", NULL) == 0)
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);
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);
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);
313 DEBUG ("netlink plugin: Ignoring %s/if_detail.", dev);
317 } /* int link_filter */
319 static int qos_filter (const struct sockaddr_nl *sa,
320 struct nlmsghdr *nmh, void *args)
324 struct rtattr *attrs[TCA_MAX + 1];
326 int wanted_ifindex = *((int *) args);
330 /* char *type_instance; */
332 char tc_inst[DATA_MAX_NAME_LEN];
334 if (nmh->nlmsg_type == RTM_NEWQDISC)
336 else if (nmh->nlmsg_type == RTM_NEWTCLASS)
338 else if (nmh->nlmsg_type == RTM_NEWTFILTER)
342 ERROR ("netlink plugin: qos_filter: Don't know how to handle type %i.",
347 msg = NLMSG_DATA (nmh);
349 msg_len = nmh->nlmsg_len - sizeof (struct tcmsg);
352 ERROR ("netlink plugin: qos_filter: msg_len = %i < 0;", msg_len);
356 if (msg->tcm_ifindex != wanted_ifindex)
358 DEBUG ("netlink plugin: qos_filter: Got %s for interface #%i, "
360 tc_type, msg->tcm_ifindex, wanted_ifindex);
364 if (msg->tcm_ifindex >= iflist_len)
366 ERROR ("netlink plugin: qos_filter: msg->tcm_ifindex = %i "
367 ">= iflist_len = %zu",
368 msg->tcm_ifindex, iflist_len);
372 dev = iflist[msg->tcm_ifindex];
375 ERROR ("netlink plugin: qos_filter: iflist[%i] == NULL",
380 memset (attrs, '\0', sizeof (attrs));
381 if (parse_rtattr (attrs, TCA_MAX, TCA_RTA (msg), msg_len) != 0)
383 ERROR ("netlink plugin: qos_filter: parse_rtattr failed.");
387 if (attrs[TCA_KIND] == NULL)
389 ERROR ("netlink plugin: qos_filter: attrs[TCA_KIND] == NULL");
394 uint32_t numberic_id;
396 numberic_id = msg->tcm_handle;
397 if (strcmp (tc_type, "filter") == 0)
398 numberic_id = msg->tcm_parent;
400 ssnprintf (tc_inst, sizeof (tc_inst), "%s-%x:%x",
401 (const char *) RTA_DATA (attrs[TCA_KIND]),
403 numberic_id & 0x0000FFFF);
406 DEBUG ("netlink plugin: qos_filter: got %s for %s (%i).",
407 tc_type, dev, msg->tcm_ifindex);
409 if (check_ignorelist (dev, tc_type, tc_inst))
413 if (attrs[TCA_STATS2])
415 struct rtattr *attrs_stats[TCA_STATS_MAX + 1];
417 memset (attrs_stats, '\0', sizeof (attrs_stats));
418 parse_rtattr_nested (attrs_stats, TCA_STATS_MAX, attrs[TCA_STATS2]);
420 if (attrs_stats[TCA_STATS_BASIC])
422 struct gnet_stats_basic bs;
423 char type_instance[DATA_MAX_NAME_LEN];
425 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
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)));
432 submit_one (dev, "ipt_bytes", type_instance, bs.bytes);
433 submit_one (dev, "ipt_packets", type_instance, bs.packets);
436 #endif /* TCA_STATS2 */
437 #if HAVE_TCA_STATS && HAVE_TCA_STATS2
441 if (attrs[TCA_STATS] != NULL)
444 char type_instance[DATA_MAX_NAME_LEN];
446 ssnprintf (type_instance, sizeof (type_instance), "%s-%s",
449 memset(&ts, '\0', sizeof (ts));
450 memcpy(&ts, RTA_DATA (attrs[TCA_STATS]),
451 MIN (RTA_PAYLOAD (attrs[TCA_STATS]), sizeof (ts)));
453 submit_one (dev, "ipt_bytes", type_instance, ts.bytes);
454 submit_one (dev, "ipt_packets", type_instance, ts.packets);
456 #endif /* TCA_STATS */
457 #if HAVE_TCA_STATS || HAVE_TCA_STATS2
461 DEBUG ("netlink plugin: qos_filter: Have neither TCA_STATS2 nor "
466 } /* int qos_filter */
468 static int ir_config (const char *key, const char *value)
475 new_val = strdup (value);
479 fields_num = strsplit (new_val, fields, STATIC_ARRAY_SIZE (fields));
480 if ((fields_num < 1) || (fields_num > 8))
486 if ((strcasecmp (key, "Interface") == 0)
487 || (strcasecmp (key, "VerboseInterface") == 0))
491 ERROR ("netlink plugin: Invalid number of fields for option "
492 "`%s'. Got %i, expected 1.", key, fields_num);
497 add_ignorelist (fields[0], "interface", NULL);
498 if (strcasecmp (key, "VerboseInterface") == 0)
499 add_ignorelist (fields[0], "if_detail", NULL);
503 else if ((strcasecmp (key, "QDisc") == 0)
504 || (strcasecmp (key, "Class") == 0)
505 || (strcasecmp (key, "Filter") == 0))
507 if ((fields_num < 1) || (fields_num > 2))
509 ERROR ("netlink plugin: Invalid number of fields for option "
510 "`%s'. Got %i, expected 1 or 2.", key, fields_num);
515 add_ignorelist (fields[0], key,
516 (fields_num == 2) ? fields[1] : NULL);
520 else if (strcasecmp (key, "IgnoreSelected") == 0)
524 ERROR ("netlink plugin: Invalid number of fields for option "
525 "`IgnoreSelected'. Got %i, expected 1.", fields_num);
530 if ((strcasecmp (fields[0], "yes") == 0)
531 || (strcasecmp (fields[0], "true") == 0)
532 || (strcasecmp (fields[0], "on") == 0))
533 ir_ignorelist_invert = 0;
535 ir_ignorelist_invert = 1;
543 } /* int ir_config */
545 static int ir_init (void)
547 memset (&rth, '\0', sizeof (rth));
549 if (rtnl_open (&rth, 0) != 0)
551 ERROR ("netlink plugin: ir_init: rtnl_open failed.");
558 static int ir_read (void)
564 static const int type_id[] = { RTM_GETQDISC, RTM_GETTCLASS, RTM_GETTFILTER };
565 static const char *type_name[] = { "qdisc", "class", "filter" };
567 memset (&im, '\0', sizeof (im));
568 im.ifi_type = AF_UNSPEC;
570 if (rtnl_dump_request (&rth, RTM_GETLINK, &im, sizeof (im)) < 0)
572 ERROR ("netlink plugin: ir_read: rtnl_dump_request failed.");
576 if (rtnl_dump_filter (&rth, link_filter, /* arg1 = */ NULL,
579 ERROR ("netlink plugin: ir_read: rtnl_dump_filter failed.");
583 /* `link_filter' will update `iflist' which is used here to iterate over all
585 for (ifindex = 0; ifindex < iflist_len; ifindex++)
589 if (iflist[ifindex] == NULL)
592 for (type_index = 0; type_index < STATIC_ARRAY_SIZE (type_id); type_index++)
594 if (check_ignorelist (iflist[ifindex], type_name[type_index], NULL))
596 DEBUG ("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
597 "== TRUE", iflist[ifindex], type_name[type_index]);
601 DEBUG ("netlink plugin: ir_read: querying %s from %s (%i).",
602 type_name[type_index], iflist[ifindex], ifindex);
604 memset (&tm, '\0', sizeof (tm));
605 tm.tcm_family = AF_UNSPEC;
606 tm.tcm_ifindex = ifindex;
608 if (rtnl_dump_request (&rth, type_id[type_index], &tm, sizeof (tm)) < 0)
610 ERROR ("netlink plugin: ir_read: rtnl_dump_request failed.");
614 if (rtnl_dump_filter (&rth, qos_filter, (void *) &ifindex,
617 ERROR ("netlink plugin: ir_read: rtnl_dump_filter failed.");
620 } /* for (type_index) */
621 } /* for (if_index) */
626 static int ir_shutdown (void)
628 if ((rth.fd != 0) || (rth.seq != 0) || (rth.dump != 0))
631 memset (&rth, '\0', sizeof (rth));
635 } /* int ir_shutdown */
637 void module_register (void)
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 */
646 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :