8#include <linux/netlink.h>
10#include <netlink/cli/utils.h>
11#include <netlink/cli/link.h>
13static void print_usage(
void)
16 "Usage: nl-link-stats [OPTION]... [LINK] [ListOfStats]\n"
19 " -l, --list List available statistic names\n"
20 " -h, --help Show this help\n"
21 " -v, --version Show versioning information\n"
24 " -n, --name=NAME link name\n"
25 " -i, --index=NUM interface index\n"
30static void list_stat_names(
void)
35 for (i = 0; i <= RTNL_LINK_STATS_MAX; i++)
36 printf(
"%s\n", rtnl_link_stat2str(i, buf,
sizeof(buf)));
43static void dump_stat(
struct rtnl_link *link,
int id)
49 rtnl_link_stat2str(
id, buf,
sizeof(buf)), st);
52static void dump_stats(
struct nl_object *obj,
void *arg)
57 if (optind >= gargc) {
60 for (i = 0; i <= RTNL_LINK_STATS_MAX; i++)
63 while (optind < gargc) {
64 int id = rtnl_link_str2stat(argv[optind]);
67 fprintf(stderr,
"Warning: Unknown statistic "
68 "\"%s\"\n", argv[optind]);
77int main(
int argc,
char *argv[])
80 struct nl_cache *link_cache;
83 sock = nl_cli_alloc_socket();
84 nl_cli_connect(sock, NETLINK_ROUTE);
85 link_cache = nl_cli_link_alloc_cache(sock);
86 link = nl_cli_link_alloc();
90 static struct option long_opts[] = {
91 {
"list", 0, 0,
'l' },
92 {
"help", 0, 0,
'h' },
93 {
"version", 0, 0,
'v' },
94 {
"name", 1, 0,
'n' },
95 {
"index", 1, 0,
'i' },
99 c = getopt_long(argc, argv,
"lhvn:i:", long_opts, &optidx);
104 case 'l': list_stat_names();
break;
105 case 'h': print_usage();
break;
106 case 'v': nl_cli_print_version();
break;
107 case 'n': nl_cli_link_parse_name(link, optarg);
break;
108 case 'i': nl_cli_link_parse_ifindex(link, optarg);
break;
void nl_cache_foreach_filter(struct nl_cache *cache, struct nl_object *filter, void(*cb)(struct nl_object *, void *), void *arg)
Call a callback on each element of the cache (filtered).
uint64_t rtnl_link_get_stat(struct rtnl_link *link, rtnl_link_stat_id_t id)
Return value of link statistics counter.
char * rtnl_link_get_name(struct rtnl_link *link)
Return name of link object.