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 0
134 if ((i->inst != NULL)
135 && ((type_instance == NULL)
136 || (strcasecmp (i->inst, type_instance) != 0)))
137 continue;
138 #endif
139 if ((i->inst != NULL) && (type_instance != NULL)
140 && (strcasecmp (i->inst, type_instance) != 0))
141 continue;
143 DEBUG ("netlink plugin: check_ignorelist: "
144 "(dev = %s; type = %s; inst = %s) matched "
145 "(dev = %s; type = %s; inst = %s)",
146 dev, type,
147 type_instance == NULL ? "(nil)" : type_instance,
148 i->device == NULL ? "(nil)" : i->device,
149 i->type,
150 i->inst == NULL ? "(nil)" : i->inst);
152 return (ir_ignorelist_invert ? 0 : 1);
153 } /* for i */
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)
160 {
161 value_t values[1];
162 value_list_t vl = VALUE_LIST_INIT;
164 values[0].counter = value;
166 vl.values = values;
167 vl.values_len = 1;
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)
182 {
183 value_t values[2];
184 value_list_t vl = VALUE_LIST_INIT;
186 values[0].counter = rx;
187 values[1].counter = tx;
189 vl.values = values;
190 vl.values_len = 2;
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,
203 void *args)
204 {
205 struct ifinfomsg *msg;
206 int msg_len;
207 struct rtattr *attrs[IFLA_MAX + 1];
208 struct rtnl_link_stats *stats;
210 const char *dev;
212 if (nmh->nlmsg_type != RTM_NEWLINK)
213 {
214 ERROR ("netlink plugin: link_filter: Don't know how to handle type %i.",
215 nmh->nlmsg_type);
216 return (-1);
217 }
219 msg = NLMSG_DATA (nmh);
221 msg_len = nmh->nlmsg_len - sizeof (struct ifinfomsg);
222 if (msg_len < 0)
223 {
224 ERROR ("netlink plugin: link_filter: msg_len = %i < 0;", msg_len);
225 return (-1);
226 }
228 memset (attrs, '\0', sizeof (attrs));
229 if (parse_rtattr (attrs, IFLA_MAX, IFLA_RTA (msg), msg_len) != 0)
230 {
231 ERROR ("netlink plugin: link_filter: parse_rtattr failed.");
232 return (-1);
233 }
235 if (attrs[IFLA_STATS] == NULL)
236 return (-1);
237 stats = RTA_DATA (attrs[IFLA_STATS]);
239 if (attrs[IFLA_IFNAME] == NULL)
240 {
241 ERROR ("netlink plugin: link_filter: attrs[IFLA_IFNAME] == NULL");
242 return (-1);
243 }
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)
249 {
250 char **temp;
252 temp = (char **) realloc (iflist, (msg->ifi_index + 1) * sizeof (char *));
253 if (temp == NULL)
254 {
255 ERROR ("netlink plugin: link_filter: realloc failed.");
256 return (-1);
257 }
259 memset (temp + iflist_len, '\0',
260 (msg->ifi_index + 1 - iflist_len) * sizeof (char *));
261 iflist = temp;
262 iflist_len = msg->ifi_index + 1;
263 }
264 if ((iflist[msg->ifi_index] == NULL)
265 || (strcmp (iflist[msg->ifi_index], dev) != 0))
266 {
267 sfree (iflist[msg->ifi_index]);
268 iflist[msg->ifi_index] = strdup (dev);
269 }
271 if (check_ignorelist (dev, "interface", NULL) == 0)
272 {
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);
276 }
277 else
278 {
279 DEBUG ("netlink plugin: Ignoring %s/interface.", dev);
280 }
282 if (check_ignorelist (dev, "if_detail", NULL) == 0)
283 {
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);
300 }
301 else
302 {
303 DEBUG ("netlink plugin: Ignoring %s/if_detail.", dev);
304 }
306 return (0);
307 } /* int link_filter */
309 static int qos_filter (const struct sockaddr_nl *sa, struct nlmsghdr *nmh,
310 void *args)
311 {
312 struct tcmsg *msg;
313 int msg_len;
314 struct rtattr *attrs[TCA_MAX + 1];
316 int wanted_ifindex = *((int *) args);
318 const char *dev;
320 /* char *type_instance; */
321 char *tc_type;
322 char tc_inst[DATA_MAX_NAME_LEN];
324 if (nmh->nlmsg_type == RTM_NEWQDISC)
325 tc_type = "qdisc";
326 else if (nmh->nlmsg_type == RTM_NEWTCLASS)
327 tc_type = "class";
328 else if (nmh->nlmsg_type == RTM_NEWTFILTER)
329 tc_type = "filter";
330 else
331 {
332 ERROR ("netlink plugin: qos_filter: Don't know how to handle type %i.",
333 nmh->nlmsg_type);
334 return (-1);
335 }
337 msg = NLMSG_DATA (nmh);
339 msg_len = nmh->nlmsg_len - sizeof (struct tcmsg);
340 if (msg_len < 0)
341 {
342 ERROR ("netlink plugin: qos_filter: msg_len = %i < 0;", msg_len);
343 return (-1);
344 }
346 if (msg->tcm_ifindex != wanted_ifindex)
347 {
348 DEBUG ("netlink plugin: qos_filter: Got %s for interface #%i, "
349 "but expected #%i.",
350 tc_type, msg->tcm_ifindex, wanted_ifindex);
351 return (0);
352 }
354 if (msg->tcm_ifindex >= iflist_len)
355 {
356 ERROR ("netlink plugin: qos_filter: msg->tcm_ifindex = %i "
357 ">= iflist_len = %i",
358 msg->tcm_ifindex, iflist_len);
359 return (-1);
360 }
362 dev = iflist[msg->tcm_ifindex];
363 if (dev == NULL)
364 {
365 ERROR ("netlink plugin: qos_filter: iflist[%i] == NULL",
366 msg->tcm_ifindex);
367 return (-1);
368 }
370 memset (attrs, '\0', sizeof (attrs));
371 if (parse_rtattr (attrs, TCA_MAX, TCA_RTA (msg), msg_len) != 0)
372 {
373 ERROR ("netlink plugin: qos_filter: parse_rtattr failed.");
374 return (-1);
375 }
377 if (attrs[TCA_KIND] == NULL)
378 {
379 ERROR ("netlink plugin: qos_filter: attrs[TCA_KIND] == NULL");
380 return (-1);
381 }
383 { /* The the ID */
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]),
392 numberic_id >> 16,
393 numberic_id & 0x0000FFFF);
394 tc_inst[sizeof (tc_inst) - 1] = '\0';
395 }
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))
401 return (0);
403 if (attrs[TCA_STATS2])
404 {
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])
411 {
412 struct gnet_stats_basic bs;
413 char type_instance[DATA_MAX_NAME_LEN];
415 snprintf (type_instance, sizeof (type_instance), "%s-%s",
416 tc_type, tc_inst);
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);
425 }
426 }
428 return (0);
429 } /* int qos_filter */
431 static int ir_config (const char *key, const char *value)
432 {
433 char *new_val;
434 char *fields[8];
435 int fields_num;
436 int status = 1;
438 new_val = strdup (value);
439 if (new_val == NULL)
440 return (-1);
442 fields_num = strsplit (new_val, fields, STATIC_ARRAY_SIZE (fields));
443 if ((fields_num < 1) || (fields_num > 8))
444 {
445 sfree (new_val);
446 return (-1);
447 }
449 if ((strcasecmp (key, "Interface") == 0)
450 || (strcasecmp (key, "VerboseInterface") == 0))
451 {
452 if (fields_num != 1)
453 {
454 ERROR ("netlink plugin: Invalid number of fields for option "
455 "`%s'. Got %i, expected 1.", key, fields_num);
456 status = -1;
457 }
458 else
459 {
460 add_ignorelist (fields[0], "interface", NULL);
461 if (strcasecmp (key, "VerboseInterface") == 0)
462 add_ignorelist (fields[0], "if_detail", NULL);
463 status = 0;
464 }
465 }
466 else if ((strcasecmp (key, "QDisc") == 0)
467 || (strcasecmp (key, "Class") == 0)
468 || (strcasecmp (key, "Filter") == 0))
469 {
470 if ((fields_num < 1) || (fields_num > 2))
471 {
472 ERROR ("netlink plugin: Invalid number of fields for option "
473 "`%s'. Got %i, expected 1 or 2.", key, fields_num);
474 return (-1);
475 }
476 else
477 {
478 add_ignorelist (fields[0], key,
479 (fields_num == 2) ? fields[1] : NULL);
480 status = 0;
481 }
482 }
483 else if (strcasecmp (key, "IgnoreSelected") == 0)
484 {
485 if (fields_num != 1)
486 {
487 ERROR ("netlink plugin: Invalid number of fields for option "
488 "`IgnoreSelected'. Got %i, expected 1.", fields_num);
489 status = -1;
490 }
491 else
492 {
493 if ((strcasecmp (fields[0], "yes") == 0)
494 || (strcasecmp (fields[0], "true") == 0)
495 || (strcasecmp (fields[0], "on") == 0))
496 ir_ignorelist_invert = 0;
497 else
498 ir_ignorelist_invert = 1;
499 status = 0;
500 }
501 }
503 sfree (new_val);
505 return (status);
506 } /* int ir_config */
508 static int ir_init (void)
509 {
510 memset (&rth, '\0', sizeof (rth));
512 if (rtnl_open (&rth, 0) != 0)
513 {
514 ERROR ("netlink plugin: ir_read: rtnl_open failed.");
515 return (-1);
516 }
518 if (ll_init_map (&rth) != 0)
519 {
520 ERROR ("netlink plugin: ir_read: ll_init_map failed.");
521 return (-1);
522 }
524 return (0);
525 } /* int ir_init */
527 static int ir_read (void)
528 {
529 struct ifinfomsg im;
530 struct tcmsg tm;
531 int ifindex;
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)
540 {
541 ERROR ("netlink plugin: ir_read: rtnl_dump_request failed.");
542 return (-1);
543 }
545 if (rtnl_dump_filter (&rth, link_filter, /* arg1 = */ NULL,
546 NULL, NULL) != 0)
547 {
548 ERROR ("netlink plugin: ir_read: rtnl_dump_filter failed.");
549 return (-1);
550 }
552 /* `link_filter' will update `iflist' which is used here to iterate over all
553 * interfaces. */
554 for (ifindex = 0; ifindex < iflist_len; ifindex++)
555 {
556 int type_index;
558 if (iflist[ifindex] == NULL)
559 continue;
561 for (type_index = 0; type_index < STATIC_ARRAY_SIZE (type_id); type_index++)
562 {
563 if (check_ignorelist (iflist[ifindex], type_name[type_index], NULL))
564 {
565 DEBUG ("netlink plugin: ir_read: check_ignorelist (%s, %s, (nil)) "
566 "== TRUE", iflist[ifindex], type_name[type_index]);
567 continue;
568 }
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)
578 {
579 ERROR ("netlink plugin: ir_read: rtnl_dump_request failed.");
580 continue;
581 }
583 if (rtnl_dump_filter (&rth, qos_filter, (void *) &ifindex,
584 NULL, NULL) != 0)
585 {
586 ERROR ("netlink plugin: ir_read: rtnl_dump_filter failed.");
587 continue;
588 }
589 } /* for (type_index) */
590 } /* for (if_index) */
592 return (0);
593 } /* int ir_read */
595 static int ir_shutdown (void)
596 {
597 if ((rth.fd != 0) || (rth.seq != 0) || (rth.dump != 0))
598 {
599 rtnl_close(&rth);
600 memset (&rth, '\0', sizeof (rth));
601 }
603 return (0);
604 } /* int ir_shutdown */
606 void module_register (void)
607 {
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 */
614 /*
615 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :
616 */