13 #include <netlink/cli/utils.h> 14 #include <netlink/cli/link.h> 16 #include <linux/netlink.h> 18 static void print_usage(
void)
21 "Usage: nl-link-stats [OPTION]... [LINK] [ListOfStats]\n" 24 " -l, --list List available statistic names\n" 25 " -h, --help Show this help\n" 26 " -v, --version Show versioning information\n" 29 " -n, --name=NAME link name\n" 30 " -i, --index=NUM interface index\n" 35 static void list_stat_names(
void)
40 for (i = 0; i <= RTNL_LINK_STATS_MAX; i++)
41 printf(
"%s\n", rtnl_link_stat2str(i, buf,
sizeof(buf)));
48 static void dump_stat(
struct rtnl_link *link,
int id)
54 rtnl_link_stat2str(
id, buf,
sizeof(buf)), st);
57 static void dump_stats(
struct nl_object *obj,
void *arg)
62 if (optind >= gargc) {
65 for (i = 0; i <= RTNL_LINK_STATS_MAX; i++)
68 while (optind < gargc) {
69 int id = rtnl_link_str2stat(argv[optind]);
72 fprintf(stderr,
"Warning: Unknown statistic " 73 "\"%s\"\n", argv[optind]);
82 int main(
int argc,
char *argv[])
85 struct nl_cache *link_cache;
88 sock = nl_cli_alloc_socket();
89 nl_cli_connect(sock, NETLINK_ROUTE);
90 link_cache = nl_cli_link_alloc_cache(sock);
91 link = nl_cli_link_alloc();
95 static struct option long_opts[] = {
96 {
"list", 0, 0,
'l' },
97 {
"help", 0, 0,
'h' },
98 {
"version", 0, 0,
'v' },
99 {
"name", 1, 0,
'n' },
100 {
"index", 1, 0,
'i' },
104 c = getopt_long(argc, argv,
"lhvn:i:", long_opts, &optidx);
109 case 'l': list_stat_names();
break;
110 case 'h': print_usage();
break;
111 case 'v': nl_cli_print_version();
break;
112 case 'n': nl_cli_link_parse_name(link, optarg);
break;
113 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.