6#include <netlink/cli/utils.h>
7#include <netlink/cli/link.h>
9#include <linux/netlink.h>
11static void print_usage(
void)
14 "Usage: nl-link-stats [OPTION]... [LINK] [ListOfStats]\n"
17 " -l, --list List available statistic names\n"
18 " -h, --help Show this help\n"
19 " -v, --version Show versioning information\n"
22 " -n, --name=NAME link name\n"
23 " -i, --index=NUM interface index\n"
28static void list_stat_names(
void)
33 for (i = 0; i <= RTNL_LINK_STATS_MAX; i++)
34 printf(
"%s\n", rtnl_link_stat2str(i, buf,
sizeof(buf)));
41static void dump_stat(
struct rtnl_link *link,
int id)
47 rtnl_link_stat2str(
id, buf,
sizeof(buf)), st);
50static void dump_stats(
struct nl_object *obj,
void *arg)
55 if (optind >= gargc) {
58 for (i = 0; i <= RTNL_LINK_STATS_MAX; i++)
61 while (optind < gargc) {
62 int id = rtnl_link_str2stat(argv[optind]);
65 fprintf(stderr,
"Warning: Unknown statistic "
66 "\"%s\"\n", argv[optind]);
75int main(
int argc,
char *argv[])
78 struct nl_cache *link_cache;
81 sock = nl_cli_alloc_socket();
82 nl_cli_connect(sock, NETLINK_ROUTE);
83 link_cache = nl_cli_link_alloc_cache(sock);
84 link = nl_cli_link_alloc();
88 static struct option long_opts[] = {
89 {
"list", 0, 0,
'l' },
90 {
"help", 0, 0,
'h' },
91 {
"version", 0, 0,
'v' },
92 {
"name", 1, 0,
'n' },
93 {
"index", 1, 0,
'i' },
97 c = getopt_long(argc, argv,
"lhvn:i:", long_opts, &optidx);
102 case 'l': list_stat_names();
break;
103 case 'h': print_usage();
break;
104 case 'v': nl_cli_print_version();
break;
105 case 'n': nl_cli_link_parse_name(link, optarg);
break;
106 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.