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 **/
22 #include "collectd.h"
23 #include "plugin.h"
24 #include "common.h"
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
36 {
37 char *device;
38 char *type;
39 char *inst;
40 struct ir_ignorelist_s *next;
41 } ir_ignorelist_t;
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[] =
52 {
53 "Interface",
54 "VerboseInterface",
55 "QDisc",
56 "Class",
57 "Filter",
58 "IgnoreSelected"
59 };
60 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
62 static int add_ignorelist (const char *dev, const char *type,
63 const char *inst)
64 {
65 ir_ignorelist_t *entry;
67 entry = (ir_ignorelist_t *) malloc (sizeof (ir_ignorelist_t));
68 if (entry == NULL)
69 return (-1);
71 memset (entry, '\0', sizeof (ir_ignorelist_t));
73 if (strcasecmp (dev, "All") != 0)
74 {
75 entry->device = strdup (dev);
76 if (entry->device == NULL)
77 {
78 sfree (entry);
79 return (-1);
80 }
81 }
83 entry->type = strdup (type);
84 if (entry->type == NULL)
85 {
86 sfree (entry->device);
87 sfree (entry);
88 return (-1);
89 }
91 if (inst != NULL)
92 {
93 entry->inst = strdup (inst);
94 if (entry->inst == NULL)
95 {
96 sfree (entry->type);
97 sfree (entry->device);
98 sfree (entry);
99 return (-1);
100 }
101 }
103 entry->next = ir_ignorelist_head;
104 ir_ignorelist_head = entry;
106 return (0);
107 } /* int add_ignorelist */
109 /*
110 * Checks wether a data set should be ignored. Returns `true' is the value
111 * should be ignored, `false' otherwise.
112 */
113 static int check_ignorelist (const char *dev,
114 const char *type, const char *type_instance)
115 {
116 ir_ignorelist_t *i;
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)
124 {
125 /* i->device == NULL => match all devices */
126 if ((i->device != NULL)
127 && (strcasecmp (i->device, dev) != 0))
128 continue;
130 if (strcasecmp (i->type, type) != 0)
131 continue;
133 if ((i->inst != NULL) && (type_instance != NULL)
134 && (strcasecmp (i->inst, type_instance) != 0))
135 continue;
137 DEBUG ("netlink plugin: check_ignorelist: "
138 "(dev = %s; type = %s; inst = %s) matched "
139 "(dev = %s; type = %s; inst = %s)",
140 dev, type,
141 type_instance == NULL ? "(nil)" : type_instance,
142 i->device == NULL ? "(nil)" : i->device,
143 i->type,
144 i->inst == NULL ? "(nil)" : i->inst);
146 return (ir_ignorelist_invert ? 0 : 1);
147 } /* for i */
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)
154 {
155 value_t values[1];
156 value_list_t vl = VALUE_LIST_INIT;
158 values[0].counter = value;
160 vl.values = values;
161 vl.values_len = 1;
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)
176 {
177 value_t values[2];
178 value_list_t vl = VALUE_LIST_INIT;
180 values[0].counter = rx;
181 values[1].counter = tx;
183 vl.values = values;
184 vl.values_len = 2;
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,
197 void *args)
198 {
199 struct ifinfomsg *msg;
200 int msg_len;
201 struct rtattr *attrs[IFLA_MAX + 1];
202 struct rtnl_link_stats *stats;
204 const char *dev;
206 if (nmh->nlmsg_type != RTM_NEWLINK)
207 {
208 ERROR ("netlink plugin: link_filter: Don't know how to handle type %i.",
209 nmh->nlmsg_type);
210 return (-1);
211 }
213 msg = NLMSG_DATA (nmh);
215 msg_len = nmh->nlmsg_len - sizeof (struct ifinfomsg);
216 if (msg_len < 0)
217 {
218 ERROR ("netlink plugin: link_filter: msg_len = %i < 0;", msg_len);
219 return (-1);
220 }
222 memset (attrs, '\0', sizeof (attrs));
223 if (parse_rtattr (attrs, IFLA_MAX, IFLA_RTA (msg), msg_len) != 0)
224 {
225 ERROR ("netlink plugin: link_filter: parse_rtattr failed.");
226 return (-1);
227 }
229 if (attrs[IFLA_STATS] == NULL)
230 return (-1);
231 stats = RTA_DATA (attrs[IFLA_STATS]);
233 if (attrs[IFLA_IFNAME] == NULL)
234 {
235 ERROR ("netlink plugin: link_filter: attrs[IFLA_IFNAME] == NULL");
236 return (-1);
237 }
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)
243 {
244 char **temp;
246 temp = (char **) realloc (iflist, (msg->ifi_index + 1) * sizeof (char *));
247 if (temp == NULL)
248 {
249 ERROR ("netlink plugin: link_filter: realloc failed.");
250 return (-1);
251 }
253 memset (temp + iflist_len, '\0',
254 (msg->ifi_index + 1 - iflist_len) * sizeof (char *));
255 iflist = temp;
256 iflist_len = msg->ifi_index + 1;
257 }
258 if ((iflist[msg->ifi_index] == NULL)
259 || (strcmp (iflist[msg->ifi_index], dev) != 0))
260 {
261 sfree (iflist[msg->ifi_index]);
262 iflist[msg->ifi_index] = strdup (dev);
263 }
265 if (check_ignorelist (dev, "interface", NULL) == 0)
266 {
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);
270 }
271 else
272 {
273 DEBUG ("netlink plugin: Ignoring %s/interface.", dev);
274 }
276 if (check_ignorelist (dev, "if_detail", NULL) == 0)
277 {
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);
294 }
295 else
296 {
297 DEBUG ("netlink plugin: Ignoring %s/if_detail.", dev);
298 }
300 return (0);
301 } /* int link_filter */
303 static int qos_filter (const struct sockaddr_nl *sa, struct nlmsghdr *nmh,
304 void *args)
305 {
306 struct tcmsg *msg;
307 int msg_len;
308 struct rtattr *attrs[TCA_MAX + 1];
310 int wanted_ifindex = *((int *) args);
312 const char *dev;
314 /* char *type_instance; */
315 char *tc_type;
316 char tc_inst[DATA_MAX_NAME_LEN];
318 if (nmh->nlmsg_type == RTM_NEWQDISC)
319 tc_type = "qdisc";
320 else if (nmh->nlmsg_type == RTM_NEWTCLASS)
321 tc_type = "class";
322 else if (nmh->nlmsg_type == RTM_NEWTFILTER)
323 tc_type = "filter";
324 else
325 {
326 ERROR ("netlink plugin: qos_filter: Don't know how to handle type %i.",
327 nmh->nlmsg_type);
328 return (-1);
329 }
331 msg = NLMSG_DATA (nmh);
333 msg_len = nmh->nlmsg_len - sizeof (struct tcmsg);
334 if (msg_len < 0)
335 {
336 ERROR ("netlink plugin: qos_filter: msg_len = %i < 0;", msg_len);
337 return (-1);
338 }
340 if (msg->tcm_ifindex != wanted_ifindex)
341 {
342 DEBUG ("netlink plugin: qos_filter: Got %s for interface #%i, "
343 "but expected #%i.",
344 tc_type, msg->tcm_ifindex, wanted_ifindex);
345 return (0);
346 }
348 if (msg->tcm_ifindex >= iflist_len)
349 {
350 ERROR ("netlink plugin: qos_filter: msg->tcm_ifindex = %i "
351 ">= iflist_len = %i",
352 msg->tcm_ifindex, iflist_len);
353 return (-1);
354 }
356 dev = iflist[msg->tcm_ifindex];
357 if (dev == NULL)
358 {
359 ERROR ("netlink plugin: qos_filter: iflist[%i] == NULL",
360 msg->tcm_ifindex);
361 return (-1);
362 }
364 memset (attrs, '\0', sizeof (attrs));
365 if (parse_rtattr (attrs, TCA_MAX, TCA_RTA (msg), msg_len) != 0)
366 {
367 ERROR ("netlink plugin: qos_filter: parse_rtattr failed.");
368 return (-1);
369 }
371 if (attrs[TCA_KIND] == NULL)
372 {
373 ERROR ("netlink plugin: qos_filter: attrs[TCA_KIND] == NULL");
374 return (-1);
375 }
377 { /* The the ID */
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]),
386 numberic_id >> 16,
387 numberic_id & 0x0000FFFF);
388 tc_inst[sizeof (tc_inst) - 1] = '\0';
389 }
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))
395 return (0);
397 if (attrs[TCA_STATS2])
398 {
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])
405 {
406 struct gnet_stats_basic bs;
407 char type_instance[DATA_MAX_NAME_LEN];
409 snprintf (type_instance, sizeof (type_instance), "%s-%s",
410 tc_type, tc_inst);
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);
419 }
420 }
422 return (0);
423 } /* int qos_filter */
425 static int ir_config (const char *key, const char *value)
426 {
427 char *new_val;
428 char *fields[8];
429 int fields_num;
430 int status = 1;
432 new_val = strdup (value);
433 if (new_val == NULL)
434 return (-1);
436 fields_num = strsplit (new_val, fields, STATIC_ARRAY_SIZE (fields));
437 if ((fields_num < 1) || (fields_num > 8))
438 {
439 sfree (new_val);
440 return (-1);
441 }
443 if ((strcasecmp (key, "Interface") == 0)
444 || (strcasecmp (key, "VerboseInterface") == 0))
445 {
446 if (fields_num != 1)
447 {
448 ERROR ("netlink plugin: Invalid number of fields for option "
449 "`%s'. Got %i, expected 1.", key, fields_num);
450 status = -1;
451 }
452 else
453 {
454 add_ignorelist (fields[0], "interface", NULL);
455 if (strcasecmp (key, "VerboseInterface") == 0)
456 add_ignorelist (fields[0], "if_detail", NULL);
457 status = 0;
458 }
459 }
460 else if ((strcasecmp (key, "QDisc") == 0)
461 || (strcasecmp (key, "Class") == 0)
462 || (strcasecmp (key, "Filter") == 0))
463 {
464 if ((fields_num < 1) || (fields_num > 2))
465 {
466 ERROR ("netlink plugin: Invalid number of fields for option "
467 "`%s'. Got %i, expected 1 or 2.", key, fields_num);
468 return (-1);
469 }
470 else
471 {
472 add_ignorelist (fields[0], key,
473 (fields_num == 2) ? fields[1] : NULL);
474 status = 0;
475 }
476 }
477 else if (strcasecmp (key, "IgnoreSelected") == 0)
478 {
479 if (fields_num != 1)
480 {
481 ERROR ("netlink plugin: Invalid number of fields for option "
482 "`IgnoreSelected'. Got %i, expected 1.", fields_num);
483 status = -1;
484 }
485 else
486 {
487 if ((strcasecmp (fields[0], "yes") == 0)
488 || (strcasecmp (fields[0], "true") == 0)
489 || (strcasecmp (fields[0], "on") == 0))
490 ir_ignorelist_invert = 0;
491 else
492 ir_ignorelist_invert = 1;
493 status = 0;
494 }
495 }
497 sfree (new_val);
499 return (status);
500 } /* int ir_config */
502 static int ir_init (void)
503 {
504 memset (&rth, '\0', sizeof (rth));
506 if (rtnl_open (&rth, 0) != 0)
507 {
508 ERROR ("netlink plugin: ir_read: rtnl_open failed.");
509 return (-1);
510 }
512 if (ll_init_map (&rth) != 0)
513 {
514 ERROR ("netlink plugin: ir_read: ll_init_map failed.");
515 return (-1);
516 }
518 return (0);
519 } /* int ir_init */
521 static int ir_read (void)
522 {
523 struct ifinfomsg im;
524 struct tcmsg tm;
525 int ifindex;
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)
534 {
535 ERROR ("netlink plugin: ir_read: rtnl_dump_request failed.");
536 return (-1);
537 }
539 if (rtnl_dump_filter (&rth, link_filter, /* arg1 = */ NULL,
540 NULL, NULL) != 0)
541 {
542 ERROR ("netlink plugin: ir_read: rtnl_dump_filter failed.");
543 return (-1);
544 }
546 /* `link_filter' will update `iflist' which is used here to iterate over all
547 * interfaces. */
548 for (ifindex = 0; ifindex < iflist_len; ifindex++)
549 {
550 int type_index;
552 if (iflist[ifindex] == NULL)
553 continue;
555 for (type_index = 0; type_index < STATIC_ARRAY_SIZE (type_id); type_index++)
556 {
557 if (check_ignorelist (iflist[ifindex], type_name[type_index], NULL))
558 {
559 DEBUG ("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
560 "== TRUE", iflist[ifindex], type_name[type_index]);
561 continue;
562 }
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)
572 {
573 ERROR ("netlink plugin: ir_read: rtnl_dump_request failed.");
574 continue;
575 }
577 if (rtnl_dump_filter (&rth, qos_filter, (void *) &ifindex,
578 NULL, NULL) != 0)
579 {
580 ERROR ("netlink plugin: ir_read: rtnl_dump_filter failed.");
581 continue;
582 }
583 } /* for (type_index) */
584 } /* for (if_index) */
586 return (0);
587 } /* int ir_read */
589 static int ir_shutdown (void)
590 {
591 if ((rth.fd != 0) || (rth.seq != 0) || (rth.dump != 0))
592 {
593 rtnl_close(&rth);
594 memset (&rth, '\0', sizeof (rth));
595 }
597 return (0);
598 } /* int ir_shutdown */
600 void module_register (void)
601 {
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 */
608 /*
609 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
610 */