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>
28 #include <linux/netlink.h>
29 #include <linux/rtnetlink.h>
30 #include <linux/gen_stats.h>
33 # include <libnetlink.h>
34 #elif HAVE_IPROUTE_LIBNETLINK_H
35 # include <iproute/libnetlink.h>
36 #elif HAVE_LINUX_LIBNETLINK_H
37 # include <linux/libnetlink.h>
40 typedef struct ir_ignorelist_s
45 struct ir_ignorelist_s *next;
48 static int ir_ignorelist_invert = 1;
49 static ir_ignorelist_t *ir_ignorelist_head = NULL;
51 static struct rtnl_handle rth;
53 static char **iflist = NULL;
54 static size_t iflist_len = 0;
56 static const char *config_keys[] =
65 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
67 static int add_ignorelist (const char *dev, const char *type,
70 ir_ignorelist_t *entry;
72 entry = (ir_ignorelist_t *) malloc (sizeof (ir_ignorelist_t));
76 memset (entry, '\0', sizeof (ir_ignorelist_t));
78 if (strcasecmp (dev, "All") != 0)
80 entry->device = strdup (dev);
81 if (entry->device == NULL)
88 entry->type = strdup (type);
89 if (entry->type == NULL)
91 sfree (entry->device);
98 entry->inst = strdup (inst);
99 if (entry->inst == NULL)
102 sfree (entry->device);
108 entry->next = ir_ignorelist_head;
109 ir_ignorelist_head = entry;
112 } /* int add_ignorelist */
115 * Checks wether a data set should be ignored. Returns `true' is the value
116 * should be ignored, `false' otherwise.
118 static int check_ignorelist (const char *dev,
119 const char *type, const char *type_instance)
123 assert ((dev != NULL) && (type != NULL));
125 if (ir_ignorelist_head == NULL)
126 return (ir_ignorelist_invert ? 0 : 1);
128 for (i = ir_ignorelist_head; i != NULL; i = i->next)
130 /* i->device == NULL => match all devices */
131 if ((i->device != NULL)
132 && (strcasecmp (i->device, dev) != 0))
135 if (strcasecmp (i->type, type) != 0)
138 if ((i->inst != NULL) && (type_instance != NULL)
139 && (strcasecmp (i->inst, type_instance) != 0))
142 DEBUG ("netlink plugin: check_ignorelist: "
143 "(dev = %s; type = %s; inst = %s) matched "
144 "(dev = %s; type = %s; inst = %s)",
146 type_instance == NULL ? "(nil)" : type_instance,
147 i->device == NULL ? "(nil)" : i->device,
149 i->inst == NULL ? "(nil)" : i->inst);
151 return (ir_ignorelist_invert ? 0 : 1);
154 return (ir_ignorelist_invert);
155 } /* int check_ignorelist */
157 static void submit_one (const char *dev, const char *type,
158 const char *type_instance, counter_t value)
161 value_list_t vl = VALUE_LIST_INIT;
163 values[0].counter = value;
167 vl.time = time (NULL);
168 strcpy (vl.host, hostname_g);
169 strcpy (vl.plugin, "netlink");
170 strncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
172 if (type_instance != NULL)
173 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
175 plugin_dispatch_values (type, &vl);
176 } /* void submit_one */
178 static void submit_two (const char *dev, const char *type,
179 const char *type_instance,
180 counter_t rx, counter_t tx)
183 value_list_t vl = VALUE_LIST_INIT;
185 values[0].counter = rx;
186 values[1].counter = tx;
190 vl.time = time (NULL);
191 strcpy (vl.host, hostname_g);
192 strcpy (vl.plugin, "netlink");
193 strncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
195 if (type_instance != NULL)
196 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
198 plugin_dispatch_values (type, &vl);
199 } /* void submit_two */
201 static int link_filter (const struct sockaddr_nl *sa, struct nlmsghdr *nmh,
204 struct ifinfomsg *msg;
206 struct rtattr *attrs[IFLA_MAX + 1];
207 struct rtnl_link_stats *stats;
211 if (nmh->nlmsg_type != RTM_NEWLINK)
213 ERROR ("netlink plugin: link_filter: Don't know how to handle type %i.",
218 msg = NLMSG_DATA (nmh);
220 msg_len = nmh->nlmsg_len - sizeof (struct ifinfomsg);
223 ERROR ("netlink plugin: link_filter: msg_len = %i < 0;", msg_len);
227 memset (attrs, '\0', sizeof (attrs));
228 if (parse_rtattr (attrs, IFLA_MAX, IFLA_RTA (msg), msg_len) != 0)
230 ERROR ("netlink plugin: link_filter: parse_rtattr failed.");
234 if (attrs[IFLA_STATS] == NULL)
236 stats = RTA_DATA (attrs[IFLA_STATS]);
238 if (attrs[IFLA_IFNAME] == NULL)
240 ERROR ("netlink plugin: link_filter: attrs[IFLA_IFNAME] == NULL");
243 dev = RTA_DATA (attrs[IFLA_IFNAME]);
245 /* Update the `iflist'. It's used to know which interfaces exist and query
246 * them later for qdiscs and classes. */
247 if (msg->ifi_index >= iflist_len)
251 temp = (char **) realloc (iflist, (msg->ifi_index + 1) * sizeof (char *));
254 ERROR ("netlink plugin: link_filter: realloc failed.");
258 memset (temp + iflist_len, '\0',
259 (msg->ifi_index + 1 - iflist_len) * sizeof (char *));
261 iflist_len = msg->ifi_index + 1;
263 if ((iflist[msg->ifi_index] == NULL)
264 || (strcmp (iflist[msg->ifi_index], dev) != 0))
266 sfree (iflist[msg->ifi_index]);
267 iflist[msg->ifi_index] = strdup (dev);
270 if (check_ignorelist (dev, "interface", NULL) == 0)
272 submit_two (dev, "if_octets", NULL, stats->rx_bytes, stats->tx_bytes);
273 submit_two (dev, "if_packets", NULL, stats->rx_packets, stats->tx_packets);
274 submit_two (dev, "if_errors", NULL, stats->rx_errors, stats->tx_errors);
278 DEBUG ("netlink plugin: Ignoring %s/interface.", dev);
281 if (check_ignorelist (dev, "if_detail", NULL) == 0)
283 submit_two (dev, "if_dropped", NULL, stats->rx_dropped, stats->tx_dropped);
284 submit_one (dev, "if_multicast", NULL, stats->multicast);
285 submit_one (dev, "if_collisions", NULL, stats->collisions);
287 submit_one (dev, "if_rx_errors", "length", stats->rx_length_errors);
288 submit_one (dev, "if_rx_errors", "over", stats->rx_over_errors);
289 submit_one (dev, "if_rx_errors", "crc", stats->rx_crc_errors);
290 submit_one (dev, "if_rx_errors", "frame", stats->rx_frame_errors);
291 submit_one (dev, "if_rx_errors", "fifo", stats->rx_fifo_errors);
292 submit_one (dev, "if_rx_errors", "missed", stats->rx_missed_errors);
294 submit_one (dev, "if_tx_errors", "aborted", stats->tx_aborted_errors);
295 submit_one (dev, "if_tx_errors", "carrier", stats->tx_carrier_errors);
296 submit_one (dev, "if_tx_errors", "fifo", stats->tx_fifo_errors);
297 submit_one (dev, "if_tx_errors", "heartbeat", stats->tx_heartbeat_errors);
298 submit_one (dev, "if_tx_errors", "window", stats->tx_window_errors);
302 DEBUG ("netlink plugin: Ignoring %s/if_detail.", dev);
306 } /* int link_filter */
308 static int qos_filter (const struct sockaddr_nl *sa, struct nlmsghdr *nmh,
313 struct rtattr *attrs[TCA_MAX + 1];
315 int wanted_ifindex = *((int *) args);
319 /* char *type_instance; */
321 char tc_inst[DATA_MAX_NAME_LEN];
323 if (nmh->nlmsg_type == RTM_NEWQDISC)
325 else if (nmh->nlmsg_type == RTM_NEWTCLASS)
327 else if (nmh->nlmsg_type == RTM_NEWTFILTER)
331 ERROR ("netlink plugin: qos_filter: Don't know how to handle type %i.",
336 msg = NLMSG_DATA (nmh);
338 msg_len = nmh->nlmsg_len - sizeof (struct tcmsg);
341 ERROR ("netlink plugin: qos_filter: msg_len = %i < 0;", msg_len);
345 if (msg->tcm_ifindex != wanted_ifindex)
347 DEBUG ("netlink plugin: qos_filter: Got %s for interface #%i, "
349 tc_type, msg->tcm_ifindex, wanted_ifindex);
353 if (msg->tcm_ifindex >= iflist_len)
355 ERROR ("netlink plugin: qos_filter: msg->tcm_ifindex = %i "
356 ">= iflist_len = %i",
357 msg->tcm_ifindex, iflist_len);
361 dev = iflist[msg->tcm_ifindex];
364 ERROR ("netlink plugin: qos_filter: iflist[%i] == NULL",
369 memset (attrs, '\0', sizeof (attrs));
370 if (parse_rtattr (attrs, TCA_MAX, TCA_RTA (msg), msg_len) != 0)
372 ERROR ("netlink plugin: qos_filter: parse_rtattr failed.");
376 if (attrs[TCA_KIND] == NULL)
378 ERROR ("netlink plugin: qos_filter: attrs[TCA_KIND] == NULL");
383 uint32_t numberic_id;
385 numberic_id = msg->tcm_handle;
386 if (strcmp (tc_type, "filter") == 0)
387 numberic_id = msg->tcm_parent;
389 snprintf (tc_inst, sizeof (tc_inst), "%s-%x:%x",
390 (const char *) RTA_DATA (attrs[TCA_KIND]),
392 numberic_id & 0x0000FFFF);
393 tc_inst[sizeof (tc_inst) - 1] = '\0';
396 DEBUG ("netlink plugin: qos_filter: got %s for %s (%i).",
397 tc_type, dev, msg->tcm_ifindex);
399 if (check_ignorelist (dev, tc_type, tc_inst))
402 if (attrs[TCA_STATS2])
404 struct rtattr *attrs_stats[TCA_STATS_MAX + 1];
406 memset (attrs_stats, '\0', sizeof (attrs_stats));
407 parse_rtattr_nested (attrs_stats, TCA_STATS_MAX, attrs[TCA_STATS2]);
409 if (attrs_stats[TCA_STATS_BASIC])
411 struct gnet_stats_basic bs;
412 char type_instance[DATA_MAX_NAME_LEN];
414 snprintf (type_instance, sizeof (type_instance), "%s-%s",
416 type_instance[sizeof (type_instance) - 1] = '\0';
418 memset (&bs, '\0', sizeof (bs));
419 memcpy (&bs, RTA_DATA (attrs_stats[TCA_STATS_BASIC]),
420 MIN (RTA_PAYLOAD (attrs_stats[TCA_STATS_BASIC]), sizeof(bs)));
422 submit_one (dev, "ipt_bytes", type_instance, bs.bytes);
423 submit_one (dev, "ipt_packets", type_instance, bs.packets);
428 } /* int qos_filter */
430 static int ir_config (const char *key, const char *value)
437 new_val = strdup (value);
441 fields_num = strsplit (new_val, fields, STATIC_ARRAY_SIZE (fields));
442 if ((fields_num < 1) || (fields_num > 8))
448 if ((strcasecmp (key, "Interface") == 0)
449 || (strcasecmp (key, "VerboseInterface") == 0))
453 ERROR ("netlink plugin: Invalid number of fields for option "
454 "`%s'. Got %i, expected 1.", key, fields_num);
459 add_ignorelist (fields[0], "interface", NULL);
460 if (strcasecmp (key, "VerboseInterface") == 0)
461 add_ignorelist (fields[0], "if_detail", NULL);
465 else if ((strcasecmp (key, "QDisc") == 0)
466 || (strcasecmp (key, "Class") == 0)
467 || (strcasecmp (key, "Filter") == 0))
469 if ((fields_num < 1) || (fields_num > 2))
471 ERROR ("netlink plugin: Invalid number of fields for option "
472 "`%s'. Got %i, expected 1 or 2.", key, fields_num);
477 add_ignorelist (fields[0], key,
478 (fields_num == 2) ? fields[1] : NULL);
482 else if (strcasecmp (key, "IgnoreSelected") == 0)
486 ERROR ("netlink plugin: Invalid number of fields for option "
487 "`IgnoreSelected'. Got %i, expected 1.", fields_num);
492 if ((strcasecmp (fields[0], "yes") == 0)
493 || (strcasecmp (fields[0], "true") == 0)
494 || (strcasecmp (fields[0], "on") == 0))
495 ir_ignorelist_invert = 0;
497 ir_ignorelist_invert = 1;
505 } /* int ir_config */
507 static int ir_init (void)
509 memset (&rth, '\0', sizeof (rth));
511 if (rtnl_open (&rth, 0) != 0)
513 ERROR ("netlink plugin: ir_init: rtnl_open failed.");
520 static int ir_read (void)
526 static const int type_id[] = { RTM_GETQDISC, RTM_GETTCLASS, RTM_GETTFILTER };
527 static const char *type_name[] = { "qdisc", "class", "filter" };
529 memset (&im, '\0', sizeof (im));
530 im.ifi_type = AF_UNSPEC;
532 if (rtnl_dump_request (&rth, RTM_GETLINK, &im, sizeof (im)) < 0)
534 ERROR ("netlink plugin: ir_read: rtnl_dump_request failed.");
538 if (rtnl_dump_filter (&rth, link_filter, /* arg1 = */ NULL,
541 ERROR ("netlink plugin: ir_read: rtnl_dump_filter failed.");
545 /* `link_filter' will update `iflist' which is used here to iterate over all
547 for (ifindex = 0; ifindex < iflist_len; ifindex++)
551 if (iflist[ifindex] == NULL)
554 for (type_index = 0; type_index < STATIC_ARRAY_SIZE (type_id); type_index++)
556 if (check_ignorelist (iflist[ifindex], type_name[type_index], NULL))
558 DEBUG ("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
559 "== TRUE", iflist[ifindex], type_name[type_index]);
563 DEBUG ("netlink plugin: ir_read: querying %s from %s (%i).",
564 type_name[type_index], iflist[ifindex], ifindex);
566 memset (&tm, '\0', sizeof (tm));
567 tm.tcm_family = AF_UNSPEC;
568 tm.tcm_ifindex = ifindex;
570 if (rtnl_dump_request (&rth, type_id[type_index], &tm, sizeof (tm)) < 0)
572 ERROR ("netlink plugin: ir_read: rtnl_dump_request failed.");
576 if (rtnl_dump_filter (&rth, qos_filter, (void *) &ifindex,
579 ERROR ("netlink plugin: ir_read: rtnl_dump_filter failed.");
582 } /* for (type_index) */
583 } /* for (if_index) */
588 static int ir_shutdown (void)
590 if ((rth.fd != 0) || (rth.seq != 0) || (rth.dump != 0))
593 memset (&rth, '\0', sizeof (rth));
597 } /* int ir_shutdown */
599 void module_register (void)
601 plugin_register_config ("netlink", ir_config, config_keys, config_keys_num);
602 plugin_register_init ("netlink", ir_init);
603 plugin_register_read ("netlink", ir_read);
604 plugin_register_shutdown ("netlink", ir_shutdown);
605 } /* void module_register */
608 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :