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 <iproute/libnetlink.h>
29 #include <linux/netlink.h>
30 #include <linux/rtnetlink.h>
31 #include <linux/gen_stats.h>
33 #include <iproute/ll_map.h>
35 typedef struct ir_ignorelist_s
40 struct ir_ignorelist_s *next;
43 static int ir_ignorelist_invert = 1;
44 static ir_ignorelist_t *ir_ignorelist_head = NULL;
46 static struct rtnl_handle rth;
48 static char **iflist = NULL;
49 static size_t iflist_len = 0;
51 static const char *config_keys[] =
60 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
62 static int add_ignorelist (const char *dev, const char *type,
65 ir_ignorelist_t *entry;
67 entry = (ir_ignorelist_t *) malloc (sizeof (ir_ignorelist_t));
71 memset (entry, '\0', sizeof (ir_ignorelist_t));
73 if (strcasecmp (dev, "All") != 0)
75 entry->device = strdup (dev);
76 if (entry->device == NULL)
83 entry->type = strdup (type);
84 if (entry->type == NULL)
86 sfree (entry->device);
93 entry->inst = strdup (inst);
94 if (entry->inst == NULL)
97 sfree (entry->device);
103 entry->next = ir_ignorelist_head;
104 ir_ignorelist_head = entry;
107 } /* int add_ignorelist */
110 * Checks wether a data set should be ignored. Returns `true' is the value
111 * should be ignored, `false' otherwise.
113 static int check_ignorelist (const char *dev,
114 const char *type, const char *type_instance)
118 assert ((dev != NULL) && (type != NULL));
120 if (ir_ignorelist_head == NULL)
121 return (ir_ignorelist_invert ? 0 : 1);
123 for (i = ir_ignorelist_head; i != NULL; i = i->next)
125 /* i->device == NULL => match all devices */
126 if ((i->device != NULL)
127 && (strcasecmp (i->device, dev) != 0))
130 if (strcasecmp (i->type, type) != 0)
133 if ((i->inst != NULL) && (type_instance != NULL)
134 && (strcasecmp (i->inst, type_instance) != 0))
137 DEBUG ("netlink plugin: check_ignorelist: "
138 "(dev = %s; type = %s; inst = %s) matched "
139 "(dev = %s; type = %s; inst = %s)",
141 type_instance == NULL ? "(nil)" : type_instance,
142 i->device == NULL ? "(nil)" : i->device,
144 i->inst == NULL ? "(nil)" : i->inst);
146 return (ir_ignorelist_invert ? 0 : 1);
149 return (ir_ignorelist_invert);
150 } /* int check_ignorelist */
152 static void submit_one (const char *dev, const char *type,
153 const char *type_instance, counter_t value)
156 value_list_t vl = VALUE_LIST_INIT;
158 values[0].counter = value;
162 vl.time = time (NULL);
163 strcpy (vl.host, hostname_g);
164 strcpy (vl.plugin, "netlink");
165 strncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
167 if (type_instance != NULL)
168 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
170 plugin_dispatch_values (type, &vl);
171 } /* void submit_one */
173 static void submit_two (const char *dev, const char *type,
174 const char *type_instance,
175 counter_t rx, counter_t tx)
178 value_list_t vl = VALUE_LIST_INIT;
180 values[0].counter = rx;
181 values[1].counter = tx;
185 vl.time = time (NULL);
186 strcpy (vl.host, hostname_g);
187 strcpy (vl.plugin, "netlink");
188 strncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
190 if (type_instance != NULL)
191 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
193 plugin_dispatch_values (type, &vl);
194 } /* void submit_two */
196 static int link_filter (const struct sockaddr_nl *sa, struct nlmsghdr *nmh,
199 struct ifinfomsg *msg;
201 struct rtattr *attrs[IFLA_MAX + 1];
202 struct rtnl_link_stats *stats;
206 if (nmh->nlmsg_type != RTM_NEWLINK)
208 ERROR ("netlink plugin: link_filter: Don't know how to handle type %i.",
213 msg = NLMSG_DATA (nmh);
215 msg_len = nmh->nlmsg_len - sizeof (struct ifinfomsg);
218 ERROR ("netlink plugin: link_filter: msg_len = %i < 0;", msg_len);
222 memset (attrs, '\0', sizeof (attrs));
223 if (parse_rtattr (attrs, IFLA_MAX, IFLA_RTA (msg), msg_len) != 0)
225 ERROR ("netlink plugin: link_filter: parse_rtattr failed.");
229 if (attrs[IFLA_STATS] == NULL)
231 stats = RTA_DATA (attrs[IFLA_STATS]);
233 if (attrs[IFLA_IFNAME] == NULL)
235 ERROR ("netlink plugin: link_filter: attrs[IFLA_IFNAME] == NULL");
238 dev = RTA_DATA (attrs[IFLA_IFNAME]);
240 /* Update the `iflist'. It's used to know which interfaces exist and query
241 * them later for qdiscs and classes. */
242 if (msg->ifi_index >= iflist_len)
246 temp = (char **) realloc (iflist, (msg->ifi_index + 1) * sizeof (char *));
249 ERROR ("netlink plugin: link_filter: realloc failed.");
253 memset (temp + iflist_len, '\0',
254 (msg->ifi_index + 1 - iflist_len) * sizeof (char *));
256 iflist_len = msg->ifi_index + 1;
258 if ((iflist[msg->ifi_index] == NULL)
259 || (strcmp (iflist[msg->ifi_index], dev) != 0))
261 sfree (iflist[msg->ifi_index]);
262 iflist[msg->ifi_index] = strdup (dev);
265 if (check_ignorelist (dev, "interface", NULL) == 0)
267 submit_two (dev, "if_octets", NULL, stats->rx_bytes, stats->tx_bytes);
268 submit_two (dev, "if_packets", NULL, stats->rx_packets, stats->tx_packets);
269 submit_two (dev, "if_errors", NULL, stats->rx_errors, stats->tx_errors);
273 DEBUG ("netlink plugin: Ignoring %s/interface.", dev);
276 if (check_ignorelist (dev, "if_detail", NULL) == 0)
278 submit_two (dev, "if_dropped", NULL, stats->rx_dropped, stats->tx_dropped);
279 submit_one (dev, "if_multicast", NULL, stats->multicast);
280 submit_one (dev, "if_collisions", NULL, stats->collisions);
282 submit_one (dev, "if_rx_errors", "length", stats->rx_length_errors);
283 submit_one (dev, "if_rx_errors", "over", stats->rx_over_errors);
284 submit_one (dev, "if_rx_errors", "crc", stats->rx_crc_errors);
285 submit_one (dev, "if_rx_errors", "frame", stats->rx_frame_errors);
286 submit_one (dev, "if_rx_errors", "fifo", stats->rx_fifo_errors);
287 submit_one (dev, "if_rx_errors", "missed", stats->rx_missed_errors);
289 submit_one (dev, "if_tx_errors", "aborted", stats->tx_aborted_errors);
290 submit_one (dev, "if_tx_errors", "carrier", stats->tx_carrier_errors);
291 submit_one (dev, "if_tx_errors", "fifo", stats->tx_fifo_errors);
292 submit_one (dev, "if_tx_errors", "heartbeat", stats->tx_heartbeat_errors);
293 submit_one (dev, "if_tx_errors", "window", stats->tx_window_errors);
297 DEBUG ("netlink plugin: Ignoring %s/if_detail.", dev);
301 } /* int link_filter */
303 static int qos_filter (const struct sockaddr_nl *sa, struct nlmsghdr *nmh,
308 struct rtattr *attrs[TCA_MAX + 1];
310 int wanted_ifindex = *((int *) args);
314 /* char *type_instance; */
316 char tc_inst[DATA_MAX_NAME_LEN];
318 if (nmh->nlmsg_type == RTM_NEWQDISC)
320 else if (nmh->nlmsg_type == RTM_NEWTCLASS)
322 else if (nmh->nlmsg_type == RTM_NEWTFILTER)
326 ERROR ("netlink plugin: qos_filter: Don't know how to handle type %i.",
331 msg = NLMSG_DATA (nmh);
333 msg_len = nmh->nlmsg_len - sizeof (struct tcmsg);
336 ERROR ("netlink plugin: qos_filter: msg_len = %i < 0;", msg_len);
340 if (msg->tcm_ifindex != wanted_ifindex)
342 DEBUG ("netlink plugin: qos_filter: Got %s for interface #%i, "
344 tc_type, msg->tcm_ifindex, wanted_ifindex);
348 if (msg->tcm_ifindex >= iflist_len)
350 ERROR ("netlink plugin: qos_filter: msg->tcm_ifindex = %i "
351 ">= iflist_len = %i",
352 msg->tcm_ifindex, iflist_len);
356 dev = iflist[msg->tcm_ifindex];
359 ERROR ("netlink plugin: qos_filter: iflist[%i] == NULL",
364 memset (attrs, '\0', sizeof (attrs));
365 if (parse_rtattr (attrs, TCA_MAX, TCA_RTA (msg), msg_len) != 0)
367 ERROR ("netlink plugin: qos_filter: parse_rtattr failed.");
371 if (attrs[TCA_KIND] == NULL)
373 ERROR ("netlink plugin: qos_filter: attrs[TCA_KIND] == NULL");
378 uint32_t numberic_id;
380 numberic_id = msg->tcm_handle;
381 if (strcmp (tc_type, "filter") == 0)
382 numberic_id = msg->tcm_parent;
384 snprintf (tc_inst, sizeof (tc_inst), "%s-%x:%x",
385 (const char *) RTA_DATA (attrs[TCA_KIND]),
387 numberic_id & 0x0000FFFF);
388 tc_inst[sizeof (tc_inst) - 1] = '\0';
391 DEBUG ("netlink plugin: qos_filter: got %s for %s (%i).",
392 tc_type, dev, msg->tcm_ifindex);
394 if (check_ignorelist (dev, tc_type, tc_inst))
397 if (attrs[TCA_STATS2])
399 struct rtattr *attrs_stats[TCA_STATS_MAX + 1];
401 memset (attrs_stats, '\0', sizeof (attrs_stats));
402 parse_rtattr_nested (attrs_stats, TCA_STATS_MAX, attrs[TCA_STATS2]);
404 if (attrs_stats[TCA_STATS_BASIC])
406 struct gnet_stats_basic bs;
407 char type_instance[DATA_MAX_NAME_LEN];
409 snprintf (type_instance, sizeof (type_instance), "%s-%s",
411 type_instance[sizeof (type_instance) - 1] = '\0';
413 memset (&bs, '\0', sizeof (bs));
414 memcpy (&bs, RTA_DATA (attrs_stats[TCA_STATS_BASIC]),
415 MIN (RTA_PAYLOAD (attrs_stats[TCA_STATS_BASIC]), sizeof(bs)));
417 submit_one (dev, "ipt_bytes", type_instance, bs.bytes);
418 submit_one (dev, "ipt_packets", type_instance, bs.packets);
423 } /* int qos_filter */
425 static int ir_config (const char *key, const char *value)
432 new_val = strdup (value);
436 fields_num = strsplit (new_val, fields, STATIC_ARRAY_SIZE (fields));
437 if ((fields_num < 1) || (fields_num > 8))
443 if ((strcasecmp (key, "Interface") == 0)
444 || (strcasecmp (key, "VerboseInterface") == 0))
448 ERROR ("netlink plugin: Invalid number of fields for option "
449 "`%s'. Got %i, expected 1.", key, fields_num);
454 add_ignorelist (fields[0], "interface", NULL);
455 if (strcasecmp (key, "VerboseInterface") == 0)
456 add_ignorelist (fields[0], "if_detail", NULL);
460 else if ((strcasecmp (key, "QDisc") == 0)
461 || (strcasecmp (key, "Class") == 0)
462 || (strcasecmp (key, "Filter") == 0))
464 if ((fields_num < 1) || (fields_num > 2))
466 ERROR ("netlink plugin: Invalid number of fields for option "
467 "`%s'. Got %i, expected 1 or 2.", key, fields_num);
472 add_ignorelist (fields[0], key,
473 (fields_num == 2) ? fields[1] : NULL);
477 else if (strcasecmp (key, "IgnoreSelected") == 0)
481 ERROR ("netlink plugin: Invalid number of fields for option "
482 "`IgnoreSelected'. Got %i, expected 1.", fields_num);
487 if ((strcasecmp (fields[0], "yes") == 0)
488 || (strcasecmp (fields[0], "true") == 0)
489 || (strcasecmp (fields[0], "on") == 0))
490 ir_ignorelist_invert = 0;
492 ir_ignorelist_invert = 1;
500 } /* int ir_config */
502 static int ir_init (void)
504 memset (&rth, '\0', sizeof (rth));
506 if (rtnl_open (&rth, 0) != 0)
508 ERROR ("netlink plugin: ir_read: rtnl_open failed.");
512 if (ll_init_map (&rth) != 0)
514 ERROR ("netlink plugin: ir_read: ll_init_map failed.");
521 static int ir_read (void)
527 static const int type_id[] = { RTM_GETQDISC, RTM_GETTCLASS, RTM_GETTFILTER };
528 static const char *type_name[] = { "qdisc", "class", "filter" };
530 memset (&im, '\0', sizeof (im));
531 im.ifi_type = AF_UNSPEC;
533 if (rtnl_dump_request (&rth, RTM_GETLINK, &im, sizeof (im)) < 0)
535 ERROR ("netlink plugin: ir_read: rtnl_dump_request failed.");
539 if (rtnl_dump_filter (&rth, link_filter, /* arg1 = */ NULL,
542 ERROR ("netlink plugin: ir_read: rtnl_dump_filter failed.");
546 /* `link_filter' will update `iflist' which is used here to iterate over all
548 for (ifindex = 0; ifindex < iflist_len; ifindex++)
552 if (iflist[ifindex] == NULL)
555 for (type_index = 0; type_index < STATIC_ARRAY_SIZE (type_id); type_index++)
557 if (check_ignorelist (iflist[ifindex], type_name[type_index], NULL))
559 DEBUG ("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
560 "== TRUE", iflist[ifindex], type_name[type_index]);
564 DEBUG ("netlink plugin: ir_read: querying %s from %s (%i).",
565 type_name[type_index], iflist[ifindex], ifindex);
567 memset (&tm, '\0', sizeof (tm));
568 tm.tcm_family = AF_UNSPEC;
569 tm.tcm_ifindex = ifindex;
571 if (rtnl_dump_request (&rth, type_id[type_index], &tm, sizeof (tm)) < 0)
573 ERROR ("netlink plugin: ir_read: rtnl_dump_request failed.");
577 if (rtnl_dump_filter (&rth, qos_filter, (void *) &ifindex,
580 ERROR ("netlink plugin: ir_read: rtnl_dump_filter failed.");
583 } /* for (type_index) */
584 } /* for (if_index) */
589 static int ir_shutdown (void)
591 if ((rth.fd != 0) || (rth.seq != 0) || (rth.dump != 0))
594 memset (&rth, '\0', sizeof (rth));
598 } /* int ir_shutdown */
600 void module_register (void)
602 plugin_register_config ("netlink", ir_config, config_keys, config_keys_num);
603 plugin_register_init ("netlink", ir_init);
604 plugin_register_read ("netlink", ir_read);
605 plugin_register_shutdown ("netlink", ir_shutdown);
606 } /* void module_register */
609 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :