12 #include <netlink/cli/utils.h> 13 #include <netlink/cli/link.h> 15 #include <linux/netlink.h> 17 static void print_usage(
void)
20 "Usage: nl-link-stats [OPTION]... [LINK] [ListOfStats]\n" 23 " -l, --list List available statistic names\n" 24 " -h, --help Show this help\n" 25 " -v, --version Show versioning information\n" 28 " -n, --name=NAME link name\n" 29 " -i, --index=NUM interface index\n" 34 static void list_stat_names(
void)
39 for (i = 0; i <= RTNL_LINK_STATS_MAX; i++)
40 printf(
"%s\n", rtnl_link_stat2str(i, buf,
sizeof(buf)));
47 static void dump_stat(
struct rtnl_link *link,
int id)
53 rtnl_link_stat2str(
id, buf,
sizeof(buf)), st);
56 static void dump_stats(
struct nl_object *obj,
void *arg)
61 if (optind >= gargc) {
64 for (i = 0; i <= RTNL_LINK_STATS_MAX; i++)
67 while (optind < gargc) {
68 int id = rtnl_link_str2stat(argv[optind]);
71 fprintf(stderr,
"Warning: Unknown statistic " 72 "\"%s\"\n", argv[optind]);
81 int main(
int argc,
char *argv[])
84 struct nl_cache *link_cache;
87 sock = nl_cli_alloc_socket();
88 nl_cli_connect(sock, NETLINK_ROUTE);
89 link_cache = nl_cli_link_alloc_cache(sock);
90 link = nl_cli_link_alloc();
94 static struct option long_opts[] = {
95 {
"list", 0, 0,
'l' },
96 {
"help", 0, 0,
'h' },
97 {
"version", 0, 0,
'v' },
98 {
"name", 1, 0,
'n' },
99 {
"index", 1, 0,
'i' },
103 c = getopt_long(argc, argv,
"lhvn:i:", long_opts, &optidx);
108 case 'l': list_stat_names();
break;
109 case 'h': print_usage();
break;
110 case 'v': nl_cli_print_version();
break;
111 case 'n': nl_cli_link_parse_name(link, optarg);
break;
112 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.