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)
134 if ((i->inst != NULL)
135 && ((type_instance == NULL)
136 || (strcasecmp (i->inst, type_instance) != 0)))
139 if ((i->inst != NULL) && (type_instance != NULL)
140 && (strcasecmp (i->inst, type_instance) != 0))
143 DEBUG ("netlink plugin: check_ignorelist: "
144 "(dev = %s; type = %s; inst = %s) matched "
145 "(dev = %s; type = %s; inst = %s)",
147 type_instance == NULL ? "(nil)" : type_instance,
148 i->device == NULL ? "(nil)" : i->device,
150 i->inst == NULL ? "(nil)" : i->inst);
152 return (ir_ignorelist_invert ? 0 : 1);
155 return (ir_ignorelist_invert);
156 } /* int check_ignorelist */
158 static void submit_one (const char *dev, const char *type,
159 const char *type_instance, counter_t value)
162 value_list_t vl = VALUE_LIST_INIT;
164 values[0].counter = value;
168 vl.time = time (NULL);
169 strcpy (vl.host, hostname_g);
170 strcpy (vl.plugin, "netlink");
171 strncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
173 if (type_instance != NULL)
174 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
176 plugin_dispatch_values (type, &vl);
177 } /* void submit_one */
179 static void submit_two (const char *dev, const char *type,
180 const char *type_instance,
181 counter_t rx, counter_t tx)
184 value_list_t vl = VALUE_LIST_INIT;
186 values[0].counter = rx;
187 values[1].counter = tx;
191 vl.time = time (NULL);
192 strcpy (vl.host, hostname_g);
193 strcpy (vl.plugin, "netlink");
194 strncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
196 if (type_instance != NULL)
197 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
199 plugin_dispatch_values (type, &vl);
200 } /* void submit_two */
202 static int link_filter (const struct sockaddr_nl *sa, struct nlmsghdr *nmh,
205 struct ifinfomsg *msg;
207 struct rtattr *attrs[IFLA_MAX + 1];
208 struct rtnl_link_stats *stats;
212 if (nmh->nlmsg_type != RTM_NEWLINK)
214 ERROR ("netlink plugin: link_filter: Don't know how to handle type %i.",
219 msg = NLMSG_DATA (nmh);
221 msg_len = nmh->nlmsg_len - sizeof (struct ifinfomsg);
224 ERROR ("netlink plugin: link_filter: msg_len = %i < 0;", msg_len);
228 memset (attrs, '\0', sizeof (attrs));
229 if (parse_rtattr (attrs, IFLA_MAX, IFLA_RTA (msg), msg_len) != 0)
231 ERROR ("netlink plugin: link_filter: parse_rtattr failed.");
235 if (attrs[IFLA_STATS] == NULL)
237 stats = RTA_DATA (attrs[IFLA_STATS]);
239 if (attrs[IFLA_IFNAME] == NULL)
241 ERROR ("netlink plugin: link_filter: attrs[IFLA_IFNAME] == NULL");
244 dev = RTA_DATA (attrs[IFLA_IFNAME]);
246 /* Update the `iflist'. It's used to know which interfaces exist and query
247 * them later for qdiscs and classes. */
248 if (msg->ifi_index >= iflist_len)
252 temp = (char **) realloc (iflist, (msg->ifi_index + 1) * sizeof (char *));
255 ERROR ("netlink plugin: link_filter: realloc failed.");
259 memset (temp + iflist_len, '\0',
260 (msg->ifi_index + 1 - iflist_len) * sizeof (char *));
262 iflist_len = msg->ifi_index + 1;
264 if ((iflist[msg->ifi_index] == NULL)
265 || (strcmp (iflist[msg->ifi_index], dev) != 0))
267 sfree (iflist[msg->ifi_index]);
268 iflist[msg->ifi_index] = strdup (dev);
271 if (check_ignorelist (dev, "interface", NULL) == 0)
273 submit_two (dev, "if_octets", NULL, stats->rx_bytes, stats->tx_bytes);
274 submit_two (dev, "if_packets", NULL, stats->rx_packets, stats->tx_packets);
275 submit_two (dev, "if_errors", NULL, stats->rx_errors, stats->tx_errors);
279 DEBUG ("netlink plugin: Ignoring %s/interface.", dev);
282 if (check_ignorelist (dev, "if_detail", NULL) == 0)
284 submit_two (dev, "if_dropped", NULL, stats->rx_dropped, stats->tx_dropped);
285 submit_one (dev, "if_multicast", NULL, stats->multicast);
286 submit_one (dev, "if_collisions", NULL, stats->collisions);
288 submit_one (dev, "if_rx_errors", "length", stats->rx_length_errors);
289 submit_one (dev, "if_rx_errors", "over", stats->rx_over_errors);
290 submit_one (dev, "if_rx_errors", "crc", stats->rx_crc_errors);
291 submit_one (dev, "if_rx_errors", "frame", stats->rx_frame_errors);
292 submit_one (dev, "if_rx_errors", "fifo", stats->rx_fifo_errors);
293 submit_one (dev, "if_rx_errors", "missed", stats->rx_missed_errors);
295 submit_one (dev, "if_tx_errors", "aborted", stats->tx_aborted_errors);
296 submit_one (dev, "if_tx_errors", "carrier", stats->tx_carrier_errors);
297 submit_one (dev, "if_tx_errors", "fifo", stats->tx_fifo_errors);
298 submit_one (dev, "if_tx_errors", "heartbeat", stats->tx_heartbeat_errors);
299 submit_one (dev, "if_tx_errors", "window", stats->tx_window_errors);
303 DEBUG ("netlink plugin: Ignoring %s/if_detail.", dev);
307 } /* int link_filter */
309 static int qos_filter (const struct sockaddr_nl *sa, struct nlmsghdr *nmh,
314 struct rtattr *attrs[TCA_MAX + 1];
316 int wanted_ifindex = *((int *) args);
320 /* char *type_instance; */
322 char tc_inst[DATA_MAX_NAME_LEN];
324 if (nmh->nlmsg_type == RTM_NEWQDISC)
326 else if (nmh->nlmsg_type == RTM_NEWTCLASS)
328 else if (nmh->nlmsg_type == RTM_NEWTFILTER)
332 ERROR ("netlink plugin: qos_filter: Don't know how to handle type %i.",
337 msg = NLMSG_DATA (nmh);
339 msg_len = nmh->nlmsg_len - sizeof (struct tcmsg);
342 ERROR ("netlink plugin: qos_filter: msg_len = %i < 0;", msg_len);
346 if (msg->tcm_ifindex != wanted_ifindex)
348 DEBUG ("netlink plugin: qos_filter: Got %s for interface #%i, "
350 tc_type, msg->tcm_ifindex, wanted_ifindex);
354 if (msg->tcm_ifindex >= iflist_len)
356 ERROR ("netlink plugin: qos_filter: msg->tcm_ifindex = %i "
357 ">= iflist_len = %i",
358 msg->tcm_ifindex, iflist_len);
362 dev = iflist[msg->tcm_ifindex];
365 ERROR ("netlink plugin: qos_filter: iflist[%i] == NULL",
370 memset (attrs, '\0', sizeof (attrs));
371 if (parse_rtattr (attrs, TCA_MAX, TCA_RTA (msg), msg_len) != 0)
373 ERROR ("netlink plugin: qos_filter: parse_rtattr failed.");
377 if (attrs[TCA_KIND] == NULL)
379 ERROR ("netlink plugin: qos_filter: attrs[TCA_KIND] == NULL");
384 uint32_t numberic_id;
386 numberic_id = msg->tcm_handle;
387 if (strcmp (tc_type, "filter") == 0)
388 numberic_id = msg->tcm_parent;
390 snprintf (tc_inst, sizeof (tc_inst), "%s-%x:%x",
391 (const char *) RTA_DATA (attrs[TCA_KIND]),
393 numberic_id & 0x0000FFFF);
394 tc_inst[sizeof (tc_inst) - 1] = '\0';
397 DEBUG ("netlink plugin: qos_filter: got %s for %s (%i).",
398 tc_type, dev, msg->tcm_ifindex);
400 if (check_ignorelist (dev, tc_type, tc_inst))
403 if (attrs[TCA_STATS2])
405 struct rtattr *attrs_stats[TCA_STATS_MAX + 1];
407 memset (attrs_stats, '\0', sizeof (attrs_stats));
408 parse_rtattr_nested (attrs_stats, TCA_STATS_MAX, attrs[TCA_STATS2]);
410 if (attrs_stats[TCA_STATS_BASIC])
412 struct gnet_stats_basic bs;
413 char type_instance[DATA_MAX_NAME_LEN];
415 snprintf (type_instance, sizeof (type_instance), "%s-%s",
417 type_instance[sizeof (type_instance) - 1] = '\0';
419 memset (&bs, '\0', sizeof (bs));
420 memcpy (&bs, RTA_DATA (attrs_stats[TCA_STATS_BASIC]),
421 MIN (RTA_PAYLOAD (attrs_stats[TCA_STATS_BASIC]), sizeof(bs)));
423 submit_one (dev, "ipt_bytes", type_instance, bs.bytes);
424 submit_one (dev, "ipt_packets", type_instance, bs.packets);
429 } /* int qos_filter */
431 static int ir_config (const char *key, const char *value)
438 new_val = strdup (value);
442 fields_num = strsplit (new_val, fields, STATIC_ARRAY_SIZE (fields));
443 if ((fields_num < 1) || (fields_num > 8))
449 if ((strcasecmp (key, "Interface") == 0)
450 || (strcasecmp (key, "VerboseInterface") == 0))
454 ERROR ("netlink plugin: Invalid number of fields for option "
455 "`%s'. Got %i, expected 1.", key, fields_num);
460 add_ignorelist (fields[0], "interface", NULL);
461 if (strcasecmp (key, "VerboseInterface") == 0)
462 add_ignorelist (fields[0], "if_detail", NULL);
466 else if ((strcasecmp (key, "QDisc") == 0)
467 || (strcasecmp (key, "Class") == 0)
468 || (strcasecmp (key, "Filter") == 0))
470 if ((fields_num < 1) || (fields_num > 2))
472 ERROR ("netlink plugin: Invalid number of fields for option "
473 "`%s'. Got %i, expected 1 or 2.", key, fields_num);
478 add_ignorelist (fields[0], key,
479 (fields_num == 2) ? fields[1] : NULL);
483 else if (strcasecmp (key, "IgnoreSelected") == 0)
487 ERROR ("netlink plugin: Invalid number of fields for option "
488 "`IgnoreSelected'. Got %i, expected 1.", fields_num);
493 if ((strcasecmp (fields[0], "yes") == 0)
494 || (strcasecmp (fields[0], "true") == 0)
495 || (strcasecmp (fields[0], "on") == 0))
496 ir_ignorelist_invert = 0;
498 ir_ignorelist_invert = 1;
506 } /* int ir_config */
508 static int ir_init (void)
510 memset (&rth, '\0', sizeof (rth));
512 if (rtnl_open (&rth, 0) != 0)
514 ERROR ("netlink plugin: ir_read: rtnl_open failed.");
518 if (ll_init_map (&rth) != 0)
520 ERROR ("netlink plugin: ir_read: ll_init_map failed.");
527 static int ir_read (void)
533 static const int type_id[] = { RTM_GETQDISC, RTM_GETTCLASS, RTM_GETTFILTER };
534 static const char *type_name[] = { "qdisc", "class", "filter" };
536 memset (&im, '\0', sizeof (im));
537 im.ifi_type = AF_UNSPEC;
539 if (rtnl_dump_request (&rth, RTM_GETLINK, &im, sizeof (im)) < 0)
541 ERROR ("netlink plugin: ir_read: rtnl_dump_request failed.");
545 if (rtnl_dump_filter (&rth, link_filter, /* arg1 = */ NULL,
548 ERROR ("netlink plugin: ir_read: rtnl_dump_filter failed.");
552 /* `link_filter' will update `iflist' which is used here to iterate over all
554 for (ifindex = 0; ifindex < iflist_len; ifindex++)
558 if (iflist[ifindex] == NULL)
561 for (type_index = 0; type_index < STATIC_ARRAY_SIZE (type_id); type_index++)
563 if (check_ignorelist (iflist[ifindex], type_name[type_index], NULL))
565 DEBUG ("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
566 "== TRUE", iflist[ifindex], type_name[type_index]);
570 DEBUG ("netlink plugin: ir_read: querying %s from %s (%i).",
571 type_name[type_index], iflist[ifindex], ifindex);
573 memset (&tm, '\0', sizeof (tm));
574 tm.tcm_family = AF_UNSPEC;
575 tm.tcm_ifindex = ifindex;
577 if (rtnl_dump_request (&rth, type_id[type_index], &tm, sizeof (tm)) < 0)
579 ERROR ("netlink plugin: ir_read: rtnl_dump_request failed.");
583 if (rtnl_dump_filter (&rth, qos_filter, (void *) &ifindex,
586 ERROR ("netlink plugin: ir_read: rtnl_dump_filter failed.");
589 } /* for (type_index) */
590 } /* for (if_index) */
595 static int ir_shutdown (void)
597 if ((rth.fd != 0) || (rth.seq != 0) || (rth.dump != 0))
600 memset (&rth, '\0', sizeof (rth));
604 } /* int ir_shutdown */
606 void module_register (void)
608 plugin_register_config ("netlink", ir_config, config_keys, config_keys_num);
609 plugin_register_init ("netlink", ir_init);
610 plugin_register_read ("netlink", ir_read);
611 plugin_register_shutdown ("netlink", ir_shutdown);
612 } /* void module_register */
615 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :