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-2009 Thomas Graf <tgraf@suug.ch>
12 #include <netlink/cli/utils.h>
13 #include <netlink/cli/link.h>
15 static void print_usage(void)
18 "Usage: nl-link-stats [OPTION]... [LINK] [ListOfStats]\n"
21 " -l, --list List available statistic names\n"
22 " -h, --help Show this help\n"
23 " -v, --version Show versioning information\n"
26 " -n, --name=NAME link name\n"
27 " -i, --index=NUM interface index\n"
32 static void list_stat_names(void)
37 for (i = 0; i < RTNL_LINK_STATS_MAX; i++)
38 printf("%s\n", rtnl_link_stat2str(i, buf, sizeof(buf)));
45 static void dump_stat(struct rtnl_link *link, int id)
47 uint64_t st = rtnl_link_get_stat(link, id);
50 printf("%s.%s %" PRIu64 "\n", rtnl_link_get_name(link),
51 rtnl_link_stat2str(id, buf, sizeof(buf)), st);
54 static void dump_stats(struct nl_object *obj, void *arg)
56 struct rtnl_link *link = (struct rtnl_link *) obj;
59 if (optind >= gargc) {
62 for (i = 0; i < RTNL_LINK_STATS_MAX; i++)
65 while (optind < gargc) {
66 int id = rtnl_link_str2stat(argv[optind]);
69 fprintf(stderr, "Warning: Unknown statistic "
70 "\"%s\"\n", argv[optind]);
79 int main(int argc, char *argv[])
82 struct nl_cache *link_cache;
83 struct rtnl_link *link;
85 sock = nl_cli_alloc_socket();
86 nl_cli_connect(sock, NETLINK_ROUTE);
87 link_cache = nl_cli_link_alloc_cache(sock);
88 link = nl_cli_link_alloc();
92 static struct option long_opts[] = {
93 { "list", 0, 0, 'l' },
94 { "help", 0, 0, 'h' },
95 { "version", 0, 0, 'v' },
96 { "name", 1, 0, 'n' },
97 { "index", 1, 0, 'i' },
101 c = getopt_long(argc, argv, "lhvn:i:", long_opts, &optidx);
106 case 'l': list_stat_names(); break;
107 case 'h': print_usage(); break;
108 case 'v': nl_cli_print_version(); break;
109 case 'n': nl_cli_link_parse_name(link, optarg); break;
110 case 'i': nl_cli_link_parse_ifindex(link, optarg); break;
115 nl_cache_foreach_filter(link_cache, OBJ_CAST(link), dump_stats, argv);