8c883f67179a9525d71dc0a1a04b11dbb9a3a1bd
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>
29 #include <linux/netlink.h>
30 #include <linux/rtnetlink.h>
31 #if HAVE_LINUX_GEN_STATS_H
32 # include <linux/gen_stats.h>
33 #endif
34 #if HAVE_LINUX_PKT_SCHED_H
35 # include <linux/pkt_sched.h>
36 #endif
38 #if HAVE_LIBNETLINK_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>
44 #endif
46 typedef struct ir_ignorelist_s
47 {
48 char *device;
49 char *type;
50 char *inst;
51 struct ir_ignorelist_s *next;
52 } ir_ignorelist_t;
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[] =
63 {
64 "Interface",
65 "VerboseInterface",
66 "QDisc",
67 "Class",
68 "Filter",
69 "IgnoreSelected"
70 };
71 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
73 static int add_ignorelist (const char *dev, const char *type,
74 const char *inst)
75 {
76 ir_ignorelist_t *entry;
78 entry = (ir_ignorelist_t *) malloc (sizeof (ir_ignorelist_t));
79 if (entry == NULL)
80 return (-1);
82 memset (entry, '\0', sizeof (ir_ignorelist_t));
84 if (strcasecmp (dev, "All") != 0)
85 {
86 entry->device = strdup (dev);
87 if (entry->device == NULL)
88 {
89 sfree (entry);
90 return (-1);
91 }
92 }
94 entry->type = strdup (type);
95 if (entry->type == NULL)
96 {
97 sfree (entry->device);
98 sfree (entry);
99 return (-1);
100 }
102 if (inst != NULL)
103 {
104 entry->inst = strdup (inst);
105 if (entry->inst == NULL)
106 {
107 sfree (entry->type);
108 sfree (entry->device);
109 sfree (entry);
110 return (-1);
111 }
112 }
114 entry->next = ir_ignorelist_head;
115 ir_ignorelist_head = entry;
117 return (0);
118 } /* int add_ignorelist */
120 /*
121 * Checks wether a data set should be ignored. Returns `true' is the value
122 * should be ignored, `false' otherwise.
123 */
124 static int check_ignorelist (const char *dev,
125 const char *type, const char *type_instance)
126 {
127 ir_ignorelist_t *i;
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)
135 {
136 /* i->device == NULL => match all devices */
137 if ((i->device != NULL)
138 && (strcasecmp (i->device, dev) != 0))
139 continue;
141 if (strcasecmp (i->type, type) != 0)
142 continue;
144 if ((i->inst != NULL) && (type_instance != NULL)
145 && (strcasecmp (i->inst, type_instance) != 0))
146 continue;
148 DEBUG ("netlink plugin: check_ignorelist: "
149 "(dev = %s; type = %s; inst = %s) matched "
150 "(dev = %s; type = %s; inst = %s)",
151 dev, type,
152 type_instance == NULL ? "(nil)" : type_instance,
153 i->device == NULL ? "(nil)" : i->device,
154 i->type,
155 i->inst == NULL ? "(nil)" : i->inst);
157 return (ir_ignorelist_invert ? 0 : 1);
158 } /* for i */
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)
165 {
166 value_t values[1];
167 value_list_t vl = VALUE_LIST_INIT;
169 values[0].counter = value;
171 vl.values = values;
172 vl.values_len = 1;
173 vl.time = time (NULL);
174 strcpy (vl.host, hostname_g);
175 strcpy (vl.plugin, "netlink");
176 strncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
178 if (type_instance != NULL)
179 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
181 plugin_dispatch_values (type, &vl);
182 } /* void submit_one */
184 static void submit_two (const char *dev, const char *type,
185 const char *type_instance,
186 counter_t rx, counter_t tx)
187 {
188 value_t values[2];
189 value_list_t vl = VALUE_LIST_INIT;
191 values[0].counter = rx;
192 values[1].counter = tx;
194 vl.values = values;
195 vl.values_len = 2;
196 vl.time = time (NULL);
197 strcpy (vl.host, hostname_g);
198 strcpy (vl.plugin, "netlink");
199 strncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
201 if (type_instance != NULL)
202 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
204 plugin_dispatch_values (type, &vl);
205 } /* void submit_two */
207 static int link_filter (const struct sockaddr_nl *sa,
208 const struct nlmsghdr *nmh, void *args)
209 {
210 struct ifinfomsg *msg;
211 int msg_len;
212 struct rtattr *attrs[IFLA_MAX + 1];
213 struct rtnl_link_stats *stats;
215 const char *dev;
217 if (nmh->nlmsg_type != RTM_NEWLINK)
218 {
219 ERROR ("netlink plugin: link_filter: Don't know how to handle type %i.",
220 nmh->nlmsg_type);
221 return (-1);
222 }
224 msg = NLMSG_DATA (nmh);
226 msg_len = nmh->nlmsg_len - sizeof (struct ifinfomsg);
227 if (msg_len < 0)
228 {
229 ERROR ("netlink plugin: link_filter: msg_len = %i < 0;", msg_len);
230 return (-1);
231 }
233 memset (attrs, '\0', sizeof (attrs));
234 if (parse_rtattr (attrs, IFLA_MAX, IFLA_RTA (msg), msg_len) != 0)
235 {
236 ERROR ("netlink plugin: link_filter: parse_rtattr failed.");
237 return (-1);
238 }
240 if (attrs[IFLA_STATS] == NULL)
241 return (-1);
242 stats = RTA_DATA (attrs[IFLA_STATS]);
244 if (attrs[IFLA_IFNAME] == NULL)
245 {
246 ERROR ("netlink plugin: link_filter: attrs[IFLA_IFNAME] == NULL");
247 return (-1);
248 }
249 dev = RTA_DATA (attrs[IFLA_IFNAME]);
251 /* Update the `iflist'. It's used to know which interfaces exist and query
252 * them later for qdiscs and classes. */
253 if (msg->ifi_index >= iflist_len)
254 {
255 char **temp;
257 temp = (char **) realloc (iflist, (msg->ifi_index + 1) * sizeof (char *));
258 if (temp == NULL)
259 {
260 ERROR ("netlink plugin: link_filter: realloc failed.");
261 return (-1);
262 }
264 memset (temp + iflist_len, '\0',
265 (msg->ifi_index + 1 - iflist_len) * sizeof (char *));
266 iflist = temp;
267 iflist_len = msg->ifi_index + 1;
268 }
269 if ((iflist[msg->ifi_index] == NULL)
270 || (strcmp (iflist[msg->ifi_index], dev) != 0))
271 {
272 sfree (iflist[msg->ifi_index]);
273 iflist[msg->ifi_index] = strdup (dev);
274 }
276 if (check_ignorelist (dev, "interface", NULL) == 0)
277 {
278 submit_two (dev, "if_octets", NULL, stats->rx_bytes, stats->tx_bytes);
279 submit_two (dev, "if_packets", NULL, stats->rx_packets, stats->tx_packets);
280 submit_two (dev, "if_errors", NULL, stats->rx_errors, stats->tx_errors);
281 }
282 else
283 {
284 DEBUG ("netlink plugin: Ignoring %s/interface.", dev);
285 }
287 if (check_ignorelist (dev, "if_detail", NULL) == 0)
288 {
289 submit_two (dev, "if_dropped", NULL, stats->rx_dropped, stats->tx_dropped);
290 submit_one (dev, "if_multicast", NULL, stats->multicast);
291 submit_one (dev, "if_collisions", NULL, stats->collisions);
293 submit_one (dev, "if_rx_errors", "length", stats->rx_length_errors);
294 submit_one (dev, "if_rx_errors", "over", stats->rx_over_errors);
295 submit_one (dev, "if_rx_errors", "crc", stats->rx_crc_errors);
296 submit_one (dev, "if_rx_errors", "frame", stats->rx_frame_errors);
297 submit_one (dev, "if_rx_errors", "fifo", stats->rx_fifo_errors);
298 submit_one (dev, "if_rx_errors", "missed", stats->rx_missed_errors);
300 submit_one (dev, "if_tx_errors", "aborted", stats->tx_aborted_errors);
301 submit_one (dev, "if_tx_errors", "carrier", stats->tx_carrier_errors);
302 submit_one (dev, "if_tx_errors", "fifo", stats->tx_fifo_errors);
303 submit_one (dev, "if_tx_errors", "heartbeat", stats->tx_heartbeat_errors);
304 submit_one (dev, "if_tx_errors", "window", stats->tx_window_errors);
305 }
306 else
307 {
308 DEBUG ("netlink plugin: Ignoring %s/if_detail.", dev);
309 }
311 return (0);
312 } /* int link_filter */
314 static int qos_filter (const struct sockaddr_nl *sa,
315 const struct nlmsghdr *nmh, void *args)
316 {
317 struct tcmsg *msg;
318 int msg_len;
319 struct rtattr *attrs[TCA_MAX + 1];
321 int wanted_ifindex = *((int *) args);
323 const char *dev;
325 /* char *type_instance; */
326 char *tc_type;
327 char tc_inst[DATA_MAX_NAME_LEN];
329 if (nmh->nlmsg_type == RTM_NEWQDISC)
330 tc_type = "qdisc";
331 else if (nmh->nlmsg_type == RTM_NEWTCLASS)
332 tc_type = "class";
333 else if (nmh->nlmsg_type == RTM_NEWTFILTER)
334 tc_type = "filter";
335 else
336 {
337 ERROR ("netlink plugin: qos_filter: Don't know how to handle type %i.",
338 nmh->nlmsg_type);
339 return (-1);
340 }
342 msg = NLMSG_DATA (nmh);
344 msg_len = nmh->nlmsg_len - sizeof (struct tcmsg);
345 if (msg_len < 0)
346 {
347 ERROR ("netlink plugin: qos_filter: msg_len = %i < 0;", msg_len);
348 return (-1);
349 }
351 if (msg->tcm_ifindex != wanted_ifindex)
352 {
353 DEBUG ("netlink plugin: qos_filter: Got %s for interface #%i, "
354 "but expected #%i.",
355 tc_type, msg->tcm_ifindex, wanted_ifindex);
356 return (0);
357 }
359 if (msg->tcm_ifindex >= iflist_len)
360 {
361 ERROR ("netlink plugin: qos_filter: msg->tcm_ifindex = %i "
362 ">= iflist_len = %i",
363 msg->tcm_ifindex, iflist_len);
364 return (-1);
365 }
367 dev = iflist[msg->tcm_ifindex];
368 if (dev == NULL)
369 {
370 ERROR ("netlink plugin: qos_filter: iflist[%i] == NULL",
371 msg->tcm_ifindex);
372 return (-1);
373 }
375 memset (attrs, '\0', sizeof (attrs));
376 if (parse_rtattr (attrs, TCA_MAX, TCA_RTA (msg), msg_len) != 0)
377 {
378 ERROR ("netlink plugin: qos_filter: parse_rtattr failed.");
379 return (-1);
380 }
382 if (attrs[TCA_KIND] == NULL)
383 {
384 ERROR ("netlink plugin: qos_filter: attrs[TCA_KIND] == NULL");
385 return (-1);
386 }
388 { /* The the ID */
389 uint32_t numberic_id;
391 numberic_id = msg->tcm_handle;
392 if (strcmp (tc_type, "filter") == 0)
393 numberic_id = msg->tcm_parent;
395 snprintf (tc_inst, sizeof (tc_inst), "%s-%x:%x",
396 (const char *) RTA_DATA (attrs[TCA_KIND]),
397 numberic_id >> 16,
398 numberic_id & 0x0000FFFF);
399 tc_inst[sizeof (tc_inst) - 1] = '\0';
400 }
402 DEBUG ("netlink plugin: qos_filter: got %s for %s (%i).",
403 tc_type, dev, msg->tcm_ifindex);
405 if (check_ignorelist (dev, tc_type, tc_inst))
406 return (0);
408 #if HAVE_TCA_STATS2
409 if (attrs[TCA_STATS2])
410 {
411 struct rtattr *attrs_stats[TCA_STATS_MAX + 1];
413 memset (attrs_stats, '\0', sizeof (attrs_stats));
414 parse_rtattr_nested (attrs_stats, TCA_STATS_MAX, attrs[TCA_STATS2]);
416 if (attrs_stats[TCA_STATS_BASIC])
417 {
418 struct gnet_stats_basic bs;
419 char type_instance[DATA_MAX_NAME_LEN];
421 snprintf (type_instance, sizeof (type_instance), "%s-%s",
422 tc_type, tc_inst);
423 type_instance[sizeof (type_instance) - 1] = '\0';
425 memset (&bs, '\0', sizeof (bs));
426 memcpy (&bs, RTA_DATA (attrs_stats[TCA_STATS_BASIC]),
427 MIN (RTA_PAYLOAD (attrs_stats[TCA_STATS_BASIC]), sizeof(bs)));
429 submit_one (dev, "ipt_bytes", type_instance, bs.bytes);
430 submit_one (dev, "ipt_packets", type_instance, bs.packets);
431 }
432 }
433 #endif /* TCA_STATS2 */
434 #if HAVE_TCA_STATS && HAVE_TCA_STATS2
435 else
436 #endif
437 #if HAVE_TCA_STATS
438 if (attrs[TCA_STATS] != NULL)
439 {
440 struct tc_stats ts;
441 char type_instance[DATA_MAX_NAME_LEN];
443 snprintf (type_instance, sizeof (type_instance), "%s-%s",
444 tc_type, tc_inst);
445 type_instance[sizeof (type_instance) - 1] = '\0';
447 memset(&ts, '\0', sizeof (ts));
448 memcpy(&ts, RTA_DATA (attrs[TCA_STATS]),
449 MIN (RTA_PAYLOAD (attrs[TCA_STATS]), sizeof (ts)));
451 submit_one (dev, "ipt_bytes", type_instance, ts.bytes);
452 submit_one (dev, "ipt_packets", type_instance, ts.packets);
453 }
454 #endif /* TCA_STATS */
455 #if HAVE_TCA_STATS || HAVE_TCA_STATS2
456 else
457 #endif
458 {
459 DEBUG ("netlink plugin: qos_filter: Have neither TCA_STATS2 nor "
460 "TCA_STATS.");
461 }
463 return (0);
464 } /* int qos_filter */
466 static int ir_config (const char *key, const char *value)
467 {
468 char *new_val;
469 char *fields[8];
470 int fields_num;
471 int status = 1;
473 new_val = strdup (value);
474 if (new_val == NULL)
475 return (-1);
477 fields_num = strsplit (new_val, fields, STATIC_ARRAY_SIZE (fields));
478 if ((fields_num < 1) || (fields_num > 8))
479 {
480 sfree (new_val);
481 return (-1);
482 }
484 if ((strcasecmp (key, "Interface") == 0)
485 || (strcasecmp (key, "VerboseInterface") == 0))
486 {
487 if (fields_num != 1)
488 {
489 ERROR ("netlink plugin: Invalid number of fields for option "
490 "`%s'. Got %i, expected 1.", key, fields_num);
491 status = -1;
492 }
493 else
494 {
495 add_ignorelist (fields[0], "interface", NULL);
496 if (strcasecmp (key, "VerboseInterface") == 0)
497 add_ignorelist (fields[0], "if_detail", NULL);
498 status = 0;
499 }
500 }
501 else if ((strcasecmp (key, "QDisc") == 0)
502 || (strcasecmp (key, "Class") == 0)
503 || (strcasecmp (key, "Filter") == 0))
504 {
505 if ((fields_num < 1) || (fields_num > 2))
506 {
507 ERROR ("netlink plugin: Invalid number of fields for option "
508 "`%s'. Got %i, expected 1 or 2.", key, fields_num);
509 return (-1);
510 }
511 else
512 {
513 add_ignorelist (fields[0], key,
514 (fields_num == 2) ? fields[1] : NULL);
515 status = 0;
516 }
517 }
518 else if (strcasecmp (key, "IgnoreSelected") == 0)
519 {
520 if (fields_num != 1)
521 {
522 ERROR ("netlink plugin: Invalid number of fields for option "
523 "`IgnoreSelected'. Got %i, expected 1.", fields_num);
524 status = -1;
525 }
526 else
527 {
528 if ((strcasecmp (fields[0], "yes") == 0)
529 || (strcasecmp (fields[0], "true") == 0)
530 || (strcasecmp (fields[0], "on") == 0))
531 ir_ignorelist_invert = 0;
532 else
533 ir_ignorelist_invert = 1;
534 status = 0;
535 }
536 }
538 sfree (new_val);
540 return (status);
541 } /* int ir_config */
543 static int ir_init (void)
544 {
545 memset (&rth, '\0', sizeof (rth));
547 if (rtnl_open (&rth, 0) != 0)
548 {
549 ERROR ("netlink plugin: ir_init: rtnl_open failed.");
550 return (-1);
551 }
553 return (0);
554 } /* int ir_init */
556 static int ir_read (void)
557 {
558 struct ifinfomsg im;
559 struct tcmsg tm;
560 int ifindex;
562 static const int type_id[] = { RTM_GETQDISC, RTM_GETTCLASS, RTM_GETTFILTER };
563 static const char *type_name[] = { "qdisc", "class", "filter" };
565 memset (&im, '\0', sizeof (im));
566 im.ifi_type = AF_UNSPEC;
568 if (rtnl_dump_request (&rth, RTM_GETLINK, &im, sizeof (im)) < 0)
569 {
570 ERROR ("netlink plugin: ir_read: rtnl_dump_request failed.");
571 return (-1);
572 }
574 if (rtnl_dump_filter (&rth, link_filter, /* arg1 = */ NULL,
575 NULL, NULL) != 0)
576 {
577 ERROR ("netlink plugin: ir_read: rtnl_dump_filter failed.");
578 return (-1);
579 }
581 /* `link_filter' will update `iflist' which is used here to iterate over all
582 * interfaces. */
583 for (ifindex = 0; ifindex < iflist_len; ifindex++)
584 {
585 int type_index;
587 if (iflist[ifindex] == NULL)
588 continue;
590 for (type_index = 0; type_index < STATIC_ARRAY_SIZE (type_id); type_index++)
591 {
592 if (check_ignorelist (iflist[ifindex], type_name[type_index], NULL))
593 {
594 DEBUG ("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
595 "== TRUE", iflist[ifindex], type_name[type_index]);
596 continue;
597 }
599 DEBUG ("netlink plugin: ir_read: querying %s from %s (%i).",
600 type_name[type_index], iflist[ifindex], ifindex);
602 memset (&tm, '\0', sizeof (tm));
603 tm.tcm_family = AF_UNSPEC;
604 tm.tcm_ifindex = ifindex;
606 if (rtnl_dump_request (&rth, type_id[type_index], &tm, sizeof (tm)) < 0)
607 {
608 ERROR ("netlink plugin: ir_read: rtnl_dump_request failed.");
609 continue;
610 }
612 if (rtnl_dump_filter (&rth, qos_filter, (void *) &ifindex,
613 NULL, NULL) != 0)
614 {
615 ERROR ("netlink plugin: ir_read: rtnl_dump_filter failed.");
616 continue;
617 }
618 } /* for (type_index) */
619 } /* for (if_index) */
621 return (0);
622 } /* int ir_read */
624 static int ir_shutdown (void)
625 {
626 if ((rth.fd != 0) || (rth.seq != 0) || (rth.dump != 0))
627 {
628 rtnl_close(&rth);
629 memset (&rth, '\0', sizeof (rth));
630 }
632 return (0);
633 } /* int ir_shutdown */
635 void module_register (void)
636 {
637 plugin_register_config ("netlink", ir_config, config_keys, config_keys_num);
638 plugin_register_init ("netlink", ir_init);
639 plugin_register_read ("netlink", ir_read);
640 plugin_register_shutdown ("netlink", ir_shutdown);
641 } /* void module_register */
643 /*
644 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
645 */