libnl  3.3.0
nl-link-stats.c
1 /*
2  * src/nl-link-stats.c Retrieve link statistics
3  *
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
7  * of the License.
8  *
9  * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 #include <netlink/cli/utils.h>
13 #include <netlink/cli/link.h>
14 
15 #include <linux/netlink.h>
16 
17 static void print_usage(void)
18 {
19  printf(
20  "Usage: nl-link-stats [OPTION]... [LINK] [ListOfStats]\n"
21  "\n"
22  "Options\n"
23  " -l, --list List available statistic names\n"
24  " -h, --help Show this help\n"
25  " -v, --version Show versioning information\n"
26  "\n"
27  "Link Options\n"
28  " -n, --name=NAME link name\n"
29  " -i, --index=NUM interface index\n"
30  );
31  exit(0);
32 }
33 
34 static void list_stat_names(void)
35 {
36  char buf[64];
37  int i;
38 
39  for (i = 0; i <= RTNL_LINK_STATS_MAX; i++)
40  printf("%s\n", rtnl_link_stat2str(i, buf, sizeof(buf)));
41 
42  exit(0);
43 }
44 
45 static int gargc;
46 
47 static void dump_stat(struct rtnl_link *link, int id)
48 {
49  uint64_t st = rtnl_link_get_stat(link, id);
50  char buf[64];
51 
52  printf("%s.%s %" PRIu64 "\n", rtnl_link_get_name(link),
53  rtnl_link_stat2str(id, buf, sizeof(buf)), st);
54 }
55 
56 static void dump_stats(struct nl_object *obj, void *arg)
57 {
58  struct rtnl_link *link = (struct rtnl_link *) obj;
59  char **argv = arg;
60 
61  if (optind >= gargc) {
62  int i;
63 
64  for (i = 0; i <= RTNL_LINK_STATS_MAX; i++)
65  dump_stat(link, i);
66  } else {
67  while (optind < gargc) {
68  int id = rtnl_link_str2stat(argv[optind]);
69 
70  if (id < 0)
71  fprintf(stderr, "Warning: Unknown statistic "
72  "\"%s\"\n", argv[optind]);
73  else
74  dump_stat(link, id);
75 
76  optind++;
77  }
78  }
79 }
80 
81 int main(int argc, char *argv[])
82 {
83  struct nl_sock *sock;
84  struct nl_cache *link_cache;
85  struct rtnl_link *link;
86 
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();
91 
92  for (;;) {
93  int c, optidx = 0;
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' },
100  { 0, 0, 0, 0 }
101  };
102 
103  c = getopt_long(argc, argv, "lhvn:i:", long_opts, &optidx);
104  if (c == -1)
105  break;
106 
107  switch (c) {
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;
113  }
114  }
115 
116  gargc = argc;
117  nl_cache_foreach_filter(link_cache, OBJ_CAST(link), dump_stats, argv);
118 
119  return 0;
120 }
121 
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).
Definition: cache.c:1282