2 * src/nl-link-stats.c Retrieve link statistics
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation version 2.1
9 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
14 static void print_usage(void)
17 "Usage: nl-link-stats <ifindex> [<statistic> ...]\n"
18 " ifindex := { all | number }\n"
19 " statistic := { (rx|tx)_packets | (rx|tx)_bytes | (rx|tx)_errors |\n"
20 " (rx|tx)_dropped | (rx|tx)_compressed | (rx|tx)_fifo_err |\n" \
21 " rx_len_err | rx_over_err | rx_crc_err | rx_frame_err |\n"
22 " rx_missed_err | tx_abort_err | tx_carrier_err |\n"
23 " tx_hbeat_err | tx_win_err | tx_collision | multicast }\n");
30 static void dump_stat(struct rtnl_link *link, int id)
32 uint64_t st = rtnl_link_get_stat(link, id);
35 printf("%s.%s %" PRIu64 "\n", rtnl_link_get_name(link),
36 rtnl_link_stat2str(id, buf, sizeof(buf)), st);
39 static void dump_stats(struct nl_object *obj, void *arg)
42 struct rtnl_link *link = (struct rtnl_link *) obj;
44 if (!strcasecmp(gargv[0], "all")) {
45 for (i = 0; i < RTNL_LINK_STATS_MAX; i++)
48 for (i = 0; i < gargc; i++) {
49 int id = rtnl_link_str2stat(gargv[i]);
52 fprintf(stderr, "Warning: Unknown statistic "
53 "\"%s\"\n", gargv[i]);
60 int main(int argc, char *argv[])
62 struct nl_handle *nlh;
63 struct nl_cache *link_cache;
66 if (nltool_init(argc, argv) < 0)
69 if (argc < 3 || !strcmp(argv[1], "-h"))
72 nlh = nltool_alloc_handle();
76 if (nltool_connect(nlh, NETLINK_ROUTE) < 0)
79 link_cache = nltool_alloc_link_cache(nlh);
86 if (!strcasecmp(argv[1], "all"))
87 nl_cache_foreach(link_cache, dump_stats, NULL);
89 int ifindex = strtoul(argv[1], NULL, 0);
90 struct rtnl_link *link = rtnl_link_get(link_cache, ifindex);
93 fprintf(stderr, "Could not find ifindex %d\n", ifindex);
94 goto errout_link_cache;
97 dump_stats((struct nl_object *) link, NULL);
103 nl_cache_free(link_cache);
107 nl_handle_destroy(nlh);