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 const char *config_keys[] =
57 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
59 static int add_ignorelist (const char *dev, const char *type,
62 ir_ignorelist_t *entry;
64 entry = (ir_ignorelist_t *) malloc (sizeof (ir_ignorelist_t));
68 memset (entry, '\0', sizeof (ir_ignorelist_t));
70 if (strcasecmp (dev, "All") != 0)
72 entry->device = strdup (dev);
73 if (entry->device == NULL)
80 entry->type = strdup (type);
81 if (entry->type == NULL)
83 sfree (entry->device);
90 entry->inst = strdup (inst);
91 if (entry->inst == NULL)
94 sfree (entry->device);
100 entry->next = ir_ignorelist_head;
101 ir_ignorelist_head = entry;
104 } /* int add_ignorelist */
107 * Checks wether a data set should be ignored. Returns `true' is the value
108 * should be ignored, `false' otherwise.
110 static int check_ignorelist (const char *dev,
111 const char *type, const char *type_instance)
115 if (ir_ignorelist_head == NULL)
116 return (ir_ignorelist_invert ? 0 : 1);
118 for (i = ir_ignorelist_head; i != NULL; i = i->next)
120 if ((strcasecmp (i->device, dev) != 0)
121 || (strcasecmp (i->type, type) != 0))
124 if ((i->inst != NULL)
125 && ((type_instance == NULL)
126 || (strcasecmp (i->inst, type_instance) != 0)))
129 return (ir_ignorelist_invert ? 0 : 1);
132 return (ir_ignorelist_invert);
133 } /* int check_ignorelist */
135 static void submit_one (const char *dev, const char *type,
136 const char *type_instance, counter_t value)
139 value_list_t vl = VALUE_LIST_INIT;
141 values[0].counter = value;
145 vl.time = time (NULL);
146 strcpy (vl.host, hostname_g);
147 strcpy (vl.plugin, "netlink");
148 strncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
150 if (type_instance != NULL)
151 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
153 plugin_dispatch_values (type, &vl);
154 } /* void submit_one */
156 static void submit_two (const char *dev, const char *type,
157 const char *type_instance,
158 counter_t rx, counter_t tx)
161 value_list_t vl = VALUE_LIST_INIT;
163 values[0].counter = rx;
164 values[1].counter = tx;
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_two */
179 static int link_filter (const struct sockaddr_nl *sa, struct nlmsghdr *nmh,
182 struct ifinfomsg *msg;
184 struct rtattr *attrs[IFLA_MAX + 1];
185 struct rtnl_link_stats *stats;
189 if (nmh->nlmsg_type != RTM_NEWLINK)
191 ERROR ("netlink plugin: link_filter: Don't know how to handle type %i.\n",
196 msg = NLMSG_DATA (nmh);
198 msg_len = nmh->nlmsg_len - sizeof (struct ifinfomsg);
201 ERROR ("netlink plugin: link_filter: msg_len = %i < 0;\n", msg_len);
205 memset (attrs, '\0', sizeof (attrs));
206 if (parse_rtattr (attrs, IFLA_MAX, IFLA_RTA (msg), msg_len) != 0)
208 ERROR ("netlink plugin: link_filter: parse_rtattr failed.\n");
212 if (attrs[IFLA_STATS] == NULL)
214 stats = RTA_DATA (attrs[IFLA_STATS]);
216 if (attrs[IFLA_IFNAME] == NULL)
218 ERROR ("netlink plugin: link_filter: attrs[IFLA_IFNAME] == NULL\n");
221 dev = RTA_DATA (attrs[IFLA_IFNAME]);
223 if (check_ignorelist (dev, "interface", NULL) == 0)
225 submit_two (dev, "if_octets", NULL, stats->rx_bytes, stats->tx_bytes);
226 submit_two (dev, "if_packets", NULL, stats->rx_packets, stats->tx_packets);
227 submit_two (dev, "if_errors", NULL, stats->rx_errors, stats->tx_errors);
231 DEBUG ("netlink plugin: Ignoring %s/interface.", dev);
234 if (check_ignorelist (dev, "if_detail", NULL) == 0)
236 submit_two (dev, "if_dropped", NULL, stats->rx_dropped, stats->tx_dropped);
237 submit_one (dev, "if_multicast", NULL, stats->multicast);
238 submit_one (dev, "if_collisions", NULL, stats->collisions);
240 submit_one (dev, "if_rx_errors", "length", stats->rx_length_errors);
241 submit_one (dev, "if_rx_errors", "over", stats->rx_over_errors);
242 submit_one (dev, "if_rx_errors", "crc", stats->rx_crc_errors);
243 submit_one (dev, "if_rx_errors", "frame", stats->rx_frame_errors);
244 submit_one (dev, "if_rx_errors", "fifo", stats->rx_fifo_errors);
245 submit_one (dev, "if_rx_errors", "missed", stats->rx_missed_errors);
247 submit_one (dev, "if_tx_errors", "aborted", stats->tx_aborted_errors);
248 submit_one (dev, "if_tx_errors", "carrier", stats->tx_carrier_errors);
249 submit_one (dev, "if_tx_errors", "fifo", stats->tx_fifo_errors);
250 submit_one (dev, "if_tx_errors", "heartbeat", stats->tx_heartbeat_errors);
251 submit_one (dev, "if_tx_errors", "window", stats->tx_window_errors);
255 DEBUG ("netlink plugin: Ignoring %s/if_detail.", dev);
259 } /* int link_filter */
261 static int qos_filter (const struct sockaddr_nl *sa, struct nlmsghdr *nmh,
266 struct rtattr *attrs[TCA_MAX + 1];
270 /* char *type_instance; */
272 char tc_inst[DATA_MAX_NAME_LEN];
274 if (nmh->nlmsg_type == RTM_NEWQDISC)
276 else if (nmh->nlmsg_type == RTM_NEWTCLASS)
278 else if (nmh->nlmsg_type == RTM_NEWTFILTER)
282 ERROR ("netlink plugin: qos_filter: Don't know how to handle type %i.\n",
287 msg = NLMSG_DATA (nmh);
289 msg_len = nmh->nlmsg_len - sizeof (struct tcmsg);
292 ERROR ("netlink plugin: qos_filter: msg_len = %i < 0;\n", msg_len);
296 dev = ll_index_to_name (msg->tcm_ifindex);
299 ERROR ("netlink plugin: qos_filter: ll_index_to_name (%i) failed.\n",
304 memset (attrs, '\0', sizeof (attrs));
305 if (parse_rtattr (attrs, TCA_MAX, TCA_RTA (msg), msg_len) != 0)
307 ERROR ("netlink plugin: qos_filter: parse_rtattr failed.\n");
311 if (attrs[TCA_KIND] == NULL)
313 ERROR ("netlink plugin: qos_filter: attrs[TCA_KIND] == NULL\n");
318 uint32_t numberic_id;
320 numberic_id = msg->tcm_handle;
321 if (strcmp (tc_type, "filter") == 0)
322 numberic_id = msg->tcm_parent;
324 snprintf (tc_inst, sizeof (tc_inst), "%s-%x:%x",
325 (const char *) RTA_DATA (attrs[TCA_KIND]),
327 numberic_id & 0x0000FFFF);
328 tc_inst[sizeof (tc_inst) - 1] = '\0';
331 if (check_ignorelist (dev, tc_type, tc_inst))
334 if (attrs[TCA_STATS2])
336 struct rtattr *attrs_stats[TCA_STATS_MAX + 1];
338 memset (attrs_stats, '\0', sizeof (attrs_stats));
339 parse_rtattr_nested (attrs_stats, TCA_STATS_MAX, attrs[TCA_STATS2]);
341 if (attrs_stats[TCA_STATS_BASIC])
343 struct gnet_stats_basic bs;
344 char type_instance[DATA_MAX_NAME_LEN];
346 snprintf (type_instance, sizeof (type_instance), "%s-%s",
348 type_instance[sizeof (type_instance) - 1] = '\0';
350 memset (&bs, '\0', sizeof (bs));
351 memcpy (&bs, RTA_DATA (attrs_stats[TCA_STATS_BASIC]),
352 MIN (RTA_PAYLOAD (attrs_stats[TCA_STATS_BASIC]), sizeof(bs)));
354 submit_one (dev, "ipt_bytes", type_instance, bs.bytes);
355 submit_one (dev, "ipt_packets", type_instance, bs.packets);
360 } /* int qos_filter */
362 static int ir_config (const char *key, const char *value)
369 new_val = strdup (value);
373 fields_num = strsplit (new_val, fields, STATIC_ARRAY_SIZE (fields));
374 if ((fields_num < 1) || (fields_num > 8))
380 if ((strcasecmp (key, "Interface") == 0)
381 || (strcasecmp (key, "VerboseInterface") == 0))
385 ERROR ("netlink plugin: Invalid number of fields for option "
386 "`%s'. Got %i, expected 1.", key, fields_num);
391 add_ignorelist (fields[0], "interface", NULL);
392 if (strcasecmp (key, "VerboseInterface") == 0)
393 add_ignorelist (fields[0], "if_detail", NULL);
397 else if ((strcasecmp (key, "QDisc") == 0)
398 || (strcasecmp (key, "Class") == 0)
399 || (strcasecmp (key, "Filter") == 0))
401 if ((fields_num < 1) || (fields_num > 2))
403 ERROR ("netlink plugin: Invalid number of fields for option "
404 "`%s'. Got %i, expected 1 or 2.", key, fields_num);
409 add_ignorelist (fields[0], key,
410 (fields_num == 2) ? fields[1] : NULL);
414 else if (strcasecmp (key, "IgnoreSelected"))
418 ERROR ("netlink plugin: Invalid number of fields for option "
419 "`IgnoreSelected'. Got %i, expected 1.", fields_num);
424 if ((strcasecmp (fields[0], "yes") == 0)
425 || (strcasecmp (fields[0], "true") == 0)
426 || (strcasecmp (fields[0], "on") == 0))
427 ir_ignorelist_invert = 0;
429 ir_ignorelist_invert = 1;
437 } /* int ir_config */
439 static int ir_init (void)
441 memset (&rth, '\0', sizeof (rth));
443 if (rtnl_open (&rth, 0) != 0)
445 ERROR ("netlink plugin: print_stats: rtnl_open failed.\n");
449 if (ll_init_map (&rth) != 0)
451 ERROR ("netlink plugin: print_stats: ll_init_map failed.\n");
458 static int ir_read (void)
463 memset (&im, '\0', sizeof (im));
464 im.ifi_type = AF_UNSPEC;
466 memset (&tm, '\0', sizeof (tm));
467 tm.tcm_family = AF_UNSPEC;
469 if (rtnl_dump_request (&rth, RTM_GETLINK, &im, sizeof (im)) < 0)
471 ERROR ("netlink plugin: print_stats: rtnl_dump_request failed.\n");
475 if (rtnl_dump_filter (&rth, link_filter, /* arg1 = */ NULL,
478 ERROR ("netlink plugin: print_stats: rtnl_dump_filter failed.\n");
482 /* Get QDisc stats */
483 if (rtnl_dump_request (&rth, RTM_GETQDISC, &tm, sizeof (tm)) < 0)
485 ERROR ("netlink plugin: print_stats: rtnl_dump_request failed.\n");
489 if (rtnl_dump_filter (&rth, qos_filter, /* arg1 = */ NULL,
492 ERROR ("netlink plugin: print_stats: rtnl_dump_filter failed.\n");
496 /* Get Class stats */
497 if (rtnl_dump_request (&rth, RTM_GETTCLASS, &tm, sizeof (tm)) < 0)
499 ERROR ("netlink plugin: print_stats: rtnl_dump_request failed.\n");
503 if (rtnl_dump_filter (&rth, qos_filter, /* arg1 = */ NULL,
506 ERROR ("netlink plugin: print_stats: rtnl_dump_filter failed.\n");
510 /* Get Filter stats */
511 if (rtnl_dump_request (&rth, RTM_GETTFILTER, &tm, sizeof (tm)) < 0)
513 ERROR ("netlink plugin: print_stats: rtnl_dump_request failed.\n");
517 if (rtnl_dump_filter (&rth, qos_filter, /* arg1 = */ NULL,
520 ERROR ("netlink plugin: print_stats: rtnl_dump_filter failed.\n");
526 } /* int print_stats */
528 static int ir_shutdown (void)
530 if ((rth.fd != 0) || (rth.seq != 0) || (rth.dump != 0))
533 memset (&rth, '\0', sizeof (rth));
537 } /* int ir_shutdown */
539 void module_register (void)
541 plugin_register_config ("netlink", ir_config, config_keys, config_keys_num);
542 plugin_register_init ("netlink", ir_init);
543 plugin_register_read ("netlink", ir_read);
544 plugin_register_shutdown ("netlink", ir_shutdown);
545 } /* void module_register */
548 * vim: set shiftwidth=2 softtabstop=2 tabstop=8 :